blob: 7a774e51e1fa46df4a2f92d2a38e977253bf1500 [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
Jaeden Amerof24c7f82018-06-27 17:20:43 +010022/** An invalid export length that will never be set by psa_export_key(). */
23static const size_t INVALID_EXPORT_LENGTH = ~0U;
24
Gilles Peskinea7aa4422018-08-14 15:17:54 +020025/** Test if a buffer contains a constant byte value.
26 *
27 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020028 *
29 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020030 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020031 * \param size Size of the buffer in bytes.
32 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020033 * \return 1 if the buffer is all-bits-zero.
34 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020035 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020036static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037{
38 size_t i;
39 for( i = 0; i < size; i++ )
40 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020042 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020043 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020044 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020045}
Gilles Peskine818ca122018-06-20 18:16:48 +020046
Gilles Peskine0b352bc2018-06-28 00:16:11 +020047/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
48static int asn1_write_10x( unsigned char **p,
49 unsigned char *start,
50 size_t bits,
51 unsigned char x )
52{
53 int ret;
54 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020055 if( bits == 0 )
56 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
57 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020058 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030059 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020060 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
61 *p -= len;
62 ( *p )[len-1] = x;
63 if( bits % 8 == 0 )
64 ( *p )[1] |= 1;
65 else
66 ( *p )[0] |= 1 << ( bits % 8 );
67 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
68 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
69 MBEDTLS_ASN1_INTEGER ) );
70 return( len );
71}
72
73static int construct_fake_rsa_key( unsigned char *buffer,
74 size_t buffer_size,
75 unsigned char **p,
76 size_t bits,
77 int keypair )
78{
79 size_t half_bits = ( bits + 1 ) / 2;
80 int ret;
81 int len = 0;
82 /* Construct something that looks like a DER encoding of
83 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
84 * RSAPrivateKey ::= SEQUENCE {
85 * version Version,
86 * modulus INTEGER, -- n
87 * publicExponent INTEGER, -- e
88 * privateExponent INTEGER, -- d
89 * prime1 INTEGER, -- p
90 * prime2 INTEGER, -- q
91 * exponent1 INTEGER, -- d mod (p-1)
92 * exponent2 INTEGER, -- d mod (q-1)
93 * coefficient INTEGER, -- (inverse of q) mod p
94 * otherPrimeInfos OtherPrimeInfos OPTIONAL
95 * }
96 * Or, for a public key, the same structure with only
97 * version, modulus and publicExponent.
98 */
99 *p = buffer + buffer_size;
100 if( keypair )
101 {
102 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
103 asn1_write_10x( p, buffer, half_bits, 1 ) );
104 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
105 asn1_write_10x( p, buffer, half_bits, 1 ) );
106 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
107 asn1_write_10x( p, buffer, half_bits, 1 ) );
108 MBEDTLS_ASN1_CHK_ADD( len, /* q */
109 asn1_write_10x( p, buffer, half_bits, 1 ) );
110 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
111 asn1_write_10x( p, buffer, half_bits, 3 ) );
112 MBEDTLS_ASN1_CHK_ADD( len, /* d */
113 asn1_write_10x( p, buffer, bits, 1 ) );
114 }
115 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
116 asn1_write_10x( p, buffer, 17, 1 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* n */
118 asn1_write_10x( p, buffer, bits, 1 ) );
119 if( keypair )
120 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
121 mbedtls_asn1_write_int( p, buffer, 0 ) );
122 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
123 {
124 const unsigned char tag =
125 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
126 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
127 }
128 return( len );
129}
130
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100131int exercise_mac_setup( psa_key_type_t key_type,
132 const unsigned char *key_bytes,
133 size_t key_length,
134 psa_algorithm_t alg,
135 psa_mac_operation_t *operation,
136 psa_status_t *status )
137{
Ronald Cron5425a212020-08-04 14:58:35 +0200138 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200139 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100140
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100141 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200142 psa_set_key_algorithm( &attributes, alg );
143 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200144 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100145
Ronald Cron5425a212020-08-04 14:58:35 +0200146 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100147 /* Whether setup succeeded or failed, abort must succeed. */
148 PSA_ASSERT( psa_mac_abort( operation ) );
149 /* If setup failed, reproduce the failure, so that the caller can
150 * test the resulting state of the operation object. */
151 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100152 {
Ronald Cron5425a212020-08-04 14:58:35 +0200153 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100154 }
155
Ronald Cron5425a212020-08-04 14:58:35 +0200156 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100157 return( 1 );
158
159exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200160 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100161 return( 0 );
162}
163
164int exercise_cipher_setup( psa_key_type_t key_type,
165 const unsigned char *key_bytes,
166 size_t key_length,
167 psa_algorithm_t alg,
168 psa_cipher_operation_t *operation,
169 psa_status_t *status )
170{
Ronald Cron5425a212020-08-04 14:58:35 +0200171 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200172 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100173
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200174 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
175 psa_set_key_algorithm( &attributes, alg );
176 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200177 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100178
Ronald Cron5425a212020-08-04 14:58:35 +0200179 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100180 /* Whether setup succeeded or failed, abort must succeed. */
181 PSA_ASSERT( psa_cipher_abort( operation ) );
182 /* If setup failed, reproduce the failure, so that the caller can
183 * test the resulting state of the operation object. */
184 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185 {
Ronald Cron5425a212020-08-04 14:58:35 +0200186 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100187 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100188 }
189
Ronald Cron5425a212020-08-04 14:58:35 +0200190 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100191 return( 1 );
192
193exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200194 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100195 return( 0 );
196}
197
Ronald Cron5425a212020-08-04 14:58:35 +0200198static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200199{
200 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200201 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200202 uint8_t buffer[1];
203 size_t length;
204 int ok = 0;
205
Ronald Cronecfb2372020-07-23 17:13:42 +0200206 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200207 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
208 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
209 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200210 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000211 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200212 TEST_EQUAL(
213 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
214 TEST_EQUAL(
215 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200216 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200217 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
218 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
219 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
220 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
221
Ronald Cron5425a212020-08-04 14:58:35 +0200222 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000223 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200224 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200225 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000226 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200227
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200228 ok = 1;
229
230exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100231 /*
232 * Key attributes may have been returned by psa_get_key_attributes()
233 * thus reset them as required.
234 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200235 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100236
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200237 return( ok );
238}
239
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200240/* Assert that a key isn't reported as having a slot number. */
241#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
242#define ASSERT_NO_SLOT_NUMBER( attributes ) \
243 do \
244 { \
245 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
246 TEST_EQUAL( psa_get_key_slot_number( \
247 attributes, \
248 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
249 PSA_ERROR_INVALID_ARGUMENT ); \
250 } \
251 while( 0 )
252#else /* MBEDTLS_PSA_CRYPTO_SE_C */
253#define ASSERT_NO_SLOT_NUMBER( attributes ) \
254 ( (void) 0 )
255#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
256
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100257/* An overapproximation of the amount of storage needed for a key of the
258 * given type and with the given content. The API doesn't make it easy
259 * to find a good value for the size. The current implementation doesn't
260 * care about the value anyway. */
261#define KEY_BITS_FROM_DATA( type, data ) \
262 ( data )->len
263
Darryl Green0c6575a2018-11-07 16:05:30 +0000264typedef enum {
265 IMPORT_KEY = 0,
266 GENERATE_KEY = 1,
267 DERIVE_KEY = 2
268} generate_method;
269
Gilles Peskinee59236f2018-01-27 23:32:46 +0100270/* END_HEADER */
271
272/* BEGIN_DEPENDENCIES
273 * depends_on:MBEDTLS_PSA_CRYPTO_C
274 * END_DEPENDENCIES
275 */
276
277/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200278void static_checks( )
279{
280 size_t max_truncated_mac_size =
281 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
282
283 /* Check that the length for a truncated MAC always fits in the algorithm
284 * encoding. The shifted mask is the maximum truncated value. The
285 * untruncated algorithm may be one byte larger. */
286 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
287}
288/* END_CASE */
289
290/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200291void import_with_policy( int type_arg,
292 int usage_arg, int alg_arg,
293 int expected_status_arg )
294{
295 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
296 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200297 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200298 psa_key_type_t type = type_arg;
299 psa_key_usage_t usage = usage_arg;
300 psa_algorithm_t alg = alg_arg;
301 psa_status_t expected_status = expected_status_arg;
302 const uint8_t key_material[16] = {0};
303 psa_status_t status;
304
305 PSA_ASSERT( psa_crypto_init( ) );
306
307 psa_set_key_type( &attributes, type );
308 psa_set_key_usage_flags( &attributes, usage );
309 psa_set_key_algorithm( &attributes, alg );
310
311 status = psa_import_key( &attributes,
312 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200313 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200314 TEST_EQUAL( status, expected_status );
315 if( status != PSA_SUCCESS )
316 goto exit;
317
Ronald Cron5425a212020-08-04 14:58:35 +0200318 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200319 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
320 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
321 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200322 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200323
Ronald Cron5425a212020-08-04 14:58:35 +0200324 PSA_ASSERT( psa_destroy_key( key ) );
325 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200326
327exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100328 /*
329 * Key attributes may have been returned by psa_get_key_attributes()
330 * thus reset them as required.
331 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200332 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100333
334 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200335 PSA_DONE( );
336}
337/* END_CASE */
338
339/* BEGIN_CASE */
340void import_with_data( data_t *data, int type_arg,
341 int attr_bits_arg,
342 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200343{
344 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
345 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200346 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200347 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200348 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200349 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100350 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100351
Gilles Peskine8817f612018-12-18 00:18:46 +0100352 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100353
Gilles Peskine4747d192019-04-17 15:05:45 +0200354 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200355 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200356
Ronald Cron5425a212020-08-04 14:58:35 +0200357 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100358 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200359 if( status != PSA_SUCCESS )
360 goto exit;
361
Ronald Cron5425a212020-08-04 14:58:35 +0200362 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200363 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200364 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200365 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200366 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200367
Ronald Cron5425a212020-08-04 14:58:35 +0200368 PSA_ASSERT( psa_destroy_key( key ) );
369 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100370
371exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100372 /*
373 * Key attributes may have been returned by psa_get_key_attributes()
374 * thus reset them as required.
375 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200376 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100377
378 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200379 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100380}
381/* END_CASE */
382
383/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200384void import_large_key( int type_arg, int byte_size_arg,
385 int expected_status_arg )
386{
387 psa_key_type_t type = type_arg;
388 size_t byte_size = byte_size_arg;
389 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
390 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200391 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200392 psa_status_t status;
393 uint8_t *buffer = NULL;
394 size_t buffer_size = byte_size + 1;
395 size_t n;
396
Steven Cooreman69967ce2021-01-18 18:01:08 +0100397 /* Skip the test case if the target running the test cannot
398 * accomodate large keys due to heap size constraints */
399 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200400 memset( buffer, 'K', byte_size );
401
402 PSA_ASSERT( psa_crypto_init( ) );
403
404 /* Try importing the key */
405 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
406 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200407 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100408 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200409 TEST_EQUAL( status, expected_status );
410
411 if( status == PSA_SUCCESS )
412 {
Ronald Cron5425a212020-08-04 14:58:35 +0200413 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200414 TEST_EQUAL( psa_get_key_type( &attributes ), type );
415 TEST_EQUAL( psa_get_key_bits( &attributes ),
416 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200417 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200418 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200419 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200420 for( n = 0; n < byte_size; n++ )
421 TEST_EQUAL( buffer[n], 'K' );
422 for( n = byte_size; n < buffer_size; n++ )
423 TEST_EQUAL( buffer[n], 0 );
424 }
425
426exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100427 /*
428 * Key attributes may have been returned by psa_get_key_attributes()
429 * thus reset them as required.
430 */
431 psa_reset_key_attributes( &attributes );
432
Ronald Cron5425a212020-08-04 14:58:35 +0200433 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200434 PSA_DONE( );
435 mbedtls_free( buffer );
436}
437/* END_CASE */
438
439/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200440void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
441{
Ronald Cron5425a212020-08-04 14:58:35 +0200442 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200443 size_t bits = bits_arg;
444 psa_status_t expected_status = expected_status_arg;
445 psa_status_t status;
446 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200447 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200448 size_t buffer_size = /* Slight overapproximations */
449 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200450 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200451 unsigned char *p;
452 int ret;
453 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200454 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200455
Gilles Peskine8817f612018-12-18 00:18:46 +0100456 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200457 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200458
459 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
460 bits, keypair ) ) >= 0 );
461 length = ret;
462
463 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200464 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200465 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100466 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200467
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200468 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200469 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200470
471exit:
472 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200473 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200474}
475/* END_CASE */
476
477/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300478void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300479 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200480 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100481 int expected_bits,
482 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200483 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100484 int canonical_input )
485{
Ronald Cron5425a212020-08-04 14:58:35 +0200486 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100487 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200488 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200489 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100490 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100491 unsigned char *exported = NULL;
492 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100493 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100494 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100495 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200496 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200497 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100498
Moran Pekercb088e72018-07-17 17:36:59 +0300499 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200500 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100501 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200502 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100503 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100504
Gilles Peskine4747d192019-04-17 15:05:45 +0200505 psa_set_key_usage_flags( &attributes, usage_arg );
506 psa_set_key_algorithm( &attributes, alg );
507 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700508
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100509 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200510 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100511
512 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200513 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200514 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
515 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200516 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100517
518 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200519 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100520 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100521
522 /* The exported length must be set by psa_export_key() to a value between 0
523 * and export_size. On errors, the exported length must be 0. */
524 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
525 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
526 TEST_ASSERT( exported_length <= export_size );
527
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200528 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200529 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100530 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200531 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100532 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100533 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200534 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100535
Gilles Peskineea38a922021-02-13 00:05:16 +0100536 /* Run sanity checks on the exported key. For non-canonical inputs,
537 * this validates the canonical representations. For canonical inputs,
538 * this doesn't directly validate the implementation, but it still helps
539 * by cross-validating the test data with the sanity check code. */
540 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200541 goto exit;
542
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100543 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200544 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100545 else
546 {
Ronald Cron5425a212020-08-04 14:58:35 +0200547 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200548 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200549 &key2 ) );
550 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100551 reexported,
552 export_size,
553 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200554 ASSERT_COMPARE( exported, exported_length,
555 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200556 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100557 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100558 TEST_ASSERT( exported_length <=
559 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
560 psa_get_key_bits( &got_attributes ) ) );
561 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100562
563destroy:
564 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200565 PSA_ASSERT( psa_destroy_key( key ) );
566 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100567
568exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100569 /*
570 * Key attributes may have been returned by psa_get_key_attributes()
571 * thus reset them as required.
572 */
573 psa_reset_key_attributes( &got_attributes );
574
itayzafrir3e02b3b2018-06-12 17:06:52 +0300575 mbedtls_free( exported );
576 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200577 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100578}
579/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100580
Moran Pekerf709f4a2018-06-06 17:26:04 +0300581/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300582void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200583 int type_arg,
584 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100585 int export_size_delta,
586 int expected_export_status_arg,
587 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300588{
Ronald Cron5425a212020-08-04 14:58:35 +0200589 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300590 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200591 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200592 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300593 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300594 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100595 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100596 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200597 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300598
Gilles Peskine8817f612018-12-18 00:18:46 +0100599 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300600
Gilles Peskine4747d192019-04-17 15:05:45 +0200601 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
602 psa_set_key_algorithm( &attributes, alg );
603 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300604
605 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200606 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300607
Gilles Peskine49c25912018-10-29 15:15:31 +0100608 /* Export the public key */
609 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200610 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200611 exported, export_size,
612 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100613 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100614 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100615 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200616 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100617 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200618 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200619 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100620 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100621 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100622 TEST_ASSERT( expected_public_key->len <=
623 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
624 TEST_ASSERT( expected_public_key->len <=
625 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100626 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
627 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100628 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300629
630exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100631 /*
632 * Key attributes may have been returned by psa_get_key_attributes()
633 * thus reset them as required.
634 */
635 psa_reset_key_attributes( &attributes );
636
itayzafrir3e02b3b2018-06-12 17:06:52 +0300637 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200638 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200639 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300640}
641/* END_CASE */
642
Gilles Peskine20035e32018-02-03 22:44:14 +0100643/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200644void import_and_exercise_key( data_t *data,
645 int type_arg,
646 int bits_arg,
647 int alg_arg )
648{
Ronald Cron5425a212020-08-04 14:58:35 +0200649 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200650 psa_key_type_t type = type_arg;
651 size_t bits = bits_arg;
652 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100653 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200654 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200655 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200656
Gilles Peskine8817f612018-12-18 00:18:46 +0100657 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200658
Gilles Peskine4747d192019-04-17 15:05:45 +0200659 psa_set_key_usage_flags( &attributes, usage );
660 psa_set_key_algorithm( &attributes, alg );
661 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200662
663 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200664 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200665
666 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200667 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200668 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
669 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200670
671 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100672 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200673 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200674
Ronald Cron5425a212020-08-04 14:58:35 +0200675 PSA_ASSERT( psa_destroy_key( key ) );
676 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200677
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200678exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100679 /*
680 * Key attributes may have been returned by psa_get_key_attributes()
681 * thus reset them as required.
682 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200683 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100684
685 psa_reset_key_attributes( &attributes );
686 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200687 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200688}
689/* END_CASE */
690
691/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100692void effective_key_attributes( int type_arg, int expected_type_arg,
693 int bits_arg, int expected_bits_arg,
694 int usage_arg, int expected_usage_arg,
695 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200696{
Ronald Cron5425a212020-08-04 14:58:35 +0200697 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100698 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100699 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100700 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100701 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200702 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100703 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200704 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100705 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200706 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200707
Gilles Peskine8817f612018-12-18 00:18:46 +0100708 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200709
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200710 psa_set_key_usage_flags( &attributes, usage );
711 psa_set_key_algorithm( &attributes, alg );
712 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +0100713 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +0200714
Ronald Cron5425a212020-08-04 14:58:35 +0200715 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +0100716 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +0200717
Ronald Cron5425a212020-08-04 14:58:35 +0200718 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +0100719 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
720 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
721 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
722 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200723
724exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100725 /*
726 * Key attributes may have been returned by psa_get_key_attributes()
727 * thus reset them as required.
728 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200729 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100730
731 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200732 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +0200733}
734/* END_CASE */
735
736/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100737void check_key_policy( int type_arg, int bits_arg,
738 int usage_arg, int alg_arg )
739{
740 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
741 usage_arg, usage_arg, alg_arg, alg_arg );
742 goto exit;
743}
744/* END_CASE */
745
746/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200747void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +0000748{
749 /* Test each valid way of initializing the object, except for `= {0}`, as
750 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
751 * though it's OK by the C standard. We could test for this, but we'd need
752 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200753 psa_key_attributes_t func = psa_key_attributes_init( );
754 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
755 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000756
757 memset( &zero, 0, sizeof( zero ) );
758
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200759 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
760 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
761 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000762
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200763 TEST_EQUAL( psa_get_key_type( &func ), 0 );
764 TEST_EQUAL( psa_get_key_type( &init ), 0 );
765 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
766
767 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
768 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
769 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
770
771 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
772 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
773 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
774
775 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
776 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
777 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +0000778}
779/* END_CASE */
780
781/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200782void mac_key_policy( int policy_usage,
783 int policy_alg,
784 int key_type,
785 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100786 int exercise_alg,
787 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200788{
Ronald Cron5425a212020-08-04 14:58:35 +0200789 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200790 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000791 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200792 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100793 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200794 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200795
Gilles Peskine8817f612018-12-18 00:18:46 +0100796 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200797
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200798 psa_set_key_usage_flags( &attributes, policy_usage );
799 psa_set_key_algorithm( &attributes, policy_alg );
800 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200801
Gilles Peskine049c7532019-05-15 20:22:09 +0200802 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200803 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200804
Ronald Cron5425a212020-08-04 14:58:35 +0200805 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100806 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100807 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100808 else
809 TEST_EQUAL( status, expected_status );
810
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200811 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200812
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200813 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200814 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100815 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100816 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100817 else
818 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200819
820exit:
821 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200822 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200823 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200824}
825/* END_CASE */
826
827/* BEGIN_CASE */
828void cipher_key_policy( int policy_usage,
829 int policy_alg,
830 int key_type,
831 data_t *key_data,
832 int exercise_alg )
833{
Ronald Cron5425a212020-08-04 14:58:35 +0200834 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200835 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000836 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200837 psa_status_t status;
838
Gilles Peskine8817f612018-12-18 00:18:46 +0100839 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200840
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200841 psa_set_key_usage_flags( &attributes, policy_usage );
842 psa_set_key_algorithm( &attributes, policy_alg );
843 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200844
Gilles Peskine049c7532019-05-15 20:22:09 +0200845 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200846 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200847
Ronald Cron5425a212020-08-04 14:58:35 +0200848 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200849 if( policy_alg == exercise_alg &&
850 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100851 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200852 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100853 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200854 psa_cipher_abort( &operation );
855
Ronald Cron5425a212020-08-04 14:58:35 +0200856 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200857 if( policy_alg == exercise_alg &&
858 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100859 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200860 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100861 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200862
863exit:
864 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200865 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200866 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200867}
868/* END_CASE */
869
870/* BEGIN_CASE */
871void aead_key_policy( int policy_usage,
872 int policy_alg,
873 int key_type,
874 data_t *key_data,
875 int nonce_length_arg,
876 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100877 int exercise_alg,
878 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200879{
Ronald Cron5425a212020-08-04 14:58:35 +0200880 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200881 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200882 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100883 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200884 unsigned char nonce[16] = {0};
885 size_t nonce_length = nonce_length_arg;
886 unsigned char tag[16];
887 size_t tag_length = tag_length_arg;
888 size_t output_length;
889
890 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
891 TEST_ASSERT( tag_length <= sizeof( tag ) );
892
Gilles Peskine8817f612018-12-18 00:18:46 +0100893 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200894
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200895 psa_set_key_usage_flags( &attributes, policy_usage );
896 psa_set_key_algorithm( &attributes, policy_alg );
897 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200898
Gilles Peskine049c7532019-05-15 20:22:09 +0200899 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200900 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200901
Ronald Cron5425a212020-08-04 14:58:35 +0200902 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200903 nonce, nonce_length,
904 NULL, 0,
905 NULL, 0,
906 tag, tag_length,
907 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100908 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
909 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200910 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100911 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200912
913 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200914 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200915 nonce, nonce_length,
916 NULL, 0,
917 tag, tag_length,
918 NULL, 0,
919 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100920 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
921 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
922 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100923 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200924 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100925 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200926
927exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200928 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200929 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200930}
931/* END_CASE */
932
933/* BEGIN_CASE */
934void asymmetric_encryption_key_policy( int policy_usage,
935 int policy_alg,
936 int key_type,
937 data_t *key_data,
938 int exercise_alg )
939{
Ronald Cron5425a212020-08-04 14:58:35 +0200940 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200941 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200942 psa_status_t status;
943 size_t key_bits;
944 size_t buffer_length;
945 unsigned char *buffer = NULL;
946 size_t output_length;
947
Gilles Peskine8817f612018-12-18 00:18:46 +0100948 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200949
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200950 psa_set_key_usage_flags( &attributes, policy_usage );
951 psa_set_key_algorithm( &attributes, policy_alg );
952 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200953
Gilles Peskine049c7532019-05-15 20:22:09 +0200954 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200955 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200956
Ronald Cron5425a212020-08-04 14:58:35 +0200957 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200958 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200959 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
960 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200961 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200962
Ronald Cron5425a212020-08-04 14:58:35 +0200963 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200964 NULL, 0,
965 NULL, 0,
966 buffer, buffer_length,
967 &output_length );
968 if( policy_alg == exercise_alg &&
969 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100970 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200971 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100972 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200973
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +0200974 if( buffer_length != 0 )
975 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200976 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200977 buffer, buffer_length,
978 NULL, 0,
979 buffer, buffer_length,
980 &output_length );
981 if( policy_alg == exercise_alg &&
982 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100983 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200984 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100985 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200986
987exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100988 /*
989 * Key attributes may have been returned by psa_get_key_attributes()
990 * thus reset them as required.
991 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200992 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100993
994 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200995 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200996 mbedtls_free( buffer );
997}
998/* END_CASE */
999
1000/* BEGIN_CASE */
1001void asymmetric_signature_key_policy( int policy_usage,
1002 int policy_alg,
1003 int key_type,
1004 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001005 int exercise_alg,
1006 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001007{
Ronald Cron5425a212020-08-04 14:58:35 +02001008 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001009 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001010 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001011 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1012 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1013 * compatible with the policy and `payload_length_arg` is supposed to be
1014 * a valid input length to sign. If `payload_length_arg <= 0`,
1015 * `exercise_alg` is supposed to be forbidden by the policy. */
1016 int compatible_alg = payload_length_arg > 0;
1017 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001018 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001019 size_t signature_length;
1020
Gilles Peskine8817f612018-12-18 00:18:46 +01001021 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001022
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001023 psa_set_key_usage_flags( &attributes, policy_usage );
1024 psa_set_key_algorithm( &attributes, policy_alg );
1025 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001026
Gilles Peskine049c7532019-05-15 20:22:09 +02001027 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001028 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001029
Ronald Cron5425a212020-08-04 14:58:35 +02001030 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001031 payload, payload_length,
1032 signature, sizeof( signature ),
1033 &signature_length );
1034 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001035 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001036 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001037 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001038
1039 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001040 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001041 payload, payload_length,
1042 signature, sizeof( signature ) );
1043 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001044 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001045 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001046 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001047
1048exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001049 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001050 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001051}
1052/* END_CASE */
1053
Janos Follathba3fab92019-06-11 14:50:16 +01001054/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001055void derive_key_policy( int policy_usage,
1056 int policy_alg,
1057 int key_type,
1058 data_t *key_data,
1059 int exercise_alg )
1060{
Ronald Cron5425a212020-08-04 14:58:35 +02001061 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001062 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001063 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001064 psa_status_t status;
1065
Gilles Peskine8817f612018-12-18 00:18:46 +01001066 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001067
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001068 psa_set_key_usage_flags( &attributes, policy_usage );
1069 psa_set_key_algorithm( &attributes, policy_alg );
1070 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001071
Gilles Peskine049c7532019-05-15 20:22:09 +02001072 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001073 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001074
Janos Follathba3fab92019-06-11 14:50:16 +01001075 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1076
1077 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1078 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001079 {
Janos Follathba3fab92019-06-11 14:50:16 +01001080 PSA_ASSERT( psa_key_derivation_input_bytes(
1081 &operation,
1082 PSA_KEY_DERIVATION_INPUT_SEED,
1083 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001084 }
Janos Follathba3fab92019-06-11 14:50:16 +01001085
1086 status = psa_key_derivation_input_key( &operation,
1087 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001088 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001089
Gilles Peskineea0fb492018-07-12 17:17:20 +02001090 if( policy_alg == exercise_alg &&
1091 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001092 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001093 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001094 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001095
1096exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001097 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001098 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001099 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001100}
1101/* END_CASE */
1102
1103/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001104void agreement_key_policy( int policy_usage,
1105 int policy_alg,
1106 int key_type_arg,
1107 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001108 int exercise_alg,
1109 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001110{
Ronald Cron5425a212020-08-04 14:58:35 +02001111 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001112 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001113 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001114 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001115 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001116 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001117
Gilles Peskine8817f612018-12-18 00:18:46 +01001118 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001119
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001120 psa_set_key_usage_flags( &attributes, policy_usage );
1121 psa_set_key_algorithm( &attributes, policy_alg );
1122 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001123
Gilles Peskine049c7532019-05-15 20:22:09 +02001124 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001125 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001126
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001127 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001128 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001129
Steven Cooremance48e852020-10-05 16:02:45 +02001130 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001131
1132exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001133 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001134 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001135 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001136}
1137/* END_CASE */
1138
1139/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001140void key_policy_alg2( int key_type_arg, data_t *key_data,
1141 int usage_arg, int alg_arg, int alg2_arg )
1142{
Ronald Cron5425a212020-08-04 14:58:35 +02001143 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001144 psa_key_type_t key_type = key_type_arg;
1145 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1146 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1147 psa_key_usage_t usage = usage_arg;
1148 psa_algorithm_t alg = alg_arg;
1149 psa_algorithm_t alg2 = alg2_arg;
1150
1151 PSA_ASSERT( psa_crypto_init( ) );
1152
1153 psa_set_key_usage_flags( &attributes, usage );
1154 psa_set_key_algorithm( &attributes, alg );
1155 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1156 psa_set_key_type( &attributes, key_type );
1157 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001158 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001159
Ronald Cron5425a212020-08-04 14:58:35 +02001160 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001161 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1162 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1163 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1164
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001165 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001166 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001167 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001168 goto exit;
1169
1170exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001171 /*
1172 * Key attributes may have been returned by psa_get_key_attributes()
1173 * thus reset them as required.
1174 */
1175 psa_reset_key_attributes( &got_attributes );
1176
Ronald Cron5425a212020-08-04 14:58:35 +02001177 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001178 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001179}
1180/* END_CASE */
1181
1182/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001183void raw_agreement_key_policy( int policy_usage,
1184 int policy_alg,
1185 int key_type_arg,
1186 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001187 int exercise_alg,
1188 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001189{
Ronald Cron5425a212020-08-04 14:58:35 +02001190 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001191 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001192 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001193 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001194 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001195 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001196
1197 PSA_ASSERT( psa_crypto_init( ) );
1198
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001199 psa_set_key_usage_flags( &attributes, policy_usage );
1200 psa_set_key_algorithm( &attributes, policy_alg );
1201 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001202
Gilles Peskine049c7532019-05-15 20:22:09 +02001203 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001204 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001205
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001206 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001207
Steven Cooremance48e852020-10-05 16:02:45 +02001208 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001209
1210exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001211 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001212 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001213 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001214}
1215/* END_CASE */
1216
1217/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001218void copy_success( int source_usage_arg,
1219 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001220 int type_arg, data_t *material,
1221 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001222 int target_usage_arg,
1223 int target_alg_arg, int target_alg2_arg,
1224 int expected_usage_arg,
1225 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001226{
Gilles Peskineca25db92019-04-19 11:43:08 +02001227 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1228 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001229 psa_key_usage_t expected_usage = expected_usage_arg;
1230 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001231 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001232 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1233 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001234 uint8_t *export_buffer = NULL;
1235
Gilles Peskine57ab7212019-01-28 13:03:09 +01001236 PSA_ASSERT( psa_crypto_init( ) );
1237
Gilles Peskineca25db92019-04-19 11:43:08 +02001238 /* Prepare the source key. */
1239 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1240 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001241 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001242 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001243 PSA_ASSERT( psa_import_key( &source_attributes,
1244 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001245 &source_key ) );
1246 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001247
Gilles Peskineca25db92019-04-19 11:43:08 +02001248 /* Prepare the target attributes. */
1249 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001250 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001251 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001252 /* Set volatile lifetime to reset the key identifier to 0. */
1253 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1254 }
1255
Gilles Peskineca25db92019-04-19 11:43:08 +02001256 if( target_usage_arg != -1 )
1257 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1258 if( target_alg_arg != -1 )
1259 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001260 if( target_alg2_arg != -1 )
1261 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001262
1263 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001264 PSA_ASSERT( psa_copy_key( source_key,
1265 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001266
1267 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001268 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001269
1270 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001271 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001272 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1273 psa_get_key_type( &target_attributes ) );
1274 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1275 psa_get_key_bits( &target_attributes ) );
1276 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1277 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001278 TEST_EQUAL( expected_alg2,
1279 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001280 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1281 {
1282 size_t length;
1283 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001284 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001285 material->len, &length ) );
1286 ASSERT_COMPARE( material->x, material->len,
1287 export_buffer, length );
1288 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001289
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001290 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001291 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001292 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001293 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001294
Ronald Cron5425a212020-08-04 14:58:35 +02001295 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001296
1297exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001298 /*
1299 * Source and target key attributes may have been returned by
1300 * psa_get_key_attributes() thus reset them as required.
1301 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001302 psa_reset_key_attributes( &source_attributes );
1303 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001304
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001305 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001306 mbedtls_free( export_buffer );
1307}
1308/* END_CASE */
1309
1310/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001311void copy_fail( int source_usage_arg,
1312 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001313 int type_arg, data_t *material,
1314 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001315 int target_usage_arg,
1316 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001317 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001318 int expected_status_arg )
1319{
1320 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1321 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001322 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1323 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001324 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001325
1326 PSA_ASSERT( psa_crypto_init( ) );
1327
1328 /* Prepare the source key. */
1329 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1330 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001331 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001332 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001333 PSA_ASSERT( psa_import_key( &source_attributes,
1334 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001335 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001336
1337 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001338 psa_set_key_id( &target_attributes, key_id );
1339 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001340 psa_set_key_type( &target_attributes, target_type_arg );
1341 psa_set_key_bits( &target_attributes, target_bits_arg );
1342 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1343 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001344 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001345
1346 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001347 TEST_EQUAL( psa_copy_key( source_key,
1348 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001349 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001350
Ronald Cron5425a212020-08-04 14:58:35 +02001351 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001352
Gilles Peskine4a644642019-05-03 17:14:08 +02001353exit:
1354 psa_reset_key_attributes( &source_attributes );
1355 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001356 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001357}
1358/* END_CASE */
1359
1360/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001361void hash_operation_init( )
1362{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001363 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001364 /* Test each valid way of initializing the object, except for `= {0}`, as
1365 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1366 * though it's OK by the C standard. We could test for this, but we'd need
1367 * to supress the Clang warning for the test. */
1368 psa_hash_operation_t func = psa_hash_operation_init( );
1369 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1370 psa_hash_operation_t zero;
1371
1372 memset( &zero, 0, sizeof( zero ) );
1373
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001374 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001375 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1376 PSA_ERROR_BAD_STATE );
1377 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1378 PSA_ERROR_BAD_STATE );
1379 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1380 PSA_ERROR_BAD_STATE );
1381
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001382 /* A default hash operation should be abortable without error. */
1383 PSA_ASSERT( psa_hash_abort( &func ) );
1384 PSA_ASSERT( psa_hash_abort( &init ) );
1385 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001386}
1387/* END_CASE */
1388
1389/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001390void hash_setup( int alg_arg,
1391 int expected_status_arg )
1392{
1393 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001394 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001395 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001396 psa_status_t status;
1397
Gilles Peskine8817f612018-12-18 00:18:46 +01001398 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001399
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001400 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001401 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001402
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001403 /* Whether setup succeeded or failed, abort must succeed. */
1404 PSA_ASSERT( psa_hash_abort( &operation ) );
1405
1406 /* If setup failed, reproduce the failure, so as to
1407 * test the resulting state of the operation object. */
1408 if( status != PSA_SUCCESS )
1409 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1410
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001411 /* Now the operation object should be reusable. */
1412#if defined(KNOWN_SUPPORTED_HASH_ALG)
1413 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1414 PSA_ASSERT( psa_hash_abort( &operation ) );
1415#endif
1416
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001417exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001418 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001419}
1420/* END_CASE */
1421
1422/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001423void hash_compute_fail( int alg_arg, data_t *input,
1424 int output_size_arg, int expected_status_arg )
1425{
1426 psa_algorithm_t alg = alg_arg;
1427 uint8_t *output = NULL;
1428 size_t output_size = output_size_arg;
1429 size_t output_length = INVALID_EXPORT_LENGTH;
1430 psa_status_t expected_status = expected_status_arg;
1431 psa_status_t status;
1432
1433 ASSERT_ALLOC( output, output_size );
1434
1435 PSA_ASSERT( psa_crypto_init( ) );
1436
1437 status = psa_hash_compute( alg, input->x, input->len,
1438 output, output_size, &output_length );
1439 TEST_EQUAL( status, expected_status );
1440 TEST_ASSERT( output_length <= output_size );
1441
1442exit:
1443 mbedtls_free( output );
1444 PSA_DONE( );
1445}
1446/* END_CASE */
1447
1448/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001449void hash_compare_fail( int alg_arg, data_t *input,
1450 data_t *reference_hash,
1451 int expected_status_arg )
1452{
1453 psa_algorithm_t alg = alg_arg;
1454 psa_status_t expected_status = expected_status_arg;
1455 psa_status_t status;
1456
1457 PSA_ASSERT( psa_crypto_init( ) );
1458
1459 status = psa_hash_compare( alg, input->x, input->len,
1460 reference_hash->x, reference_hash->len );
1461 TEST_EQUAL( status, expected_status );
1462
1463exit:
1464 PSA_DONE( );
1465}
1466/* END_CASE */
1467
1468/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001469void hash_compute_compare( int alg_arg, data_t *input,
1470 data_t *expected_output )
1471{
1472 psa_algorithm_t alg = alg_arg;
1473 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1474 size_t output_length = INVALID_EXPORT_LENGTH;
1475 size_t i;
1476
1477 PSA_ASSERT( psa_crypto_init( ) );
1478
1479 /* Compute with tight buffer */
1480 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001481 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001482 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001483 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001484 ASSERT_COMPARE( output, output_length,
1485 expected_output->x, expected_output->len );
1486
1487 /* Compute with larger buffer */
1488 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1489 output, sizeof( output ),
1490 &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 /* Compare with correct hash */
1496 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1497 output, output_length ) );
1498
1499 /* Compare with trailing garbage */
1500 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1501 output, output_length + 1 ),
1502 PSA_ERROR_INVALID_SIGNATURE );
1503
1504 /* Compare with truncated hash */
1505 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1506 output, output_length - 1 ),
1507 PSA_ERROR_INVALID_SIGNATURE );
1508
1509 /* Compare with corrupted value */
1510 for( i = 0; i < output_length; i++ )
1511 {
Chris Jones9634bb12021-01-20 15:56:42 +00001512 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001513 output[i] ^= 1;
1514 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1515 output, output_length ),
1516 PSA_ERROR_INVALID_SIGNATURE );
1517 output[i] ^= 1;
1518 }
1519
1520exit:
1521 PSA_DONE( );
1522}
1523/* END_CASE */
1524
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001525/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001526void hash_bad_order( )
1527{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001528 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001529 unsigned char input[] = "";
1530 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001531 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001532 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1533 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1534 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001535 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001536 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001537 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001538
Gilles Peskine8817f612018-12-18 00:18:46 +01001539 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001540
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001541 /* Call setup twice in a row. */
1542 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1543 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1544 PSA_ERROR_BAD_STATE );
1545 PSA_ASSERT( psa_hash_abort( &operation ) );
1546
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001547 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001548 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001549 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001550 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001551
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001552 /* Call update after finish. */
1553 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1554 PSA_ASSERT( psa_hash_finish( &operation,
1555 hash, sizeof( hash ), &hash_len ) );
1556 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001557 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001558 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001559
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001560 /* Call verify without calling setup beforehand. */
1561 TEST_EQUAL( psa_hash_verify( &operation,
1562 valid_hash, sizeof( valid_hash ) ),
1563 PSA_ERROR_BAD_STATE );
1564 PSA_ASSERT( psa_hash_abort( &operation ) );
1565
1566 /* Call verify after finish. */
1567 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1568 PSA_ASSERT( psa_hash_finish( &operation,
1569 hash, sizeof( hash ), &hash_len ) );
1570 TEST_EQUAL( psa_hash_verify( &operation,
1571 valid_hash, sizeof( valid_hash ) ),
1572 PSA_ERROR_BAD_STATE );
1573 PSA_ASSERT( psa_hash_abort( &operation ) );
1574
1575 /* Call verify twice in a row. */
1576 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1577 PSA_ASSERT( psa_hash_verify( &operation,
1578 valid_hash, sizeof( valid_hash ) ) );
1579 TEST_EQUAL( psa_hash_verify( &operation,
1580 valid_hash, sizeof( valid_hash ) ),
1581 PSA_ERROR_BAD_STATE );
1582 PSA_ASSERT( psa_hash_abort( &operation ) );
1583
1584 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001585 TEST_EQUAL( psa_hash_finish( &operation,
1586 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001587 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001588 PSA_ASSERT( psa_hash_abort( &operation ) );
1589
1590 /* Call finish twice in a row. */
1591 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1592 PSA_ASSERT( psa_hash_finish( &operation,
1593 hash, sizeof( hash ), &hash_len ) );
1594 TEST_EQUAL( psa_hash_finish( &operation,
1595 hash, sizeof( hash ), &hash_len ),
1596 PSA_ERROR_BAD_STATE );
1597 PSA_ASSERT( psa_hash_abort( &operation ) );
1598
1599 /* Call finish after calling verify. */
1600 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1601 PSA_ASSERT( psa_hash_verify( &operation,
1602 valid_hash, sizeof( valid_hash ) ) );
1603 TEST_EQUAL( psa_hash_finish( &operation,
1604 hash, sizeof( hash ), &hash_len ),
1605 PSA_ERROR_BAD_STATE );
1606 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001607
1608exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001609 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001610}
1611/* END_CASE */
1612
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001613/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001614void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001615{
1616 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001617 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1618 * appended to it */
1619 unsigned char hash[] = {
1620 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1621 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1622 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001623 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001624 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001625
Gilles Peskine8817f612018-12-18 00:18:46 +01001626 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001627
itayzafrir27e69452018-11-01 14:26:34 +02001628 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001629 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001630 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001631 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001632
itayzafrir27e69452018-11-01 14:26:34 +02001633 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001634 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001635 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001636 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001637
itayzafrir27e69452018-11-01 14:26:34 +02001638 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001639 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001640 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001641 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001642
itayzafrirec93d302018-10-18 18:01:10 +03001643exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001644 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001645}
1646/* END_CASE */
1647
Ronald Cronee414c72021-03-18 18:50:08 +01001648/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001649void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001650{
1651 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001652 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001653 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001654 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001655 size_t hash_len;
1656
Gilles Peskine8817f612018-12-18 00:18:46 +01001657 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001658
itayzafrir58028322018-10-25 10:22:01 +03001659 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001660 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001661 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001662 hash, expected_size - 1, &hash_len ),
1663 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001664
1665exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001666 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001667}
1668/* END_CASE */
1669
Ronald Cronee414c72021-03-18 18:50:08 +01001670/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001671void hash_clone_source_state( )
1672{
1673 psa_algorithm_t alg = PSA_ALG_SHA_256;
1674 unsigned char hash[PSA_HASH_MAX_SIZE];
1675 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1676 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1677 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1678 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1679 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1680 size_t hash_len;
1681
1682 PSA_ASSERT( psa_crypto_init( ) );
1683 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1684
1685 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1686 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1687 PSA_ASSERT( psa_hash_finish( &op_finished,
1688 hash, sizeof( hash ), &hash_len ) );
1689 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1690 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1691
1692 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1693 PSA_ERROR_BAD_STATE );
1694
1695 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1696 PSA_ASSERT( psa_hash_finish( &op_init,
1697 hash, sizeof( hash ), &hash_len ) );
1698 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1699 PSA_ASSERT( psa_hash_finish( &op_finished,
1700 hash, sizeof( hash ), &hash_len ) );
1701 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1702 PSA_ASSERT( psa_hash_finish( &op_aborted,
1703 hash, sizeof( hash ), &hash_len ) );
1704
1705exit:
1706 psa_hash_abort( &op_source );
1707 psa_hash_abort( &op_init );
1708 psa_hash_abort( &op_setup );
1709 psa_hash_abort( &op_finished );
1710 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001711 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001712}
1713/* END_CASE */
1714
Ronald Cronee414c72021-03-18 18:50:08 +01001715/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001716void hash_clone_target_state( )
1717{
1718 psa_algorithm_t alg = PSA_ALG_SHA_256;
1719 unsigned char hash[PSA_HASH_MAX_SIZE];
1720 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1721 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1722 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1723 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1724 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1725 size_t hash_len;
1726
1727 PSA_ASSERT( psa_crypto_init( ) );
1728
1729 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1730 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1731 PSA_ASSERT( psa_hash_finish( &op_finished,
1732 hash, sizeof( hash ), &hash_len ) );
1733 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1734 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1735
1736 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1737 PSA_ASSERT( psa_hash_finish( &op_target,
1738 hash, sizeof( hash ), &hash_len ) );
1739
1740 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1741 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1742 PSA_ERROR_BAD_STATE );
1743 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1744 PSA_ERROR_BAD_STATE );
1745
1746exit:
1747 psa_hash_abort( &op_target );
1748 psa_hash_abort( &op_init );
1749 psa_hash_abort( &op_setup );
1750 psa_hash_abort( &op_finished );
1751 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001752 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001753}
1754/* END_CASE */
1755
itayzafrir58028322018-10-25 10:22:01 +03001756/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001757void mac_operation_init( )
1758{
Jaeden Amero252ef282019-02-15 14:05:35 +00001759 const uint8_t input[1] = { 0 };
1760
Jaeden Amero769ce272019-01-04 11:48:03 +00001761 /* Test each valid way of initializing the object, except for `= {0}`, as
1762 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1763 * though it's OK by the C standard. We could test for this, but we'd need
1764 * to supress the Clang warning for the test. */
1765 psa_mac_operation_t func = psa_mac_operation_init( );
1766 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1767 psa_mac_operation_t zero;
1768
1769 memset( &zero, 0, sizeof( zero ) );
1770
Jaeden Amero252ef282019-02-15 14:05:35 +00001771 /* A freshly-initialized MAC operation should not be usable. */
1772 TEST_EQUAL( psa_mac_update( &func,
1773 input, sizeof( input ) ),
1774 PSA_ERROR_BAD_STATE );
1775 TEST_EQUAL( psa_mac_update( &init,
1776 input, sizeof( input ) ),
1777 PSA_ERROR_BAD_STATE );
1778 TEST_EQUAL( psa_mac_update( &zero,
1779 input, sizeof( input ) ),
1780 PSA_ERROR_BAD_STATE );
1781
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001782 /* A default MAC operation should be abortable without error. */
1783 PSA_ASSERT( psa_mac_abort( &func ) );
1784 PSA_ASSERT( psa_mac_abort( &init ) );
1785 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001786}
1787/* END_CASE */
1788
1789/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001790void mac_setup( int key_type_arg,
1791 data_t *key,
1792 int alg_arg,
1793 int expected_status_arg )
1794{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001795 psa_key_type_t key_type = key_type_arg;
1796 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001797 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001798 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001799 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1800#if defined(KNOWN_SUPPORTED_MAC_ALG)
1801 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1802#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001803
Gilles Peskine8817f612018-12-18 00:18:46 +01001804 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001805
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001806 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1807 &operation, &status ) )
1808 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001809 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001810
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001811 /* The operation object should be reusable. */
1812#if defined(KNOWN_SUPPORTED_MAC_ALG)
1813 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1814 smoke_test_key_data,
1815 sizeof( smoke_test_key_data ),
1816 KNOWN_SUPPORTED_MAC_ALG,
1817 &operation, &status ) )
1818 goto exit;
1819 TEST_EQUAL( status, PSA_SUCCESS );
1820#endif
1821
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001822exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001823 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001824}
1825/* END_CASE */
1826
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001827/* 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 +00001828void mac_bad_order( )
1829{
Ronald Cron5425a212020-08-04 14:58:35 +02001830 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001831 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1832 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001833 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001834 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1835 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1836 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001837 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001838 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1839 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1840 size_t sign_mac_length = 0;
1841 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1842 const uint8_t verify_mac[] = {
1843 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1844 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1845 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1846
1847 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001848 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001849 psa_set_key_algorithm( &attributes, alg );
1850 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001851
Ronald Cron5425a212020-08-04 14:58:35 +02001852 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1853 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001854
Jaeden Amero252ef282019-02-15 14:05:35 +00001855 /* Call update without calling setup beforehand. */
1856 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1857 PSA_ERROR_BAD_STATE );
1858 PSA_ASSERT( psa_mac_abort( &operation ) );
1859
1860 /* Call sign finish without calling setup beforehand. */
1861 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1862 &sign_mac_length),
1863 PSA_ERROR_BAD_STATE );
1864 PSA_ASSERT( psa_mac_abort( &operation ) );
1865
1866 /* Call verify finish without calling setup beforehand. */
1867 TEST_EQUAL( psa_mac_verify_finish( &operation,
1868 verify_mac, sizeof( verify_mac ) ),
1869 PSA_ERROR_BAD_STATE );
1870 PSA_ASSERT( psa_mac_abort( &operation ) );
1871
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001872 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001873 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
1874 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001875 PSA_ERROR_BAD_STATE );
1876 PSA_ASSERT( psa_mac_abort( &operation ) );
1877
Jaeden Amero252ef282019-02-15 14:05:35 +00001878 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001879 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001880 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1881 PSA_ASSERT( psa_mac_sign_finish( &operation,
1882 sign_mac, sizeof( sign_mac ),
1883 &sign_mac_length ) );
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 update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001889 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001890 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1891 PSA_ASSERT( psa_mac_verify_finish( &operation,
1892 verify_mac, sizeof( verify_mac ) ) );
1893 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1894 PSA_ERROR_BAD_STATE );
1895 PSA_ASSERT( psa_mac_abort( &operation ) );
1896
1897 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001898 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001899 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1900 PSA_ASSERT( psa_mac_sign_finish( &operation,
1901 sign_mac, sizeof( sign_mac ),
1902 &sign_mac_length ) );
1903 TEST_EQUAL( psa_mac_sign_finish( &operation,
1904 sign_mac, sizeof( sign_mac ),
1905 &sign_mac_length ),
1906 PSA_ERROR_BAD_STATE );
1907 PSA_ASSERT( psa_mac_abort( &operation ) );
1908
1909 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001910 PSA_ASSERT( psa_mac_verify_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_verify_finish( &operation,
1913 verify_mac, sizeof( verify_mac ) ) );
1914 TEST_EQUAL( psa_mac_verify_finish( &operation,
1915 verify_mac, sizeof( verify_mac ) ),
1916 PSA_ERROR_BAD_STATE );
1917 PSA_ASSERT( psa_mac_abort( &operation ) );
1918
1919 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02001920 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001921 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1922 TEST_EQUAL( psa_mac_verify_finish( &operation,
1923 verify_mac, sizeof( verify_mac ) ),
1924 PSA_ERROR_BAD_STATE );
1925 PSA_ASSERT( psa_mac_abort( &operation ) );
1926
1927 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02001928 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001929 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1930 TEST_EQUAL( psa_mac_sign_finish( &operation,
1931 sign_mac, sizeof( sign_mac ),
1932 &sign_mac_length ),
1933 PSA_ERROR_BAD_STATE );
1934 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001935
Ronald Cron5425a212020-08-04 14:58:35 +02001936 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001937
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001938exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001939 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001940}
1941/* END_CASE */
1942
1943/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001944void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02001945 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001946 int alg_arg,
1947 data_t *input,
1948 data_t *expected_mac )
1949{
Ronald Cron5425a212020-08-04 14:58:35 +02001950 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001951 psa_key_type_t key_type = key_type_arg;
1952 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001953 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001954 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02001955 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001956 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001957 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001958 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02001959 const size_t output_sizes_to_test[] = {
1960 0,
1961 1,
1962 expected_mac->len - 1,
1963 expected_mac->len,
1964 expected_mac->len + 1,
1965 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001966
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001967 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001968 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02001969 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001970
Gilles Peskine8817f612018-12-18 00:18:46 +01001971 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001972
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001973 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001974 psa_set_key_algorithm( &attributes, alg );
1975 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001976
Ronald Cron5425a212020-08-04 14:58:35 +02001977 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1978 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001979
Gilles Peskine8b356b52020-08-25 23:44:59 +02001980 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
1981 {
1982 const size_t output_size = output_sizes_to_test[i];
1983 psa_status_t expected_status =
1984 ( output_size >= expected_mac->len ? PSA_SUCCESS :
1985 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02001986
Chris Jones9634bb12021-01-20 15:56:42 +00001987 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02001988 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001989
Gilles Peskine8b356b52020-08-25 23:44:59 +02001990 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02001991 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02001992 PSA_ASSERT( psa_mac_update( &operation,
1993 input->x, input->len ) );
1994 TEST_EQUAL( psa_mac_sign_finish( &operation,
1995 actual_mac, output_size,
1996 &mac_length ),
1997 expected_status );
1998 PSA_ASSERT( psa_mac_abort( &operation ) );
1999
2000 if( expected_status == PSA_SUCCESS )
2001 {
2002 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2003 actual_mac, mac_length );
2004 }
2005 mbedtls_free( actual_mac );
2006 actual_mac = NULL;
2007 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002008
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002009exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002010 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002011 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002012 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002013 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002014}
2015/* END_CASE */
2016
2017/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002018void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002019 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002020 int alg_arg,
2021 data_t *input,
2022 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002023{
Ronald Cron5425a212020-08-04 14:58:35 +02002024 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002025 psa_key_type_t key_type = key_type_arg;
2026 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002027 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002028 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002029 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002030
Gilles Peskine69c12672018-06-28 00:07:19 +02002031 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2032
Gilles Peskine8817f612018-12-18 00:18:46 +01002033 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002034
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002035 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002036 psa_set_key_algorithm( &attributes, alg );
2037 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002038
Ronald Cron5425a212020-08-04 14:58:35 +02002039 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2040 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002041
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002042 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002043 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002044 PSA_ASSERT( psa_mac_update( &operation,
2045 input->x, input->len ) );
2046 PSA_ASSERT( psa_mac_verify_finish( &operation,
2047 expected_mac->x,
2048 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002049
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002050 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002051 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002052 PSA_ASSERT( psa_mac_update( &operation,
2053 input->x, input->len ) );
2054 TEST_EQUAL( psa_mac_verify_finish( &operation,
2055 expected_mac->x,
2056 expected_mac->len - 1 ),
2057 PSA_ERROR_INVALID_SIGNATURE );
2058
2059 /* Test a MAC that's too long. */
2060 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2061 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002062 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002063 PSA_ASSERT( psa_mac_update( &operation,
2064 input->x, input->len ) );
2065 TEST_EQUAL( psa_mac_verify_finish( &operation,
2066 perturbed_mac,
2067 expected_mac->len + 1 ),
2068 PSA_ERROR_INVALID_SIGNATURE );
2069
2070 /* Test changing one byte. */
2071 for( size_t i = 0; i < expected_mac->len; i++ )
2072 {
Chris Jones9634bb12021-01-20 15:56:42 +00002073 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002074 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002075 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002076 PSA_ASSERT( psa_mac_update( &operation,
2077 input->x, input->len ) );
2078 TEST_EQUAL( psa_mac_verify_finish( &operation,
2079 perturbed_mac,
2080 expected_mac->len ),
2081 PSA_ERROR_INVALID_SIGNATURE );
2082 perturbed_mac[i] ^= 1;
2083 }
2084
Gilles Peskine8c9def32018-02-08 10:02:12 +01002085exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002086 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002087 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002088 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002089 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002090}
2091/* END_CASE */
2092
2093/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002094void cipher_operation_init( )
2095{
Jaeden Ameroab439972019-02-15 14:12:05 +00002096 const uint8_t input[1] = { 0 };
2097 unsigned char output[1] = { 0 };
2098 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002099 /* Test each valid way of initializing the object, except for `= {0}`, as
2100 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2101 * though it's OK by the C standard. We could test for this, but we'd need
2102 * to supress the Clang warning for the test. */
2103 psa_cipher_operation_t func = psa_cipher_operation_init( );
2104 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2105 psa_cipher_operation_t zero;
2106
2107 memset( &zero, 0, sizeof( zero ) );
2108
Jaeden Ameroab439972019-02-15 14:12:05 +00002109 /* A freshly-initialized cipher operation should not be usable. */
2110 TEST_EQUAL( psa_cipher_update( &func,
2111 input, sizeof( input ),
2112 output, sizeof( output ),
2113 &output_length ),
2114 PSA_ERROR_BAD_STATE );
2115 TEST_EQUAL( psa_cipher_update( &init,
2116 input, sizeof( input ),
2117 output, sizeof( output ),
2118 &output_length ),
2119 PSA_ERROR_BAD_STATE );
2120 TEST_EQUAL( psa_cipher_update( &zero,
2121 input, sizeof( input ),
2122 output, sizeof( output ),
2123 &output_length ),
2124 PSA_ERROR_BAD_STATE );
2125
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002126 /* A default cipher operation should be abortable without error. */
2127 PSA_ASSERT( psa_cipher_abort( &func ) );
2128 PSA_ASSERT( psa_cipher_abort( &init ) );
2129 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002130}
2131/* END_CASE */
2132
2133/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002134void cipher_setup( int key_type_arg,
2135 data_t *key,
2136 int alg_arg,
2137 int expected_status_arg )
2138{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002139 psa_key_type_t key_type = key_type_arg;
2140 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002141 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002142 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002143 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002144#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002145 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2146#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002147
Gilles Peskine8817f612018-12-18 00:18:46 +01002148 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002149
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002150 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2151 &operation, &status ) )
2152 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002153 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002154
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002155 /* The operation object should be reusable. */
2156#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2157 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2158 smoke_test_key_data,
2159 sizeof( smoke_test_key_data ),
2160 KNOWN_SUPPORTED_CIPHER_ALG,
2161 &operation, &status ) )
2162 goto exit;
2163 TEST_EQUAL( status, PSA_SUCCESS );
2164#endif
2165
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002166exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002167 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002168 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002169}
2170/* END_CASE */
2171
Ronald Cronee414c72021-03-18 18:50:08 +01002172/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002173void cipher_bad_order( )
2174{
Ronald Cron5425a212020-08-04 14:58:35 +02002175 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002176 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2177 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002178 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002179 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002180 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002181 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002182 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2183 0xaa, 0xaa, 0xaa, 0xaa };
2184 const uint8_t text[] = {
2185 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2186 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002187 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002188 size_t length = 0;
2189
2190 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002191 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2192 psa_set_key_algorithm( &attributes, alg );
2193 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002194 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2195 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002196
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002197 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002198 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2199 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002200 PSA_ERROR_BAD_STATE );
2201 PSA_ASSERT( psa_cipher_abort( &operation ) );
2202
2203 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002204 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2205 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002206 PSA_ERROR_BAD_STATE );
2207 PSA_ASSERT( psa_cipher_abort( &operation ) );
2208
Jaeden Ameroab439972019-02-15 14:12:05 +00002209 /* Generate an IV without calling setup beforehand. */
2210 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2211 buffer, sizeof( buffer ),
2212 &length ),
2213 PSA_ERROR_BAD_STATE );
2214 PSA_ASSERT( psa_cipher_abort( &operation ) );
2215
2216 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002217 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002218 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2219 buffer, sizeof( buffer ),
2220 &length ) );
2221 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2222 buffer, sizeof( buffer ),
2223 &length ),
2224 PSA_ERROR_BAD_STATE );
2225 PSA_ASSERT( psa_cipher_abort( &operation ) );
2226
2227 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002228 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002229 PSA_ASSERT( psa_cipher_set_iv( &operation,
2230 iv, sizeof( iv ) ) );
2231 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2232 buffer, sizeof( buffer ),
2233 &length ),
2234 PSA_ERROR_BAD_STATE );
2235 PSA_ASSERT( psa_cipher_abort( &operation ) );
2236
2237 /* Set an IV without calling setup beforehand. */
2238 TEST_EQUAL( psa_cipher_set_iv( &operation,
2239 iv, sizeof( iv ) ),
2240 PSA_ERROR_BAD_STATE );
2241 PSA_ASSERT( psa_cipher_abort( &operation ) );
2242
2243 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002244 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002245 PSA_ASSERT( psa_cipher_set_iv( &operation,
2246 iv, sizeof( iv ) ) );
2247 TEST_EQUAL( psa_cipher_set_iv( &operation,
2248 iv, sizeof( iv ) ),
2249 PSA_ERROR_BAD_STATE );
2250 PSA_ASSERT( psa_cipher_abort( &operation ) );
2251
2252 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002253 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002254 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2255 buffer, sizeof( buffer ),
2256 &length ) );
2257 TEST_EQUAL( psa_cipher_set_iv( &operation,
2258 iv, sizeof( iv ) ),
2259 PSA_ERROR_BAD_STATE );
2260 PSA_ASSERT( psa_cipher_abort( &operation ) );
2261
2262 /* Call update without calling setup beforehand. */
2263 TEST_EQUAL( psa_cipher_update( &operation,
2264 text, sizeof( text ),
2265 buffer, sizeof( buffer ),
2266 &length ),
2267 PSA_ERROR_BAD_STATE );
2268 PSA_ASSERT( psa_cipher_abort( &operation ) );
2269
2270 /* Call update without an IV where an IV is required. */
2271 TEST_EQUAL( psa_cipher_update( &operation,
2272 text, sizeof( text ),
2273 buffer, sizeof( buffer ),
2274 &length ),
2275 PSA_ERROR_BAD_STATE );
2276 PSA_ASSERT( psa_cipher_abort( &operation ) );
2277
2278 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002279 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002280 PSA_ASSERT( psa_cipher_set_iv( &operation,
2281 iv, sizeof( iv ) ) );
2282 PSA_ASSERT( psa_cipher_finish( &operation,
2283 buffer, sizeof( buffer ), &length ) );
2284 TEST_EQUAL( psa_cipher_update( &operation,
2285 text, sizeof( text ),
2286 buffer, sizeof( buffer ),
2287 &length ),
2288 PSA_ERROR_BAD_STATE );
2289 PSA_ASSERT( psa_cipher_abort( &operation ) );
2290
2291 /* Call finish without calling setup beforehand. */
2292 TEST_EQUAL( psa_cipher_finish( &operation,
2293 buffer, sizeof( buffer ), &length ),
2294 PSA_ERROR_BAD_STATE );
2295 PSA_ASSERT( psa_cipher_abort( &operation ) );
2296
2297 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002298 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002299 /* Not calling update means we are encrypting an empty buffer, which is OK
2300 * for cipher modes with padding. */
2301 TEST_EQUAL( psa_cipher_finish( &operation,
2302 buffer, sizeof( buffer ), &length ),
2303 PSA_ERROR_BAD_STATE );
2304 PSA_ASSERT( psa_cipher_abort( &operation ) );
2305
2306 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002307 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002308 PSA_ASSERT( psa_cipher_set_iv( &operation,
2309 iv, sizeof( iv ) ) );
2310 PSA_ASSERT( psa_cipher_finish( &operation,
2311 buffer, sizeof( buffer ), &length ) );
2312 TEST_EQUAL( psa_cipher_finish( &operation,
2313 buffer, sizeof( buffer ), &length ),
2314 PSA_ERROR_BAD_STATE );
2315 PSA_ASSERT( psa_cipher_abort( &operation ) );
2316
Ronald Cron5425a212020-08-04 14:58:35 +02002317 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002318
Jaeden Ameroab439972019-02-15 14:12:05 +00002319exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002320 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002321 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002322}
2323/* END_CASE */
2324
2325/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002326void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002327 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002328 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002329 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002330{
Ronald Cron5425a212020-08-04 14:58:35 +02002331 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002332 psa_status_t status;
2333 psa_key_type_t key_type = key_type_arg;
2334 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002335 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002336 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002337 size_t output_buffer_size = 0;
2338 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002339 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002340 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002341 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002342
Gilles Peskine8817f612018-12-18 00:18:46 +01002343 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002344
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002345 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2346 psa_set_key_algorithm( &attributes, alg );
2347 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002348
Ronald Cron5425a212020-08-04 14:58:35 +02002349 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2350 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002351
Ronald Cron5425a212020-08-04 14:58:35 +02002352 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002353
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002354 if( iv->len > 0 )
2355 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002356 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002357 }
2358
gabor-mezei-armceface22021-01-21 12:26:17 +01002359 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2360 TEST_ASSERT( output_buffer_size <=
2361 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002362 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002363
Gilles Peskine8817f612018-12-18 00:18:46 +01002364 PSA_ASSERT( psa_cipher_update( &operation,
2365 input->x, input->len,
2366 output, output_buffer_size,
2367 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002368 TEST_ASSERT( function_output_length <=
2369 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2370 TEST_ASSERT( function_output_length <=
2371 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002372 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002373
Gilles Peskine50e586b2018-06-08 14:28:46 +02002374 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002375 ( output_buffer_size == 0 ? NULL :
2376 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002377 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002378 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002379 TEST_ASSERT( function_output_length <=
2380 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2381 TEST_ASSERT( function_output_length <=
2382 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002383 total_output_length += function_output_length;
2384
Gilles Peskinefe11b722018-12-18 00:24:04 +01002385 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002386 if( expected_status == PSA_SUCCESS )
2387 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002388 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002389 ASSERT_COMPARE( expected_output->x, expected_output->len,
2390 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002391 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002392
Gilles Peskine50e586b2018-06-08 14:28:46 +02002393exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002394 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002395 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002396 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002397 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002398}
2399/* END_CASE */
2400
2401/* BEGIN_CASE */
2402void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002403 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002404 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002405 int first_part_size_arg,
2406 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002407 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002408{
Ronald Cron5425a212020-08-04 14:58:35 +02002409 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002410 psa_key_type_t key_type = key_type_arg;
2411 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002412 size_t first_part_size = first_part_size_arg;
2413 size_t output1_length = output1_length_arg;
2414 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002415 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002416 size_t output_buffer_size = 0;
2417 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002418 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002419 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002420 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002421
Gilles Peskine8817f612018-12-18 00:18:46 +01002422 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002423
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002424 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2425 psa_set_key_algorithm( &attributes, alg );
2426 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002427
Ronald Cron5425a212020-08-04 14:58:35 +02002428 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2429 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002430
Ronald Cron5425a212020-08-04 14:58:35 +02002431 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002432
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002433 if( iv->len > 0 )
2434 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002435 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002436 }
2437
gabor-mezei-armceface22021-01-21 12:26:17 +01002438 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2439 TEST_ASSERT( output_buffer_size <=
2440 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002441 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002442
Gilles Peskinee0866522019-02-19 19:44:00 +01002443 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002444 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2445 output, output_buffer_size,
2446 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002447 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002448 TEST_ASSERT( function_output_length <=
2449 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2450 TEST_ASSERT( function_output_length <=
2451 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002452 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002453
Gilles Peskine8817f612018-12-18 00:18:46 +01002454 PSA_ASSERT( psa_cipher_update( &operation,
2455 input->x + first_part_size,
2456 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002457 ( output_buffer_size == 0 ? NULL :
2458 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002459 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002460 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002461 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002462 TEST_ASSERT( function_output_length <=
2463 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2464 alg,
2465 input->len - first_part_size ) );
2466 TEST_ASSERT( function_output_length <=
2467 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002468 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002469
Gilles Peskine8817f612018-12-18 00:18:46 +01002470 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002471 ( output_buffer_size == 0 ? NULL :
2472 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002473 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002474 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002475 TEST_ASSERT( function_output_length <=
2476 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2477 TEST_ASSERT( function_output_length <=
2478 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002479 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002480 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002481
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
2485exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002486 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002487 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002488 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002489 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002490}
2491/* END_CASE */
2492
2493/* BEGIN_CASE */
2494void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002495 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002496 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002497 int first_part_size_arg,
2498 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002499 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002500{
Ronald Cron5425a212020-08-04 14:58:35 +02002501 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002502 psa_key_type_t key_type = key_type_arg;
2503 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002504 size_t first_part_size = first_part_size_arg;
2505 size_t output1_length = output1_length_arg;
2506 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002507 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002508 size_t output_buffer_size = 0;
2509 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002510 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002511 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002512 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002513
Gilles Peskine8817f612018-12-18 00:18:46 +01002514 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002515
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002516 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2517 psa_set_key_algorithm( &attributes, alg );
2518 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002519
Ronald Cron5425a212020-08-04 14:58:35 +02002520 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2521 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002522
Ronald Cron5425a212020-08-04 14:58:35 +02002523 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002524
Steven Cooreman177deba2020-09-07 17:14:14 +02002525 if( iv->len > 0 )
2526 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002527 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002528 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002529
gabor-mezei-armceface22021-01-21 12:26:17 +01002530 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2531 TEST_ASSERT( output_buffer_size <=
2532 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002533 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002534
Gilles Peskinee0866522019-02-19 19:44:00 +01002535 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002536 PSA_ASSERT( psa_cipher_update( &operation,
2537 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
Gilles Peskine50e586b2018-06-08 14:28:46 +02002586/* BEGIN_CASE */
2587void cipher_decrypt( 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, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002590 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002591{
Ronald Cron5425a212020-08-04 14:58:35 +02002592 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002593 psa_status_t status;
2594 psa_key_type_t key_type = key_type_arg;
2595 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002596 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002597 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002598 size_t output_buffer_size = 0;
2599 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002600 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002601 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002602 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002603
Gilles Peskine8817f612018-12-18 00:18:46 +01002604 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002605
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002606 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2607 psa_set_key_algorithm( &attributes, alg );
2608 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002609
Ronald Cron5425a212020-08-04 14:58:35 +02002610 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2611 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002612
Ronald Cron5425a212020-08-04 14:58:35 +02002613 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002614
Steven Cooreman177deba2020-09-07 17:14:14 +02002615 if( iv->len > 0 )
2616 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002617 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002618 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002619
gabor-mezei-armceface22021-01-21 12:26:17 +01002620 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2621 TEST_ASSERT( output_buffer_size <=
2622 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002623 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002624
Gilles Peskine8817f612018-12-18 00:18:46 +01002625 PSA_ASSERT( psa_cipher_update( &operation,
2626 input->x, input->len,
2627 output, output_buffer_size,
2628 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002629 TEST_ASSERT( function_output_length <=
2630 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2631 TEST_ASSERT( function_output_length <=
2632 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002633 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002634
Gilles Peskine50e586b2018-06-08 14:28:46 +02002635 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002636 ( output_buffer_size == 0 ? NULL :
2637 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002638 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002639 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002640 TEST_ASSERT( function_output_length <=
2641 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2642 TEST_ASSERT( function_output_length <=
2643 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002644 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002645 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002646
2647 if( expected_status == PSA_SUCCESS )
2648 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002649 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002650 ASSERT_COMPARE( expected_output->x, expected_output->len,
2651 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002652 }
2653
Gilles Peskine50e586b2018-06-08 14:28:46 +02002654exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002655 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002656 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002657 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002658 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002659}
2660/* END_CASE */
2661
Gilles Peskine50e586b2018-06-08 14:28:46 +02002662/* BEGIN_CASE */
2663void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002664 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002665 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002666{
Ronald Cron5425a212020-08-04 14:58:35 +02002667 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002668 psa_key_type_t key_type = key_type_arg;
2669 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002670 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002671 size_t iv_size = 16;
2672 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002673 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002674 size_t output1_size = 0;
2675 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002676 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002677 size_t output2_size = 0;
2678 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002679 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002680 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2681 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002682 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002683
Gilles Peskine8817f612018-12-18 00:18:46 +01002684 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002685
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002686 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2687 psa_set_key_algorithm( &attributes, alg );
2688 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002689
Ronald Cron5425a212020-08-04 14:58:35 +02002690 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2691 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002692
Ronald Cron5425a212020-08-04 14:58:35 +02002693 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2694 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002695
Steven Cooreman177deba2020-09-07 17:14:14 +02002696 if( alg != PSA_ALG_ECB_NO_PADDING )
2697 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002698 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2699 iv, iv_size,
2700 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002701 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002702 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2703 TEST_ASSERT( output1_size <=
2704 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002705 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002706
Gilles Peskine8817f612018-12-18 00:18:46 +01002707 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2708 output1, output1_size,
2709 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002710 TEST_ASSERT( output1_length <=
2711 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2712 TEST_ASSERT( output1_length <=
2713 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2714
Gilles Peskine8817f612018-12-18 00:18:46 +01002715 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002716 output1 + output1_length,
2717 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002718 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002719 TEST_ASSERT( function_output_length <=
2720 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2721 TEST_ASSERT( function_output_length <=
2722 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002723
Gilles Peskine048b7f02018-06-08 14:20:49 +02002724 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002725
Gilles Peskine8817f612018-12-18 00:18:46 +01002726 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002727
2728 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002729 TEST_ASSERT( output2_size <=
2730 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2731 TEST_ASSERT( output2_size <=
2732 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002733 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002734
Steven Cooreman177deba2020-09-07 17:14:14 +02002735 if( iv_length > 0 )
2736 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002737 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2738 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002739 }
2740
Gilles Peskine8817f612018-12-18 00:18:46 +01002741 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2742 output2, output2_size,
2743 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002744 TEST_ASSERT( output2_length <=
2745 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
2746 TEST_ASSERT( output2_length <=
2747 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
2748
Gilles Peskine048b7f02018-06-08 14:20:49 +02002749 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002750 PSA_ASSERT( psa_cipher_finish( &operation2,
2751 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002752 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002753 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002754 TEST_ASSERT( function_output_length <=
2755 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2756 TEST_ASSERT( function_output_length <=
2757 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03002758
Gilles Peskine048b7f02018-06-08 14:20:49 +02002759 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002760
Gilles Peskine8817f612018-12-18 00:18:46 +01002761 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002762
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002763 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002764
2765exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002766 psa_cipher_abort( &operation1 );
2767 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002768 mbedtls_free( output1 );
2769 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002770 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002771 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03002772}
2773/* END_CASE */
2774
2775/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002776void cipher_verify_output_multipart( int alg_arg,
2777 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002778 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002779 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002780 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03002781{
Ronald Cron5425a212020-08-04 14:58:35 +02002782 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002783 psa_key_type_t key_type = key_type_arg;
2784 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002785 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002786 unsigned char iv[16] = {0};
2787 size_t iv_size = 16;
2788 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002789 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002790 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002791 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002792 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002793 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002794 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002795 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002796 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2797 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002798 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002799
Gilles Peskine8817f612018-12-18 00:18:46 +01002800 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002801
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002802 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2803 psa_set_key_algorithm( &attributes, alg );
2804 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002805
Ronald Cron5425a212020-08-04 14:58:35 +02002806 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2807 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03002808
Ronald Cron5425a212020-08-04 14:58:35 +02002809 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2810 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002811
Steven Cooreman177deba2020-09-07 17:14:14 +02002812 if( alg != PSA_ALG_ECB_NO_PADDING )
2813 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002814 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2815 iv, iv_size,
2816 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002817 }
2818
gabor-mezei-armceface22021-01-21 12:26:17 +01002819 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2820 TEST_ASSERT( output1_buffer_size <=
2821 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002822 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002823
Gilles Peskinee0866522019-02-19 19:44:00 +01002824 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002825
Gilles Peskine8817f612018-12-18 00:18:46 +01002826 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2827 output1, output1_buffer_size,
2828 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002829 TEST_ASSERT( function_output_length <=
2830 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2831 TEST_ASSERT( function_output_length <=
2832 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002833 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002834
Gilles Peskine8817f612018-12-18 00:18:46 +01002835 PSA_ASSERT( psa_cipher_update( &operation1,
2836 input->x + first_part_size,
2837 input->len - first_part_size,
2838 output1, output1_buffer_size,
2839 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002840 TEST_ASSERT( function_output_length <=
2841 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2842 alg,
2843 input->len - first_part_size ) );
2844 TEST_ASSERT( function_output_length <=
2845 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002846 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002847
Gilles Peskine8817f612018-12-18 00:18:46 +01002848 PSA_ASSERT( psa_cipher_finish( &operation1,
2849 output1 + output1_length,
2850 output1_buffer_size - output1_length,
2851 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002852 TEST_ASSERT( function_output_length <=
2853 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2854 TEST_ASSERT( function_output_length <=
2855 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002856 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002857
Gilles Peskine8817f612018-12-18 00:18:46 +01002858 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002859
Gilles Peskine048b7f02018-06-08 14:20:49 +02002860 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002861 TEST_ASSERT( output2_buffer_size <=
2862 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2863 TEST_ASSERT( output2_buffer_size <=
2864 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002865 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002866
Steven Cooreman177deba2020-09-07 17:14:14 +02002867 if( iv_length > 0 )
2868 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002869 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2870 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002871 }
Moran Pekerded84402018-06-06 16:36:50 +03002872
Gilles Peskine8817f612018-12-18 00:18:46 +01002873 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2874 output2, output2_buffer_size,
2875 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002876 TEST_ASSERT( function_output_length <=
2877 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2878 TEST_ASSERT( function_output_length <=
2879 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002880 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002881
Gilles Peskine8817f612018-12-18 00:18:46 +01002882 PSA_ASSERT( psa_cipher_update( &operation2,
2883 output1 + first_part_size,
2884 output1_length - first_part_size,
2885 output2, output2_buffer_size,
2886 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002887 TEST_ASSERT( function_output_length <=
2888 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2889 alg,
2890 output1_length - first_part_size ) );
2891 TEST_ASSERT( function_output_length <=
2892 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002893 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002894
Gilles Peskine8817f612018-12-18 00:18:46 +01002895 PSA_ASSERT( psa_cipher_finish( &operation2,
2896 output2 + output2_length,
2897 output2_buffer_size - output2_length,
2898 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002899 TEST_ASSERT( function_output_length <=
2900 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2901 TEST_ASSERT( function_output_length <=
2902 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002903 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002904
Gilles Peskine8817f612018-12-18 00:18:46 +01002905 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002906
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002907 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002908
2909exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002910 psa_cipher_abort( &operation1 );
2911 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002912 mbedtls_free( output1 );
2913 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002914 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002915 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002916}
2917/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002918
Gilles Peskine20035e32018-02-03 22:44:14 +01002919/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002920void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002921 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002922 data_t *nonce,
2923 data_t *additional_data,
2924 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002925 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002926{
Ronald Cron5425a212020-08-04 14:58:35 +02002927 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002928 psa_key_type_t key_type = key_type_arg;
2929 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01002930 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002931 unsigned char *output_data = NULL;
2932 size_t output_size = 0;
2933 size_t output_length = 0;
2934 unsigned char *output_data2 = NULL;
2935 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01002936 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002937 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002938 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002939
Gilles Peskine8817f612018-12-18 00:18:46 +01002940 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002941
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002942 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2943 psa_set_key_algorithm( &attributes, alg );
2944 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002945
Gilles Peskine049c7532019-05-15 20:22:09 +02002946 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002947 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01002948 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
2949 key_bits = psa_get_key_bits( &attributes );
2950
2951 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
2952 alg );
2953 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
2954 * should be exact. */
2955 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
2956 expected_result != PSA_ERROR_NOT_SUPPORTED )
2957 {
2958 TEST_EQUAL( output_size,
2959 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
2960 TEST_ASSERT( output_size <=
2961 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
2962 }
2963 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002964
Steven Cooremanf49478b2021-02-15 15:19:25 +01002965 status = psa_aead_encrypt( key, alg,
2966 nonce->x, nonce->len,
2967 additional_data->x,
2968 additional_data->len,
2969 input_data->x, input_data->len,
2970 output_data, output_size,
2971 &output_length );
2972
2973 /* If the operation is not supported, just skip and not fail in case the
2974 * encryption involves a common limitation of cryptography hardwares and
2975 * an alternative implementation. */
2976 if( status == PSA_ERROR_NOT_SUPPORTED )
2977 {
2978 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
2979 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
2980 }
2981
2982 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002983
2984 if( PSA_SUCCESS == expected_result )
2985 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002986 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002987
Gilles Peskine003a4a92019-05-14 16:09:40 +02002988 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
2989 * should be exact. */
2990 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01002991 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02002992
gabor-mezei-armceface22021-01-21 12:26:17 +01002993 TEST_ASSERT( input_data->len <=
2994 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
2995
Ronald Cron5425a212020-08-04 14:58:35 +02002996 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01002997 nonce->x, nonce->len,
2998 additional_data->x,
2999 additional_data->len,
3000 output_data, output_length,
3001 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003002 &output_length2 ),
3003 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003004
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003005 ASSERT_COMPARE( input_data->x, input_data->len,
3006 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003007 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003008
Gilles Peskinea1cac842018-06-11 19:33:02 +02003009exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003010 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003011 mbedtls_free( output_data );
3012 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003013 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003014}
3015/* END_CASE */
3016
3017/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003018void aead_encrypt( int key_type_arg, data_t *key_data,
3019 int alg_arg,
3020 data_t *nonce,
3021 data_t *additional_data,
3022 data_t *input_data,
3023 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003024{
Ronald Cron5425a212020-08-04 14:58:35 +02003025 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003026 psa_key_type_t key_type = key_type_arg;
3027 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003028 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003029 unsigned char *output_data = NULL;
3030 size_t output_size = 0;
3031 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003032 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003033 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003034
Gilles Peskine8817f612018-12-18 00:18:46 +01003035 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003036
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003037 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3038 psa_set_key_algorithm( &attributes, alg );
3039 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003040
Gilles Peskine049c7532019-05-15 20:22:09 +02003041 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003042 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003043 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3044 key_bits = psa_get_key_bits( &attributes );
3045
3046 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3047 alg );
3048 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3049 * should be exact. */
3050 TEST_EQUAL( output_size,
3051 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3052 TEST_ASSERT( output_size <=
3053 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3054 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003055
Steven Cooremand588ea12021-01-11 19:36:04 +01003056 status = psa_aead_encrypt( key, alg,
3057 nonce->x, nonce->len,
3058 additional_data->x, additional_data->len,
3059 input_data->x, input_data->len,
3060 output_data, output_size,
3061 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003062
Ronald Cron28a45ed2021-02-09 20:35:42 +01003063 /* If the operation is not supported, just skip and not fail in case the
3064 * encryption involves a common limitation of cryptography hardwares and
3065 * an alternative implementation. */
3066 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003067 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003068 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3069 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003070 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003071
3072 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003073 ASSERT_COMPARE( expected_result->x, expected_result->len,
3074 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003075
Gilles Peskinea1cac842018-06-11 19:33:02 +02003076exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003077 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003078 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003079 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003080}
3081/* END_CASE */
3082
3083/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003084void aead_decrypt( int key_type_arg, data_t *key_data,
3085 int alg_arg,
3086 data_t *nonce,
3087 data_t *additional_data,
3088 data_t *input_data,
3089 data_t *expected_data,
3090 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003091{
Ronald Cron5425a212020-08-04 14:58:35 +02003092 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003093 psa_key_type_t key_type = key_type_arg;
3094 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003095 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003096 unsigned char *output_data = NULL;
3097 size_t output_size = 0;
3098 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003099 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003100 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003101 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003102
Gilles Peskine8817f612018-12-18 00:18:46 +01003103 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003104
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003105 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3106 psa_set_key_algorithm( &attributes, alg );
3107 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003108
Gilles Peskine049c7532019-05-15 20:22:09 +02003109 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003110 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003111 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3112 key_bits = psa_get_key_bits( &attributes );
3113
3114 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3115 alg );
3116 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3117 expected_result != PSA_ERROR_NOT_SUPPORTED )
3118 {
3119 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3120 * should be exact. */
3121 TEST_EQUAL( output_size,
3122 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3123 TEST_ASSERT( output_size <=
3124 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3125 }
3126 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003127
Steven Cooremand588ea12021-01-11 19:36:04 +01003128 status = psa_aead_decrypt( key, alg,
3129 nonce->x, nonce->len,
3130 additional_data->x,
3131 additional_data->len,
3132 input_data->x, input_data->len,
3133 output_data, output_size,
3134 &output_length );
3135
Ronald Cron28a45ed2021-02-09 20:35:42 +01003136 /* If the operation is not supported, just skip and not fail in case the
3137 * decryption involves a common limitation of cryptography hardwares and
3138 * an alternative implementation. */
3139 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003140 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003141 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3142 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003143 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003144
3145 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003146
Gilles Peskine2d277862018-06-18 15:41:12 +02003147 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003148 ASSERT_COMPARE( expected_data->x, expected_data->len,
3149 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003150
Gilles Peskinea1cac842018-06-11 19:33:02 +02003151exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003152 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003153 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003154 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003155}
3156/* END_CASE */
3157
3158/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003159void signature_size( int type_arg,
3160 int bits,
3161 int alg_arg,
3162 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003163{
3164 psa_key_type_t type = type_arg;
3165 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003166 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003167
Gilles Peskinefe11b722018-12-18 00:24:04 +01003168 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003169
Gilles Peskinee59236f2018-01-27 23:32:46 +01003170exit:
3171 ;
3172}
3173/* END_CASE */
3174
3175/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003176void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3177 int alg_arg, data_t *input_data,
3178 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003179{
Ronald Cron5425a212020-08-04 14:58:35 +02003180 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003181 psa_key_type_t key_type = key_type_arg;
3182 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003183 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003184 unsigned char *signature = NULL;
3185 size_t signature_size;
3186 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003187 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003188
Gilles Peskine8817f612018-12-18 00:18:46 +01003189 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003190
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003191 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003192 psa_set_key_algorithm( &attributes, alg );
3193 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003194
Gilles Peskine049c7532019-05-15 20:22:09 +02003195 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003196 &key ) );
3197 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003198 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003199
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003200 /* Allocate a buffer which has the size advertized by the
3201 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003202 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003203 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003204 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003205 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003206 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003207
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003208 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003209 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003210 input_data->x, input_data->len,
3211 signature, signature_size,
3212 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003213 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003214 ASSERT_COMPARE( output_data->x, output_data->len,
3215 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003216
3217exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003218 /*
3219 * Key attributes may have been returned by psa_get_key_attributes()
3220 * thus reset them as required.
3221 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003222 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003223
Ronald Cron5425a212020-08-04 14:58:35 +02003224 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003225 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003226 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003227}
3228/* END_CASE */
3229
3230/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003231void sign_hash_fail( int key_type_arg, data_t *key_data,
3232 int alg_arg, data_t *input_data,
3233 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003234{
Ronald Cron5425a212020-08-04 14:58:35 +02003235 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003236 psa_key_type_t key_type = key_type_arg;
3237 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003238 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003239 psa_status_t actual_status;
3240 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003241 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003242 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003243 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003244
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003245 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003246
Gilles Peskine8817f612018-12-18 00:18:46 +01003247 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003248
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003249 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003250 psa_set_key_algorithm( &attributes, alg );
3251 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003252
Gilles Peskine049c7532019-05-15 20:22:09 +02003253 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003254 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003255
Ronald Cron5425a212020-08-04 14:58:35 +02003256 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003257 input_data->x, input_data->len,
3258 signature, signature_size,
3259 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003260 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003261 /* The value of *signature_length is unspecified on error, but
3262 * whatever it is, it should be less than signature_size, so that
3263 * if the caller tries to read *signature_length bytes without
3264 * checking the error code then they don't overflow a buffer. */
3265 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003266
3267exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003268 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003269 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003270 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003271 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003272}
3273/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003274
3275/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003276void sign_verify_hash( int key_type_arg, data_t *key_data,
3277 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003278{
Ronald Cron5425a212020-08-04 14:58:35 +02003279 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003280 psa_key_type_t key_type = key_type_arg;
3281 psa_algorithm_t alg = alg_arg;
3282 size_t key_bits;
3283 unsigned char *signature = NULL;
3284 size_t signature_size;
3285 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003286 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003287
Gilles Peskine8817f612018-12-18 00:18:46 +01003288 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003289
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003290 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003291 psa_set_key_algorithm( &attributes, alg );
3292 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003293
Gilles Peskine049c7532019-05-15 20:22:09 +02003294 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003295 &key ) );
3296 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003297 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003298
3299 /* Allocate a buffer which has the size advertized by the
3300 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003301 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003302 key_bits, alg );
3303 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003304 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003305 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003306
3307 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003308 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003309 input_data->x, input_data->len,
3310 signature, signature_size,
3311 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003312 /* Check that the signature length looks sensible. */
3313 TEST_ASSERT( signature_length <= signature_size );
3314 TEST_ASSERT( signature_length > 0 );
3315
3316 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003317 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003318 input_data->x, input_data->len,
3319 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003320
3321 if( input_data->len != 0 )
3322 {
3323 /* Flip a bit in the input and verify that the signature is now
3324 * detected as invalid. Flip a bit at the beginning, not at the end,
3325 * because ECDSA may ignore the last few bits of the input. */
3326 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003327 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003328 input_data->x, input_data->len,
3329 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003330 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003331 }
3332
3333exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003334 /*
3335 * Key attributes may have been returned by psa_get_key_attributes()
3336 * thus reset them as required.
3337 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003338 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003339
Ronald Cron5425a212020-08-04 14:58:35 +02003340 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003341 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003342 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003343}
3344/* END_CASE */
3345
3346/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003347void verify_hash( int key_type_arg, data_t *key_data,
3348 int alg_arg, data_t *hash_data,
3349 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003350{
Ronald Cron5425a212020-08-04 14:58:35 +02003351 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003352 psa_key_type_t key_type = key_type_arg;
3353 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003354 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003355
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003356 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003357
Gilles Peskine8817f612018-12-18 00:18:46 +01003358 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003359
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003360 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003361 psa_set_key_algorithm( &attributes, alg );
3362 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003363
Gilles Peskine049c7532019-05-15 20:22:09 +02003364 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003365 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003366
Ronald Cron5425a212020-08-04 14:58:35 +02003367 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003368 hash_data->x, hash_data->len,
3369 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003370
itayzafrir5c753392018-05-08 11:18:38 +03003371exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003372 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003373 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003374 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003375}
3376/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003377
3378/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003379void verify_hash_fail( int key_type_arg, data_t *key_data,
3380 int alg_arg, data_t *hash_data,
3381 data_t *signature_data,
3382 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003383{
Ronald Cron5425a212020-08-04 14:58:35 +02003384 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003385 psa_key_type_t key_type = key_type_arg;
3386 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003387 psa_status_t actual_status;
3388 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003389 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003390
Gilles Peskine8817f612018-12-18 00:18:46 +01003391 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003392
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003393 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003394 psa_set_key_algorithm( &attributes, alg );
3395 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003396
Gilles Peskine049c7532019-05-15 20:22:09 +02003397 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003398 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003399
Ronald Cron5425a212020-08-04 14:58:35 +02003400 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003401 hash_data->x, hash_data->len,
3402 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003403 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003404
3405exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003406 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003407 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003408 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003409}
3410/* END_CASE */
3411
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003412/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02003413void sign_message_deterministic( int key_type_arg,
3414 data_t *key_data,
3415 int alg_arg,
3416 data_t *input_data,
3417 data_t *output_data )
3418{
3419 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3420 psa_key_type_t key_type = key_type_arg;
3421 psa_algorithm_t alg = alg_arg;
3422 size_t key_bits;
3423 unsigned char *signature = NULL;
3424 size_t signature_size;
3425 size_t signature_length = 0xdeadbeef;
3426 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3427
3428 PSA_ASSERT( psa_crypto_init( ) );
3429
3430 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3431 psa_set_key_algorithm( &attributes, alg );
3432 psa_set_key_type( &attributes, key_type );
3433
3434 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3435 &key ) );
3436 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3437 key_bits = psa_get_key_bits( &attributes );
3438
3439 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3440 TEST_ASSERT( signature_size != 0 );
3441 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3442 ASSERT_ALLOC( signature, signature_size );
3443
3444 PSA_ASSERT( psa_sign_message( key, alg,
3445 input_data->x, input_data->len,
3446 signature, signature_size,
3447 &signature_length ) );
3448
3449 ASSERT_COMPARE( output_data->x, output_data->len,
3450 signature, signature_length );
3451
3452exit:
3453 psa_reset_key_attributes( &attributes );
3454
3455 psa_destroy_key( key );
3456 mbedtls_free( signature );
3457 PSA_DONE( );
3458
3459}
3460/* END_CASE */
3461
3462/* BEGIN_CASE */
3463void sign_message_fail( int key_type_arg,
3464 data_t *key_data,
3465 int alg_arg,
3466 data_t *input_data,
3467 int signature_size_arg,
3468 int expected_status_arg )
3469{
3470 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3471 psa_key_type_t key_type = key_type_arg;
3472 psa_algorithm_t alg = alg_arg;
3473 size_t signature_size = signature_size_arg;
3474 psa_status_t actual_status;
3475 psa_status_t expected_status = expected_status_arg;
3476 unsigned char *signature = NULL;
3477 size_t signature_length = 0xdeadbeef;
3478 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3479
3480 ASSERT_ALLOC( signature, signature_size );
3481
3482 PSA_ASSERT( psa_crypto_init( ) );
3483
3484 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3485 psa_set_key_algorithm( &attributes, alg );
3486 psa_set_key_type( &attributes, key_type );
3487
3488 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3489 &key ) );
3490
3491 actual_status = psa_sign_message( key, alg,
3492 input_data->x, input_data->len,
3493 signature, signature_size,
3494 &signature_length );
3495 TEST_EQUAL( actual_status, expected_status );
3496 /* The value of *signature_length is unspecified on error, but
3497 * whatever it is, it should be less than signature_size, so that
3498 * if the caller tries to read *signature_length bytes without
3499 * checking the error code then they don't overflow a buffer. */
3500 TEST_ASSERT( signature_length <= signature_size );
3501
3502exit:
3503 psa_reset_key_attributes( &attributes );
3504 psa_destroy_key( key );
3505 mbedtls_free( signature );
3506 PSA_DONE( );
3507}
3508/* END_CASE */
3509
3510/* BEGIN_CASE */
3511void sign_verify_message( int key_type_arg,
3512 data_t *key_data,
3513 int alg_arg,
3514 data_t *input_data )
3515{
3516 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3517 psa_key_type_t key_type = key_type_arg;
3518 psa_algorithm_t alg = alg_arg;
3519 size_t key_bits;
3520 unsigned char *signature = NULL;
3521 size_t signature_size;
3522 size_t signature_length = 0xdeadbeef;
3523 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3524
3525 PSA_ASSERT( psa_crypto_init( ) );
3526
3527 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3528 PSA_KEY_USAGE_VERIFY_MESSAGE );
3529 psa_set_key_algorithm( &attributes, alg );
3530 psa_set_key_type( &attributes, key_type );
3531
3532 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3533 &key ) );
3534 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3535 key_bits = psa_get_key_bits( &attributes );
3536
3537 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3538 TEST_ASSERT( signature_size != 0 );
3539 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3540 ASSERT_ALLOC( signature, signature_size );
3541
3542 PSA_ASSERT( psa_sign_message( key, alg,
3543 input_data->x, input_data->len,
3544 signature, signature_size,
3545 &signature_length ) );
3546 TEST_ASSERT( signature_length <= signature_size );
3547 TEST_ASSERT( signature_length > 0 );
3548
3549 PSA_ASSERT( psa_verify_message( key, alg,
3550 input_data->x, input_data->len,
3551 signature, signature_length ) );
3552
3553 if( input_data->len != 0 )
3554 {
3555 /* Flip a bit in the input and verify that the signature is now
3556 * detected as invalid. Flip a bit at the beginning, not at the end,
3557 * because ECDSA may ignore the last few bits of the input. */
3558 input_data->x[0] ^= 1;
3559 TEST_EQUAL( psa_verify_message( key, alg,
3560 input_data->x, input_data->len,
3561 signature, signature_length ),
3562 PSA_ERROR_INVALID_SIGNATURE );
3563 }
3564
3565exit:
3566 psa_reset_key_attributes( &attributes );
3567
3568 psa_destroy_key( key );
3569 mbedtls_free( signature );
3570 PSA_DONE( );
3571}
3572/* END_CASE */
3573
3574/* BEGIN_CASE */
3575void verify_message( int key_type_arg,
3576 data_t *key_data,
3577 int alg_arg,
3578 data_t *input_data,
3579 data_t *signature_data )
3580{
3581 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3582 psa_key_type_t key_type = key_type_arg;
3583 psa_algorithm_t alg = alg_arg;
3584 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3585
3586 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3587
3588 PSA_ASSERT( psa_crypto_init( ) );
3589
3590 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3591 psa_set_key_algorithm( &attributes, alg );
3592 psa_set_key_type( &attributes, key_type );
3593
3594 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3595 &key ) );
3596
3597 PSA_ASSERT( psa_verify_message( key, alg,
3598 input_data->x, input_data->len,
3599 signature_data->x, signature_data->len ) );
3600
3601exit:
3602 psa_reset_key_attributes( &attributes );
3603 psa_destroy_key( key );
3604 PSA_DONE( );
3605}
3606/* END_CASE */
3607
3608/* BEGIN_CASE */
3609void verify_message_fail( int key_type_arg,
3610 data_t *key_data,
3611 int alg_arg,
3612 data_t *hash_data,
3613 data_t *signature_data,
3614 int expected_status_arg )
3615{
3616 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3617 psa_key_type_t key_type = key_type_arg;
3618 psa_algorithm_t alg = alg_arg;
3619 psa_status_t actual_status;
3620 psa_status_t expected_status = expected_status_arg;
3621 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3622
3623 PSA_ASSERT( psa_crypto_init( ) );
3624
3625 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3626 psa_set_key_algorithm( &attributes, alg );
3627 psa_set_key_type( &attributes, key_type );
3628
3629 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3630 &key ) );
3631
3632 actual_status = psa_verify_message( key, alg,
3633 hash_data->x, hash_data->len,
3634 signature_data->x,
3635 signature_data->len );
3636 TEST_EQUAL( actual_status, expected_status );
3637
3638exit:
3639 psa_reset_key_attributes( &attributes );
3640 psa_destroy_key( key );
3641 PSA_DONE( );
3642}
3643/* END_CASE */
3644
3645/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003646void asymmetric_encrypt( int key_type_arg,
3647 data_t *key_data,
3648 int alg_arg,
3649 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003650 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003651 int expected_output_length_arg,
3652 int expected_status_arg )
3653{
Ronald Cron5425a212020-08-04 14:58:35 +02003654 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003655 psa_key_type_t key_type = key_type_arg;
3656 psa_algorithm_t alg = alg_arg;
3657 size_t expected_output_length = expected_output_length_arg;
3658 size_t key_bits;
3659 unsigned char *output = NULL;
3660 size_t output_size;
3661 size_t output_length = ~0;
3662 psa_status_t actual_status;
3663 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003664 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003665
Gilles Peskine8817f612018-12-18 00:18:46 +01003666 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003667
Gilles Peskine656896e2018-06-29 19:12:28 +02003668 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003669 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3670 psa_set_key_algorithm( &attributes, alg );
3671 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003672 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003673 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003674
3675 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003676 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003677 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003678
Gilles Peskine656896e2018-06-29 19:12:28 +02003679 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003680 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003681 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003682
3683 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003684 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003685 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003686 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003687 output, output_size,
3688 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003689 TEST_EQUAL( actual_status, expected_status );
3690 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003691
Gilles Peskine68428122018-06-30 18:42:41 +02003692 /* If the label is empty, the test framework puts a non-null pointer
3693 * in label->x. Test that a null pointer works as well. */
3694 if( label->len == 0 )
3695 {
3696 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003697 if( output_size != 0 )
3698 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003699 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003700 input_data->x, input_data->len,
3701 NULL, label->len,
3702 output, output_size,
3703 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003704 TEST_EQUAL( actual_status, expected_status );
3705 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003706 }
3707
Gilles Peskine656896e2018-06-29 19:12:28 +02003708exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003709 /*
3710 * Key attributes may have been returned by psa_get_key_attributes()
3711 * thus reset them as required.
3712 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003713 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003714
Ronald Cron5425a212020-08-04 14:58:35 +02003715 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02003716 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003717 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003718}
3719/* END_CASE */
3720
3721/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003722void asymmetric_encrypt_decrypt( int key_type_arg,
3723 data_t *key_data,
3724 int alg_arg,
3725 data_t *input_data,
3726 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003727{
Ronald Cron5425a212020-08-04 14:58:35 +02003728 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003729 psa_key_type_t key_type = key_type_arg;
3730 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003731 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003732 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003733 size_t output_size;
3734 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003735 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003736 size_t output2_size;
3737 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003738 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003739
Gilles Peskine8817f612018-12-18 00:18:46 +01003740 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003741
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003742 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3743 psa_set_key_algorithm( &attributes, alg );
3744 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003745
Gilles Peskine049c7532019-05-15 20:22:09 +02003746 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003747 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003748
3749 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02003750 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003751 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003752
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003753 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003754 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003755 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01003756
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003757 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01003758 TEST_ASSERT( output2_size <=
3759 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
3760 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003761 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003762
Gilles Peskineeebd7382018-06-08 18:11:54 +02003763 /* We test encryption by checking that encrypt-then-decrypt gives back
3764 * the original plaintext because of the non-optional random
3765 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02003766 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003767 input_data->x, input_data->len,
3768 label->x, label->len,
3769 output, output_size,
3770 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003771 /* We don't know what ciphertext length to expect, but check that
3772 * it looks sensible. */
3773 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003774
Ronald Cron5425a212020-08-04 14:58:35 +02003775 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003776 output, output_length,
3777 label->x, label->len,
3778 output2, output2_size,
3779 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003780 ASSERT_COMPARE( input_data->x, input_data->len,
3781 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003782
3783exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003784 /*
3785 * Key attributes may have been returned by psa_get_key_attributes()
3786 * thus reset them as required.
3787 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003788 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003789
Ronald Cron5425a212020-08-04 14:58:35 +02003790 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003791 mbedtls_free( output );
3792 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003793 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003794}
3795/* END_CASE */
3796
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003797/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003798void asymmetric_decrypt( int key_type_arg,
3799 data_t *key_data,
3800 int alg_arg,
3801 data_t *input_data,
3802 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003803 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003804{
Ronald Cron5425a212020-08-04 14:58:35 +02003805 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003806 psa_key_type_t key_type = key_type_arg;
3807 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01003808 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003809 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003810 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003811 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003812 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003813
Gilles Peskine8817f612018-12-18 00:18:46 +01003814 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003815
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003816 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3817 psa_set_key_algorithm( &attributes, alg );
3818 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003819
Gilles Peskine049c7532019-05-15 20:22:09 +02003820 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003821 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003822
gabor-mezei-armceface22021-01-21 12:26:17 +01003823 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3824 key_bits = psa_get_key_bits( &attributes );
3825
3826 /* Determine the maximum ciphertext length */
3827 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
3828 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
3829 ASSERT_ALLOC( output, output_size );
3830
Ronald Cron5425a212020-08-04 14:58:35 +02003831 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003832 input_data->x, input_data->len,
3833 label->x, label->len,
3834 output,
3835 output_size,
3836 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003837 ASSERT_COMPARE( expected_data->x, expected_data->len,
3838 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003839
Gilles Peskine68428122018-06-30 18:42:41 +02003840 /* If the label is empty, the test framework puts a non-null pointer
3841 * in label->x. Test that a null pointer works as well. */
3842 if( label->len == 0 )
3843 {
3844 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003845 if( output_size != 0 )
3846 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003847 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003848 input_data->x, input_data->len,
3849 NULL, label->len,
3850 output,
3851 output_size,
3852 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003853 ASSERT_COMPARE( expected_data->x, expected_data->len,
3854 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003855 }
3856
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003857exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003858 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003859 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003860 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003861 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003862}
3863/* END_CASE */
3864
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003865/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003866void asymmetric_decrypt_fail( int key_type_arg,
3867 data_t *key_data,
3868 int alg_arg,
3869 data_t *input_data,
3870 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00003871 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02003872 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003873{
Ronald Cron5425a212020-08-04 14:58:35 +02003874 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003875 psa_key_type_t key_type = key_type_arg;
3876 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003877 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00003878 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003879 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003880 psa_status_t actual_status;
3881 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003882 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003883
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003884 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003885
Gilles Peskine8817f612018-12-18 00:18:46 +01003886 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003887
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003888 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3889 psa_set_key_algorithm( &attributes, alg );
3890 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003891
Gilles Peskine049c7532019-05-15 20:22:09 +02003892 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003893 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003894
Ronald Cron5425a212020-08-04 14:58:35 +02003895 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003896 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003897 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003898 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003899 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003900 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003901 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003902
Gilles Peskine68428122018-06-30 18:42:41 +02003903 /* If the label is empty, the test framework puts a non-null pointer
3904 * in label->x. Test that a null pointer works as well. */
3905 if( label->len == 0 )
3906 {
3907 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003908 if( output_size != 0 )
3909 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003910 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003911 input_data->x, input_data->len,
3912 NULL, label->len,
3913 output, output_size,
3914 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003915 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003916 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003917 }
3918
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003919exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003920 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003921 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02003922 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003923 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003924}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003925/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003926
3927/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003928void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00003929{
3930 /* Test each valid way of initializing the object, except for `= {0}`, as
3931 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3932 * though it's OK by the C standard. We could test for this, but we'd need
3933 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003934 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003935 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
3936 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
3937 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00003938
3939 memset( &zero, 0, sizeof( zero ) );
3940
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003941 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003942 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003943 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003944 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003945 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003946 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003947 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003948
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003949 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003950 PSA_ASSERT( psa_key_derivation_abort(&func) );
3951 PSA_ASSERT( psa_key_derivation_abort(&init) );
3952 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00003953}
3954/* END_CASE */
3955
Janos Follath16de4a42019-06-13 16:32:24 +01003956/* BEGIN_CASE */
3957void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02003958{
Gilles Peskineea0fb492018-07-12 17:17:20 +02003959 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003960 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003961 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003962
Gilles Peskine8817f612018-12-18 00:18:46 +01003963 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003964
Janos Follath16de4a42019-06-13 16:32:24 +01003965 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003966 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003967
3968exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003969 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003970 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003971}
3972/* END_CASE */
3973
Janos Follathaf3c2a02019-06-12 12:34:34 +01003974/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01003975void derive_set_capacity( int alg_arg, int capacity_arg,
3976 int expected_status_arg )
3977{
3978 psa_algorithm_t alg = alg_arg;
3979 size_t capacity = capacity_arg;
3980 psa_status_t expected_status = expected_status_arg;
3981 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
3982
3983 PSA_ASSERT( psa_crypto_init( ) );
3984
3985 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
3986
3987 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
3988 expected_status );
3989
3990exit:
3991 psa_key_derivation_abort( &operation );
3992 PSA_DONE( );
3993}
3994/* END_CASE */
3995
3996/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01003997void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02003998 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01003999 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004000 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004001 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004002 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004003 int expected_status_arg3,
4004 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004005{
4006 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004007 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4008 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004009 psa_status_t expected_statuses[] = {expected_status_arg1,
4010 expected_status_arg2,
4011 expected_status_arg3};
4012 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004013 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4014 MBEDTLS_SVC_KEY_ID_INIT,
4015 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004016 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4017 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4018 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004019 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004020 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004021 psa_status_t expected_output_status = expected_output_status_arg;
4022 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004023
4024 PSA_ASSERT( psa_crypto_init( ) );
4025
4026 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4027 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004028
4029 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4030
4031 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4032 {
Gilles Peskine4023c012021-05-27 13:21:20 +02004033 mbedtls_test_set_step( i );
4034 if( steps[i] == 0 )
4035 {
4036 /* Skip this step */
4037 }
4038 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004039 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004040 psa_set_key_type( &attributes, key_types[i] );
4041 PSA_ASSERT( psa_import_key( &attributes,
4042 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004043 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004044 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4045 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4046 {
4047 // When taking a private key as secret input, use key agreement
4048 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004049 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4050 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004051 expected_statuses[i] );
4052 }
4053 else
4054 {
4055 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004056 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004057 expected_statuses[i] );
4058 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004059 }
4060 else
4061 {
4062 TEST_EQUAL( psa_key_derivation_input_bytes(
4063 &operation, steps[i],
4064 inputs[i]->x, inputs[i]->len ),
4065 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004066 }
4067 }
4068
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004069 if( output_key_type != PSA_KEY_TYPE_NONE )
4070 {
4071 psa_reset_key_attributes( &attributes );
4072 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4073 psa_set_key_bits( &attributes, 8 );
4074 actual_output_status =
4075 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004076 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004077 }
4078 else
4079 {
4080 uint8_t buffer[1];
4081 actual_output_status =
4082 psa_key_derivation_output_bytes( &operation,
4083 buffer, sizeof( buffer ) );
4084 }
4085 TEST_EQUAL( actual_output_status, expected_output_status );
4086
Janos Follathaf3c2a02019-06-12 12:34:34 +01004087exit:
4088 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004089 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4090 psa_destroy_key( keys[i] );
4091 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004092 PSA_DONE( );
4093}
4094/* END_CASE */
4095
Janos Follathd958bb72019-07-03 15:02:16 +01004096/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02004097void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004098{
Janos Follathd958bb72019-07-03 15:02:16 +01004099 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004100 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004101 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004102 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004103 unsigned char input1[] = "Input 1";
4104 size_t input1_length = sizeof( input1 );
4105 unsigned char input2[] = "Input 2";
4106 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004107 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004108 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004109 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4110 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4111 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004112 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004113
Gilles Peskine8817f612018-12-18 00:18:46 +01004114 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004115
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004116 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4117 psa_set_key_algorithm( &attributes, alg );
4118 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004119
Gilles Peskine73676cb2019-05-15 20:15:10 +02004120 PSA_ASSERT( psa_import_key( &attributes,
4121 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004122 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004123
4124 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004125 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4126 input1, input1_length,
4127 input2, input2_length,
4128 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004129 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004130
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004131 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004132 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004133 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004134
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004135 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004136
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004137 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004138 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004139
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004140exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004141 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004142 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004143 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004144}
4145/* END_CASE */
4146
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004147/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02004148void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004149{
4150 uint8_t output_buffer[16];
4151 size_t buffer_size = 16;
4152 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004153 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004154
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004155 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4156 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004157 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004158
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004159 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004160 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004161
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004162 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004163
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004164 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4165 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004166 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004167
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004168 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004169 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004170
4171exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004172 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004173}
4174/* END_CASE */
4175
4176/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004177void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004178 int step1_arg, data_t *input1,
4179 int step2_arg, data_t *input2,
4180 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004181 int requested_capacity_arg,
4182 data_t *expected_output1,
4183 data_t *expected_output2 )
4184{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004185 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004186 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4187 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004188 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4189 MBEDTLS_SVC_KEY_ID_INIT,
4190 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004191 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004192 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004193 uint8_t *expected_outputs[2] =
4194 {expected_output1->x, expected_output2->x};
4195 size_t output_sizes[2] =
4196 {expected_output1->len, expected_output2->len};
4197 size_t output_buffer_size = 0;
4198 uint8_t *output_buffer = NULL;
4199 size_t expected_capacity;
4200 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004201 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004202 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004203 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004204
4205 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4206 {
4207 if( output_sizes[i] > output_buffer_size )
4208 output_buffer_size = output_sizes[i];
4209 if( output_sizes[i] == 0 )
4210 expected_outputs[i] = NULL;
4211 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004212 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004213 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004214
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004215 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4216 psa_set_key_algorithm( &attributes, alg );
4217 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004218
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004219 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004220 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4221 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4222 requested_capacity ) );
4223 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004224 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004225 switch( steps[i] )
4226 {
4227 case 0:
4228 break;
4229 case PSA_KEY_DERIVATION_INPUT_SECRET:
4230 PSA_ASSERT( psa_import_key( &attributes,
4231 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004232 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004233
4234 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4235 {
4236 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4237 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4238 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4239 }
4240
Gilles Peskine1468da72019-05-29 17:35:49 +02004241 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004242 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004243 break;
4244 default:
4245 PSA_ASSERT( psa_key_derivation_input_bytes(
4246 &operation, steps[i],
4247 inputs[i]->x, inputs[i]->len ) );
4248 break;
4249 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004250 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004251
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004252 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004253 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004254 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004255 expected_capacity = requested_capacity;
4256
4257 /* Expansion phase. */
4258 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4259 {
4260 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004261 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004262 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004263 if( expected_capacity == 0 && output_sizes[i] == 0 )
4264 {
4265 /* Reading 0 bytes when 0 bytes are available can go either way. */
4266 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004267 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004268 continue;
4269 }
4270 else if( expected_capacity == 0 ||
4271 output_sizes[i] > expected_capacity )
4272 {
4273 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004274 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004275 expected_capacity = 0;
4276 continue;
4277 }
4278 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004279 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004280 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004281 ASSERT_COMPARE( output_buffer, output_sizes[i],
4282 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004283 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004284 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004285 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004286 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004287 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004288 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004289 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004290
4291exit:
4292 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004293 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004294 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4295 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004296 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004297}
4298/* END_CASE */
4299
4300/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004301void derive_full( int alg_arg,
4302 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004303 data_t *input1,
4304 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004305 int requested_capacity_arg )
4306{
Ronald Cron5425a212020-08-04 14:58:35 +02004307 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004308 psa_algorithm_t alg = alg_arg;
4309 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004310 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004311 unsigned char output_buffer[16];
4312 size_t expected_capacity = requested_capacity;
4313 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004314 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004315
Gilles Peskine8817f612018-12-18 00:18:46 +01004316 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004317
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004318 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4319 psa_set_key_algorithm( &attributes, alg );
4320 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004321
Gilles Peskine049c7532019-05-15 20:22:09 +02004322 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004323 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004324
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004325 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4326 input1->x, input1->len,
4327 input2->x, input2->len,
4328 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004329 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004330
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004331 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004332 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004333 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004334
4335 /* Expansion phase. */
4336 while( current_capacity > 0 )
4337 {
4338 size_t read_size = sizeof( output_buffer );
4339 if( read_size > current_capacity )
4340 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004341 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004342 output_buffer,
4343 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004344 expected_capacity -= read_size;
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, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004348 }
4349
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004350 /* Check that the operation refuses to go over capacity. */
4351 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004352 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004353
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004354 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004355
4356exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004357 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004358 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004359 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004360}
4361/* END_CASE */
4362
Janos Follathe60c9052019-07-03 13:51:30 +01004363/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004364void derive_key_exercise( int alg_arg,
4365 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004366 data_t *input1,
4367 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004368 int derived_type_arg,
4369 int derived_bits_arg,
4370 int derived_usage_arg,
4371 int derived_alg_arg )
4372{
Ronald Cron5425a212020-08-04 14:58:35 +02004373 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4374 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004375 psa_algorithm_t alg = alg_arg;
4376 psa_key_type_t derived_type = derived_type_arg;
4377 size_t derived_bits = derived_bits_arg;
4378 psa_key_usage_t derived_usage = derived_usage_arg;
4379 psa_algorithm_t derived_alg = derived_alg_arg;
4380 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004381 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004382 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004383 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004384
Gilles Peskine8817f612018-12-18 00:18:46 +01004385 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004386
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004387 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4388 psa_set_key_algorithm( &attributes, alg );
4389 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004390 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004391 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004392
4393 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004394 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4395 input1->x, input1->len,
4396 input2->x, input2->len,
4397 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004398 goto exit;
4399
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004400 psa_set_key_usage_flags( &attributes, derived_usage );
4401 psa_set_key_algorithm( &attributes, derived_alg );
4402 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004403 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004404 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004405 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004406
4407 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004408 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004409 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4410 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004411
4412 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004413 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004414 goto exit;
4415
4416exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004417 /*
4418 * Key attributes may have been returned by psa_get_key_attributes()
4419 * thus reset them as required.
4420 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004421 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004422
4423 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004424 psa_destroy_key( base_key );
4425 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004426 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004427}
4428/* END_CASE */
4429
Janos Follath42fd8882019-07-03 14:17:09 +01004430/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004431void derive_key_export( int alg_arg,
4432 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004433 data_t *input1,
4434 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004435 int bytes1_arg,
4436 int bytes2_arg )
4437{
Ronald Cron5425a212020-08-04 14:58:35 +02004438 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4439 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004440 psa_algorithm_t alg = alg_arg;
4441 size_t bytes1 = bytes1_arg;
4442 size_t bytes2 = bytes2_arg;
4443 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004444 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004445 uint8_t *output_buffer = NULL;
4446 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004447 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4448 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004449 size_t length;
4450
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004451 ASSERT_ALLOC( output_buffer, capacity );
4452 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004453 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004454
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004455 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4456 psa_set_key_algorithm( &base_attributes, alg );
4457 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004458 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004459 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004460
4461 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004462 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4463 input1->x, input1->len,
4464 input2->x, input2->len,
4465 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004466 goto exit;
4467
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004468 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004469 output_buffer,
4470 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004471 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004472
4473 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004474 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4475 input1->x, input1->len,
4476 input2->x, input2->len,
4477 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004478 goto exit;
4479
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004480 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4481 psa_set_key_algorithm( &derived_attributes, 0 );
4482 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004483 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004484 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004485 &derived_key ) );
4486 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004487 export_buffer, bytes1,
4488 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004489 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004490 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004491 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004492 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004493 &derived_key ) );
4494 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004495 export_buffer + bytes1, bytes2,
4496 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004497 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004498
4499 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004500 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4501 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004502
4503exit:
4504 mbedtls_free( output_buffer );
4505 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004506 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004507 psa_destroy_key( base_key );
4508 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004509 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004510}
4511/* END_CASE */
4512
4513/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004514void derive_key( int alg_arg,
4515 data_t *key_data, data_t *input1, data_t *input2,
4516 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004517 int expected_status_arg,
4518 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004519{
Ronald Cron5425a212020-08-04 14:58:35 +02004520 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4521 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004522 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004523 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004524 size_t bits = bits_arg;
4525 psa_status_t expected_status = expected_status_arg;
4526 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4527 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4528 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4529
4530 PSA_ASSERT( psa_crypto_init( ) );
4531
4532 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4533 psa_set_key_algorithm( &base_attributes, alg );
4534 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4535 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004536 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004537
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004538 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4539 input1->x, input1->len,
4540 input2->x, input2->len,
4541 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004542 goto exit;
4543
4544 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4545 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004546 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004547 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004548
4549 psa_status_t status =
4550 psa_key_derivation_output_key( &derived_attributes,
4551 &operation,
4552 &derived_key );
4553 if( is_large_output > 0 )
4554 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4555 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004556
4557exit:
4558 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004559 psa_destroy_key( base_key );
4560 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004561 PSA_DONE( );
4562}
4563/* END_CASE */
4564
4565/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004566void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004567 int our_key_type_arg, int our_key_alg_arg,
4568 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004569 int expected_status_arg )
4570{
Ronald Cron5425a212020-08-04 14:58:35 +02004571 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004572 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004573 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004574 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004575 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004576 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004577 psa_status_t expected_status = expected_status_arg;
4578 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004579
Gilles Peskine8817f612018-12-18 00:18:46 +01004580 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004581
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004582 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004583 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004584 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004585 PSA_ASSERT( psa_import_key( &attributes,
4586 our_key_data->x, our_key_data->len,
4587 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004588
Gilles Peskine77f40d82019-04-11 21:27:06 +02004589 /* The tests currently include inputs that should fail at either step.
4590 * Test cases that fail at the setup step should be changed to call
4591 * key_derivation_setup instead, and this function should be renamed
4592 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004593 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004594 if( status == PSA_SUCCESS )
4595 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004596 TEST_EQUAL( psa_key_derivation_key_agreement(
4597 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4598 our_key,
4599 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004600 expected_status );
4601 }
4602 else
4603 {
4604 TEST_ASSERT( status == expected_status );
4605 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004606
4607exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004608 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004609 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004610 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004611}
4612/* END_CASE */
4613
4614/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004615void raw_key_agreement( int alg_arg,
4616 int our_key_type_arg, data_t *our_key_data,
4617 data_t *peer_key_data,
4618 data_t *expected_output )
4619{
Ronald Cron5425a212020-08-04 14:58:35 +02004620 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004621 psa_algorithm_t alg = alg_arg;
4622 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004623 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004624 unsigned char *output = NULL;
4625 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004626 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004627
4628 ASSERT_ALLOC( output, expected_output->len );
4629 PSA_ASSERT( psa_crypto_init( ) );
4630
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004631 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4632 psa_set_key_algorithm( &attributes, alg );
4633 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004634 PSA_ASSERT( psa_import_key( &attributes,
4635 our_key_data->x, our_key_data->len,
4636 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004637
gabor-mezei-armceface22021-01-21 12:26:17 +01004638 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4639 key_bits = psa_get_key_bits( &attributes );
4640
Gilles Peskinebe697d82019-05-16 18:00:41 +02004641 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4642 peer_key_data->x, peer_key_data->len,
4643 output, expected_output->len,
4644 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004645 ASSERT_COMPARE( output, output_length,
4646 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01004647 TEST_ASSERT( output_length <=
4648 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
4649 TEST_ASSERT( output_length <=
4650 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004651
4652exit:
4653 mbedtls_free( output );
4654 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004655 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004656}
4657/* END_CASE */
4658
4659/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004660void key_agreement_capacity( int alg_arg,
4661 int our_key_type_arg, data_t *our_key_data,
4662 data_t *peer_key_data,
4663 int expected_capacity_arg )
4664{
Ronald Cron5425a212020-08-04 14:58:35 +02004665 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004666 psa_algorithm_t alg = alg_arg;
4667 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 Peskine59685592018-09-18 12:11:34 +02004670 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004671 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004672
Gilles Peskine8817f612018-12-18 00:18:46 +01004673 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004674
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004675 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4676 psa_set_key_algorithm( &attributes, alg );
4677 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 Peskine59685592018-09-18 12:11:34 +02004681
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004682 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004683 PSA_ASSERT( psa_key_derivation_key_agreement(
4684 &operation,
4685 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4686 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004687 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4688 {
4689 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004690 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004691 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004692 NULL, 0 ) );
4693 }
Gilles Peskine59685592018-09-18 12:11:34 +02004694
Gilles Peskinebf491972018-10-25 22:36:12 +02004695 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004696 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004697 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004698 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004699
Gilles Peskinebf491972018-10-25 22:36:12 +02004700 /* Test the actual capacity by reading the output. */
4701 while( actual_capacity > sizeof( output ) )
4702 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004703 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004704 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004705 actual_capacity -= sizeof( output );
4706 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004707 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004708 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004709 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004710 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004711
Gilles Peskine59685592018-09-18 12:11:34 +02004712exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004713 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004714 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004715 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004716}
4717/* END_CASE */
4718
4719/* BEGIN_CASE */
4720void key_agreement_output( int alg_arg,
4721 int our_key_type_arg, data_t *our_key_data,
4722 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004723 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004724{
Ronald Cron5425a212020-08-04 14:58:35 +02004725 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004726 psa_algorithm_t alg = alg_arg;
4727 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004728 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004729 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004730 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004731
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004732 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4733 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004734
Gilles Peskine8817f612018-12-18 00:18:46 +01004735 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004736
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004737 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4738 psa_set_key_algorithm( &attributes, alg );
4739 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004740 PSA_ASSERT( psa_import_key( &attributes,
4741 our_key_data->x, our_key_data->len,
4742 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004743
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004744 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004745 PSA_ASSERT( psa_key_derivation_key_agreement(
4746 &operation,
4747 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4748 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004749 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4750 {
4751 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004752 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004753 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004754 NULL, 0 ) );
4755 }
Gilles Peskine59685592018-09-18 12:11:34 +02004756
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004757 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004758 actual_output,
4759 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004760 ASSERT_COMPARE( actual_output, expected_output1->len,
4761 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004762 if( expected_output2->len != 0 )
4763 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004764 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004765 actual_output,
4766 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004767 ASSERT_COMPARE( actual_output, expected_output2->len,
4768 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004769 }
Gilles Peskine59685592018-09-18 12:11:34 +02004770
4771exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004772 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004773 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004774 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004775 mbedtls_free( actual_output );
4776}
4777/* END_CASE */
4778
4779/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004780void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004781{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004782 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004783 unsigned char *output = NULL;
4784 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004785 size_t i;
4786 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004787
Simon Butcher49f8e312020-03-03 15:51:50 +00004788 TEST_ASSERT( bytes_arg >= 0 );
4789
Gilles Peskine91892022021-02-08 19:50:26 +01004790 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004791 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02004792
Gilles Peskine8817f612018-12-18 00:18:46 +01004793 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004794
Gilles Peskinea50d7392018-06-21 10:22:13 +02004795 /* Run several times, to ensure that every output byte will be
4796 * nonzero at least once with overwhelming probability
4797 * (2^(-8*number_of_runs)). */
4798 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004799 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004800 if( bytes != 0 )
4801 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004802 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004803
Gilles Peskinea50d7392018-06-21 10:22:13 +02004804 for( i = 0; i < bytes; i++ )
4805 {
4806 if( output[i] != 0 )
4807 ++changed[i];
4808 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004809 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004810
4811 /* Check that every byte was changed to nonzero at least once. This
4812 * validates that psa_generate_random is overwriting every byte of
4813 * the output buffer. */
4814 for( i = 0; i < bytes; i++ )
4815 {
4816 TEST_ASSERT( changed[i] != 0 );
4817 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004818
4819exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004820 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004821 mbedtls_free( output );
4822 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004823}
4824/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004825
4826/* BEGIN_CASE */
4827void generate_key( int type_arg,
4828 int bits_arg,
4829 int usage_arg,
4830 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004831 int expected_status_arg,
4832 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004833{
Ronald Cron5425a212020-08-04 14:58:35 +02004834 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004835 psa_key_type_t type = type_arg;
4836 psa_key_usage_t usage = usage_arg;
4837 size_t bits = bits_arg;
4838 psa_algorithm_t alg = alg_arg;
4839 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004840 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004841 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004842
Gilles Peskine8817f612018-12-18 00:18:46 +01004843 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004844
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004845 psa_set_key_usage_flags( &attributes, usage );
4846 psa_set_key_algorithm( &attributes, alg );
4847 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004848 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004849
4850 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01004851 psa_status_t status = psa_generate_key( &attributes, &key );
4852
4853 if( is_large_key > 0 )
4854 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4855 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004856 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004857 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004858
4859 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004860 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004861 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
4862 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004863
Gilles Peskine818ca122018-06-20 18:16:48 +02004864 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004865 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004866 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004867
4868exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004869 /*
4870 * Key attributes may have been returned by psa_get_key_attributes()
4871 * thus reset them as required.
4872 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004873 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004874
Ronald Cron5425a212020-08-04 14:58:35 +02004875 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004876 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004877}
4878/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004879
Ronald Cronee414c72021-03-18 18:50:08 +01004880/* 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 +02004881void generate_key_rsa( int bits_arg,
4882 data_t *e_arg,
4883 int expected_status_arg )
4884{
Ronald Cron5425a212020-08-04 14:58:35 +02004885 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004886 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02004887 size_t bits = bits_arg;
4888 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
4889 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
4890 psa_status_t expected_status = expected_status_arg;
4891 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4892 uint8_t *exported = NULL;
4893 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004894 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004895 size_t exported_length = SIZE_MAX;
4896 uint8_t *e_read_buffer = NULL;
4897 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02004898 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004899 size_t e_read_length = SIZE_MAX;
4900
4901 if( e_arg->len == 0 ||
4902 ( e_arg->len == 3 &&
4903 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
4904 {
4905 is_default_public_exponent = 1;
4906 e_read_size = 0;
4907 }
4908 ASSERT_ALLOC( e_read_buffer, e_read_size );
4909 ASSERT_ALLOC( exported, exported_size );
4910
4911 PSA_ASSERT( psa_crypto_init( ) );
4912
4913 psa_set_key_usage_flags( &attributes, usage );
4914 psa_set_key_algorithm( &attributes, alg );
4915 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
4916 e_arg->x, e_arg->len ) );
4917 psa_set_key_bits( &attributes, bits );
4918
4919 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02004920 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004921 if( expected_status != PSA_SUCCESS )
4922 goto exit;
4923
4924 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004925 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004926 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4927 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4928 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
4929 e_read_buffer, e_read_size,
4930 &e_read_length ) );
4931 if( is_default_public_exponent )
4932 TEST_EQUAL( e_read_length, 0 );
4933 else
4934 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
4935
4936 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004937 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02004938 goto exit;
4939
4940 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02004941 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02004942 exported, exported_size,
4943 &exported_length ) );
4944 {
4945 uint8_t *p = exported;
4946 uint8_t *end = exported + exported_length;
4947 size_t len;
4948 /* RSAPublicKey ::= SEQUENCE {
4949 * modulus INTEGER, -- n
4950 * publicExponent INTEGER } -- e
4951 */
4952 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004953 MBEDTLS_ASN1_SEQUENCE |
4954 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01004955 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004956 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
4957 MBEDTLS_ASN1_INTEGER ) );
4958 if( len >= 1 && p[0] == 0 )
4959 {
4960 ++p;
4961 --len;
4962 }
4963 if( e_arg->len == 0 )
4964 {
4965 TEST_EQUAL( len, 3 );
4966 TEST_EQUAL( p[0], 1 );
4967 TEST_EQUAL( p[1], 0 );
4968 TEST_EQUAL( p[2], 1 );
4969 }
4970 else
4971 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
4972 }
4973
4974exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004975 /*
4976 * Key attributes may have been returned by psa_get_key_attributes() or
4977 * set by psa_set_key_domain_parameters() thus reset them as required.
4978 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02004979 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004980
Ronald Cron5425a212020-08-04 14:58:35 +02004981 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004982 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004983 mbedtls_free( e_read_buffer );
4984 mbedtls_free( exported );
4985}
4986/* END_CASE */
4987
Darryl Greend49a4992018-06-18 17:27:26 +01004988/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004989void persistent_key_load_key_from_storage( data_t *data,
4990 int type_arg, int bits_arg,
4991 int usage_flags_arg, int alg_arg,
4992 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01004993{
Ronald Cron71016a92020-08-28 19:01:50 +02004994 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004995 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02004996 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4997 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004998 psa_key_type_t type = type_arg;
4999 size_t bits = bits_arg;
5000 psa_key_usage_t usage_flags = usage_flags_arg;
5001 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005002 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005003 unsigned char *first_export = NULL;
5004 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005005 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005006 size_t first_exported_length;
5007 size_t second_exported_length;
5008
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005009 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5010 {
5011 ASSERT_ALLOC( first_export, export_size );
5012 ASSERT_ALLOC( second_export, export_size );
5013 }
Darryl Greend49a4992018-06-18 17:27:26 +01005014
Gilles Peskine8817f612018-12-18 00:18:46 +01005015 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005016
Gilles Peskinec87af662019-05-15 16:12:22 +02005017 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005018 psa_set_key_usage_flags( &attributes, usage_flags );
5019 psa_set_key_algorithm( &attributes, alg );
5020 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005021 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005022
Darryl Green0c6575a2018-11-07 16:05:30 +00005023 switch( generation_method )
5024 {
5025 case IMPORT_KEY:
5026 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005027 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005028 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005029 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005030
Darryl Green0c6575a2018-11-07 16:05:30 +00005031 case GENERATE_KEY:
5032 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005033 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005034 break;
5035
5036 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005037#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005038 {
5039 /* Create base key */
5040 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5041 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5042 psa_set_key_usage_flags( &base_attributes,
5043 PSA_KEY_USAGE_DERIVE );
5044 psa_set_key_algorithm( &base_attributes, derive_alg );
5045 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005046 PSA_ASSERT( psa_import_key( &base_attributes,
5047 data->x, data->len,
5048 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005049 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005050 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005051 PSA_ASSERT( psa_key_derivation_input_key(
5052 &operation,
5053 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005054 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005055 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005056 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005057 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5058 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005059 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005060 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005061 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005062 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005063 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005064#else
5065 TEST_ASSUME( ! "KDF not supported in this configuration" );
5066#endif
5067 break;
5068
5069 default:
5070 TEST_ASSERT( ! "generation_method not implemented in test" );
5071 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005072 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005073 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005074
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005075 /* Export the key if permitted by the key policy. */
5076 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5077 {
Ronald Cron5425a212020-08-04 14:58:35 +02005078 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005079 first_export, export_size,
5080 &first_exported_length ) );
5081 if( generation_method == IMPORT_KEY )
5082 ASSERT_COMPARE( data->x, data->len,
5083 first_export, first_exported_length );
5084 }
Darryl Greend49a4992018-06-18 17:27:26 +01005085
5086 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005087 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005088 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005089 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005090
Darryl Greend49a4992018-06-18 17:27:26 +01005091 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005092 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005093 TEST_ASSERT( mbedtls_svc_key_id_equal(
5094 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005095 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5096 PSA_KEY_LIFETIME_PERSISTENT );
5097 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5098 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5099 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5100 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005101
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005102 /* Export the key again if permitted by the key policy. */
5103 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005104 {
Ronald Cron5425a212020-08-04 14:58:35 +02005105 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005106 second_export, export_size,
5107 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005108 ASSERT_COMPARE( first_export, first_exported_length,
5109 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005110 }
5111
5112 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005113 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005114 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005115
5116exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005117 /*
5118 * Key attributes may have been returned by psa_get_key_attributes()
5119 * thus reset them as required.
5120 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005121 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005122
Darryl Greend49a4992018-06-18 17:27:26 +01005123 mbedtls_free( first_export );
5124 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005125 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005126 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005127 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005128 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005129}
5130/* END_CASE */