blob: eee43340520a6cdca7e2a38c5b30661a9fba4561 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010018
Gilles Peskine4023c012021-05-27 13:21:20 +020019/* If this comes up, it's a bug in the test code or in the test data. */
20#define UNUSED 0xdeadbeef
21
Dave Rodgman647791d2021-06-23 12:49:59 +010022/* Assert that an operation is (not) active.
23 * This serves as a proxy for checking if the operation is aborted. */
24#define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 )
25#define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 )
26
Jaeden Amerof24c7f82018-06-27 17:20:43 +010027/** An invalid export length that will never be set by psa_export_key(). */
28static const size_t INVALID_EXPORT_LENGTH = ~0U;
29
Gilles Peskinea7aa4422018-08-14 15:17:54 +020030/** Test if a buffer contains a constant byte value.
31 *
32 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020033 *
34 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020035 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020036 * \param size Size of the buffer in bytes.
37 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020038 * \return 1 if the buffer is all-bits-zero.
39 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020040 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042{
43 size_t i;
44 for( i = 0; i < size; i++ )
45 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020046 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020047 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020048 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020049 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020050}
Gilles Peskine818ca122018-06-20 18:16:48 +020051
Gilles Peskine0b352bc2018-06-28 00:16:11 +020052/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
53static int asn1_write_10x( unsigned char **p,
54 unsigned char *start,
55 size_t bits,
56 unsigned char x )
57{
58 int ret;
59 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020060 if( bits == 0 )
61 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
62 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020063 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030064 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020065 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
66 *p -= len;
67 ( *p )[len-1] = x;
68 if( bits % 8 == 0 )
69 ( *p )[1] |= 1;
70 else
71 ( *p )[0] |= 1 << ( bits % 8 );
72 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
73 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
74 MBEDTLS_ASN1_INTEGER ) );
75 return( len );
76}
77
78static int construct_fake_rsa_key( unsigned char *buffer,
79 size_t buffer_size,
80 unsigned char **p,
81 size_t bits,
82 int keypair )
83{
84 size_t half_bits = ( bits + 1 ) / 2;
85 int ret;
86 int len = 0;
87 /* Construct something that looks like a DER encoding of
88 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
89 * RSAPrivateKey ::= SEQUENCE {
90 * version Version,
91 * modulus INTEGER, -- n
92 * publicExponent INTEGER, -- e
93 * privateExponent INTEGER, -- d
94 * prime1 INTEGER, -- p
95 * prime2 INTEGER, -- q
96 * exponent1 INTEGER, -- d mod (p-1)
97 * exponent2 INTEGER, -- d mod (q-1)
98 * coefficient INTEGER, -- (inverse of q) mod p
99 * otherPrimeInfos OtherPrimeInfos OPTIONAL
100 * }
101 * Or, for a public key, the same structure with only
102 * version, modulus and publicExponent.
103 */
104 *p = buffer + buffer_size;
105 if( keypair )
106 {
107 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
108 asn1_write_10x( p, buffer, half_bits, 1 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
110 asn1_write_10x( p, buffer, half_bits, 1 ) );
111 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
112 asn1_write_10x( p, buffer, half_bits, 1 ) );
113 MBEDTLS_ASN1_CHK_ADD( len, /* q */
114 asn1_write_10x( p, buffer, half_bits, 1 ) );
115 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
116 asn1_write_10x( p, buffer, half_bits, 3 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* d */
118 asn1_write_10x( p, buffer, bits, 1 ) );
119 }
120 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
121 asn1_write_10x( p, buffer, 17, 1 ) );
122 MBEDTLS_ASN1_CHK_ADD( len, /* n */
123 asn1_write_10x( p, buffer, bits, 1 ) );
124 if( keypair )
125 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
126 mbedtls_asn1_write_int( p, buffer, 0 ) );
127 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
128 {
129 const unsigned char tag =
130 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
131 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
132 }
133 return( len );
134}
135
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100136int exercise_mac_setup( psa_key_type_t key_type,
137 const unsigned char *key_bytes,
138 size_t key_length,
139 psa_algorithm_t alg,
140 psa_mac_operation_t *operation,
141 psa_status_t *status )
142{
Ronald Cron5425a212020-08-04 14:58:35 +0200143 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200144 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100145
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100146 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200147 psa_set_key_algorithm( &attributes, alg );
148 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200149 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100150
Ronald Cron5425a212020-08-04 14:58:35 +0200151 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100152 /* Whether setup succeeded or failed, abort must succeed. */
153 PSA_ASSERT( psa_mac_abort( operation ) );
154 /* If setup failed, reproduce the failure, so that the caller can
155 * test the resulting state of the operation object. */
156 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100157 {
Ronald Cron5425a212020-08-04 14:58:35 +0200158 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100159 }
160
Ronald Cron5425a212020-08-04 14:58:35 +0200161 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100162 return( 1 );
163
164exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200165 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100166 return( 0 );
167}
168
169int exercise_cipher_setup( psa_key_type_t key_type,
170 const unsigned char *key_bytes,
171 size_t key_length,
172 psa_algorithm_t alg,
173 psa_cipher_operation_t *operation,
174 psa_status_t *status )
175{
Ronald Cron5425a212020-08-04 14:58:35 +0200176 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200177 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100178
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200179 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
180 psa_set_key_algorithm( &attributes, alg );
181 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200182 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100183
Ronald Cron5425a212020-08-04 14:58:35 +0200184 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100185 /* Whether setup succeeded or failed, abort must succeed. */
186 PSA_ASSERT( psa_cipher_abort( operation ) );
187 /* If setup failed, reproduce the failure, so that the caller can
188 * test the resulting state of the operation object. */
189 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100190 {
Ronald Cron5425a212020-08-04 14:58:35 +0200191 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100192 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100193 }
194
Ronald Cron5425a212020-08-04 14:58:35 +0200195 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100196 return( 1 );
197
198exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200199 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100200 return( 0 );
201}
202
Ronald Cron5425a212020-08-04 14:58:35 +0200203static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200204{
205 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200206 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200207 uint8_t buffer[1];
208 size_t length;
209 int ok = 0;
210
Ronald Cronecfb2372020-07-23 17:13:42 +0200211 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200212 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
213 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
214 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200215 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000216 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200217 TEST_EQUAL(
218 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
219 TEST_EQUAL(
220 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200221 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200222 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
223 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
224 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
225 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
226
Ronald Cron5425a212020-08-04 14:58:35 +0200227 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000228 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200229 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200230 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000231 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200232
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200233 ok = 1;
234
235exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100236 /*
237 * Key attributes may have been returned by psa_get_key_attributes()
238 * thus reset them as required.
239 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200240 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100241
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200242 return( ok );
243}
244
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200245/* Assert that a key isn't reported as having a slot number. */
246#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
247#define ASSERT_NO_SLOT_NUMBER( attributes ) \
248 do \
249 { \
250 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
251 TEST_EQUAL( psa_get_key_slot_number( \
252 attributes, \
253 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
254 PSA_ERROR_INVALID_ARGUMENT ); \
255 } \
256 while( 0 )
257#else /* MBEDTLS_PSA_CRYPTO_SE_C */
258#define ASSERT_NO_SLOT_NUMBER( attributes ) \
259 ( (void) 0 )
260#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
261
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100262/* An overapproximation of the amount of storage needed for a key of the
263 * given type and with the given content. The API doesn't make it easy
264 * to find a good value for the size. The current implementation doesn't
265 * care about the value anyway. */
266#define KEY_BITS_FROM_DATA( type, data ) \
267 ( data )->len
268
Darryl Green0c6575a2018-11-07 16:05:30 +0000269typedef enum {
270 IMPORT_KEY = 0,
271 GENERATE_KEY = 1,
272 DERIVE_KEY = 2
273} generate_method;
274
Gilles Peskinee59236f2018-01-27 23:32:46 +0100275/* END_HEADER */
276
277/* BEGIN_DEPENDENCIES
278 * depends_on:MBEDTLS_PSA_CRYPTO_C
279 * END_DEPENDENCIES
280 */
281
282/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200283void static_checks( )
284{
285 size_t max_truncated_mac_size =
286 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
287
288 /* Check that the length for a truncated MAC always fits in the algorithm
289 * encoding. The shifted mask is the maximum truncated value. The
290 * untruncated algorithm may be one byte larger. */
291 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
292}
293/* END_CASE */
294
295/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200296void import_with_policy( int type_arg,
297 int usage_arg, int alg_arg,
298 int expected_status_arg )
299{
300 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
301 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200302 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200303 psa_key_type_t type = type_arg;
304 psa_key_usage_t usage = usage_arg;
305 psa_algorithm_t alg = alg_arg;
306 psa_status_t expected_status = expected_status_arg;
307 const uint8_t key_material[16] = {0};
308 psa_status_t status;
309
310 PSA_ASSERT( psa_crypto_init( ) );
311
312 psa_set_key_type( &attributes, type );
313 psa_set_key_usage_flags( &attributes, usage );
314 psa_set_key_algorithm( &attributes, alg );
315
316 status = psa_import_key( &attributes,
317 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200318 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200319 TEST_EQUAL( status, expected_status );
320 if( status != PSA_SUCCESS )
321 goto exit;
322
Ronald Cron5425a212020-08-04 14:58:35 +0200323 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200324 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200325 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
326 update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200327 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200328 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200329
Ronald Cron5425a212020-08-04 14:58:35 +0200330 PSA_ASSERT( psa_destroy_key( key ) );
331 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200332
333exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100334 /*
335 * Key attributes may have been returned by psa_get_key_attributes()
336 * thus reset them as required.
337 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200338 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100339
340 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200341 PSA_DONE( );
342}
343/* END_CASE */
344
345/* BEGIN_CASE */
346void import_with_data( data_t *data, int type_arg,
347 int attr_bits_arg,
348 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200349{
350 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
351 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200352 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200353 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200354 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200355 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100356 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100357
Gilles Peskine8817f612018-12-18 00:18:46 +0100358 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100359
Gilles Peskine4747d192019-04-17 15:05:45 +0200360 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200361 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200362
Ronald Cron5425a212020-08-04 14:58:35 +0200363 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100364 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200365 if( status != PSA_SUCCESS )
366 goto exit;
367
Ronald Cron5425a212020-08-04 14:58:35 +0200368 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200369 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200370 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200371 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200372 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200373
Ronald Cron5425a212020-08-04 14:58:35 +0200374 PSA_ASSERT( psa_destroy_key( key ) );
375 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100376
377exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100378 /*
379 * Key attributes may have been returned by psa_get_key_attributes()
380 * thus reset them as required.
381 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200382 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100383
384 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200385 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100386}
387/* END_CASE */
388
389/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200390void import_large_key( int type_arg, int byte_size_arg,
391 int expected_status_arg )
392{
393 psa_key_type_t type = type_arg;
394 size_t byte_size = byte_size_arg;
395 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
396 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200397 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200398 psa_status_t status;
399 uint8_t *buffer = NULL;
400 size_t buffer_size = byte_size + 1;
401 size_t n;
402
Steven Cooreman69967ce2021-01-18 18:01:08 +0100403 /* Skip the test case if the target running the test cannot
404 * accomodate large keys due to heap size constraints */
405 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200406 memset( buffer, 'K', byte_size );
407
408 PSA_ASSERT( psa_crypto_init( ) );
409
410 /* Try importing the key */
411 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
412 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200413 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100414 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200415 TEST_EQUAL( status, expected_status );
416
417 if( status == PSA_SUCCESS )
418 {
Ronald Cron5425a212020-08-04 14:58:35 +0200419 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200420 TEST_EQUAL( psa_get_key_type( &attributes ), type );
421 TEST_EQUAL( psa_get_key_bits( &attributes ),
422 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200423 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200424 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200425 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200426 for( n = 0; n < byte_size; n++ )
427 TEST_EQUAL( buffer[n], 'K' );
428 for( n = byte_size; n < buffer_size; n++ )
429 TEST_EQUAL( buffer[n], 0 );
430 }
431
432exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100433 /*
434 * Key attributes may have been returned by psa_get_key_attributes()
435 * thus reset them as required.
436 */
437 psa_reset_key_attributes( &attributes );
438
Ronald Cron5425a212020-08-04 14:58:35 +0200439 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200440 PSA_DONE( );
441 mbedtls_free( buffer );
442}
443/* END_CASE */
444
445/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200446void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
447{
Ronald Cron5425a212020-08-04 14:58:35 +0200448 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200449 size_t bits = bits_arg;
450 psa_status_t expected_status = expected_status_arg;
451 psa_status_t status;
452 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200453 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200454 size_t buffer_size = /* Slight overapproximations */
455 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200456 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200457 unsigned char *p;
458 int ret;
459 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200460 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200461
Gilles Peskine8817f612018-12-18 00:18:46 +0100462 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200463 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200464
465 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
466 bits, keypair ) ) >= 0 );
467 length = ret;
468
469 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200470 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200471 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100472 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200473
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200474 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200475 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200476
477exit:
478 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200479 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200480}
481/* END_CASE */
482
483/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300484void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300485 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200486 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100487 int expected_bits,
488 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200489 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100490 int canonical_input )
491{
Ronald Cron5425a212020-08-04 14:58:35 +0200492 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100493 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200494 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200495 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100496 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100497 unsigned char *exported = NULL;
498 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100499 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100500 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100501 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200502 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200503 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100504
Moran Pekercb088e72018-07-17 17:36:59 +0300505 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200506 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100507 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200508 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100509 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100510
Gilles Peskine4747d192019-04-17 15:05:45 +0200511 psa_set_key_usage_flags( &attributes, usage_arg );
512 psa_set_key_algorithm( &attributes, alg );
513 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700514
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100515 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200516 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100517
518 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200519 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200520 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
521 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200522 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100523
524 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200525 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100526 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100527
528 /* The exported length must be set by psa_export_key() to a value between 0
529 * and export_size. On errors, the exported length must be 0. */
530 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
531 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
532 TEST_ASSERT( exported_length <= export_size );
533
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200534 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200535 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100536 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200537 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100538 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100539 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200540 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100541
Gilles Peskineea38a922021-02-13 00:05:16 +0100542 /* Run sanity checks on the exported key. For non-canonical inputs,
543 * this validates the canonical representations. For canonical inputs,
544 * this doesn't directly validate the implementation, but it still helps
545 * by cross-validating the test data with the sanity check code. */
546 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200547 goto exit;
548
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100549 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200550 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100551 else
552 {
Ronald Cron5425a212020-08-04 14:58:35 +0200553 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200554 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200555 &key2 ) );
556 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100557 reexported,
558 export_size,
559 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200560 ASSERT_COMPARE( exported, exported_length,
561 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200562 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100563 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100564 TEST_ASSERT( exported_length <=
565 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
566 psa_get_key_bits( &got_attributes ) ) );
567 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100568
569destroy:
570 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200571 PSA_ASSERT( psa_destroy_key( key ) );
572 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100573
574exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100575 /*
576 * Key attributes may have been returned by psa_get_key_attributes()
577 * thus reset them as required.
578 */
579 psa_reset_key_attributes( &got_attributes );
580
itayzafrir3e02b3b2018-06-12 17:06:52 +0300581 mbedtls_free( exported );
582 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200583 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100584}
585/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100586
Moran Pekerf709f4a2018-06-06 17:26:04 +0300587/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300588void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200589 int type_arg,
590 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100591 int export_size_delta,
592 int expected_export_status_arg,
593 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300594{
Ronald Cron5425a212020-08-04 14:58:35 +0200595 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300596 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200597 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200598 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300599 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300600 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100601 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100602 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200603 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300604
Gilles Peskine8817f612018-12-18 00:18:46 +0100605 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300606
Gilles Peskine4747d192019-04-17 15:05:45 +0200607 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
608 psa_set_key_algorithm( &attributes, alg );
609 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300610
611 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200612 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300613
Gilles Peskine49c25912018-10-29 15:15:31 +0100614 /* Export the public key */
615 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200616 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200617 exported, export_size,
618 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100619 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100620 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100621 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200622 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100623 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200624 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200625 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100626 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100627 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100628 TEST_ASSERT( expected_public_key->len <=
629 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
630 TEST_ASSERT( expected_public_key->len <=
631 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100632 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
633 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100634 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300635
636exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100637 /*
638 * Key attributes may have been returned by psa_get_key_attributes()
639 * thus reset them as required.
640 */
641 psa_reset_key_attributes( &attributes );
642
itayzafrir3e02b3b2018-06-12 17:06:52 +0300643 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200644 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200645 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300646}
647/* END_CASE */
648
Gilles Peskine20035e32018-02-03 22:44:14 +0100649/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200650void import_and_exercise_key( data_t *data,
651 int type_arg,
652 int bits_arg,
653 int alg_arg )
654{
Ronald Cron5425a212020-08-04 14:58:35 +0200655 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200656 psa_key_type_t type = type_arg;
657 size_t bits = bits_arg;
658 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100659 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200660 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200661 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200662
Gilles Peskine8817f612018-12-18 00:18:46 +0100663 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200664
Gilles Peskine4747d192019-04-17 15:05:45 +0200665 psa_set_key_usage_flags( &attributes, usage );
666 psa_set_key_algorithm( &attributes, alg );
667 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200668
669 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200670 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200671
672 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200673 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200674 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
675 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200676
677 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100678 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200679 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200680
Ronald Cron5425a212020-08-04 14:58:35 +0200681 PSA_ASSERT( psa_destroy_key( key ) );
682 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200683
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200684exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100685 /*
686 * Key attributes may have been returned by psa_get_key_attributes()
687 * thus reset them as required.
688 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200689 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100690
691 psa_reset_key_attributes( &attributes );
692 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200693 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200694}
695/* END_CASE */
696
697/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100698void effective_key_attributes( int type_arg, int expected_type_arg,
699 int bits_arg, int expected_bits_arg,
700 int usage_arg, int expected_usage_arg,
701 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200702{
Ronald Cron5425a212020-08-04 14:58:35 +0200703 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100704 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100705 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100706 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100707 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200708 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100709 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200710 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100711 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200712 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200713
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200714 /* Check if all extended usage flags are deployed
715 in the expected usage flags. */
716 TEST_EQUAL( expected_usage, update_key_usage_flags( usage ) );
717
Gilles Peskine8817f612018-12-18 00:18:46 +0100718 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200719
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200720 psa_set_key_usage_flags( &attributes, usage );
721 psa_set_key_algorithm( &attributes, alg );
722 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +0100723 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +0200724
Ronald Cron5425a212020-08-04 14:58:35 +0200725 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +0100726 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +0200727
Ronald Cron5425a212020-08-04 14:58:35 +0200728 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +0100729 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
730 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
731 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
732 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200733
734exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100735 /*
736 * Key attributes may have been returned by psa_get_key_attributes()
737 * thus reset them as required.
738 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200739 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100740
741 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200742 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +0200743}
744/* END_CASE */
745
746/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100747void check_key_policy( int type_arg, int bits_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200748 int usage_arg, int expected_usage_arg, int alg_arg )
Gilles Peskine06c28892019-11-26 18:07:46 +0100749{
750 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200751 usage_arg, expected_usage_arg,
752 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +0100753 goto exit;
754}
755/* END_CASE */
756
757/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200758void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +0000759{
760 /* Test each valid way of initializing the object, except for `= {0}`, as
761 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
762 * though it's OK by the C standard. We could test for this, but we'd need
763 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200764 psa_key_attributes_t func = psa_key_attributes_init( );
765 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
766 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000767
768 memset( &zero, 0, sizeof( zero ) );
769
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200770 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
771 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
772 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000773
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200774 TEST_EQUAL( psa_get_key_type( &func ), 0 );
775 TEST_EQUAL( psa_get_key_type( &init ), 0 );
776 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
777
778 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
779 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
780 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
781
782 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
783 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
784 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
785
786 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
787 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
788 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +0000789}
790/* END_CASE */
791
792/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200793void mac_key_policy( int policy_usage_arg,
794 int policy_alg_arg,
795 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200796 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200797 int exercise_alg_arg,
798 int expected_usage_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100799 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200800{
Ronald Cron5425a212020-08-04 14:58:35 +0200801 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200802 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000803 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200804 psa_key_type_t key_type = key_type_arg;
805 psa_algorithm_t policy_alg = policy_alg_arg;
806 psa_algorithm_t exercise_alg = exercise_alg_arg;
807 psa_key_usage_t policy_usage = policy_usage_arg;
808 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200809 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100810 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200811 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200812
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200813 /* Check if all extended usage flags are deployed
814 in the expected usage flags. */
815 TEST_EQUAL( expected_usage, update_key_usage_flags( policy_usage ) );
816
Gilles Peskine8817f612018-12-18 00:18:46 +0100817 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200818
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200819 psa_set_key_usage_flags( &attributes, policy_usage );
820 psa_set_key_algorithm( &attributes, policy_alg );
821 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200822
Gilles Peskine049c7532019-05-15 20:22:09 +0200823 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200824 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200825
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200826 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
827
Ronald Cron5425a212020-08-04 14:58:35 +0200828 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100829 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100830 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100831 else
832 TEST_EQUAL( status, expected_status );
833
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200834 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200835
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200836 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200837 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100838 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100839 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100840 else
841 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200842
843exit:
844 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200845 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200846 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200847}
848/* END_CASE */
849
850/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200851void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200852 int policy_alg,
853 int key_type,
854 data_t *key_data,
855 int exercise_alg )
856{
Ronald Cron5425a212020-08-04 14:58:35 +0200857 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200858 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000859 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200860 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200861 psa_status_t status;
862
Gilles Peskine8817f612018-12-18 00:18:46 +0100863 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200864
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200865 psa_set_key_usage_flags( &attributes, policy_usage );
866 psa_set_key_algorithm( &attributes, policy_alg );
867 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200868
Gilles Peskine049c7532019-05-15 20:22:09 +0200869 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200870 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200871
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200872 /* Check if no key usage extension is done */
873 TEST_EQUAL( policy_usage, update_key_usage_flags( policy_usage ) );
874
Ronald Cron5425a212020-08-04 14:58:35 +0200875 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200876 if( policy_alg == exercise_alg &&
877 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100878 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200879 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100880 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200881 psa_cipher_abort( &operation );
882
Ronald Cron5425a212020-08-04 14:58:35 +0200883 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200884 if( policy_alg == exercise_alg &&
885 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100886 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200887 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100888 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200889
890exit:
891 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200892 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200893 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200894}
895/* END_CASE */
896
897/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200898void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200899 int policy_alg,
900 int key_type,
901 data_t *key_data,
902 int nonce_length_arg,
903 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100904 int exercise_alg,
905 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200906{
Ronald Cron5425a212020-08-04 14:58:35 +0200907 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200908 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200909 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200910 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100911 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200912 unsigned char nonce[16] = {0};
913 size_t nonce_length = nonce_length_arg;
914 unsigned char tag[16];
915 size_t tag_length = tag_length_arg;
916 size_t output_length;
917
918 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
919 TEST_ASSERT( tag_length <= sizeof( tag ) );
920
Gilles Peskine8817f612018-12-18 00:18:46 +0100921 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200922
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200923 psa_set_key_usage_flags( &attributes, policy_usage );
924 psa_set_key_algorithm( &attributes, policy_alg );
925 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200926
Gilles Peskine049c7532019-05-15 20:22:09 +0200927 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200928 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200929
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200930 /* Check if no key usage extension is done */
931 TEST_EQUAL( policy_usage, update_key_usage_flags( policy_usage ) );
932
Ronald Cron5425a212020-08-04 14:58:35 +0200933 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200934 nonce, nonce_length,
935 NULL, 0,
936 NULL, 0,
937 tag, tag_length,
938 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100939 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
940 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200941 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100942 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200943
944 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200945 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200946 nonce, nonce_length,
947 NULL, 0,
948 tag, tag_length,
949 NULL, 0,
950 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100951 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
952 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
953 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100954 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200955 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100956 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200957
958exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200959 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200960 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200961}
962/* END_CASE */
963
964/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200965void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200966 int policy_alg,
967 int key_type,
968 data_t *key_data,
969 int exercise_alg )
970{
Ronald Cron5425a212020-08-04 14:58:35 +0200971 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200972 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200973 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200974 psa_status_t status;
975 size_t key_bits;
976 size_t buffer_length;
977 unsigned char *buffer = NULL;
978 size_t output_length;
979
Gilles Peskine8817f612018-12-18 00:18:46 +0100980 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200981
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200982 psa_set_key_usage_flags( &attributes, policy_usage );
983 psa_set_key_algorithm( &attributes, policy_alg );
984 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200985
Gilles Peskine049c7532019-05-15 20:22:09 +0200986 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200987 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200988
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200989 /* Check if no key usage extension is done */
990 TEST_EQUAL( policy_usage, update_key_usage_flags( policy_usage ) );
991
Ronald Cron5425a212020-08-04 14:58:35 +0200992 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200993 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200994 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
995 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200996 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200997
Ronald Cron5425a212020-08-04 14:58:35 +0200998 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200999 NULL, 0,
1000 NULL, 0,
1001 buffer, buffer_length,
1002 &output_length );
1003 if( policy_alg == exercise_alg &&
1004 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001005 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001006 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001007 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001008
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001009 if( buffer_length != 0 )
1010 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001011 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001012 buffer, buffer_length,
1013 NULL, 0,
1014 buffer, buffer_length,
1015 &output_length );
1016 if( policy_alg == exercise_alg &&
1017 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001018 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001019 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001020 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001021
1022exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001023 /*
1024 * Key attributes may have been returned by psa_get_key_attributes()
1025 * thus reset them as required.
1026 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001027 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001028
1029 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001030 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001031 mbedtls_free( buffer );
1032}
1033/* END_CASE */
1034
1035/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001036void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001037 int policy_alg,
1038 int key_type,
1039 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001040 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001041 int payload_length_arg,
1042 int hashing_permitted,
1043 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001044{
Ronald Cron5425a212020-08-04 14:58:35 +02001045 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001046 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001047 psa_key_usage_t policy_usage = policy_usage_arg;
1048 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001049 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001050 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1051 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1052 * compatible with the policy and `payload_length_arg` is supposed to be
1053 * a valid input length to sign. If `payload_length_arg <= 0`,
1054 * `exercise_alg` is supposed to be forbidden by the policy. */
1055 int compatible_alg = payload_length_arg > 0;
1056 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001057 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001058 size_t signature_length;
1059
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001060 /* Check if all extended usage flags are deployed
1061 in the expected usage flags. */
1062 TEST_EQUAL( expected_usage, update_key_usage_flags( policy_usage ) );
1063
Gilles Peskine8817f612018-12-18 00:18:46 +01001064 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001065
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001066 psa_set_key_usage_flags( &attributes, policy_usage );
1067 psa_set_key_algorithm( &attributes, policy_alg );
1068 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001069
Gilles Peskine049c7532019-05-15 20:22:09 +02001070 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001071 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001072
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001073 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1074
Ronald Cron5425a212020-08-04 14:58:35 +02001075 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001076 payload, payload_length,
1077 signature, sizeof( signature ),
1078 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001079 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001080 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001081 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001082 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001083
1084 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001085 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001086 payload, payload_length,
1087 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001088 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001089 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001090 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001091 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001092
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001093 if( hashing_permitted )
1094 {
1095 status = psa_sign_message( key, exercise_alg,
1096 payload, payload_length,
1097 signature, sizeof( signature ),
1098 &signature_length );
1099 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1100 PSA_ASSERT( status );
1101 else
1102 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1103
1104 memset( signature, 0, sizeof( signature ) );
1105 status = psa_verify_message( key, exercise_alg,
1106 payload, payload_length,
1107 signature, sizeof( signature ) );
1108 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1109 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1110 else
1111 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1112 }
1113
Gilles Peskined5b33222018-06-18 22:20:03 +02001114exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001115 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001116 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001117}
1118/* END_CASE */
1119
Janos Follathba3fab92019-06-11 14:50:16 +01001120/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001121void derive_key_policy( int policy_usage,
1122 int policy_alg,
1123 int key_type,
1124 data_t *key_data,
1125 int exercise_alg )
1126{
Ronald Cron5425a212020-08-04 14:58:35 +02001127 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001128 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001129 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001130 psa_status_t status;
1131
Gilles Peskine8817f612018-12-18 00:18:46 +01001132 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001133
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001134 psa_set_key_usage_flags( &attributes, policy_usage );
1135 psa_set_key_algorithm( &attributes, policy_alg );
1136 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001137
Gilles Peskine049c7532019-05-15 20:22:09 +02001138 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001139 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001140
Janos Follathba3fab92019-06-11 14:50:16 +01001141 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1142
1143 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1144 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001145 {
Janos Follathba3fab92019-06-11 14:50:16 +01001146 PSA_ASSERT( psa_key_derivation_input_bytes(
1147 &operation,
1148 PSA_KEY_DERIVATION_INPUT_SEED,
1149 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001150 }
Janos Follathba3fab92019-06-11 14:50:16 +01001151
1152 status = psa_key_derivation_input_key( &operation,
1153 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001154 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001155
Gilles Peskineea0fb492018-07-12 17:17:20 +02001156 if( policy_alg == exercise_alg &&
1157 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001158 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001159 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001160 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001161
1162exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001163 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001164 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001165 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001166}
1167/* END_CASE */
1168
1169/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001170void agreement_key_policy( int policy_usage,
1171 int policy_alg,
1172 int key_type_arg,
1173 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001174 int exercise_alg,
1175 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001176{
Ronald Cron5425a212020-08-04 14:58:35 +02001177 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001178 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001179 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001180 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001181 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001182 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001183
Gilles Peskine8817f612018-12-18 00:18:46 +01001184 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001185
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001186 psa_set_key_usage_flags( &attributes, policy_usage );
1187 psa_set_key_algorithm( &attributes, policy_alg );
1188 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001189
Gilles Peskine049c7532019-05-15 20:22:09 +02001190 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001191 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001192
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001193 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001194 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001195
Steven Cooremance48e852020-10-05 16:02:45 +02001196 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001197
1198exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001199 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001200 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001201 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001202}
1203/* END_CASE */
1204
1205/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001206void key_policy_alg2( int key_type_arg, data_t *key_data,
1207 int usage_arg, int alg_arg, int alg2_arg )
1208{
Ronald Cron5425a212020-08-04 14:58:35 +02001209 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001210 psa_key_type_t key_type = key_type_arg;
1211 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1212 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1213 psa_key_usage_t usage = usage_arg;
1214 psa_algorithm_t alg = alg_arg;
1215 psa_algorithm_t alg2 = alg2_arg;
1216
1217 PSA_ASSERT( psa_crypto_init( ) );
1218
1219 psa_set_key_usage_flags( &attributes, usage );
1220 psa_set_key_algorithm( &attributes, alg );
1221 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1222 psa_set_key_type( &attributes, key_type );
1223 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001224 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001225
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02001226 /* Update the usage flags to obtain extended usage flags */
1227 usage = update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001228 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001229 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1230 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1231 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1232
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001233 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001234 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001235 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001236 goto exit;
1237
1238exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001239 /*
1240 * Key attributes may have been returned by psa_get_key_attributes()
1241 * thus reset them as required.
1242 */
1243 psa_reset_key_attributes( &got_attributes );
1244
Ronald Cron5425a212020-08-04 14:58:35 +02001245 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001246 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001247}
1248/* END_CASE */
1249
1250/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001251void raw_agreement_key_policy( int policy_usage,
1252 int policy_alg,
1253 int key_type_arg,
1254 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001255 int exercise_alg,
1256 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001257{
Ronald Cron5425a212020-08-04 14:58:35 +02001258 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001259 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001260 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001261 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001262 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001263 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001264
1265 PSA_ASSERT( psa_crypto_init( ) );
1266
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001267 psa_set_key_usage_flags( &attributes, policy_usage );
1268 psa_set_key_algorithm( &attributes, policy_alg );
1269 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001270
Gilles Peskine049c7532019-05-15 20:22:09 +02001271 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001272 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001273
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001274 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001275
Steven Cooremance48e852020-10-05 16:02:45 +02001276 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001277
1278exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001279 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001280 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001281 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001282}
1283/* END_CASE */
1284
1285/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001286void copy_success( int source_usage_arg,
1287 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001288 int type_arg, data_t *material,
1289 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001290 int target_usage_arg,
1291 int target_alg_arg, int target_alg2_arg,
1292 int expected_usage_arg,
1293 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001294{
Gilles Peskineca25db92019-04-19 11:43:08 +02001295 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1296 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001297 psa_key_usage_t expected_usage = expected_usage_arg;
1298 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001299 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001300 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1301 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001302 uint8_t *export_buffer = NULL;
1303
Gilles Peskine57ab7212019-01-28 13:03:09 +01001304 PSA_ASSERT( psa_crypto_init( ) );
1305
Gilles Peskineca25db92019-04-19 11:43:08 +02001306 /* Prepare the source key. */
1307 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1308 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001309 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001310 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001311 PSA_ASSERT( psa_import_key( &source_attributes,
1312 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001313 &source_key ) );
1314 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001315
Gilles Peskineca25db92019-04-19 11:43:08 +02001316 /* Prepare the target attributes. */
1317 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001318 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001319 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001320 /* Set volatile lifetime to reset the key identifier to 0. */
1321 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1322 }
1323
Gilles Peskineca25db92019-04-19 11:43:08 +02001324 if( target_usage_arg != -1 )
1325 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1326 if( target_alg_arg != -1 )
1327 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001328 if( target_alg2_arg != -1 )
1329 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001330
1331 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001332 PSA_ASSERT( psa_copy_key( source_key,
1333 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001334
1335 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001336 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001337
1338 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001339 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001340 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1341 psa_get_key_type( &target_attributes ) );
1342 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1343 psa_get_key_bits( &target_attributes ) );
1344 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1345 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001346 TEST_EQUAL( expected_alg2,
1347 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001348 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1349 {
1350 size_t length;
1351 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001352 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001353 material->len, &length ) );
1354 ASSERT_COMPARE( material->x, material->len,
1355 export_buffer, length );
1356 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001357
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001358 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001359 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001360 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001361 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001362
Ronald Cron5425a212020-08-04 14:58:35 +02001363 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001364
1365exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001366 /*
1367 * Source and target key attributes may have been returned by
1368 * psa_get_key_attributes() thus reset them as required.
1369 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001370 psa_reset_key_attributes( &source_attributes );
1371 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001372
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001373 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001374 mbedtls_free( export_buffer );
1375}
1376/* END_CASE */
1377
1378/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001379void copy_fail( int source_usage_arg,
1380 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001381 int type_arg, data_t *material,
1382 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001383 int target_usage_arg,
1384 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001385 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001386 int expected_status_arg )
1387{
1388 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1389 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001390 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1391 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001392 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001393
1394 PSA_ASSERT( psa_crypto_init( ) );
1395
1396 /* Prepare the source key. */
1397 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1398 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001399 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001400 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001401 PSA_ASSERT( psa_import_key( &source_attributes,
1402 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001403 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001404
1405 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001406 psa_set_key_id( &target_attributes, key_id );
1407 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001408 psa_set_key_type( &target_attributes, target_type_arg );
1409 psa_set_key_bits( &target_attributes, target_bits_arg );
1410 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1411 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001412 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001413
1414 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001415 TEST_EQUAL( psa_copy_key( source_key,
1416 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001417 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001418
Ronald Cron5425a212020-08-04 14:58:35 +02001419 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001420
Gilles Peskine4a644642019-05-03 17:14:08 +02001421exit:
1422 psa_reset_key_attributes( &source_attributes );
1423 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001424 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001425}
1426/* END_CASE */
1427
1428/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001429void hash_operation_init( )
1430{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001431 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001432 /* Test each valid way of initializing the object, except for `= {0}`, as
1433 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1434 * though it's OK by the C standard. We could test for this, but we'd need
1435 * to supress the Clang warning for the test. */
1436 psa_hash_operation_t func = psa_hash_operation_init( );
1437 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1438 psa_hash_operation_t zero;
1439
1440 memset( &zero, 0, sizeof( zero ) );
1441
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001442 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001443 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1444 PSA_ERROR_BAD_STATE );
1445 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1446 PSA_ERROR_BAD_STATE );
1447 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1448 PSA_ERROR_BAD_STATE );
1449
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001450 /* A default hash operation should be abortable without error. */
1451 PSA_ASSERT( psa_hash_abort( &func ) );
1452 PSA_ASSERT( psa_hash_abort( &init ) );
1453 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001454}
1455/* END_CASE */
1456
1457/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001458void hash_setup( int alg_arg,
1459 int expected_status_arg )
1460{
1461 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001462 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001463 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001464 psa_status_t status;
1465
Gilles Peskine8817f612018-12-18 00:18:46 +01001466 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001467
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001468 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001469 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001470
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001471 /* Whether setup succeeded or failed, abort must succeed. */
1472 PSA_ASSERT( psa_hash_abort( &operation ) );
1473
1474 /* If setup failed, reproduce the failure, so as to
1475 * test the resulting state of the operation object. */
1476 if( status != PSA_SUCCESS )
1477 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1478
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001479 /* Now the operation object should be reusable. */
1480#if defined(KNOWN_SUPPORTED_HASH_ALG)
1481 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1482 PSA_ASSERT( psa_hash_abort( &operation ) );
1483#endif
1484
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001485exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001486 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001487}
1488/* END_CASE */
1489
1490/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001491void hash_compute_fail( int alg_arg, data_t *input,
1492 int output_size_arg, int expected_status_arg )
1493{
1494 psa_algorithm_t alg = alg_arg;
1495 uint8_t *output = NULL;
1496 size_t output_size = output_size_arg;
1497 size_t output_length = INVALID_EXPORT_LENGTH;
1498 psa_status_t expected_status = expected_status_arg;
1499 psa_status_t status;
1500
1501 ASSERT_ALLOC( output, output_size );
1502
1503 PSA_ASSERT( psa_crypto_init( ) );
1504
1505 status = psa_hash_compute( alg, input->x, input->len,
1506 output, output_size, &output_length );
1507 TEST_EQUAL( status, expected_status );
1508 TEST_ASSERT( output_length <= output_size );
1509
1510exit:
1511 mbedtls_free( output );
1512 PSA_DONE( );
1513}
1514/* END_CASE */
1515
1516/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001517void hash_compare_fail( int alg_arg, data_t *input,
1518 data_t *reference_hash,
1519 int expected_status_arg )
1520{
1521 psa_algorithm_t alg = alg_arg;
1522 psa_status_t expected_status = expected_status_arg;
1523 psa_status_t status;
1524
1525 PSA_ASSERT( psa_crypto_init( ) );
1526
1527 status = psa_hash_compare( alg, input->x, input->len,
1528 reference_hash->x, reference_hash->len );
1529 TEST_EQUAL( status, expected_status );
1530
1531exit:
1532 PSA_DONE( );
1533}
1534/* END_CASE */
1535
1536/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001537void hash_compute_compare( int alg_arg, data_t *input,
1538 data_t *expected_output )
1539{
1540 psa_algorithm_t alg = alg_arg;
1541 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1542 size_t output_length = INVALID_EXPORT_LENGTH;
1543 size_t i;
1544
1545 PSA_ASSERT( psa_crypto_init( ) );
1546
1547 /* Compute with tight buffer */
1548 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001549 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001550 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001551 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001552 ASSERT_COMPARE( output, output_length,
1553 expected_output->x, expected_output->len );
1554
1555 /* Compute with larger buffer */
1556 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1557 output, sizeof( output ),
1558 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001559 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001560 ASSERT_COMPARE( output, output_length,
1561 expected_output->x, expected_output->len );
1562
1563 /* Compare with correct hash */
1564 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1565 output, output_length ) );
1566
1567 /* Compare with trailing garbage */
1568 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1569 output, output_length + 1 ),
1570 PSA_ERROR_INVALID_SIGNATURE );
1571
1572 /* Compare with truncated hash */
1573 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1574 output, output_length - 1 ),
1575 PSA_ERROR_INVALID_SIGNATURE );
1576
1577 /* Compare with corrupted value */
1578 for( i = 0; i < output_length; i++ )
1579 {
Chris Jones9634bb12021-01-20 15:56:42 +00001580 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001581 output[i] ^= 1;
1582 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1583 output, output_length ),
1584 PSA_ERROR_INVALID_SIGNATURE );
1585 output[i] ^= 1;
1586 }
1587
1588exit:
1589 PSA_DONE( );
1590}
1591/* END_CASE */
1592
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001593/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001594void hash_bad_order( )
1595{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001596 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001597 unsigned char input[] = "";
1598 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001599 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001600 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1601 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1602 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001603 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001604 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001605 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001606
Gilles Peskine8817f612018-12-18 00:18:46 +01001607 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001608
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001609 /* Call setup twice in a row. */
1610 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001611 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001612 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1613 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001614 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001615 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001616 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001617
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001618 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001619 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001620 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001621 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001622
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001623 /* Check that update calls abort on error. */
1624 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01001625 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001626 ASSERT_OPERATION_IS_ACTIVE( operation );
1627 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
1628 PSA_ERROR_BAD_STATE );
1629 ASSERT_OPERATION_IS_INACTIVE( operation );
1630 PSA_ASSERT( psa_hash_abort( &operation ) );
1631 ASSERT_OPERATION_IS_INACTIVE( operation );
1632
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001633 /* Call update after finish. */
1634 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1635 PSA_ASSERT( psa_hash_finish( &operation,
1636 hash, sizeof( hash ), &hash_len ) );
1637 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001638 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001639 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001640
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001641 /* Call verify without calling setup beforehand. */
1642 TEST_EQUAL( psa_hash_verify( &operation,
1643 valid_hash, sizeof( valid_hash ) ),
1644 PSA_ERROR_BAD_STATE );
1645 PSA_ASSERT( psa_hash_abort( &operation ) );
1646
1647 /* Call verify after finish. */
1648 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1649 PSA_ASSERT( psa_hash_finish( &operation,
1650 hash, sizeof( hash ), &hash_len ) );
1651 TEST_EQUAL( psa_hash_verify( &operation,
1652 valid_hash, sizeof( valid_hash ) ),
1653 PSA_ERROR_BAD_STATE );
1654 PSA_ASSERT( psa_hash_abort( &operation ) );
1655
1656 /* Call verify twice in a row. */
1657 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001658 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001659 PSA_ASSERT( psa_hash_verify( &operation,
1660 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001661 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001662 TEST_EQUAL( psa_hash_verify( &operation,
1663 valid_hash, sizeof( valid_hash ) ),
1664 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001665 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001666 PSA_ASSERT( psa_hash_abort( &operation ) );
1667
1668 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001669 TEST_EQUAL( psa_hash_finish( &operation,
1670 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001671 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001672 PSA_ASSERT( psa_hash_abort( &operation ) );
1673
1674 /* Call finish twice in a row. */
1675 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1676 PSA_ASSERT( psa_hash_finish( &operation,
1677 hash, sizeof( hash ), &hash_len ) );
1678 TEST_EQUAL( psa_hash_finish( &operation,
1679 hash, sizeof( hash ), &hash_len ),
1680 PSA_ERROR_BAD_STATE );
1681 PSA_ASSERT( psa_hash_abort( &operation ) );
1682
1683 /* Call finish after calling verify. */
1684 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1685 PSA_ASSERT( psa_hash_verify( &operation,
1686 valid_hash, sizeof( valid_hash ) ) );
1687 TEST_EQUAL( psa_hash_finish( &operation,
1688 hash, sizeof( hash ), &hash_len ),
1689 PSA_ERROR_BAD_STATE );
1690 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001691
1692exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001693 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001694}
1695/* END_CASE */
1696
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001697/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001698void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001699{
1700 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001701 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1702 * appended to it */
1703 unsigned char hash[] = {
1704 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1705 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1706 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001707 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001708 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001709
Gilles Peskine8817f612018-12-18 00:18:46 +01001710 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001711
itayzafrir27e69452018-11-01 14:26:34 +02001712 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001713 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001714 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001715 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001716 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001717 ASSERT_OPERATION_IS_INACTIVE( operation );
1718 PSA_ASSERT( psa_hash_abort( &operation ) );
1719 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03001720
itayzafrir27e69452018-11-01 14:26:34 +02001721 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001722 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001723 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001724 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001725
itayzafrir27e69452018-11-01 14:26:34 +02001726 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001727 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001728 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001729 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001730
itayzafrirec93d302018-10-18 18:01:10 +03001731exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001732 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001733}
1734/* END_CASE */
1735
Ronald Cronee414c72021-03-18 18:50:08 +01001736/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001737void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001738{
1739 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001740 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001741 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001742 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001743 size_t hash_len;
1744
Gilles Peskine8817f612018-12-18 00:18:46 +01001745 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001746
itayzafrir58028322018-10-25 10:22:01 +03001747 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001748 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001749 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001750 hash, expected_size - 1, &hash_len ),
1751 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001752
1753exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001754 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001755}
1756/* END_CASE */
1757
Ronald Cronee414c72021-03-18 18:50:08 +01001758/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001759void hash_clone_source_state( )
1760{
1761 psa_algorithm_t alg = PSA_ALG_SHA_256;
1762 unsigned char hash[PSA_HASH_MAX_SIZE];
1763 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1764 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1765 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1766 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1767 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1768 size_t hash_len;
1769
1770 PSA_ASSERT( psa_crypto_init( ) );
1771 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1772
1773 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1774 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1775 PSA_ASSERT( psa_hash_finish( &op_finished,
1776 hash, sizeof( hash ), &hash_len ) );
1777 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1778 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1779
1780 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1781 PSA_ERROR_BAD_STATE );
1782
1783 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1784 PSA_ASSERT( psa_hash_finish( &op_init,
1785 hash, sizeof( hash ), &hash_len ) );
1786 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1787 PSA_ASSERT( psa_hash_finish( &op_finished,
1788 hash, sizeof( hash ), &hash_len ) );
1789 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1790 PSA_ASSERT( psa_hash_finish( &op_aborted,
1791 hash, sizeof( hash ), &hash_len ) );
1792
1793exit:
1794 psa_hash_abort( &op_source );
1795 psa_hash_abort( &op_init );
1796 psa_hash_abort( &op_setup );
1797 psa_hash_abort( &op_finished );
1798 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001799 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001800}
1801/* END_CASE */
1802
Ronald Cronee414c72021-03-18 18:50:08 +01001803/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001804void hash_clone_target_state( )
1805{
1806 psa_algorithm_t alg = PSA_ALG_SHA_256;
1807 unsigned char hash[PSA_HASH_MAX_SIZE];
1808 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1809 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1810 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1811 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1812 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1813 size_t hash_len;
1814
1815 PSA_ASSERT( psa_crypto_init( ) );
1816
1817 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1818 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1819 PSA_ASSERT( psa_hash_finish( &op_finished,
1820 hash, sizeof( hash ), &hash_len ) );
1821 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1822 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1823
1824 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1825 PSA_ASSERT( psa_hash_finish( &op_target,
1826 hash, sizeof( hash ), &hash_len ) );
1827
1828 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1829 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1830 PSA_ERROR_BAD_STATE );
1831 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1832 PSA_ERROR_BAD_STATE );
1833
1834exit:
1835 psa_hash_abort( &op_target );
1836 psa_hash_abort( &op_init );
1837 psa_hash_abort( &op_setup );
1838 psa_hash_abort( &op_finished );
1839 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001840 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001841}
1842/* END_CASE */
1843
itayzafrir58028322018-10-25 10:22:01 +03001844/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001845void mac_operation_init( )
1846{
Jaeden Amero252ef282019-02-15 14:05:35 +00001847 const uint8_t input[1] = { 0 };
1848
Jaeden Amero769ce272019-01-04 11:48:03 +00001849 /* Test each valid way of initializing the object, except for `= {0}`, as
1850 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1851 * though it's OK by the C standard. We could test for this, but we'd need
1852 * to supress the Clang warning for the test. */
1853 psa_mac_operation_t func = psa_mac_operation_init( );
1854 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1855 psa_mac_operation_t zero;
1856
1857 memset( &zero, 0, sizeof( zero ) );
1858
Jaeden Amero252ef282019-02-15 14:05:35 +00001859 /* A freshly-initialized MAC operation should not be usable. */
1860 TEST_EQUAL( psa_mac_update( &func,
1861 input, sizeof( input ) ),
1862 PSA_ERROR_BAD_STATE );
1863 TEST_EQUAL( psa_mac_update( &init,
1864 input, sizeof( input ) ),
1865 PSA_ERROR_BAD_STATE );
1866 TEST_EQUAL( psa_mac_update( &zero,
1867 input, sizeof( input ) ),
1868 PSA_ERROR_BAD_STATE );
1869
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001870 /* A default MAC operation should be abortable without error. */
1871 PSA_ASSERT( psa_mac_abort( &func ) );
1872 PSA_ASSERT( psa_mac_abort( &init ) );
1873 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001874}
1875/* END_CASE */
1876
1877/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001878void mac_setup( int key_type_arg,
1879 data_t *key,
1880 int alg_arg,
1881 int expected_status_arg )
1882{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001883 psa_key_type_t key_type = key_type_arg;
1884 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001885 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001886 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001887 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1888#if defined(KNOWN_SUPPORTED_MAC_ALG)
1889 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1890#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001891
Gilles Peskine8817f612018-12-18 00:18:46 +01001892 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001893
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001894 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1895 &operation, &status ) )
1896 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001897 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001898
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001899 /* The operation object should be reusable. */
1900#if defined(KNOWN_SUPPORTED_MAC_ALG)
1901 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1902 smoke_test_key_data,
1903 sizeof( smoke_test_key_data ),
1904 KNOWN_SUPPORTED_MAC_ALG,
1905 &operation, &status ) )
1906 goto exit;
1907 TEST_EQUAL( status, PSA_SUCCESS );
1908#endif
1909
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001910exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001911 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001912}
1913/* END_CASE */
1914
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001915/* 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 +00001916void mac_bad_order( )
1917{
Ronald Cron5425a212020-08-04 14:58:35 +02001918 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001919 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1920 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001921 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001922 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1923 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1924 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001925 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001926 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1927 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1928 size_t sign_mac_length = 0;
1929 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1930 const uint8_t verify_mac[] = {
1931 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1932 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1933 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1934
1935 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001936 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001937 psa_set_key_algorithm( &attributes, alg );
1938 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001939
Ronald Cron5425a212020-08-04 14:58:35 +02001940 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1941 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001942
Jaeden Amero252ef282019-02-15 14:05:35 +00001943 /* Call update without calling setup beforehand. */
1944 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1945 PSA_ERROR_BAD_STATE );
1946 PSA_ASSERT( psa_mac_abort( &operation ) );
1947
1948 /* Call sign finish without calling setup beforehand. */
1949 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1950 &sign_mac_length),
1951 PSA_ERROR_BAD_STATE );
1952 PSA_ASSERT( psa_mac_abort( &operation ) );
1953
1954 /* Call verify finish without calling setup beforehand. */
1955 TEST_EQUAL( psa_mac_verify_finish( &operation,
1956 verify_mac, sizeof( verify_mac ) ),
1957 PSA_ERROR_BAD_STATE );
1958 PSA_ASSERT( psa_mac_abort( &operation ) );
1959
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001960 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001961 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001962 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001963 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001964 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001965 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001966 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001967 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001968
Jaeden Amero252ef282019-02-15 14:05:35 +00001969 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001970 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001971 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1972 PSA_ASSERT( psa_mac_sign_finish( &operation,
1973 sign_mac, sizeof( sign_mac ),
1974 &sign_mac_length ) );
1975 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1976 PSA_ERROR_BAD_STATE );
1977 PSA_ASSERT( psa_mac_abort( &operation ) );
1978
1979 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001980 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001981 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1982 PSA_ASSERT( psa_mac_verify_finish( &operation,
1983 verify_mac, sizeof( verify_mac ) ) );
1984 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1985 PSA_ERROR_BAD_STATE );
1986 PSA_ASSERT( psa_mac_abort( &operation ) );
1987
1988 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001989 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001990 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1991 PSA_ASSERT( psa_mac_sign_finish( &operation,
1992 sign_mac, sizeof( sign_mac ),
1993 &sign_mac_length ) );
1994 TEST_EQUAL( psa_mac_sign_finish( &operation,
1995 sign_mac, sizeof( sign_mac ),
1996 &sign_mac_length ),
1997 PSA_ERROR_BAD_STATE );
1998 PSA_ASSERT( psa_mac_abort( &operation ) );
1999
2000 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002001 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002002 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2003 PSA_ASSERT( psa_mac_verify_finish( &operation,
2004 verify_mac, sizeof( verify_mac ) ) );
2005 TEST_EQUAL( psa_mac_verify_finish( &operation,
2006 verify_mac, sizeof( verify_mac ) ),
2007 PSA_ERROR_BAD_STATE );
2008 PSA_ASSERT( psa_mac_abort( &operation ) );
2009
2010 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002011 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002012 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002013 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002014 TEST_EQUAL( psa_mac_verify_finish( &operation,
2015 verify_mac, sizeof( verify_mac ) ),
2016 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002017 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002018 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002019 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002020
2021 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002022 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002023 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002024 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002025 TEST_EQUAL( psa_mac_sign_finish( &operation,
2026 sign_mac, sizeof( sign_mac ),
2027 &sign_mac_length ),
2028 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002029 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002030 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002031 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002032
Ronald Cron5425a212020-08-04 14:58:35 +02002033 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002034
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002035exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002036 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002037}
2038/* END_CASE */
2039
2040/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002041void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002042 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002043 int alg_arg,
2044 data_t *input,
2045 data_t *expected_mac )
2046{
Ronald Cron5425a212020-08-04 14:58:35 +02002047 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002048 psa_key_type_t key_type = key_type_arg;
2049 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002050 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002051 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002052 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002053 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002054 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002055 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002056 const size_t output_sizes_to_test[] = {
2057 0,
2058 1,
2059 expected_mac->len - 1,
2060 expected_mac->len,
2061 expected_mac->len + 1,
2062 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002063
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002064 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002065 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002066 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002067
Gilles Peskine8817f612018-12-18 00:18:46 +01002068 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002069
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002070 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002071 psa_set_key_algorithm( &attributes, alg );
2072 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002073
Ronald Cron5425a212020-08-04 14:58:35 +02002074 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2075 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002076
Gilles Peskine8b356b52020-08-25 23:44:59 +02002077 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2078 {
2079 const size_t output_size = output_sizes_to_test[i];
2080 psa_status_t expected_status =
2081 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2082 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002083
Chris Jones9634bb12021-01-20 15:56:42 +00002084 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002085 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002086
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002087 /* Calculate the MAC, one-shot case. */
2088 TEST_EQUAL( psa_mac_compute( key, alg,
2089 input->x, input->len,
2090 actual_mac, output_size, &mac_length ),
2091 expected_status );
2092 if( expected_status == PSA_SUCCESS )
2093 {
2094 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2095 actual_mac, mac_length );
2096 }
2097
2098 if( output_size > 0 )
2099 memset( actual_mac, 0, output_size );
2100
2101 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002102 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002103 PSA_ASSERT( psa_mac_update( &operation,
2104 input->x, input->len ) );
2105 TEST_EQUAL( psa_mac_sign_finish( &operation,
2106 actual_mac, output_size,
2107 &mac_length ),
2108 expected_status );
2109 PSA_ASSERT( psa_mac_abort( &operation ) );
2110
2111 if( expected_status == PSA_SUCCESS )
2112 {
2113 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2114 actual_mac, mac_length );
2115 }
2116 mbedtls_free( actual_mac );
2117 actual_mac = NULL;
2118 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002119
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002120exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002121 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002122 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002123 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002124 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002125}
2126/* END_CASE */
2127
2128/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002129void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002130 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002131 int alg_arg,
2132 data_t *input,
2133 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002134{
Ronald Cron5425a212020-08-04 14:58:35 +02002135 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002136 psa_key_type_t key_type = key_type_arg;
2137 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002138 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002139 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002140 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002141
Gilles Peskine69c12672018-06-28 00:07:19 +02002142 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2143
Gilles Peskine8817f612018-12-18 00:18:46 +01002144 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002145
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002146 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002147 psa_set_key_algorithm( &attributes, alg );
2148 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002149
Ronald Cron5425a212020-08-04 14:58:35 +02002150 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2151 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002152
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002153 /* Verify correct MAC, one-shot case. */
2154 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2155 expected_mac->x, expected_mac->len ) );
2156
2157 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002158 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002159 PSA_ASSERT( psa_mac_update( &operation,
2160 input->x, input->len ) );
2161 PSA_ASSERT( psa_mac_verify_finish( &operation,
2162 expected_mac->x,
2163 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002164
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002165 /* Test a MAC that's too short, one-shot case. */
2166 TEST_EQUAL( psa_mac_verify( key, alg,
2167 input->x, input->len,
2168 expected_mac->x,
2169 expected_mac->len - 1 ),
2170 PSA_ERROR_INVALID_SIGNATURE );
2171
2172 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002173 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002174 PSA_ASSERT( psa_mac_update( &operation,
2175 input->x, input->len ) );
2176 TEST_EQUAL( psa_mac_verify_finish( &operation,
2177 expected_mac->x,
2178 expected_mac->len - 1 ),
2179 PSA_ERROR_INVALID_SIGNATURE );
2180
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002181 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002182 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2183 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002184 TEST_EQUAL( psa_mac_verify( key, alg,
2185 input->x, input->len,
2186 perturbed_mac, expected_mac->len + 1 ),
2187 PSA_ERROR_INVALID_SIGNATURE );
2188
2189 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002190 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002191 PSA_ASSERT( psa_mac_update( &operation,
2192 input->x, input->len ) );
2193 TEST_EQUAL( psa_mac_verify_finish( &operation,
2194 perturbed_mac,
2195 expected_mac->len + 1 ),
2196 PSA_ERROR_INVALID_SIGNATURE );
2197
2198 /* Test changing one byte. */
2199 for( size_t i = 0; i < expected_mac->len; i++ )
2200 {
Chris Jones9634bb12021-01-20 15:56:42 +00002201 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002202 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002203
2204 TEST_EQUAL( psa_mac_verify( key, alg,
2205 input->x, input->len,
2206 perturbed_mac, expected_mac->len ),
2207 PSA_ERROR_INVALID_SIGNATURE );
2208
Ronald Cron5425a212020-08-04 14:58:35 +02002209 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002210 PSA_ASSERT( psa_mac_update( &operation,
2211 input->x, input->len ) );
2212 TEST_EQUAL( psa_mac_verify_finish( &operation,
2213 perturbed_mac,
2214 expected_mac->len ),
2215 PSA_ERROR_INVALID_SIGNATURE );
2216 perturbed_mac[i] ^= 1;
2217 }
2218
Gilles Peskine8c9def32018-02-08 10:02:12 +01002219exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002220 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002221 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002222 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002223 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002224}
2225/* END_CASE */
2226
2227/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002228void cipher_operation_init( )
2229{
Jaeden Ameroab439972019-02-15 14:12:05 +00002230 const uint8_t input[1] = { 0 };
2231 unsigned char output[1] = { 0 };
2232 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002233 /* Test each valid way of initializing the object, except for `= {0}`, as
2234 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2235 * though it's OK by the C standard. We could test for this, but we'd need
2236 * to supress the Clang warning for the test. */
2237 psa_cipher_operation_t func = psa_cipher_operation_init( );
2238 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2239 psa_cipher_operation_t zero;
2240
2241 memset( &zero, 0, sizeof( zero ) );
2242
Jaeden Ameroab439972019-02-15 14:12:05 +00002243 /* A freshly-initialized cipher operation should not be usable. */
2244 TEST_EQUAL( psa_cipher_update( &func,
2245 input, sizeof( input ),
2246 output, sizeof( output ),
2247 &output_length ),
2248 PSA_ERROR_BAD_STATE );
2249 TEST_EQUAL( psa_cipher_update( &init,
2250 input, sizeof( input ),
2251 output, sizeof( output ),
2252 &output_length ),
2253 PSA_ERROR_BAD_STATE );
2254 TEST_EQUAL( psa_cipher_update( &zero,
2255 input, sizeof( input ),
2256 output, sizeof( output ),
2257 &output_length ),
2258 PSA_ERROR_BAD_STATE );
2259
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002260 /* A default cipher operation should be abortable without error. */
2261 PSA_ASSERT( psa_cipher_abort( &func ) );
2262 PSA_ASSERT( psa_cipher_abort( &init ) );
2263 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002264}
2265/* END_CASE */
2266
2267/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002268void cipher_setup( int key_type_arg,
2269 data_t *key,
2270 int alg_arg,
2271 int expected_status_arg )
2272{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002273 psa_key_type_t key_type = key_type_arg;
2274 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002275 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002276 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002277 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002278#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002279 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2280#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002281
Gilles Peskine8817f612018-12-18 00:18:46 +01002282 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002283
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002284 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2285 &operation, &status ) )
2286 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002287 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002288
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002289 /* The operation object should be reusable. */
2290#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2291 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2292 smoke_test_key_data,
2293 sizeof( smoke_test_key_data ),
2294 KNOWN_SUPPORTED_CIPHER_ALG,
2295 &operation, &status ) )
2296 goto exit;
2297 TEST_EQUAL( status, PSA_SUCCESS );
2298#endif
2299
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002300exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002301 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002302 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002303}
2304/* END_CASE */
2305
Ronald Cronee414c72021-03-18 18:50:08 +01002306/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002307void cipher_bad_order( )
2308{
Ronald Cron5425a212020-08-04 14:58:35 +02002309 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002310 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2311 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002312 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002313 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002314 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002315 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002316 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2317 0xaa, 0xaa, 0xaa, 0xaa };
2318 const uint8_t text[] = {
2319 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2320 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002321 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002322 size_t length = 0;
2323
2324 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002325 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2326 psa_set_key_algorithm( &attributes, alg );
2327 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002328 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2329 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002330
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002331 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002332 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002333 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002334 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002335 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002336 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002337 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002338 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002339
2340 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002341 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002342 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002343 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002344 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002345 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002346 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002347 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002348
Jaeden Ameroab439972019-02-15 14:12:05 +00002349 /* Generate an IV without calling setup beforehand. */
2350 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2351 buffer, sizeof( buffer ),
2352 &length ),
2353 PSA_ERROR_BAD_STATE );
2354 PSA_ASSERT( psa_cipher_abort( &operation ) );
2355
2356 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002357 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002358 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2359 buffer, sizeof( buffer ),
2360 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002361 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002362 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2363 buffer, sizeof( buffer ),
2364 &length ),
2365 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002366 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002367 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002368 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002369
2370 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002371 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002372 PSA_ASSERT( psa_cipher_set_iv( &operation,
2373 iv, sizeof( iv ) ) );
2374 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2375 buffer, sizeof( buffer ),
2376 &length ),
2377 PSA_ERROR_BAD_STATE );
2378 PSA_ASSERT( psa_cipher_abort( &operation ) );
2379
2380 /* Set an IV without calling setup beforehand. */
2381 TEST_EQUAL( psa_cipher_set_iv( &operation,
2382 iv, sizeof( iv ) ),
2383 PSA_ERROR_BAD_STATE );
2384 PSA_ASSERT( psa_cipher_abort( &operation ) );
2385
2386 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002387 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002388 PSA_ASSERT( psa_cipher_set_iv( &operation,
2389 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002390 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002391 TEST_EQUAL( psa_cipher_set_iv( &operation,
2392 iv, sizeof( iv ) ),
2393 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002394 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002395 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002396 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002397
2398 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002399 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002400 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2401 buffer, sizeof( buffer ),
2402 &length ) );
2403 TEST_EQUAL( psa_cipher_set_iv( &operation,
2404 iv, sizeof( iv ) ),
2405 PSA_ERROR_BAD_STATE );
2406 PSA_ASSERT( psa_cipher_abort( &operation ) );
2407
2408 /* Call update without calling setup beforehand. */
2409 TEST_EQUAL( psa_cipher_update( &operation,
2410 text, sizeof( text ),
2411 buffer, sizeof( buffer ),
2412 &length ),
2413 PSA_ERROR_BAD_STATE );
2414 PSA_ASSERT( psa_cipher_abort( &operation ) );
2415
2416 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01002417 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002418 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002419 TEST_EQUAL( psa_cipher_update( &operation,
2420 text, sizeof( text ),
2421 buffer, sizeof( buffer ),
2422 &length ),
2423 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002424 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002425 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002426 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002427
2428 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002429 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002430 PSA_ASSERT( psa_cipher_set_iv( &operation,
2431 iv, sizeof( iv ) ) );
2432 PSA_ASSERT( psa_cipher_finish( &operation,
2433 buffer, sizeof( buffer ), &length ) );
2434 TEST_EQUAL( psa_cipher_update( &operation,
2435 text, sizeof( text ),
2436 buffer, sizeof( buffer ),
2437 &length ),
2438 PSA_ERROR_BAD_STATE );
2439 PSA_ASSERT( psa_cipher_abort( &operation ) );
2440
2441 /* Call finish without calling setup beforehand. */
2442 TEST_EQUAL( psa_cipher_finish( &operation,
2443 buffer, sizeof( buffer ), &length ),
2444 PSA_ERROR_BAD_STATE );
2445 PSA_ASSERT( psa_cipher_abort( &operation ) );
2446
2447 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002448 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002449 /* Not calling update means we are encrypting an empty buffer, which is OK
2450 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01002451 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002452 TEST_EQUAL( psa_cipher_finish( &operation,
2453 buffer, sizeof( buffer ), &length ),
2454 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002455 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002456 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002457 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002458
2459 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002460 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002461 PSA_ASSERT( psa_cipher_set_iv( &operation,
2462 iv, sizeof( iv ) ) );
2463 PSA_ASSERT( psa_cipher_finish( &operation,
2464 buffer, sizeof( buffer ), &length ) );
2465 TEST_EQUAL( psa_cipher_finish( &operation,
2466 buffer, sizeof( buffer ), &length ),
2467 PSA_ERROR_BAD_STATE );
2468 PSA_ASSERT( psa_cipher_abort( &operation ) );
2469
Ronald Cron5425a212020-08-04 14:58:35 +02002470 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002471
Jaeden Ameroab439972019-02-15 14:12:05 +00002472exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002473 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002474 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002475}
2476/* END_CASE */
2477
2478/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002479void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002480 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002481 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002482 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002483{
Ronald Cron5425a212020-08-04 14:58:35 +02002484 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002485 psa_status_t status;
2486 psa_key_type_t key_type = key_type_arg;
2487 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002488 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002489 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002490 size_t output_buffer_size = 0;
2491 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002492 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002493 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002494 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002495
Gilles Peskine8817f612018-12-18 00:18:46 +01002496 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002497
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002498 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2499 psa_set_key_algorithm( &attributes, alg );
2500 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002501
Ronald Cron5425a212020-08-04 14:58:35 +02002502 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2503 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002504
Ronald Cron5425a212020-08-04 14:58:35 +02002505 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002506
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002507 if( iv->len > 0 )
2508 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002509 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002510 }
2511
gabor-mezei-armceface22021-01-21 12:26:17 +01002512 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2513 TEST_ASSERT( output_buffer_size <=
2514 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002515 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002516
Gilles Peskine8817f612018-12-18 00:18:46 +01002517 PSA_ASSERT( psa_cipher_update( &operation,
2518 input->x, input->len,
2519 output, output_buffer_size,
2520 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002521 TEST_ASSERT( function_output_length <=
2522 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2523 TEST_ASSERT( function_output_length <=
2524 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002525 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002526
Gilles Peskine50e586b2018-06-08 14:28:46 +02002527 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002528 ( output_buffer_size == 0 ? NULL :
2529 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002530 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002531 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002532 TEST_ASSERT( function_output_length <=
2533 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2534 TEST_ASSERT( function_output_length <=
2535 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002536 total_output_length += function_output_length;
2537
Gilles Peskinefe11b722018-12-18 00:24:04 +01002538 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002539 if( expected_status == PSA_SUCCESS )
2540 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002541 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002542 ASSERT_COMPARE( expected_output->x, expected_output->len,
2543 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002544 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002545
Gilles Peskine50e586b2018-06-08 14:28:46 +02002546exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002547 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002548 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002549 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002550 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002551}
2552/* END_CASE */
2553
2554/* BEGIN_CASE */
2555void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002556 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002557 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002558 int first_part_size_arg,
2559 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002560 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002561{
Ronald Cron5425a212020-08-04 14:58:35 +02002562 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002563 psa_key_type_t key_type = key_type_arg;
2564 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002565 size_t first_part_size = first_part_size_arg;
2566 size_t output1_length = output1_length_arg;
2567 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002568 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002569 size_t output_buffer_size = 0;
2570 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002571 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002572 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002573 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002574
Gilles Peskine8817f612018-12-18 00:18:46 +01002575 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002576
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002577 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2578 psa_set_key_algorithm( &attributes, alg );
2579 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002580
Ronald Cron5425a212020-08-04 14:58:35 +02002581 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2582 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002583
Ronald Cron5425a212020-08-04 14:58:35 +02002584 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002585
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002586 if( iv->len > 0 )
2587 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002588 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002589 }
2590
gabor-mezei-armceface22021-01-21 12:26:17 +01002591 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2592 TEST_ASSERT( output_buffer_size <=
2593 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002594 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002595
Gilles Peskinee0866522019-02-19 19:44:00 +01002596 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002597 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2598 output, output_buffer_size,
2599 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002600 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002601 TEST_ASSERT( function_output_length <=
2602 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2603 TEST_ASSERT( function_output_length <=
2604 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002605 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002606
Gilles Peskine8817f612018-12-18 00:18:46 +01002607 PSA_ASSERT( psa_cipher_update( &operation,
2608 input->x + first_part_size,
2609 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002610 ( output_buffer_size == 0 ? NULL :
2611 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002612 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002613 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002614 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002615 TEST_ASSERT( function_output_length <=
2616 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2617 alg,
2618 input->len - first_part_size ) );
2619 TEST_ASSERT( function_output_length <=
2620 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002621 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002622
Gilles Peskine8817f612018-12-18 00:18:46 +01002623 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002624 ( output_buffer_size == 0 ? NULL :
2625 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002626 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002627 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002628 TEST_ASSERT( function_output_length <=
2629 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2630 TEST_ASSERT( function_output_length <=
2631 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002632 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002633 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002634
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002635 ASSERT_COMPARE( expected_output->x, expected_output->len,
2636 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002637
2638exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002639 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002640 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002641 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002642 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002643}
2644/* END_CASE */
2645
2646/* BEGIN_CASE */
2647void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002648 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002649 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002650 int first_part_size_arg,
2651 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002652 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002653{
Ronald Cron5425a212020-08-04 14:58:35 +02002654 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002655 psa_key_type_t key_type = key_type_arg;
2656 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002657 size_t first_part_size = first_part_size_arg;
2658 size_t output1_length = output1_length_arg;
2659 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002660 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002661 size_t output_buffer_size = 0;
2662 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002663 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002664 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002665 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002666
Gilles Peskine8817f612018-12-18 00:18:46 +01002667 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002668
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002669 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2670 psa_set_key_algorithm( &attributes, alg );
2671 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002672
Ronald Cron5425a212020-08-04 14:58:35 +02002673 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2674 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002675
Ronald Cron5425a212020-08-04 14:58:35 +02002676 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002677
Steven Cooreman177deba2020-09-07 17:14:14 +02002678 if( iv->len > 0 )
2679 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002680 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002681 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002682
gabor-mezei-armceface22021-01-21 12:26:17 +01002683 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2684 TEST_ASSERT( output_buffer_size <=
2685 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002686 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002687
Gilles Peskinee0866522019-02-19 19:44:00 +01002688 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002689 PSA_ASSERT( psa_cipher_update( &operation,
2690 input->x, first_part_size,
2691 output, output_buffer_size,
2692 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002693 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002694 TEST_ASSERT( function_output_length <=
2695 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2696 TEST_ASSERT( function_output_length <=
2697 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002698 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002699
Gilles Peskine8817f612018-12-18 00:18:46 +01002700 PSA_ASSERT( psa_cipher_update( &operation,
2701 input->x + first_part_size,
2702 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002703 ( output_buffer_size == 0 ? NULL :
2704 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002705 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002706 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002707 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002708 TEST_ASSERT( function_output_length <=
2709 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2710 alg,
2711 input->len - first_part_size ) );
2712 TEST_ASSERT( function_output_length <=
2713 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002714 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002715
Gilles Peskine8817f612018-12-18 00:18:46 +01002716 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002717 ( output_buffer_size == 0 ? NULL :
2718 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002719 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002720 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002721 TEST_ASSERT( function_output_length <=
2722 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2723 TEST_ASSERT( function_output_length <=
2724 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002725 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002726 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002727
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002728 ASSERT_COMPARE( expected_output->x, expected_output->len,
2729 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002730
2731exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002732 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002733 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002734 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002735 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002736}
2737/* END_CASE */
2738
Gilles Peskine50e586b2018-06-08 14:28:46 +02002739/* BEGIN_CASE */
2740void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002741 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002742 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002743 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002744{
Ronald Cron5425a212020-08-04 14:58:35 +02002745 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002746 psa_status_t status;
2747 psa_key_type_t key_type = key_type_arg;
2748 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002749 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002750 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002751 size_t output_buffer_size = 0;
2752 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002753 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002754 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002755 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002756
Gilles Peskine8817f612018-12-18 00:18:46 +01002757 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002758
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002759 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2760 psa_set_key_algorithm( &attributes, alg );
2761 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002762
Ronald Cron5425a212020-08-04 14:58:35 +02002763 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2764 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002765
Ronald Cron5425a212020-08-04 14:58:35 +02002766 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002767
Steven Cooreman177deba2020-09-07 17:14:14 +02002768 if( iv->len > 0 )
2769 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002770 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002771 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002772
gabor-mezei-armceface22021-01-21 12:26:17 +01002773 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2774 TEST_ASSERT( output_buffer_size <=
2775 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002776 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002777
Gilles Peskine8817f612018-12-18 00:18:46 +01002778 PSA_ASSERT( psa_cipher_update( &operation,
2779 input->x, input->len,
2780 output, output_buffer_size,
2781 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002782 TEST_ASSERT( function_output_length <=
2783 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2784 TEST_ASSERT( function_output_length <=
2785 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002786 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002787
Gilles Peskine50e586b2018-06-08 14:28:46 +02002788 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002789 ( output_buffer_size == 0 ? NULL :
2790 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002791 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002792 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002793 TEST_ASSERT( function_output_length <=
2794 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2795 TEST_ASSERT( function_output_length <=
2796 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002797 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002798 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002799
2800 if( expected_status == PSA_SUCCESS )
2801 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002802 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002803 ASSERT_COMPARE( expected_output->x, expected_output->len,
2804 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002805 }
2806
Gilles Peskine50e586b2018-06-08 14:28:46 +02002807exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002808 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002809 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002810 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002811 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002812}
2813/* END_CASE */
2814
Gilles Peskine50e586b2018-06-08 14:28:46 +02002815/* BEGIN_CASE */
2816void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002817 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002818 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002819{
Ronald Cron5425a212020-08-04 14:58:35 +02002820 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002821 psa_key_type_t key_type = key_type_arg;
2822 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002823 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002824 size_t iv_size = 16;
2825 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002826 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002827 size_t output1_size = 0;
2828 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002829 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002830 size_t output2_size = 0;
2831 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002832 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002833 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2834 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002835 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002836
Gilles Peskine8817f612018-12-18 00:18:46 +01002837 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002838
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002839 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2840 psa_set_key_algorithm( &attributes, alg );
2841 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002842
Ronald Cron5425a212020-08-04 14:58:35 +02002843 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2844 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002845
Ronald Cron5425a212020-08-04 14:58:35 +02002846 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2847 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002848
Steven Cooreman177deba2020-09-07 17:14:14 +02002849 if( alg != PSA_ALG_ECB_NO_PADDING )
2850 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002851 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2852 iv, iv_size,
2853 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002854 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002855 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2856 TEST_ASSERT( output1_size <=
2857 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002858 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002859
Gilles Peskine8817f612018-12-18 00:18:46 +01002860 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2861 output1, output1_size,
2862 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002863 TEST_ASSERT( output1_length <=
2864 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2865 TEST_ASSERT( output1_length <=
2866 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2867
Gilles Peskine8817f612018-12-18 00:18:46 +01002868 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002869 output1 + output1_length,
2870 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002871 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002872 TEST_ASSERT( function_output_length <=
2873 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2874 TEST_ASSERT( function_output_length <=
2875 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002876
Gilles Peskine048b7f02018-06-08 14:20:49 +02002877 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002878
Gilles Peskine8817f612018-12-18 00:18:46 +01002879 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002880
2881 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002882 TEST_ASSERT( output2_size <=
2883 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2884 TEST_ASSERT( output2_size <=
2885 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002886 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002887
Steven Cooreman177deba2020-09-07 17:14:14 +02002888 if( iv_length > 0 )
2889 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002890 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2891 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002892 }
2893
Gilles Peskine8817f612018-12-18 00:18:46 +01002894 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2895 output2, output2_size,
2896 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002897 TEST_ASSERT( output2_length <=
2898 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
2899 TEST_ASSERT( output2_length <=
2900 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
2901
Gilles Peskine048b7f02018-06-08 14:20:49 +02002902 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002903 PSA_ASSERT( psa_cipher_finish( &operation2,
2904 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002905 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002906 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002907 TEST_ASSERT( function_output_length <=
2908 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2909 TEST_ASSERT( function_output_length <=
2910 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03002911
Gilles Peskine048b7f02018-06-08 14:20:49 +02002912 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002913
Gilles Peskine8817f612018-12-18 00:18:46 +01002914 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002915
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002916 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002917
2918exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002919 psa_cipher_abort( &operation1 );
2920 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002921 mbedtls_free( output1 );
2922 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002923 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002924 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03002925}
2926/* END_CASE */
2927
2928/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002929void cipher_verify_output_multipart( int alg_arg,
2930 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002931 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002932 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002933 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03002934{
Ronald Cron5425a212020-08-04 14:58:35 +02002935 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002936 psa_key_type_t key_type = key_type_arg;
2937 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002938 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002939 unsigned char iv[16] = {0};
2940 size_t iv_size = 16;
2941 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002942 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002943 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002944 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002945 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002946 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002947 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002948 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002949 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2950 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002951 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002952
Gilles Peskine8817f612018-12-18 00:18:46 +01002953 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002954
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002955 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2956 psa_set_key_algorithm( &attributes, alg );
2957 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002958
Ronald Cron5425a212020-08-04 14:58:35 +02002959 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2960 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03002961
Ronald Cron5425a212020-08-04 14:58:35 +02002962 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2963 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002964
Steven Cooreman177deba2020-09-07 17:14:14 +02002965 if( alg != PSA_ALG_ECB_NO_PADDING )
2966 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002967 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2968 iv, iv_size,
2969 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002970 }
2971
gabor-mezei-armceface22021-01-21 12:26:17 +01002972 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2973 TEST_ASSERT( output1_buffer_size <=
2974 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002975 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002976
Gilles Peskinee0866522019-02-19 19:44:00 +01002977 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002978
Gilles Peskine8817f612018-12-18 00:18:46 +01002979 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2980 output1, output1_buffer_size,
2981 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002982 TEST_ASSERT( function_output_length <=
2983 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2984 TEST_ASSERT( function_output_length <=
2985 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002986 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002987
Gilles Peskine8817f612018-12-18 00:18:46 +01002988 PSA_ASSERT( psa_cipher_update( &operation1,
2989 input->x + first_part_size,
2990 input->len - first_part_size,
2991 output1, output1_buffer_size,
2992 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002993 TEST_ASSERT( function_output_length <=
2994 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2995 alg,
2996 input->len - first_part_size ) );
2997 TEST_ASSERT( function_output_length <=
2998 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002999 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003000
Gilles Peskine8817f612018-12-18 00:18:46 +01003001 PSA_ASSERT( psa_cipher_finish( &operation1,
3002 output1 + output1_length,
3003 output1_buffer_size - output1_length,
3004 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003005 TEST_ASSERT( function_output_length <=
3006 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3007 TEST_ASSERT( function_output_length <=
3008 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003009 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003010
Gilles Peskine8817f612018-12-18 00:18:46 +01003011 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003012
Gilles Peskine048b7f02018-06-08 14:20:49 +02003013 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003014 TEST_ASSERT( output2_buffer_size <=
3015 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3016 TEST_ASSERT( output2_buffer_size <=
3017 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003018 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003019
Steven Cooreman177deba2020-09-07 17:14:14 +02003020 if( iv_length > 0 )
3021 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003022 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3023 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003024 }
Moran Pekerded84402018-06-06 16:36:50 +03003025
Gilles Peskine8817f612018-12-18 00:18:46 +01003026 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3027 output2, output2_buffer_size,
3028 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003029 TEST_ASSERT( function_output_length <=
3030 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3031 TEST_ASSERT( function_output_length <=
3032 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003033 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003034
Gilles Peskine8817f612018-12-18 00:18:46 +01003035 PSA_ASSERT( psa_cipher_update( &operation2,
3036 output1 + first_part_size,
3037 output1_length - first_part_size,
3038 output2, output2_buffer_size,
3039 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003040 TEST_ASSERT( function_output_length <=
3041 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3042 alg,
3043 output1_length - first_part_size ) );
3044 TEST_ASSERT( function_output_length <=
3045 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003046 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003047
Gilles Peskine8817f612018-12-18 00:18:46 +01003048 PSA_ASSERT( psa_cipher_finish( &operation2,
3049 output2 + output2_length,
3050 output2_buffer_size - output2_length,
3051 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003052 TEST_ASSERT( function_output_length <=
3053 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3054 TEST_ASSERT( function_output_length <=
3055 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003056 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003057
Gilles Peskine8817f612018-12-18 00:18:46 +01003058 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003059
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003060 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003061
3062exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003063 psa_cipher_abort( &operation1 );
3064 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003065 mbedtls_free( output1 );
3066 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003067 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003068 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003069}
3070/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003071
Gilles Peskine20035e32018-02-03 22:44:14 +01003072/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003073void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003074 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003075 data_t *nonce,
3076 data_t *additional_data,
3077 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003078 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003079{
Ronald Cron5425a212020-08-04 14:58:35 +02003080 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003081 psa_key_type_t key_type = key_type_arg;
3082 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003083 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003084 unsigned char *output_data = NULL;
3085 size_t output_size = 0;
3086 size_t output_length = 0;
3087 unsigned char *output_data2 = NULL;
3088 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003089 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003090 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003091 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003092
Gilles Peskine8817f612018-12-18 00:18:46 +01003093 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003094
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003095 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3096 psa_set_key_algorithm( &attributes, alg );
3097 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003098
Gilles Peskine049c7532019-05-15 20:22:09 +02003099 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003100 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003101 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3102 key_bits = psa_get_key_bits( &attributes );
3103
3104 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3105 alg );
3106 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3107 * should be exact. */
3108 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3109 expected_result != PSA_ERROR_NOT_SUPPORTED )
3110 {
3111 TEST_EQUAL( output_size,
3112 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3113 TEST_ASSERT( output_size <=
3114 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3115 }
3116 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003117
Steven Cooremanf49478b2021-02-15 15:19:25 +01003118 status = psa_aead_encrypt( key, alg,
3119 nonce->x, nonce->len,
3120 additional_data->x,
3121 additional_data->len,
3122 input_data->x, input_data->len,
3123 output_data, output_size,
3124 &output_length );
3125
3126 /* If the operation is not supported, just skip and not fail in case the
3127 * encryption involves a common limitation of cryptography hardwares and
3128 * an alternative implementation. */
3129 if( status == PSA_ERROR_NOT_SUPPORTED )
3130 {
3131 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3132 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3133 }
3134
3135 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003136
3137 if( PSA_SUCCESS == expected_result )
3138 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003139 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003140
Gilles Peskine003a4a92019-05-14 16:09:40 +02003141 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3142 * should be exact. */
3143 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003144 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003145
gabor-mezei-armceface22021-01-21 12:26:17 +01003146 TEST_ASSERT( input_data->len <=
3147 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3148
Ronald Cron5425a212020-08-04 14:58:35 +02003149 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003150 nonce->x, nonce->len,
3151 additional_data->x,
3152 additional_data->len,
3153 output_data, output_length,
3154 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003155 &output_length2 ),
3156 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003157
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003158 ASSERT_COMPARE( input_data->x, input_data->len,
3159 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003160 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003161
Gilles Peskinea1cac842018-06-11 19:33:02 +02003162exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003163 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003164 mbedtls_free( output_data );
3165 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003166 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003167}
3168/* END_CASE */
3169
3170/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003171void aead_encrypt( int key_type_arg, data_t *key_data,
3172 int alg_arg,
3173 data_t *nonce,
3174 data_t *additional_data,
3175 data_t *input_data,
3176 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003177{
Ronald Cron5425a212020-08-04 14:58:35 +02003178 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003179 psa_key_type_t key_type = key_type_arg;
3180 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003181 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003182 unsigned char *output_data = NULL;
3183 size_t output_size = 0;
3184 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003185 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003186 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003187
Gilles Peskine8817f612018-12-18 00:18:46 +01003188 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003189
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003190 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3191 psa_set_key_algorithm( &attributes, alg );
3192 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003193
Gilles Peskine049c7532019-05-15 20:22:09 +02003194 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003195 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003196 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3197 key_bits = psa_get_key_bits( &attributes );
3198
3199 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3200 alg );
3201 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3202 * should be exact. */
3203 TEST_EQUAL( output_size,
3204 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3205 TEST_ASSERT( output_size <=
3206 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3207 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003208
Steven Cooremand588ea12021-01-11 19:36:04 +01003209 status = psa_aead_encrypt( key, alg,
3210 nonce->x, nonce->len,
3211 additional_data->x, additional_data->len,
3212 input_data->x, input_data->len,
3213 output_data, output_size,
3214 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003215
Ronald Cron28a45ed2021-02-09 20:35:42 +01003216 /* If the operation is not supported, just skip and not fail in case the
3217 * encryption involves a common limitation of cryptography hardwares and
3218 * an alternative implementation. */
3219 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003220 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003221 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3222 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003223 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003224
3225 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003226 ASSERT_COMPARE( expected_result->x, expected_result->len,
3227 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003228
Gilles Peskinea1cac842018-06-11 19:33:02 +02003229exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003230 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003231 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003232 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003233}
3234/* END_CASE */
3235
3236/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003237void aead_decrypt( int key_type_arg, data_t *key_data,
3238 int alg_arg,
3239 data_t *nonce,
3240 data_t *additional_data,
3241 data_t *input_data,
3242 data_t *expected_data,
3243 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003244{
Ronald Cron5425a212020-08-04 14:58:35 +02003245 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003246 psa_key_type_t key_type = key_type_arg;
3247 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003248 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003249 unsigned char *output_data = NULL;
3250 size_t output_size = 0;
3251 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003252 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003253 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003254 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003255
Gilles Peskine8817f612018-12-18 00:18:46 +01003256 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003257
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003258 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3259 psa_set_key_algorithm( &attributes, alg );
3260 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003261
Gilles Peskine049c7532019-05-15 20:22:09 +02003262 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003263 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003264 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3265 key_bits = psa_get_key_bits( &attributes );
3266
3267 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3268 alg );
3269 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3270 expected_result != PSA_ERROR_NOT_SUPPORTED )
3271 {
3272 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3273 * should be exact. */
3274 TEST_EQUAL( output_size,
3275 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3276 TEST_ASSERT( output_size <=
3277 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3278 }
3279 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003280
Steven Cooremand588ea12021-01-11 19:36:04 +01003281 status = psa_aead_decrypt( key, alg,
3282 nonce->x, nonce->len,
3283 additional_data->x,
3284 additional_data->len,
3285 input_data->x, input_data->len,
3286 output_data, output_size,
3287 &output_length );
3288
Ronald Cron28a45ed2021-02-09 20:35:42 +01003289 /* If the operation is not supported, just skip and not fail in case the
3290 * decryption involves a common limitation of cryptography hardwares and
3291 * an alternative implementation. */
3292 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003293 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003294 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3295 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003296 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003297
3298 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003299
Gilles Peskine2d277862018-06-18 15:41:12 +02003300 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003301 ASSERT_COMPARE( expected_data->x, expected_data->len,
3302 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003303
Gilles Peskinea1cac842018-06-11 19:33:02 +02003304exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003305 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003306 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003307 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003308}
3309/* END_CASE */
3310
3311/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003312void signature_size( int type_arg,
3313 int bits,
3314 int alg_arg,
3315 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003316{
3317 psa_key_type_t type = type_arg;
3318 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003319 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003320
Gilles Peskinefe11b722018-12-18 00:24:04 +01003321 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003322
Gilles Peskinee59236f2018-01-27 23:32:46 +01003323exit:
3324 ;
3325}
3326/* END_CASE */
3327
3328/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003329void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3330 int alg_arg, data_t *input_data,
3331 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003332{
Ronald Cron5425a212020-08-04 14:58:35 +02003333 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003334 psa_key_type_t key_type = key_type_arg;
3335 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003336 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003337 unsigned char *signature = NULL;
3338 size_t signature_size;
3339 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003340 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003341
Gilles Peskine8817f612018-12-18 00:18:46 +01003342 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003343
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003344 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003345 psa_set_key_algorithm( &attributes, alg );
3346 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003347
Gilles Peskine049c7532019-05-15 20:22:09 +02003348 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003349 &key ) );
3350 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003351 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003352
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003353 /* Allocate a buffer which has the size advertized by the
3354 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003355 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003356 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003357 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003358 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003359 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003360
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003361 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003362 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003363 input_data->x, input_data->len,
3364 signature, signature_size,
3365 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003366 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003367 ASSERT_COMPARE( output_data->x, output_data->len,
3368 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003369
3370exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003371 /*
3372 * Key attributes may have been returned by psa_get_key_attributes()
3373 * thus reset them as required.
3374 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003375 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003376
Ronald Cron5425a212020-08-04 14:58:35 +02003377 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003378 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003379 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003380}
3381/* END_CASE */
3382
3383/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003384void sign_hash_fail( int key_type_arg, data_t *key_data,
3385 int alg_arg, data_t *input_data,
3386 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003387{
Ronald Cron5425a212020-08-04 14:58:35 +02003388 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003389 psa_key_type_t key_type = key_type_arg;
3390 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003391 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003392 psa_status_t actual_status;
3393 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003394 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003395 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003396 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003397
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003398 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003399
Gilles Peskine8817f612018-12-18 00:18:46 +01003400 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003401
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003402 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003403 psa_set_key_algorithm( &attributes, alg );
3404 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003405
Gilles Peskine049c7532019-05-15 20:22:09 +02003406 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003407 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003408
Ronald Cron5425a212020-08-04 14:58:35 +02003409 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003410 input_data->x, input_data->len,
3411 signature, signature_size,
3412 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003413 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003414 /* The value of *signature_length is unspecified on error, but
3415 * whatever it is, it should be less than signature_size, so that
3416 * if the caller tries to read *signature_length bytes without
3417 * checking the error code then they don't overflow a buffer. */
3418 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003419
3420exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003421 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003422 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003423 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003424 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003425}
3426/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003427
3428/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003429void sign_verify_hash( int key_type_arg, data_t *key_data,
3430 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003431{
Ronald Cron5425a212020-08-04 14:58:35 +02003432 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003433 psa_key_type_t key_type = key_type_arg;
3434 psa_algorithm_t alg = alg_arg;
3435 size_t key_bits;
3436 unsigned char *signature = NULL;
3437 size_t signature_size;
3438 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003439 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003440
Gilles Peskine8817f612018-12-18 00:18:46 +01003441 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003442
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003443 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003444 psa_set_key_algorithm( &attributes, alg );
3445 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003446
Gilles Peskine049c7532019-05-15 20:22:09 +02003447 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003448 &key ) );
3449 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003450 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003451
3452 /* Allocate a buffer which has the size advertized by the
3453 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003454 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003455 key_bits, alg );
3456 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003457 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003458 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003459
3460 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003461 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003462 input_data->x, input_data->len,
3463 signature, signature_size,
3464 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003465 /* Check that the signature length looks sensible. */
3466 TEST_ASSERT( signature_length <= signature_size );
3467 TEST_ASSERT( signature_length > 0 );
3468
3469 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003470 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003471 input_data->x, input_data->len,
3472 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003473
3474 if( input_data->len != 0 )
3475 {
3476 /* Flip a bit in the input and verify that the signature is now
3477 * detected as invalid. Flip a bit at the beginning, not at the end,
3478 * because ECDSA may ignore the last few bits of the input. */
3479 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003480 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003481 input_data->x, input_data->len,
3482 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003483 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003484 }
3485
3486exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003487 /*
3488 * Key attributes may have been returned by psa_get_key_attributes()
3489 * thus reset them as required.
3490 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003491 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003492
Ronald Cron5425a212020-08-04 14:58:35 +02003493 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003494 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003495 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003496}
3497/* END_CASE */
3498
3499/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003500void verify_hash( int key_type_arg, data_t *key_data,
3501 int alg_arg, data_t *hash_data,
3502 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003503{
Ronald Cron5425a212020-08-04 14:58:35 +02003504 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003505 psa_key_type_t key_type = key_type_arg;
3506 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003507 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003508
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003509 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003510
Gilles Peskine8817f612018-12-18 00:18:46 +01003511 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003512
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003513 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003514 psa_set_key_algorithm( &attributes, alg );
3515 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003516
Gilles Peskine049c7532019-05-15 20:22:09 +02003517 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003518 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003519
Ronald Cron5425a212020-08-04 14:58:35 +02003520 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003521 hash_data->x, hash_data->len,
3522 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003523
itayzafrir5c753392018-05-08 11:18:38 +03003524exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003525 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003526 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003527 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003528}
3529/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003530
3531/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003532void verify_hash_fail( int key_type_arg, data_t *key_data,
3533 int alg_arg, data_t *hash_data,
3534 data_t *signature_data,
3535 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003536{
Ronald Cron5425a212020-08-04 14:58:35 +02003537 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003538 psa_key_type_t key_type = key_type_arg;
3539 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003540 psa_status_t actual_status;
3541 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003542 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003543
Gilles Peskine8817f612018-12-18 00:18:46 +01003544 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003545
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003546 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003547 psa_set_key_algorithm( &attributes, alg );
3548 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003549
Gilles Peskine049c7532019-05-15 20:22:09 +02003550 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003551 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003552
Ronald Cron5425a212020-08-04 14:58:35 +02003553 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003554 hash_data->x, hash_data->len,
3555 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003556 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003557
3558exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003559 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003560 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003561 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003562}
3563/* END_CASE */
3564
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003565/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02003566void sign_message_deterministic( int key_type_arg,
3567 data_t *key_data,
3568 int alg_arg,
3569 data_t *input_data,
3570 data_t *output_data )
3571{
3572 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3573 psa_key_type_t key_type = key_type_arg;
3574 psa_algorithm_t alg = alg_arg;
3575 size_t key_bits;
3576 unsigned char *signature = NULL;
3577 size_t signature_size;
3578 size_t signature_length = 0xdeadbeef;
3579 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3580
3581 PSA_ASSERT( psa_crypto_init( ) );
3582
3583 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3584 psa_set_key_algorithm( &attributes, alg );
3585 psa_set_key_type( &attributes, key_type );
3586
3587 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3588 &key ) );
3589 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3590 key_bits = psa_get_key_bits( &attributes );
3591
3592 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3593 TEST_ASSERT( signature_size != 0 );
3594 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3595 ASSERT_ALLOC( signature, signature_size );
3596
3597 PSA_ASSERT( psa_sign_message( key, alg,
3598 input_data->x, input_data->len,
3599 signature, signature_size,
3600 &signature_length ) );
3601
3602 ASSERT_COMPARE( output_data->x, output_data->len,
3603 signature, signature_length );
3604
3605exit:
3606 psa_reset_key_attributes( &attributes );
3607
3608 psa_destroy_key( key );
3609 mbedtls_free( signature );
3610 PSA_DONE( );
3611
3612}
3613/* END_CASE */
3614
3615/* BEGIN_CASE */
3616void sign_message_fail( int key_type_arg,
3617 data_t *key_data,
3618 int alg_arg,
3619 data_t *input_data,
3620 int signature_size_arg,
3621 int expected_status_arg )
3622{
3623 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3624 psa_key_type_t key_type = key_type_arg;
3625 psa_algorithm_t alg = alg_arg;
3626 size_t signature_size = signature_size_arg;
3627 psa_status_t actual_status;
3628 psa_status_t expected_status = expected_status_arg;
3629 unsigned char *signature = NULL;
3630 size_t signature_length = 0xdeadbeef;
3631 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3632
3633 ASSERT_ALLOC( signature, signature_size );
3634
3635 PSA_ASSERT( psa_crypto_init( ) );
3636
3637 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3638 psa_set_key_algorithm( &attributes, alg );
3639 psa_set_key_type( &attributes, key_type );
3640
3641 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3642 &key ) );
3643
3644 actual_status = psa_sign_message( key, alg,
3645 input_data->x, input_data->len,
3646 signature, signature_size,
3647 &signature_length );
3648 TEST_EQUAL( actual_status, expected_status );
3649 /* The value of *signature_length is unspecified on error, but
3650 * whatever it is, it should be less than signature_size, so that
3651 * if the caller tries to read *signature_length bytes without
3652 * checking the error code then they don't overflow a buffer. */
3653 TEST_ASSERT( signature_length <= signature_size );
3654
3655exit:
3656 psa_reset_key_attributes( &attributes );
3657 psa_destroy_key( key );
3658 mbedtls_free( signature );
3659 PSA_DONE( );
3660}
3661/* END_CASE */
3662
3663/* BEGIN_CASE */
3664void sign_verify_message( int key_type_arg,
3665 data_t *key_data,
3666 int alg_arg,
3667 data_t *input_data )
3668{
3669 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3670 psa_key_type_t key_type = key_type_arg;
3671 psa_algorithm_t alg = alg_arg;
3672 size_t key_bits;
3673 unsigned char *signature = NULL;
3674 size_t signature_size;
3675 size_t signature_length = 0xdeadbeef;
3676 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3677
3678 PSA_ASSERT( psa_crypto_init( ) );
3679
3680 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3681 PSA_KEY_USAGE_VERIFY_MESSAGE );
3682 psa_set_key_algorithm( &attributes, alg );
3683 psa_set_key_type( &attributes, key_type );
3684
3685 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3686 &key ) );
3687 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3688 key_bits = psa_get_key_bits( &attributes );
3689
3690 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3691 TEST_ASSERT( signature_size != 0 );
3692 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3693 ASSERT_ALLOC( signature, signature_size );
3694
3695 PSA_ASSERT( psa_sign_message( key, alg,
3696 input_data->x, input_data->len,
3697 signature, signature_size,
3698 &signature_length ) );
3699 TEST_ASSERT( signature_length <= signature_size );
3700 TEST_ASSERT( signature_length > 0 );
3701
3702 PSA_ASSERT( psa_verify_message( key, alg,
3703 input_data->x, input_data->len,
3704 signature, signature_length ) );
3705
3706 if( input_data->len != 0 )
3707 {
3708 /* Flip a bit in the input and verify that the signature is now
3709 * detected as invalid. Flip a bit at the beginning, not at the end,
3710 * because ECDSA may ignore the last few bits of the input. */
3711 input_data->x[0] ^= 1;
3712 TEST_EQUAL( psa_verify_message( key, alg,
3713 input_data->x, input_data->len,
3714 signature, signature_length ),
3715 PSA_ERROR_INVALID_SIGNATURE );
3716 }
3717
3718exit:
3719 psa_reset_key_attributes( &attributes );
3720
3721 psa_destroy_key( key );
3722 mbedtls_free( signature );
3723 PSA_DONE( );
3724}
3725/* END_CASE */
3726
3727/* BEGIN_CASE */
3728void verify_message( int key_type_arg,
3729 data_t *key_data,
3730 int alg_arg,
3731 data_t *input_data,
3732 data_t *signature_data )
3733{
3734 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3735 psa_key_type_t key_type = key_type_arg;
3736 psa_algorithm_t alg = alg_arg;
3737 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3738
3739 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3740
3741 PSA_ASSERT( psa_crypto_init( ) );
3742
3743 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3744 psa_set_key_algorithm( &attributes, alg );
3745 psa_set_key_type( &attributes, key_type );
3746
3747 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3748 &key ) );
3749
3750 PSA_ASSERT( psa_verify_message( key, alg,
3751 input_data->x, input_data->len,
3752 signature_data->x, signature_data->len ) );
3753
3754exit:
3755 psa_reset_key_attributes( &attributes );
3756 psa_destroy_key( key );
3757 PSA_DONE( );
3758}
3759/* END_CASE */
3760
3761/* BEGIN_CASE */
3762void verify_message_fail( int key_type_arg,
3763 data_t *key_data,
3764 int alg_arg,
3765 data_t *hash_data,
3766 data_t *signature_data,
3767 int expected_status_arg )
3768{
3769 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3770 psa_key_type_t key_type = key_type_arg;
3771 psa_algorithm_t alg = alg_arg;
3772 psa_status_t actual_status;
3773 psa_status_t expected_status = expected_status_arg;
3774 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3775
3776 PSA_ASSERT( psa_crypto_init( ) );
3777
3778 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3779 psa_set_key_algorithm( &attributes, alg );
3780 psa_set_key_type( &attributes, key_type );
3781
3782 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3783 &key ) );
3784
3785 actual_status = psa_verify_message( key, alg,
3786 hash_data->x, hash_data->len,
3787 signature_data->x,
3788 signature_data->len );
3789 TEST_EQUAL( actual_status, expected_status );
3790
3791exit:
3792 psa_reset_key_attributes( &attributes );
3793 psa_destroy_key( key );
3794 PSA_DONE( );
3795}
3796/* END_CASE */
3797
3798/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003799void asymmetric_encrypt( int key_type_arg,
3800 data_t *key_data,
3801 int alg_arg,
3802 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003803 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003804 int expected_output_length_arg,
3805 int expected_status_arg )
3806{
Ronald Cron5425a212020-08-04 14:58:35 +02003807 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003808 psa_key_type_t key_type = key_type_arg;
3809 psa_algorithm_t alg = alg_arg;
3810 size_t expected_output_length = expected_output_length_arg;
3811 size_t key_bits;
3812 unsigned char *output = NULL;
3813 size_t output_size;
3814 size_t output_length = ~0;
3815 psa_status_t actual_status;
3816 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003817 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003818
Gilles Peskine8817f612018-12-18 00:18:46 +01003819 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003820
Gilles Peskine656896e2018-06-29 19:12:28 +02003821 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003822 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3823 psa_set_key_algorithm( &attributes, alg );
3824 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003825 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003826 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003827
3828 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003829 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003830 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003831
Gilles Peskine656896e2018-06-29 19:12:28 +02003832 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003833 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003834 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003835
3836 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003837 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003838 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003839 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003840 output, output_size,
3841 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003842 TEST_EQUAL( actual_status, expected_status );
3843 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003844
Gilles Peskine68428122018-06-30 18:42:41 +02003845 /* If the label is empty, the test framework puts a non-null pointer
3846 * in label->x. Test that a null pointer works as well. */
3847 if( label->len == 0 )
3848 {
3849 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003850 if( output_size != 0 )
3851 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003852 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003853 input_data->x, input_data->len,
3854 NULL, label->len,
3855 output, output_size,
3856 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003857 TEST_EQUAL( actual_status, expected_status );
3858 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003859 }
3860
Gilles Peskine656896e2018-06-29 19:12:28 +02003861exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003862 /*
3863 * Key attributes may have been returned by psa_get_key_attributes()
3864 * thus reset them as required.
3865 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003866 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003867
Ronald Cron5425a212020-08-04 14:58:35 +02003868 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02003869 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003870 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003871}
3872/* END_CASE */
3873
3874/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003875void asymmetric_encrypt_decrypt( int key_type_arg,
3876 data_t *key_data,
3877 int alg_arg,
3878 data_t *input_data,
3879 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003880{
Ronald Cron5425a212020-08-04 14:58:35 +02003881 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003882 psa_key_type_t key_type = key_type_arg;
3883 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003884 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003885 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003886 size_t output_size;
3887 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003888 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003889 size_t output2_size;
3890 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003891 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003892
Gilles Peskine8817f612018-12-18 00:18:46 +01003893 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003894
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003895 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3896 psa_set_key_algorithm( &attributes, alg );
3897 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003898
Gilles Peskine049c7532019-05-15 20:22:09 +02003899 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003900 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003901
3902 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02003903 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003904 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003905
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003906 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003907 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003908 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01003909
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003910 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01003911 TEST_ASSERT( output2_size <=
3912 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
3913 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003914 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003915
Gilles Peskineeebd7382018-06-08 18:11:54 +02003916 /* We test encryption by checking that encrypt-then-decrypt gives back
3917 * the original plaintext because of the non-optional random
3918 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02003919 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003920 input_data->x, input_data->len,
3921 label->x, label->len,
3922 output, output_size,
3923 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003924 /* We don't know what ciphertext length to expect, but check that
3925 * it looks sensible. */
3926 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003927
Ronald Cron5425a212020-08-04 14:58:35 +02003928 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003929 output, output_length,
3930 label->x, label->len,
3931 output2, output2_size,
3932 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003933 ASSERT_COMPARE( input_data->x, input_data->len,
3934 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003935
3936exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003937 /*
3938 * Key attributes may have been returned by psa_get_key_attributes()
3939 * thus reset them as required.
3940 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003941 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003942
Ronald Cron5425a212020-08-04 14:58:35 +02003943 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003944 mbedtls_free( output );
3945 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003946 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003947}
3948/* END_CASE */
3949
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003950/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003951void asymmetric_decrypt( int key_type_arg,
3952 data_t *key_data,
3953 int alg_arg,
3954 data_t *input_data,
3955 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003956 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003957{
Ronald Cron5425a212020-08-04 14:58:35 +02003958 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003959 psa_key_type_t key_type = key_type_arg;
3960 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01003961 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003962 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003963 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003964 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003965 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003966
Gilles Peskine8817f612018-12-18 00:18:46 +01003967 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003968
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003969 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3970 psa_set_key_algorithm( &attributes, alg );
3971 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003972
Gilles Peskine049c7532019-05-15 20:22:09 +02003973 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003974 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003975
gabor-mezei-armceface22021-01-21 12:26:17 +01003976 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3977 key_bits = psa_get_key_bits( &attributes );
3978
3979 /* Determine the maximum ciphertext length */
3980 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
3981 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
3982 ASSERT_ALLOC( output, output_size );
3983
Ronald Cron5425a212020-08-04 14:58:35 +02003984 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003985 input_data->x, input_data->len,
3986 label->x, label->len,
3987 output,
3988 output_size,
3989 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003990 ASSERT_COMPARE( expected_data->x, expected_data->len,
3991 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003992
Gilles Peskine68428122018-06-30 18:42:41 +02003993 /* If the label is empty, the test framework puts a non-null pointer
3994 * in label->x. Test that a null pointer works as well. */
3995 if( label->len == 0 )
3996 {
3997 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003998 if( output_size != 0 )
3999 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004000 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004001 input_data->x, input_data->len,
4002 NULL, label->len,
4003 output,
4004 output_size,
4005 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004006 ASSERT_COMPARE( expected_data->x, expected_data->len,
4007 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004008 }
4009
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004010exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004011 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004012 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004013 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004014 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004015}
4016/* END_CASE */
4017
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004018/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004019void asymmetric_decrypt_fail( int key_type_arg,
4020 data_t *key_data,
4021 int alg_arg,
4022 data_t *input_data,
4023 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004024 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004025 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004026{
Ronald Cron5425a212020-08-04 14:58:35 +02004027 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004028 psa_key_type_t key_type = key_type_arg;
4029 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004030 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004031 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004032 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004033 psa_status_t actual_status;
4034 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004035 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004036
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004037 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004038
Gilles Peskine8817f612018-12-18 00:18:46 +01004039 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004040
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004041 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4042 psa_set_key_algorithm( &attributes, alg );
4043 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004044
Gilles Peskine049c7532019-05-15 20:22:09 +02004045 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004046 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004047
Ronald Cron5425a212020-08-04 14:58:35 +02004048 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004049 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004050 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004051 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004052 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004053 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004054 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004055
Gilles Peskine68428122018-06-30 18:42:41 +02004056 /* If the label is empty, the test framework puts a non-null pointer
4057 * in label->x. Test that a null pointer works as well. */
4058 if( label->len == 0 )
4059 {
4060 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004061 if( output_size != 0 )
4062 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004063 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004064 input_data->x, input_data->len,
4065 NULL, label->len,
4066 output, output_size,
4067 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004068 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004069 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004070 }
4071
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004072exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004073 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004074 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004075 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004076 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004077}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004078/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004079
4080/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004081void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004082{
4083 /* Test each valid way of initializing the object, except for `= {0}`, as
4084 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4085 * though it's OK by the C standard. We could test for this, but we'd need
4086 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004087 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004088 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4089 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4090 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004091
4092 memset( &zero, 0, sizeof( zero ) );
4093
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004094 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004095 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004096 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004097 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004098 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004099 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004100 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004101
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004102 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004103 PSA_ASSERT( psa_key_derivation_abort(&func) );
4104 PSA_ASSERT( psa_key_derivation_abort(&init) );
4105 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004106}
4107/* END_CASE */
4108
Janos Follath16de4a42019-06-13 16:32:24 +01004109/* BEGIN_CASE */
4110void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004111{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004112 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004113 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004114 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004115
Gilles Peskine8817f612018-12-18 00:18:46 +01004116 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004117
Janos Follath16de4a42019-06-13 16:32:24 +01004118 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004119 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004120
4121exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004122 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004123 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004124}
4125/* END_CASE */
4126
Janos Follathaf3c2a02019-06-12 12:34:34 +01004127/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004128void derive_set_capacity( int alg_arg, int capacity_arg,
4129 int expected_status_arg )
4130{
4131 psa_algorithm_t alg = alg_arg;
4132 size_t capacity = capacity_arg;
4133 psa_status_t expected_status = expected_status_arg;
4134 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4135
4136 PSA_ASSERT( psa_crypto_init( ) );
4137
4138 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4139
4140 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4141 expected_status );
4142
4143exit:
4144 psa_key_derivation_abort( &operation );
4145 PSA_DONE( );
4146}
4147/* END_CASE */
4148
4149/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004150void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004151 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004152 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004153 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004154 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004155 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004156 int expected_status_arg3,
4157 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004158{
4159 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004160 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4161 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004162 psa_status_t expected_statuses[] = {expected_status_arg1,
4163 expected_status_arg2,
4164 expected_status_arg3};
4165 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004166 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4167 MBEDTLS_SVC_KEY_ID_INIT,
4168 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004169 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4170 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4171 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004172 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004173 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004174 psa_status_t expected_output_status = expected_output_status_arg;
4175 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004176
4177 PSA_ASSERT( psa_crypto_init( ) );
4178
4179 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4180 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004181
4182 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4183
4184 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4185 {
Gilles Peskine4023c012021-05-27 13:21:20 +02004186 mbedtls_test_set_step( i );
4187 if( steps[i] == 0 )
4188 {
4189 /* Skip this step */
4190 }
4191 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004192 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004193 psa_set_key_type( &attributes, key_types[i] );
4194 PSA_ASSERT( psa_import_key( &attributes,
4195 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004196 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004197 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4198 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4199 {
4200 // When taking a private key as secret input, use key agreement
4201 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004202 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4203 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004204 expected_statuses[i] );
4205 }
4206 else
4207 {
4208 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004209 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004210 expected_statuses[i] );
4211 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004212 }
4213 else
4214 {
4215 TEST_EQUAL( psa_key_derivation_input_bytes(
4216 &operation, steps[i],
4217 inputs[i]->x, inputs[i]->len ),
4218 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004219 }
4220 }
4221
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004222 if( output_key_type != PSA_KEY_TYPE_NONE )
4223 {
4224 psa_reset_key_attributes( &attributes );
4225 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4226 psa_set_key_bits( &attributes, 8 );
4227 actual_output_status =
4228 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004229 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004230 }
4231 else
4232 {
4233 uint8_t buffer[1];
4234 actual_output_status =
4235 psa_key_derivation_output_bytes( &operation,
4236 buffer, sizeof( buffer ) );
4237 }
4238 TEST_EQUAL( actual_output_status, expected_output_status );
4239
Janos Follathaf3c2a02019-06-12 12:34:34 +01004240exit:
4241 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004242 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4243 psa_destroy_key( keys[i] );
4244 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004245 PSA_DONE( );
4246}
4247/* END_CASE */
4248
Janos Follathd958bb72019-07-03 15:02:16 +01004249/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02004250void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004251{
Janos Follathd958bb72019-07-03 15:02:16 +01004252 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004253 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004254 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004255 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004256 unsigned char input1[] = "Input 1";
4257 size_t input1_length = sizeof( input1 );
4258 unsigned char input2[] = "Input 2";
4259 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004260 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004261 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004262 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4263 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4264 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004265 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004266
Gilles Peskine8817f612018-12-18 00:18:46 +01004267 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004268
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004269 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4270 psa_set_key_algorithm( &attributes, alg );
4271 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004272
Gilles Peskine73676cb2019-05-15 20:15:10 +02004273 PSA_ASSERT( psa_import_key( &attributes,
4274 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004275 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004276
4277 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004278 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4279 input1, input1_length,
4280 input2, input2_length,
4281 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004282 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004283
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004284 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004285 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004286 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004287
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004288 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004289
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004290 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004291 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004292
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004293exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004294 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004295 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004296 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004297}
4298/* END_CASE */
4299
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004300/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02004301void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004302{
4303 uint8_t output_buffer[16];
4304 size_t buffer_size = 16;
4305 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004306 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004307
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004308 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4309 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004310 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004311
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004312 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004313 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004314
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004315 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004316
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004317 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4318 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004319 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004320
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004321 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004322 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004323
4324exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004325 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004326}
4327/* END_CASE */
4328
4329/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004330void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004331 int step1_arg, data_t *input1,
4332 int step2_arg, data_t *input2,
4333 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004334 int requested_capacity_arg,
4335 data_t *expected_output1,
4336 data_t *expected_output2 )
4337{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004338 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004339 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4340 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004341 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4342 MBEDTLS_SVC_KEY_ID_INIT,
4343 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004344 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004345 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004346 uint8_t *expected_outputs[2] =
4347 {expected_output1->x, expected_output2->x};
4348 size_t output_sizes[2] =
4349 {expected_output1->len, expected_output2->len};
4350 size_t output_buffer_size = 0;
4351 uint8_t *output_buffer = NULL;
4352 size_t expected_capacity;
4353 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004354 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004355 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004356 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004357
4358 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4359 {
4360 if( output_sizes[i] > output_buffer_size )
4361 output_buffer_size = output_sizes[i];
4362 if( output_sizes[i] == 0 )
4363 expected_outputs[i] = NULL;
4364 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004365 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004366 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004367
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004368 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4369 psa_set_key_algorithm( &attributes, alg );
4370 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004371
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004372 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004373 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4374 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4375 requested_capacity ) );
4376 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004377 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004378 switch( steps[i] )
4379 {
4380 case 0:
4381 break;
4382 case PSA_KEY_DERIVATION_INPUT_SECRET:
4383 PSA_ASSERT( psa_import_key( &attributes,
4384 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004385 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004386
4387 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4388 {
4389 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4390 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4391 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4392 }
4393
Gilles Peskine1468da72019-05-29 17:35:49 +02004394 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004395 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004396 break;
4397 default:
4398 PSA_ASSERT( psa_key_derivation_input_bytes(
4399 &operation, steps[i],
4400 inputs[i]->x, inputs[i]->len ) );
4401 break;
4402 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004403 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004404
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004405 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004406 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004407 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004408 expected_capacity = requested_capacity;
4409
4410 /* Expansion phase. */
4411 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4412 {
4413 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004414 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004415 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004416 if( expected_capacity == 0 && output_sizes[i] == 0 )
4417 {
4418 /* Reading 0 bytes when 0 bytes are available can go either way. */
4419 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004420 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004421 continue;
4422 }
4423 else if( expected_capacity == 0 ||
4424 output_sizes[i] > expected_capacity )
4425 {
4426 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004427 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004428 expected_capacity = 0;
4429 continue;
4430 }
4431 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004432 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004433 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004434 ASSERT_COMPARE( output_buffer, output_sizes[i],
4435 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004436 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004437 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004438 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004439 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004440 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004441 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004442 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004443
4444exit:
4445 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004446 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004447 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4448 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004449 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004450}
4451/* END_CASE */
4452
4453/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004454void derive_full( int alg_arg,
4455 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004456 data_t *input1,
4457 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004458 int requested_capacity_arg )
4459{
Ronald Cron5425a212020-08-04 14:58:35 +02004460 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004461 psa_algorithm_t alg = alg_arg;
4462 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004463 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004464 unsigned char output_buffer[16];
4465 size_t expected_capacity = requested_capacity;
4466 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004467 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004468
Gilles Peskine8817f612018-12-18 00:18:46 +01004469 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004470
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004471 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4472 psa_set_key_algorithm( &attributes, alg );
4473 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004474
Gilles Peskine049c7532019-05-15 20:22:09 +02004475 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004476 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004477
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004478 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4479 input1->x, input1->len,
4480 input2->x, input2->len,
4481 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004482 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004483
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004484 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004485 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004486 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004487
4488 /* Expansion phase. */
4489 while( current_capacity > 0 )
4490 {
4491 size_t read_size = sizeof( output_buffer );
4492 if( read_size > current_capacity )
4493 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004494 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004495 output_buffer,
4496 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004497 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004498 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004499 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004500 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004501 }
4502
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004503 /* Check that the operation refuses to go over capacity. */
4504 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004505 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004506
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004507 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004508
4509exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004510 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004511 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004512 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004513}
4514/* END_CASE */
4515
Janos Follathe60c9052019-07-03 13:51:30 +01004516/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004517void derive_key_exercise( int alg_arg,
4518 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004519 data_t *input1,
4520 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004521 int derived_type_arg,
4522 int derived_bits_arg,
4523 int derived_usage_arg,
4524 int derived_alg_arg )
4525{
Ronald Cron5425a212020-08-04 14:58:35 +02004526 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4527 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004528 psa_algorithm_t alg = alg_arg;
4529 psa_key_type_t derived_type = derived_type_arg;
4530 size_t derived_bits = derived_bits_arg;
4531 psa_key_usage_t derived_usage = derived_usage_arg;
4532 psa_algorithm_t derived_alg = derived_alg_arg;
4533 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004534 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004535 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004536 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004537
Gilles Peskine8817f612018-12-18 00:18:46 +01004538 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004539
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004540 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4541 psa_set_key_algorithm( &attributes, alg );
4542 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004543 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004544 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004545
4546 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004547 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4548 input1->x, input1->len,
4549 input2->x, input2->len,
4550 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004551 goto exit;
4552
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004553 psa_set_key_usage_flags( &attributes, derived_usage );
4554 psa_set_key_algorithm( &attributes, derived_alg );
4555 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004556 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004557 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004558 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004559
4560 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004561 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004562 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4563 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004564
4565 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004566 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004567 goto exit;
4568
4569exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004570 /*
4571 * Key attributes may have been returned by psa_get_key_attributes()
4572 * thus reset them as required.
4573 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004574 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004575
4576 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004577 psa_destroy_key( base_key );
4578 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004579 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004580}
4581/* END_CASE */
4582
Janos Follath42fd8882019-07-03 14:17:09 +01004583/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004584void derive_key_export( int alg_arg,
4585 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004586 data_t *input1,
4587 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004588 int bytes1_arg,
4589 int bytes2_arg )
4590{
Ronald Cron5425a212020-08-04 14:58:35 +02004591 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4592 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004593 psa_algorithm_t alg = alg_arg;
4594 size_t bytes1 = bytes1_arg;
4595 size_t bytes2 = bytes2_arg;
4596 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004597 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004598 uint8_t *output_buffer = NULL;
4599 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004600 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4601 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004602 size_t length;
4603
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004604 ASSERT_ALLOC( output_buffer, capacity );
4605 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004606 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004607
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004608 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4609 psa_set_key_algorithm( &base_attributes, alg );
4610 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004611 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004612 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004613
4614 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004615 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4616 input1->x, input1->len,
4617 input2->x, input2->len,
4618 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004619 goto exit;
4620
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004621 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004622 output_buffer,
4623 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004624 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004625
4626 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004627 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4628 input1->x, input1->len,
4629 input2->x, input2->len,
4630 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004631 goto exit;
4632
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004633 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4634 psa_set_key_algorithm( &derived_attributes, 0 );
4635 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004636 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004637 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004638 &derived_key ) );
4639 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004640 export_buffer, bytes1,
4641 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004642 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004643 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004644 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004645 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004646 &derived_key ) );
4647 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004648 export_buffer + bytes1, bytes2,
4649 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004650 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004651
4652 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004653 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4654 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004655
4656exit:
4657 mbedtls_free( output_buffer );
4658 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004659 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004660 psa_destroy_key( base_key );
4661 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004662 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004663}
4664/* END_CASE */
4665
4666/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004667void derive_key( int alg_arg,
4668 data_t *key_data, data_t *input1, data_t *input2,
4669 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004670 int expected_status_arg,
4671 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004672{
Ronald Cron5425a212020-08-04 14:58:35 +02004673 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4674 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004675 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004676 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004677 size_t bits = bits_arg;
4678 psa_status_t expected_status = expected_status_arg;
4679 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4680 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4681 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4682
4683 PSA_ASSERT( psa_crypto_init( ) );
4684
4685 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4686 psa_set_key_algorithm( &base_attributes, alg );
4687 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4688 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004689 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004690
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004691 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4692 input1->x, input1->len,
4693 input2->x, input2->len,
4694 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004695 goto exit;
4696
4697 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4698 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004699 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004700 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004701
4702 psa_status_t status =
4703 psa_key_derivation_output_key( &derived_attributes,
4704 &operation,
4705 &derived_key );
4706 if( is_large_output > 0 )
4707 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4708 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004709
4710exit:
4711 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004712 psa_destroy_key( base_key );
4713 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004714 PSA_DONE( );
4715}
4716/* END_CASE */
4717
4718/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004719void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004720 int our_key_type_arg, int our_key_alg_arg,
4721 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004722 int expected_status_arg )
4723{
Ronald Cron5425a212020-08-04 14:58:35 +02004724 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004725 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004726 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004727 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 Peskine77f40d82019-04-11 21:27:06 +02004730 psa_status_t expected_status = expected_status_arg;
4731 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004732
Gilles Peskine8817f612018-12-18 00:18:46 +01004733 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004734
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004735 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004736 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004737 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004738 PSA_ASSERT( psa_import_key( &attributes,
4739 our_key_data->x, our_key_data->len,
4740 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004741
Gilles Peskine77f40d82019-04-11 21:27:06 +02004742 /* The tests currently include inputs that should fail at either step.
4743 * Test cases that fail at the setup step should be changed to call
4744 * key_derivation_setup instead, and this function should be renamed
4745 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004746 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004747 if( status == PSA_SUCCESS )
4748 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004749 TEST_EQUAL( psa_key_derivation_key_agreement(
4750 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4751 our_key,
4752 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004753 expected_status );
4754 }
4755 else
4756 {
4757 TEST_ASSERT( status == expected_status );
4758 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004759
4760exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004761 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004762 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004763 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004764}
4765/* END_CASE */
4766
4767/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004768void raw_key_agreement( int alg_arg,
4769 int our_key_type_arg, data_t *our_key_data,
4770 data_t *peer_key_data,
4771 data_t *expected_output )
4772{
Ronald Cron5425a212020-08-04 14:58:35 +02004773 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004774 psa_algorithm_t alg = alg_arg;
4775 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004776 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004777 unsigned char *output = NULL;
4778 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004779 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004780
4781 ASSERT_ALLOC( output, expected_output->len );
4782 PSA_ASSERT( psa_crypto_init( ) );
4783
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004784 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4785 psa_set_key_algorithm( &attributes, alg );
4786 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004787 PSA_ASSERT( psa_import_key( &attributes,
4788 our_key_data->x, our_key_data->len,
4789 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004790
gabor-mezei-armceface22021-01-21 12:26:17 +01004791 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4792 key_bits = psa_get_key_bits( &attributes );
4793
Gilles Peskinebe697d82019-05-16 18:00:41 +02004794 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4795 peer_key_data->x, peer_key_data->len,
4796 output, expected_output->len,
4797 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004798 ASSERT_COMPARE( output, output_length,
4799 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01004800 TEST_ASSERT( output_length <=
4801 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
4802 TEST_ASSERT( output_length <=
4803 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004804
4805exit:
4806 mbedtls_free( output );
4807 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004808 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004809}
4810/* END_CASE */
4811
4812/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004813void key_agreement_capacity( int alg_arg,
4814 int our_key_type_arg, data_t *our_key_data,
4815 data_t *peer_key_data,
4816 int expected_capacity_arg )
4817{
Ronald Cron5425a212020-08-04 14:58:35 +02004818 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004819 psa_algorithm_t alg = alg_arg;
4820 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004821 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004822 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004823 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004824 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004825
Gilles Peskine8817f612018-12-18 00:18:46 +01004826 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004827
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004828 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4829 psa_set_key_algorithm( &attributes, alg );
4830 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004831 PSA_ASSERT( psa_import_key( &attributes,
4832 our_key_data->x, our_key_data->len,
4833 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004834
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004835 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004836 PSA_ASSERT( psa_key_derivation_key_agreement(
4837 &operation,
4838 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4839 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004840 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4841 {
4842 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004843 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004844 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004845 NULL, 0 ) );
4846 }
Gilles Peskine59685592018-09-18 12:11:34 +02004847
Gilles Peskinebf491972018-10-25 22:36:12 +02004848 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004849 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004850 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004851 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004852
Gilles Peskinebf491972018-10-25 22:36:12 +02004853 /* Test the actual capacity by reading the output. */
4854 while( actual_capacity > sizeof( output ) )
4855 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004856 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004857 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004858 actual_capacity -= sizeof( output );
4859 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004860 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004861 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004862 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004863 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004864
Gilles Peskine59685592018-09-18 12:11:34 +02004865exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004866 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004867 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004868 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004869}
4870/* END_CASE */
4871
4872/* BEGIN_CASE */
4873void key_agreement_output( int alg_arg,
4874 int our_key_type_arg, data_t *our_key_data,
4875 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004876 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004877{
Ronald Cron5425a212020-08-04 14:58:35 +02004878 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004879 psa_algorithm_t alg = alg_arg;
4880 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004881 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004882 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004883 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004884
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004885 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4886 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004887
Gilles Peskine8817f612018-12-18 00:18:46 +01004888 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004889
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004890 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4891 psa_set_key_algorithm( &attributes, alg );
4892 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004893 PSA_ASSERT( psa_import_key( &attributes,
4894 our_key_data->x, our_key_data->len,
4895 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004896
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004897 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004898 PSA_ASSERT( psa_key_derivation_key_agreement(
4899 &operation,
4900 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4901 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004902 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4903 {
4904 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004905 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004906 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004907 NULL, 0 ) );
4908 }
Gilles Peskine59685592018-09-18 12:11:34 +02004909
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004910 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004911 actual_output,
4912 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004913 ASSERT_COMPARE( actual_output, expected_output1->len,
4914 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004915 if( expected_output2->len != 0 )
4916 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004917 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004918 actual_output,
4919 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004920 ASSERT_COMPARE( actual_output, expected_output2->len,
4921 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004922 }
Gilles Peskine59685592018-09-18 12:11:34 +02004923
4924exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004925 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004926 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004927 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004928 mbedtls_free( actual_output );
4929}
4930/* END_CASE */
4931
4932/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004933void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004934{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004935 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004936 unsigned char *output = NULL;
4937 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004938 size_t i;
4939 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004940
Simon Butcher49f8e312020-03-03 15:51:50 +00004941 TEST_ASSERT( bytes_arg >= 0 );
4942
Gilles Peskine91892022021-02-08 19:50:26 +01004943 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004944 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02004945
Gilles Peskine8817f612018-12-18 00:18:46 +01004946 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004947
Gilles Peskinea50d7392018-06-21 10:22:13 +02004948 /* Run several times, to ensure that every output byte will be
4949 * nonzero at least once with overwhelming probability
4950 * (2^(-8*number_of_runs)). */
4951 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004952 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004953 if( bytes != 0 )
4954 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004955 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004956
Gilles Peskinea50d7392018-06-21 10:22:13 +02004957 for( i = 0; i < bytes; i++ )
4958 {
4959 if( output[i] != 0 )
4960 ++changed[i];
4961 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004962 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004963
4964 /* Check that every byte was changed to nonzero at least once. This
4965 * validates that psa_generate_random is overwriting every byte of
4966 * the output buffer. */
4967 for( i = 0; i < bytes; i++ )
4968 {
4969 TEST_ASSERT( changed[i] != 0 );
4970 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004971
4972exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004973 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004974 mbedtls_free( output );
4975 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004976}
4977/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004978
4979/* BEGIN_CASE */
4980void generate_key( int type_arg,
4981 int bits_arg,
4982 int usage_arg,
4983 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004984 int expected_status_arg,
4985 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004986{
Ronald Cron5425a212020-08-04 14:58:35 +02004987 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004988 psa_key_type_t type = type_arg;
4989 psa_key_usage_t usage = usage_arg;
4990 size_t bits = bits_arg;
4991 psa_algorithm_t alg = alg_arg;
4992 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004993 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004994 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004995
Gilles Peskine8817f612018-12-18 00:18:46 +01004996 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004997
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004998 psa_set_key_usage_flags( &attributes, usage );
4999 psa_set_key_algorithm( &attributes, alg );
5000 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005001 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005002
5003 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005004 psa_status_t status = psa_generate_key( &attributes, &key );
5005
5006 if( is_large_key > 0 )
5007 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5008 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005009 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005010 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005011
5012 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005013 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005014 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5015 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005016
Gilles Peskine818ca122018-06-20 18:16:48 +02005017 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005018 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005019 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005020
5021exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005022 /*
5023 * Key attributes may have been returned by psa_get_key_attributes()
5024 * thus reset them as required.
5025 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005026 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005027
Ronald Cron5425a212020-08-04 14:58:35 +02005028 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005029 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005030}
5031/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005032
Ronald Cronee414c72021-03-18 18:50:08 +01005033/* 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 +02005034void generate_key_rsa( int bits_arg,
5035 data_t *e_arg,
5036 int expected_status_arg )
5037{
Ronald Cron5425a212020-08-04 14:58:35 +02005038 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005039 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005040 size_t bits = bits_arg;
5041 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5042 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5043 psa_status_t expected_status = expected_status_arg;
5044 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5045 uint8_t *exported = NULL;
5046 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005047 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005048 size_t exported_length = SIZE_MAX;
5049 uint8_t *e_read_buffer = NULL;
5050 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005051 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005052 size_t e_read_length = SIZE_MAX;
5053
5054 if( e_arg->len == 0 ||
5055 ( e_arg->len == 3 &&
5056 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5057 {
5058 is_default_public_exponent = 1;
5059 e_read_size = 0;
5060 }
5061 ASSERT_ALLOC( e_read_buffer, e_read_size );
5062 ASSERT_ALLOC( exported, exported_size );
5063
5064 PSA_ASSERT( psa_crypto_init( ) );
5065
5066 psa_set_key_usage_flags( &attributes, usage );
5067 psa_set_key_algorithm( &attributes, alg );
5068 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5069 e_arg->x, e_arg->len ) );
5070 psa_set_key_bits( &attributes, bits );
5071
5072 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005073 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005074 if( expected_status != PSA_SUCCESS )
5075 goto exit;
5076
5077 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005078 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005079 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5080 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5081 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5082 e_read_buffer, e_read_size,
5083 &e_read_length ) );
5084 if( is_default_public_exponent )
5085 TEST_EQUAL( e_read_length, 0 );
5086 else
5087 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5088
5089 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005090 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005091 goto exit;
5092
5093 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005094 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005095 exported, exported_size,
5096 &exported_length ) );
5097 {
5098 uint8_t *p = exported;
5099 uint8_t *end = exported + exported_length;
5100 size_t len;
5101 /* RSAPublicKey ::= SEQUENCE {
5102 * modulus INTEGER, -- n
5103 * publicExponent INTEGER } -- e
5104 */
5105 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005106 MBEDTLS_ASN1_SEQUENCE |
5107 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005108 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005109 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5110 MBEDTLS_ASN1_INTEGER ) );
5111 if( len >= 1 && p[0] == 0 )
5112 {
5113 ++p;
5114 --len;
5115 }
5116 if( e_arg->len == 0 )
5117 {
5118 TEST_EQUAL( len, 3 );
5119 TEST_EQUAL( p[0], 1 );
5120 TEST_EQUAL( p[1], 0 );
5121 TEST_EQUAL( p[2], 1 );
5122 }
5123 else
5124 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5125 }
5126
5127exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005128 /*
5129 * Key attributes may have been returned by psa_get_key_attributes() or
5130 * set by psa_set_key_domain_parameters() thus reset them as required.
5131 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005132 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005133
Ronald Cron5425a212020-08-04 14:58:35 +02005134 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005135 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005136 mbedtls_free( e_read_buffer );
5137 mbedtls_free( exported );
5138}
5139/* END_CASE */
5140
Darryl Greend49a4992018-06-18 17:27:26 +01005141/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005142void persistent_key_load_key_from_storage( data_t *data,
5143 int type_arg, int bits_arg,
5144 int usage_flags_arg, int alg_arg,
5145 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005146{
Ronald Cron71016a92020-08-28 19:01:50 +02005147 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005148 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005149 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5150 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005151 psa_key_type_t type = type_arg;
5152 size_t bits = bits_arg;
5153 psa_key_usage_t usage_flags = usage_flags_arg;
5154 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005155 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005156 unsigned char *first_export = NULL;
5157 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005158 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005159 size_t first_exported_length;
5160 size_t second_exported_length;
5161
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005162 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5163 {
5164 ASSERT_ALLOC( first_export, export_size );
5165 ASSERT_ALLOC( second_export, export_size );
5166 }
Darryl Greend49a4992018-06-18 17:27:26 +01005167
Gilles Peskine8817f612018-12-18 00:18:46 +01005168 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005169
Gilles Peskinec87af662019-05-15 16:12:22 +02005170 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005171 psa_set_key_usage_flags( &attributes, usage_flags );
5172 psa_set_key_algorithm( &attributes, alg );
5173 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005174 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005175
Darryl Green0c6575a2018-11-07 16:05:30 +00005176 switch( generation_method )
5177 {
5178 case IMPORT_KEY:
5179 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005180 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005181 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005182 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005183
Darryl Green0c6575a2018-11-07 16:05:30 +00005184 case GENERATE_KEY:
5185 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005186 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005187 break;
5188
5189 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005190#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005191 {
5192 /* Create base key */
5193 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5194 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5195 psa_set_key_usage_flags( &base_attributes,
5196 PSA_KEY_USAGE_DERIVE );
5197 psa_set_key_algorithm( &base_attributes, derive_alg );
5198 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005199 PSA_ASSERT( psa_import_key( &base_attributes,
5200 data->x, data->len,
5201 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005202 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005203 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005204 PSA_ASSERT( psa_key_derivation_input_key(
5205 &operation,
5206 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005207 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005208 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005209 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005210 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5211 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005212 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005213 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005214 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005215 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005216 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005217#else
5218 TEST_ASSUME( ! "KDF not supported in this configuration" );
5219#endif
5220 break;
5221
5222 default:
5223 TEST_ASSERT( ! "generation_method not implemented in test" );
5224 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005225 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005226 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005227
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005228 /* Export the key if permitted by the key policy. */
5229 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5230 {
Ronald Cron5425a212020-08-04 14:58:35 +02005231 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005232 first_export, export_size,
5233 &first_exported_length ) );
5234 if( generation_method == IMPORT_KEY )
5235 ASSERT_COMPARE( data->x, data->len,
5236 first_export, first_exported_length );
5237 }
Darryl Greend49a4992018-06-18 17:27:26 +01005238
5239 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005240 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005241 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005242 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005243
Darryl Greend49a4992018-06-18 17:27:26 +01005244 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005245 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005246 TEST_ASSERT( mbedtls_svc_key_id_equal(
5247 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005248 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5249 PSA_KEY_LIFETIME_PERSISTENT );
5250 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5251 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02005252 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
5253 update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005254 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005255
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005256 /* Export the key again if permitted by the key policy. */
5257 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005258 {
Ronald Cron5425a212020-08-04 14:58:35 +02005259 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005260 second_export, export_size,
5261 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005262 ASSERT_COMPARE( first_export, first_exported_length,
5263 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005264 }
5265
5266 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005267 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005268 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005269
5270exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005271 /*
5272 * Key attributes may have been returned by psa_get_key_attributes()
5273 * thus reset them as required.
5274 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005275 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005276
Darryl Greend49a4992018-06-18 17:27:26 +01005277 mbedtls_free( first_export );
5278 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005279 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005280 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005281 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005282 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005283}
5284/* END_CASE */