blob: a54521142940efaa126cc126f4e6be58d1d6033b [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 ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200326 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200327 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200328 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200329
Ronald Cron5425a212020-08-04 14:58:35 +0200330 PSA_ASSERT( psa_destroy_key( key ) );
331 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200332
333exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100334 /*
335 * Key attributes may have been returned by psa_get_key_attributes()
336 * thus reset them as required.
337 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200338 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100339
340 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200341 PSA_DONE( );
342}
343/* END_CASE */
344
345/* BEGIN_CASE */
346void import_with_data( data_t *data, int type_arg,
347 int attr_bits_arg,
348 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200349{
350 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
351 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200352 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200353 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200354 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200355 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100356 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100357
Gilles Peskine8817f612018-12-18 00:18:46 +0100358 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100359
Gilles Peskine4747d192019-04-17 15:05:45 +0200360 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200361 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200362
Ronald Cron5425a212020-08-04 14:58:35 +0200363 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100364 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200365 if( status != PSA_SUCCESS )
366 goto exit;
367
Ronald Cron5425a212020-08-04 14:58:35 +0200368 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200369 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200370 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200371 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200372 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200373
Ronald Cron5425a212020-08-04 14:58:35 +0200374 PSA_ASSERT( psa_destroy_key( key ) );
375 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100376
377exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100378 /*
379 * Key attributes may have been returned by psa_get_key_attributes()
380 * thus reset them as required.
381 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200382 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100383
384 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200385 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100386}
387/* END_CASE */
388
389/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200390void import_large_key( int type_arg, int byte_size_arg,
391 int expected_status_arg )
392{
393 psa_key_type_t type = type_arg;
394 size_t byte_size = byte_size_arg;
395 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
396 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200397 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200398 psa_status_t status;
399 uint8_t *buffer = NULL;
400 size_t buffer_size = byte_size + 1;
401 size_t n;
402
Steven Cooreman69967ce2021-01-18 18:01:08 +0100403 /* Skip the test case if the target running the test cannot
404 * accomodate large keys due to heap size constraints */
405 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200406 memset( buffer, 'K', byte_size );
407
408 PSA_ASSERT( psa_crypto_init( ) );
409
410 /* Try importing the key */
411 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
412 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200413 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100414 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200415 TEST_EQUAL( status, expected_status );
416
417 if( status == PSA_SUCCESS )
418 {
Ronald Cron5425a212020-08-04 14:58:35 +0200419 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200420 TEST_EQUAL( psa_get_key_type( &attributes ), type );
421 TEST_EQUAL( psa_get_key_bits( &attributes ),
422 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200423 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200424 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200425 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200426 for( n = 0; n < byte_size; n++ )
427 TEST_EQUAL( buffer[n], 'K' );
428 for( n = byte_size; n < buffer_size; n++ )
429 TEST_EQUAL( buffer[n], 0 );
430 }
431
432exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100433 /*
434 * Key attributes may have been returned by psa_get_key_attributes()
435 * thus reset them as required.
436 */
437 psa_reset_key_attributes( &attributes );
438
Ronald Cron5425a212020-08-04 14:58:35 +0200439 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200440 PSA_DONE( );
441 mbedtls_free( buffer );
442}
443/* END_CASE */
444
445/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200446void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
447{
Ronald Cron5425a212020-08-04 14:58:35 +0200448 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200449 size_t bits = bits_arg;
450 psa_status_t expected_status = expected_status_arg;
451 psa_status_t status;
452 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200453 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200454 size_t buffer_size = /* Slight overapproximations */
455 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200456 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200457 unsigned char *p;
458 int ret;
459 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200460 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200461
Gilles Peskine8817f612018-12-18 00:18:46 +0100462 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200463 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200464
465 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
466 bits, keypair ) ) >= 0 );
467 length = ret;
468
469 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200470 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200471 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100472 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200473
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200474 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200475 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200476
477exit:
478 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200479 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200480}
481/* END_CASE */
482
483/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300484void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300485 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200486 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100487 int expected_bits,
488 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200489 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100490 int canonical_input )
491{
Ronald Cron5425a212020-08-04 14:58:35 +0200492 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100493 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200494 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200495 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100496 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100497 unsigned char *exported = NULL;
498 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100499 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100500 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100501 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200502 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200503 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100504
Moran Pekercb088e72018-07-17 17:36:59 +0300505 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200506 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100507 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200508 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100509 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100510
Gilles Peskine4747d192019-04-17 15:05:45 +0200511 psa_set_key_usage_flags( &attributes, usage_arg );
512 psa_set_key_algorithm( &attributes, alg );
513 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700514
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100515 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200516 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100517
518 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200519 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200520 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
521 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200522 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100523
524 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200525 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100526 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100527
528 /* The exported length must be set by psa_export_key() to a value between 0
529 * and export_size. On errors, the exported length must be 0. */
530 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
531 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
532 TEST_ASSERT( exported_length <= export_size );
533
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200534 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200535 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100536 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200537 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100538 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100539 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200540 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100541
Gilles Peskineea38a922021-02-13 00:05:16 +0100542 /* Run sanity checks on the exported key. For non-canonical inputs,
543 * this validates the canonical representations. For canonical inputs,
544 * this doesn't directly validate the implementation, but it still helps
545 * by cross-validating the test data with the sanity check code. */
546 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200547 goto exit;
548
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100549 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200550 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100551 else
552 {
Ronald Cron5425a212020-08-04 14:58:35 +0200553 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200554 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200555 &key2 ) );
556 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100557 reexported,
558 export_size,
559 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200560 ASSERT_COMPARE( exported, exported_length,
561 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200562 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100563 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100564 TEST_ASSERT( exported_length <=
565 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
566 psa_get_key_bits( &got_attributes ) ) );
567 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100568
569destroy:
570 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200571 PSA_ASSERT( psa_destroy_key( key ) );
572 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100573
574exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100575 /*
576 * Key attributes may have been returned by psa_get_key_attributes()
577 * thus reset them as required.
578 */
579 psa_reset_key_attributes( &got_attributes );
580
itayzafrir3e02b3b2018-06-12 17:06:52 +0300581 mbedtls_free( exported );
582 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200583 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100584}
585/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100586
Moran Pekerf709f4a2018-06-06 17:26:04 +0300587/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300588void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200589 int type_arg,
590 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100591 int export_size_delta,
592 int expected_export_status_arg,
593 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300594{
Ronald Cron5425a212020-08-04 14:58:35 +0200595 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300596 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200597 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200598 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300599 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300600 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100601 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100602 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200603 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300604
Gilles Peskine8817f612018-12-18 00:18:46 +0100605 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300606
Gilles Peskine4747d192019-04-17 15:05:45 +0200607 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
608 psa_set_key_algorithm( &attributes, alg );
609 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300610
611 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200612 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300613
Gilles Peskine49c25912018-10-29 15:15:31 +0100614 /* Export the public key */
615 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200616 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200617 exported, export_size,
618 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100619 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100620 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100621 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200622 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100623 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200624 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200625 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100626 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100627 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100628 TEST_ASSERT( expected_public_key->len <=
629 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
630 TEST_ASSERT( expected_public_key->len <=
631 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100632 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
633 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100634 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300635
636exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100637 /*
638 * Key attributes may have been returned by psa_get_key_attributes()
639 * thus reset them as required.
640 */
641 psa_reset_key_attributes( &attributes );
642
itayzafrir3e02b3b2018-06-12 17:06:52 +0300643 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200644 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200645 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300646}
647/* END_CASE */
648
Gilles Peskine20035e32018-02-03 22:44:14 +0100649/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200650void import_and_exercise_key( data_t *data,
651 int type_arg,
652 int bits_arg,
653 int alg_arg )
654{
Ronald Cron5425a212020-08-04 14:58:35 +0200655 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200656 psa_key_type_t type = type_arg;
657 size_t bits = bits_arg;
658 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100659 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200660 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200661 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200662
Gilles Peskine8817f612018-12-18 00:18:46 +0100663 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200664
Gilles Peskine4747d192019-04-17 15:05:45 +0200665 psa_set_key_usage_flags( &attributes, usage );
666 psa_set_key_algorithm( &attributes, alg );
667 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200668
669 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200670 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200671
672 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200673 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200674 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
675 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200676
677 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100678 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200679 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200680
Ronald Cron5425a212020-08-04 14:58:35 +0200681 PSA_ASSERT( psa_destroy_key( key ) );
682 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200683
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200684exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100685 /*
686 * Key attributes may have been returned by psa_get_key_attributes()
687 * thus reset them as required.
688 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200689 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100690
691 psa_reset_key_attributes( &attributes );
692 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200693 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200694}
695/* END_CASE */
696
697/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100698void effective_key_attributes( int type_arg, int expected_type_arg,
699 int bits_arg, int expected_bits_arg,
700 int usage_arg, int expected_usage_arg,
701 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200702{
Ronald Cron5425a212020-08-04 14:58:35 +0200703 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100704 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100705 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100706 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100707 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200708 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100709 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200710 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100711 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200712 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200713
Gilles Peskine8817f612018-12-18 00:18:46 +0100714 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200715
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200716 psa_set_key_usage_flags( &attributes, usage );
717 psa_set_key_algorithm( &attributes, alg );
718 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +0100719 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +0200720
Ronald Cron5425a212020-08-04 14:58:35 +0200721 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +0100722 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +0200723
Ronald Cron5425a212020-08-04 14:58:35 +0200724 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +0100725 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
726 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
727 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
728 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200729
730exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100731 /*
732 * Key attributes may have been returned by psa_get_key_attributes()
733 * thus reset them as required.
734 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200735 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100736
737 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200738 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +0200739}
740/* END_CASE */
741
742/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100743void check_key_policy( int type_arg, int bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +0200744 int usage_arg, int alg_arg )
Gilles Peskine06c28892019-11-26 18:07:46 +0100745{
746 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +0200747 usage_arg,
748 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200749 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +0100750 goto exit;
751}
752/* END_CASE */
753
754/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200755void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +0000756{
757 /* Test each valid way of initializing the object, except for `= {0}`, as
758 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
759 * though it's OK by the C standard. We could test for this, but we'd need
760 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200761 psa_key_attributes_t func = psa_key_attributes_init( );
762 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
763 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000764
765 memset( &zero, 0, sizeof( zero ) );
766
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200767 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
768 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
769 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000770
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200771 TEST_EQUAL( psa_get_key_type( &func ), 0 );
772 TEST_EQUAL( psa_get_key_type( &init ), 0 );
773 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
774
775 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
776 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
777 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
778
779 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
780 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
781 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
782
783 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
784 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
785 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +0000786}
787/* END_CASE */
788
789/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200790void mac_key_policy( int policy_usage_arg,
791 int policy_alg_arg,
792 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200793 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200794 int exercise_alg_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100795 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200796{
Ronald Cron5425a212020-08-04 14:58:35 +0200797 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200798 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000799 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200800 psa_key_type_t key_type = key_type_arg;
801 psa_algorithm_t policy_alg = policy_alg_arg;
802 psa_algorithm_t exercise_alg = exercise_alg_arg;
803 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200804 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100805 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200806 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200807
Gilles Peskine8817f612018-12-18 00:18:46 +0100808 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200809
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200810 psa_set_key_usage_flags( &attributes, policy_usage );
811 psa_set_key_algorithm( &attributes, policy_alg );
812 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200813
Gilles Peskine049c7532019-05-15 20:22:09 +0200814 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200815 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200816
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +0200817 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
818 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200819
Ronald Cron5425a212020-08-04 14:58:35 +0200820 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100821 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100822 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100823 else
824 TEST_EQUAL( status, expected_status );
825
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200826 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200827
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200828 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200829 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100830 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100831 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100832 else
833 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200834
835exit:
836 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200837 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200838 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200839}
840/* END_CASE */
841
842/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200843void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200844 int policy_alg,
845 int key_type,
846 data_t *key_data,
847 int exercise_alg )
848{
Ronald Cron5425a212020-08-04 14:58:35 +0200849 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200850 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000851 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200852 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200853 psa_status_t status;
854
Gilles Peskine8817f612018-12-18 00:18:46 +0100855 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200856
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200857 psa_set_key_usage_flags( &attributes, policy_usage );
858 psa_set_key_algorithm( &attributes, policy_alg );
859 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200860
Gilles Peskine049c7532019-05-15 20:22:09 +0200861 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200862 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200863
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200864 /* Check if no key usage flag implication is done */
865 TEST_EQUAL( policy_usage,
866 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200867
Ronald Cron5425a212020-08-04 14:58:35 +0200868 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200869 if( policy_alg == exercise_alg &&
870 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100871 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200872 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100873 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200874 psa_cipher_abort( &operation );
875
Ronald Cron5425a212020-08-04 14:58:35 +0200876 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200877 if( policy_alg == exercise_alg &&
878 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100879 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200880 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100881 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200882
883exit:
884 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200885 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200886 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200887}
888/* END_CASE */
889
890/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200891void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200892 int policy_alg,
893 int key_type,
894 data_t *key_data,
895 int nonce_length_arg,
896 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100897 int exercise_alg,
898 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200899{
Ronald Cron5425a212020-08-04 14:58:35 +0200900 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200901 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200902 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200903 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100904 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200905 unsigned char nonce[16] = {0};
906 size_t nonce_length = nonce_length_arg;
907 unsigned char tag[16];
908 size_t tag_length = tag_length_arg;
909 size_t output_length;
910
911 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
912 TEST_ASSERT( tag_length <= sizeof( tag ) );
913
Gilles Peskine8817f612018-12-18 00:18:46 +0100914 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200915
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200916 psa_set_key_usage_flags( &attributes, policy_usage );
917 psa_set_key_algorithm( &attributes, policy_alg );
918 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200919
Gilles Peskine049c7532019-05-15 20:22:09 +0200920 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200921 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200922
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200923 /* Check if no key usage implication is done */
924 TEST_EQUAL( policy_usage,
925 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200926
Ronald Cron5425a212020-08-04 14:58:35 +0200927 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200928 nonce, nonce_length,
929 NULL, 0,
930 NULL, 0,
931 tag, tag_length,
932 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100933 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
934 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200935 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100936 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200937
938 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200939 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200940 nonce, nonce_length,
941 NULL, 0,
942 tag, tag_length,
943 NULL, 0,
944 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100945 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
946 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
947 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100948 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200949 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100950 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200951
952exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200953 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200954 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200955}
956/* END_CASE */
957
958/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200959void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200960 int policy_alg,
961 int key_type,
962 data_t *key_data,
963 int exercise_alg )
964{
Ronald Cron5425a212020-08-04 14:58:35 +0200965 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200966 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200967 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200968 psa_status_t status;
969 size_t key_bits;
970 size_t buffer_length;
971 unsigned char *buffer = NULL;
972 size_t output_length;
973
Gilles Peskine8817f612018-12-18 00:18:46 +0100974 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200975
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200976 psa_set_key_usage_flags( &attributes, policy_usage );
977 psa_set_key_algorithm( &attributes, policy_alg );
978 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200979
Gilles Peskine049c7532019-05-15 20:22:09 +0200980 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200981 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200982
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200983 /* Check if no key usage implication is done */
984 TEST_EQUAL( policy_usage,
985 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200986
Ronald Cron5425a212020-08-04 14:58:35 +0200987 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200988 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200989 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
990 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200991 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200992
Ronald Cron5425a212020-08-04 14:58:35 +0200993 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200994 NULL, 0,
995 NULL, 0,
996 buffer, buffer_length,
997 &output_length );
998 if( policy_alg == exercise_alg &&
999 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001000 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001001 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001002 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001003
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001004 if( buffer_length != 0 )
1005 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001006 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001007 buffer, buffer_length,
1008 NULL, 0,
1009 buffer, buffer_length,
1010 &output_length );
1011 if( policy_alg == exercise_alg &&
1012 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001013 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001014 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001015 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001016
1017exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001018 /*
1019 * Key attributes may have been returned by psa_get_key_attributes()
1020 * thus reset them as required.
1021 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001022 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001023
1024 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001025 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001026 mbedtls_free( buffer );
1027}
1028/* END_CASE */
1029
1030/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001031void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001032 int policy_alg,
1033 int key_type,
1034 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001035 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001036 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001037 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001038{
Ronald Cron5425a212020-08-04 14:58:35 +02001039 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001040 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001041 psa_key_usage_t policy_usage = policy_usage_arg;
1042 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001043 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001044 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1045 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1046 * compatible with the policy and `payload_length_arg` is supposed to be
1047 * a valid input length to sign. If `payload_length_arg <= 0`,
1048 * `exercise_alg` is supposed to be forbidden by the policy. */
1049 int compatible_alg = payload_length_arg > 0;
1050 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001051 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001052 size_t signature_length;
1053
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001054 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001055 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001056 TEST_EQUAL( expected_usage,
1057 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001058
Gilles Peskine8817f612018-12-18 00:18:46 +01001059 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001060
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001061 psa_set_key_usage_flags( &attributes, policy_usage );
1062 psa_set_key_algorithm( &attributes, policy_alg );
1063 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001064
Gilles Peskine049c7532019-05-15 20:22:09 +02001065 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001066 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001067
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001068 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1069
Ronald Cron5425a212020-08-04 14:58:35 +02001070 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001071 payload, payload_length,
1072 signature, sizeof( signature ),
1073 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001074 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001075 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001076 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001077 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001078
1079 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001080 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001081 payload, payload_length,
1082 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001083 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001084 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001085 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001086 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001087
gabor-mezei-armd851d682021-06-28 14:53:49 +02001088 if( PSA_ALG_IS_HASH_AND_SIGN( exercise_alg ) &&
1089 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001090 {
1091 status = psa_sign_message( key, exercise_alg,
1092 payload, payload_length,
1093 signature, sizeof( signature ),
1094 &signature_length );
1095 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1096 PSA_ASSERT( status );
1097 else
1098 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1099
1100 memset( signature, 0, sizeof( signature ) );
1101 status = psa_verify_message( key, exercise_alg,
1102 payload, payload_length,
1103 signature, sizeof( signature ) );
1104 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1105 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1106 else
1107 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1108 }
1109
Gilles Peskined5b33222018-06-18 22:20:03 +02001110exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001111 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001112 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001113}
1114/* END_CASE */
1115
Janos Follathba3fab92019-06-11 14:50:16 +01001116/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001117void derive_key_policy( int policy_usage,
1118 int policy_alg,
1119 int key_type,
1120 data_t *key_data,
1121 int exercise_alg )
1122{
Ronald Cron5425a212020-08-04 14:58:35 +02001123 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001124 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001125 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001126 psa_status_t status;
1127
Gilles Peskine8817f612018-12-18 00:18:46 +01001128 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001129
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001130 psa_set_key_usage_flags( &attributes, policy_usage );
1131 psa_set_key_algorithm( &attributes, policy_alg );
1132 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001133
Gilles Peskine049c7532019-05-15 20:22:09 +02001134 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001135 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001136
Janos Follathba3fab92019-06-11 14:50:16 +01001137 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1138
1139 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1140 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001141 {
Janos Follathba3fab92019-06-11 14:50:16 +01001142 PSA_ASSERT( psa_key_derivation_input_bytes(
1143 &operation,
1144 PSA_KEY_DERIVATION_INPUT_SEED,
1145 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001146 }
Janos Follathba3fab92019-06-11 14:50:16 +01001147
1148 status = psa_key_derivation_input_key( &operation,
1149 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001150 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001151
Gilles Peskineea0fb492018-07-12 17:17:20 +02001152 if( policy_alg == exercise_alg &&
1153 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001154 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001155 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001156 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001157
1158exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001159 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001160 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001161 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001162}
1163/* END_CASE */
1164
1165/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001166void agreement_key_policy( int policy_usage,
1167 int policy_alg,
1168 int key_type_arg,
1169 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001170 int exercise_alg,
1171 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001172{
Ronald Cron5425a212020-08-04 14:58:35 +02001173 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001174 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001175 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001176 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001177 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001178 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001179
Gilles Peskine8817f612018-12-18 00:18:46 +01001180 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001181
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001182 psa_set_key_usage_flags( &attributes, policy_usage );
1183 psa_set_key_algorithm( &attributes, policy_alg );
1184 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001185
Gilles Peskine049c7532019-05-15 20:22:09 +02001186 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001187 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001188
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001189 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001190 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001191
Steven Cooremance48e852020-10-05 16:02:45 +02001192 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001193
1194exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001195 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001196 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001197 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001198}
1199/* END_CASE */
1200
1201/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001202void key_policy_alg2( int key_type_arg, data_t *key_data,
1203 int usage_arg, int alg_arg, int alg2_arg )
1204{
Ronald Cron5425a212020-08-04 14:58:35 +02001205 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001206 psa_key_type_t key_type = key_type_arg;
1207 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1208 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1209 psa_key_usage_t usage = usage_arg;
1210 psa_algorithm_t alg = alg_arg;
1211 psa_algorithm_t alg2 = alg2_arg;
1212
1213 PSA_ASSERT( psa_crypto_init( ) );
1214
1215 psa_set_key_usage_flags( &attributes, usage );
1216 psa_set_key_algorithm( &attributes, alg );
1217 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1218 psa_set_key_type( &attributes, key_type );
1219 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001220 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001221
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001222 /* Update the usage flags to obtain implicit usage flags */
1223 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001224 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001225 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1226 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1227 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1228
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001229 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001230 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001231 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001232 goto exit;
1233
1234exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001235 /*
1236 * Key attributes may have been returned by psa_get_key_attributes()
1237 * thus reset them as required.
1238 */
1239 psa_reset_key_attributes( &got_attributes );
1240
Ronald Cron5425a212020-08-04 14:58:35 +02001241 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001242 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001243}
1244/* END_CASE */
1245
1246/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001247void raw_agreement_key_policy( int policy_usage,
1248 int policy_alg,
1249 int key_type_arg,
1250 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001251 int exercise_alg,
1252 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001253{
Ronald Cron5425a212020-08-04 14:58:35 +02001254 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001255 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001256 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001257 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001258 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001259 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001260
1261 PSA_ASSERT( psa_crypto_init( ) );
1262
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001263 psa_set_key_usage_flags( &attributes, policy_usage );
1264 psa_set_key_algorithm( &attributes, policy_alg );
1265 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001266
Gilles Peskine049c7532019-05-15 20:22:09 +02001267 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001268 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001269
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001270 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001271
Steven Cooremance48e852020-10-05 16:02:45 +02001272 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001273
1274exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001275 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001276 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001277 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001278}
1279/* END_CASE */
1280
1281/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001282void copy_success( int source_usage_arg,
1283 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001284 int type_arg, data_t *material,
1285 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001286 int target_usage_arg,
1287 int target_alg_arg, int target_alg2_arg,
1288 int expected_usage_arg,
1289 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001290{
Gilles Peskineca25db92019-04-19 11:43:08 +02001291 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1292 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001293 psa_key_usage_t expected_usage = expected_usage_arg;
1294 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001295 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001296 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1297 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001298 uint8_t *export_buffer = NULL;
1299
Gilles Peskine57ab7212019-01-28 13:03:09 +01001300 PSA_ASSERT( psa_crypto_init( ) );
1301
Gilles Peskineca25db92019-04-19 11:43:08 +02001302 /* Prepare the source key. */
1303 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1304 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001305 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001306 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001307 PSA_ASSERT( psa_import_key( &source_attributes,
1308 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001309 &source_key ) );
1310 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001311
Gilles Peskineca25db92019-04-19 11:43:08 +02001312 /* Prepare the target attributes. */
1313 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001314 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001315 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001316 /* Set volatile lifetime to reset the key identifier to 0. */
1317 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1318 }
1319
Gilles Peskineca25db92019-04-19 11:43:08 +02001320 if( target_usage_arg != -1 )
1321 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1322 if( target_alg_arg != -1 )
1323 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001324 if( target_alg2_arg != -1 )
1325 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001326
1327 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001328 PSA_ASSERT( psa_copy_key( source_key,
1329 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001330
1331 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001332 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001333
1334 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001335 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001336 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1337 psa_get_key_type( &target_attributes ) );
1338 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1339 psa_get_key_bits( &target_attributes ) );
1340 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1341 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001342 TEST_EQUAL( expected_alg2,
1343 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001344 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1345 {
1346 size_t length;
1347 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001348 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001349 material->len, &length ) );
1350 ASSERT_COMPARE( material->x, material->len,
1351 export_buffer, length );
1352 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001353
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001354 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001355 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001356 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001357 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001358
Ronald Cron5425a212020-08-04 14:58:35 +02001359 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001360
1361exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001362 /*
1363 * Source and target key attributes may have been returned by
1364 * psa_get_key_attributes() thus reset them as required.
1365 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001366 psa_reset_key_attributes( &source_attributes );
1367 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001368
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001369 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001370 mbedtls_free( export_buffer );
1371}
1372/* END_CASE */
1373
1374/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001375void copy_fail( int source_usage_arg,
1376 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001377 int type_arg, data_t *material,
1378 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001379 int target_usage_arg,
1380 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001381 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001382 int expected_status_arg )
1383{
1384 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1385 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001386 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1387 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001388 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001389
1390 PSA_ASSERT( psa_crypto_init( ) );
1391
1392 /* Prepare the source key. */
1393 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1394 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001395 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001396 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001397 PSA_ASSERT( psa_import_key( &source_attributes,
1398 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001399 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001400
1401 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001402 psa_set_key_id( &target_attributes, key_id );
1403 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001404 psa_set_key_type( &target_attributes, target_type_arg );
1405 psa_set_key_bits( &target_attributes, target_bits_arg );
1406 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1407 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001408 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001409
1410 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001411 TEST_EQUAL( psa_copy_key( source_key,
1412 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001413 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001414
Ronald Cron5425a212020-08-04 14:58:35 +02001415 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001416
Gilles Peskine4a644642019-05-03 17:14:08 +02001417exit:
1418 psa_reset_key_attributes( &source_attributes );
1419 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001420 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001421}
1422/* END_CASE */
1423
1424/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001425void hash_operation_init( )
1426{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001427 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001428 /* Test each valid way of initializing the object, except for `= {0}`, as
1429 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1430 * though it's OK by the C standard. We could test for this, but we'd need
1431 * to supress the Clang warning for the test. */
1432 psa_hash_operation_t func = psa_hash_operation_init( );
1433 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1434 psa_hash_operation_t zero;
1435
1436 memset( &zero, 0, sizeof( zero ) );
1437
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001438 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001439 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1440 PSA_ERROR_BAD_STATE );
1441 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1442 PSA_ERROR_BAD_STATE );
1443 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1444 PSA_ERROR_BAD_STATE );
1445
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001446 /* A default hash operation should be abortable without error. */
1447 PSA_ASSERT( psa_hash_abort( &func ) );
1448 PSA_ASSERT( psa_hash_abort( &init ) );
1449 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001450}
1451/* END_CASE */
1452
1453/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001454void hash_setup( int alg_arg,
1455 int expected_status_arg )
1456{
1457 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001458 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001459 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001460 psa_status_t status;
1461
Gilles Peskine8817f612018-12-18 00:18:46 +01001462 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001463
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001464 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001465 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001466
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001467 /* Whether setup succeeded or failed, abort must succeed. */
1468 PSA_ASSERT( psa_hash_abort( &operation ) );
1469
1470 /* If setup failed, reproduce the failure, so as to
1471 * test the resulting state of the operation object. */
1472 if( status != PSA_SUCCESS )
1473 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1474
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001475 /* Now the operation object should be reusable. */
1476#if defined(KNOWN_SUPPORTED_HASH_ALG)
1477 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1478 PSA_ASSERT( psa_hash_abort( &operation ) );
1479#endif
1480
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001481exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001482 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001483}
1484/* END_CASE */
1485
1486/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001487void hash_compute_fail( int alg_arg, data_t *input,
1488 int output_size_arg, int expected_status_arg )
1489{
1490 psa_algorithm_t alg = alg_arg;
1491 uint8_t *output = NULL;
1492 size_t output_size = output_size_arg;
1493 size_t output_length = INVALID_EXPORT_LENGTH;
1494 psa_status_t expected_status = expected_status_arg;
1495 psa_status_t status;
1496
1497 ASSERT_ALLOC( output, output_size );
1498
1499 PSA_ASSERT( psa_crypto_init( ) );
1500
1501 status = psa_hash_compute( alg, input->x, input->len,
1502 output, output_size, &output_length );
1503 TEST_EQUAL( status, expected_status );
1504 TEST_ASSERT( output_length <= output_size );
1505
1506exit:
1507 mbedtls_free( output );
1508 PSA_DONE( );
1509}
1510/* END_CASE */
1511
1512/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001513void hash_compare_fail( int alg_arg, data_t *input,
1514 data_t *reference_hash,
1515 int expected_status_arg )
1516{
1517 psa_algorithm_t alg = alg_arg;
1518 psa_status_t expected_status = expected_status_arg;
1519 psa_status_t status;
1520
1521 PSA_ASSERT( psa_crypto_init( ) );
1522
1523 status = psa_hash_compare( alg, input->x, input->len,
1524 reference_hash->x, reference_hash->len );
1525 TEST_EQUAL( status, expected_status );
1526
1527exit:
1528 PSA_DONE( );
1529}
1530/* END_CASE */
1531
1532/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001533void hash_compute_compare( int alg_arg, data_t *input,
1534 data_t *expected_output )
1535{
1536 psa_algorithm_t alg = alg_arg;
1537 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1538 size_t output_length = INVALID_EXPORT_LENGTH;
1539 size_t i;
1540
1541 PSA_ASSERT( psa_crypto_init( ) );
1542
1543 /* Compute with tight buffer */
1544 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001545 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001546 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001547 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001548 ASSERT_COMPARE( output, output_length,
1549 expected_output->x, expected_output->len );
1550
1551 /* Compute with larger buffer */
1552 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1553 output, sizeof( output ),
1554 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001555 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001556 ASSERT_COMPARE( output, output_length,
1557 expected_output->x, expected_output->len );
1558
1559 /* Compare with correct hash */
1560 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1561 output, output_length ) );
1562
1563 /* Compare with trailing garbage */
1564 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1565 output, output_length + 1 ),
1566 PSA_ERROR_INVALID_SIGNATURE );
1567
1568 /* Compare with truncated hash */
1569 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1570 output, output_length - 1 ),
1571 PSA_ERROR_INVALID_SIGNATURE );
1572
1573 /* Compare with corrupted value */
1574 for( i = 0; i < output_length; i++ )
1575 {
Chris Jones9634bb12021-01-20 15:56:42 +00001576 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001577 output[i] ^= 1;
1578 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1579 output, output_length ),
1580 PSA_ERROR_INVALID_SIGNATURE );
1581 output[i] ^= 1;
1582 }
1583
1584exit:
1585 PSA_DONE( );
1586}
1587/* END_CASE */
1588
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001589/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001590void hash_bad_order( )
1591{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001592 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001593 unsigned char input[] = "";
1594 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001595 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001596 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1597 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1598 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001599 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001600 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001601 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001602
Gilles Peskine8817f612018-12-18 00:18:46 +01001603 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001604
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001605 /* Call setup twice in a row. */
1606 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001607 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001608 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1609 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001610 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001611 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001612 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001613
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001614 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001615 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001616 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001617 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001618
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001619 /* Check that update calls abort on error. */
1620 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01001621 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001622 ASSERT_OPERATION_IS_ACTIVE( operation );
1623 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
1624 PSA_ERROR_BAD_STATE );
1625 ASSERT_OPERATION_IS_INACTIVE( operation );
1626 PSA_ASSERT( psa_hash_abort( &operation ) );
1627 ASSERT_OPERATION_IS_INACTIVE( operation );
1628
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001629 /* Call update after finish. */
1630 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1631 PSA_ASSERT( psa_hash_finish( &operation,
1632 hash, sizeof( hash ), &hash_len ) );
1633 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001634 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001635 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001636
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001637 /* Call verify without calling setup beforehand. */
1638 TEST_EQUAL( psa_hash_verify( &operation,
1639 valid_hash, sizeof( valid_hash ) ),
1640 PSA_ERROR_BAD_STATE );
1641 PSA_ASSERT( psa_hash_abort( &operation ) );
1642
1643 /* Call verify after finish. */
1644 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1645 PSA_ASSERT( psa_hash_finish( &operation,
1646 hash, sizeof( hash ), &hash_len ) );
1647 TEST_EQUAL( psa_hash_verify( &operation,
1648 valid_hash, sizeof( valid_hash ) ),
1649 PSA_ERROR_BAD_STATE );
1650 PSA_ASSERT( psa_hash_abort( &operation ) );
1651
1652 /* Call verify twice in a row. */
1653 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001654 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001655 PSA_ASSERT( psa_hash_verify( &operation,
1656 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001657 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001658 TEST_EQUAL( psa_hash_verify( &operation,
1659 valid_hash, sizeof( valid_hash ) ),
1660 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001661 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001662 PSA_ASSERT( psa_hash_abort( &operation ) );
1663
1664 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001665 TEST_EQUAL( psa_hash_finish( &operation,
1666 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001667 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001668 PSA_ASSERT( psa_hash_abort( &operation ) );
1669
1670 /* Call finish twice in a row. */
1671 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1672 PSA_ASSERT( psa_hash_finish( &operation,
1673 hash, sizeof( hash ), &hash_len ) );
1674 TEST_EQUAL( psa_hash_finish( &operation,
1675 hash, sizeof( hash ), &hash_len ),
1676 PSA_ERROR_BAD_STATE );
1677 PSA_ASSERT( psa_hash_abort( &operation ) );
1678
1679 /* Call finish after calling verify. */
1680 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1681 PSA_ASSERT( psa_hash_verify( &operation,
1682 valid_hash, sizeof( valid_hash ) ) );
1683 TEST_EQUAL( psa_hash_finish( &operation,
1684 hash, sizeof( hash ), &hash_len ),
1685 PSA_ERROR_BAD_STATE );
1686 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001687
1688exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001689 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001690}
1691/* END_CASE */
1692
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001693/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001694void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001695{
1696 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001697 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1698 * appended to it */
1699 unsigned char hash[] = {
1700 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1701 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1702 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001703 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001704 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001705
Gilles Peskine8817f612018-12-18 00:18:46 +01001706 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001707
itayzafrir27e69452018-11-01 14:26:34 +02001708 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001709 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001710 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001711 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001712 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001713 ASSERT_OPERATION_IS_INACTIVE( operation );
1714 PSA_ASSERT( psa_hash_abort( &operation ) );
1715 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03001716
itayzafrir27e69452018-11-01 14:26:34 +02001717 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001718 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001719 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001720 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001721
itayzafrir27e69452018-11-01 14:26:34 +02001722 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001723 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001724 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001725 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001726
itayzafrirec93d302018-10-18 18:01:10 +03001727exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001728 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001729}
1730/* END_CASE */
1731
Ronald Cronee414c72021-03-18 18:50:08 +01001732/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001733void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001734{
1735 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001736 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001737 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001738 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001739 size_t hash_len;
1740
Gilles Peskine8817f612018-12-18 00:18:46 +01001741 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001742
itayzafrir58028322018-10-25 10:22:01 +03001743 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001744 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001745 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001746 hash, expected_size - 1, &hash_len ),
1747 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001748
1749exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001750 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001751}
1752/* END_CASE */
1753
Ronald Cronee414c72021-03-18 18:50:08 +01001754/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001755void hash_clone_source_state( )
1756{
1757 psa_algorithm_t alg = PSA_ALG_SHA_256;
1758 unsigned char hash[PSA_HASH_MAX_SIZE];
1759 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1760 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1761 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1762 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1763 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1764 size_t hash_len;
1765
1766 PSA_ASSERT( psa_crypto_init( ) );
1767 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1768
1769 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1770 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1771 PSA_ASSERT( psa_hash_finish( &op_finished,
1772 hash, sizeof( hash ), &hash_len ) );
1773 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1774 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1775
1776 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1777 PSA_ERROR_BAD_STATE );
1778
1779 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1780 PSA_ASSERT( psa_hash_finish( &op_init,
1781 hash, sizeof( hash ), &hash_len ) );
1782 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1783 PSA_ASSERT( psa_hash_finish( &op_finished,
1784 hash, sizeof( hash ), &hash_len ) );
1785 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1786 PSA_ASSERT( psa_hash_finish( &op_aborted,
1787 hash, sizeof( hash ), &hash_len ) );
1788
1789exit:
1790 psa_hash_abort( &op_source );
1791 psa_hash_abort( &op_init );
1792 psa_hash_abort( &op_setup );
1793 psa_hash_abort( &op_finished );
1794 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001795 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001796}
1797/* END_CASE */
1798
Ronald Cronee414c72021-03-18 18:50:08 +01001799/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001800void hash_clone_target_state( )
1801{
1802 psa_algorithm_t alg = PSA_ALG_SHA_256;
1803 unsigned char hash[PSA_HASH_MAX_SIZE];
1804 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1805 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1806 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1807 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1808 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1809 size_t hash_len;
1810
1811 PSA_ASSERT( psa_crypto_init( ) );
1812
1813 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1814 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1815 PSA_ASSERT( psa_hash_finish( &op_finished,
1816 hash, sizeof( hash ), &hash_len ) );
1817 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1818 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1819
1820 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1821 PSA_ASSERT( psa_hash_finish( &op_target,
1822 hash, sizeof( hash ), &hash_len ) );
1823
1824 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1825 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1826 PSA_ERROR_BAD_STATE );
1827 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1828 PSA_ERROR_BAD_STATE );
1829
1830exit:
1831 psa_hash_abort( &op_target );
1832 psa_hash_abort( &op_init );
1833 psa_hash_abort( &op_setup );
1834 psa_hash_abort( &op_finished );
1835 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001836 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001837}
1838/* END_CASE */
1839
itayzafrir58028322018-10-25 10:22:01 +03001840/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001841void mac_operation_init( )
1842{
Jaeden Amero252ef282019-02-15 14:05:35 +00001843 const uint8_t input[1] = { 0 };
1844
Jaeden Amero769ce272019-01-04 11:48:03 +00001845 /* Test each valid way of initializing the object, except for `= {0}`, as
1846 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1847 * though it's OK by the C standard. We could test for this, but we'd need
1848 * to supress the Clang warning for the test. */
1849 psa_mac_operation_t func = psa_mac_operation_init( );
1850 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1851 psa_mac_operation_t zero;
1852
1853 memset( &zero, 0, sizeof( zero ) );
1854
Jaeden Amero252ef282019-02-15 14:05:35 +00001855 /* A freshly-initialized MAC operation should not be usable. */
1856 TEST_EQUAL( psa_mac_update( &func,
1857 input, sizeof( input ) ),
1858 PSA_ERROR_BAD_STATE );
1859 TEST_EQUAL( psa_mac_update( &init,
1860 input, sizeof( input ) ),
1861 PSA_ERROR_BAD_STATE );
1862 TEST_EQUAL( psa_mac_update( &zero,
1863 input, sizeof( input ) ),
1864 PSA_ERROR_BAD_STATE );
1865
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001866 /* A default MAC operation should be abortable without error. */
1867 PSA_ASSERT( psa_mac_abort( &func ) );
1868 PSA_ASSERT( psa_mac_abort( &init ) );
1869 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001870}
1871/* END_CASE */
1872
1873/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001874void mac_setup( int key_type_arg,
1875 data_t *key,
1876 int alg_arg,
1877 int expected_status_arg )
1878{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001879 psa_key_type_t key_type = key_type_arg;
1880 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001881 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001882 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001883 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1884#if defined(KNOWN_SUPPORTED_MAC_ALG)
1885 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1886#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001887
Gilles Peskine8817f612018-12-18 00:18:46 +01001888 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001889
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001890 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1891 &operation, &status ) )
1892 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001893 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001894
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001895 /* The operation object should be reusable. */
1896#if defined(KNOWN_SUPPORTED_MAC_ALG)
1897 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1898 smoke_test_key_data,
1899 sizeof( smoke_test_key_data ),
1900 KNOWN_SUPPORTED_MAC_ALG,
1901 &operation, &status ) )
1902 goto exit;
1903 TEST_EQUAL( status, PSA_SUCCESS );
1904#endif
1905
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001906exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001907 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001908}
1909/* END_CASE */
1910
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001911/* 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 +00001912void mac_bad_order( )
1913{
Ronald Cron5425a212020-08-04 14:58:35 +02001914 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001915 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1916 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001917 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001918 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1919 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1920 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001921 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001922 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1923 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1924 size_t sign_mac_length = 0;
1925 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1926 const uint8_t verify_mac[] = {
1927 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1928 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1929 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1930
1931 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001932 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001933 psa_set_key_algorithm( &attributes, alg );
1934 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001935
Ronald Cron5425a212020-08-04 14:58:35 +02001936 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1937 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001938
Jaeden Amero252ef282019-02-15 14:05:35 +00001939 /* Call update without calling setup beforehand. */
1940 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1941 PSA_ERROR_BAD_STATE );
1942 PSA_ASSERT( psa_mac_abort( &operation ) );
1943
1944 /* Call sign finish without calling setup beforehand. */
1945 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1946 &sign_mac_length),
1947 PSA_ERROR_BAD_STATE );
1948 PSA_ASSERT( psa_mac_abort( &operation ) );
1949
1950 /* Call verify finish without calling setup beforehand. */
1951 TEST_EQUAL( psa_mac_verify_finish( &operation,
1952 verify_mac, sizeof( verify_mac ) ),
1953 PSA_ERROR_BAD_STATE );
1954 PSA_ASSERT( psa_mac_abort( &operation ) );
1955
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001956 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001957 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001958 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001959 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001960 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001961 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001962 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001963 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001964
Jaeden Amero252ef282019-02-15 14:05:35 +00001965 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001966 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001967 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1968 PSA_ASSERT( psa_mac_sign_finish( &operation,
1969 sign_mac, sizeof( sign_mac ),
1970 &sign_mac_length ) );
1971 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1972 PSA_ERROR_BAD_STATE );
1973 PSA_ASSERT( psa_mac_abort( &operation ) );
1974
1975 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001976 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001977 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1978 PSA_ASSERT( psa_mac_verify_finish( &operation,
1979 verify_mac, sizeof( verify_mac ) ) );
1980 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1981 PSA_ERROR_BAD_STATE );
1982 PSA_ASSERT( psa_mac_abort( &operation ) );
1983
1984 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001985 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001986 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1987 PSA_ASSERT( psa_mac_sign_finish( &operation,
1988 sign_mac, sizeof( sign_mac ),
1989 &sign_mac_length ) );
1990 TEST_EQUAL( psa_mac_sign_finish( &operation,
1991 sign_mac, sizeof( sign_mac ),
1992 &sign_mac_length ),
1993 PSA_ERROR_BAD_STATE );
1994 PSA_ASSERT( psa_mac_abort( &operation ) );
1995
1996 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001997 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001998 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1999 PSA_ASSERT( psa_mac_verify_finish( &operation,
2000 verify_mac, sizeof( verify_mac ) ) );
2001 TEST_EQUAL( psa_mac_verify_finish( &operation,
2002 verify_mac, sizeof( verify_mac ) ),
2003 PSA_ERROR_BAD_STATE );
2004 PSA_ASSERT( psa_mac_abort( &operation ) );
2005
2006 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002007 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002008 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002009 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002010 TEST_EQUAL( psa_mac_verify_finish( &operation,
2011 verify_mac, sizeof( verify_mac ) ),
2012 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002013 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002014 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002015 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002016
2017 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002018 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002019 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002020 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002021 TEST_EQUAL( psa_mac_sign_finish( &operation,
2022 sign_mac, sizeof( sign_mac ),
2023 &sign_mac_length ),
2024 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002025 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002026 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002027 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002028
Ronald Cron5425a212020-08-04 14:58:35 +02002029 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002030
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002031exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002032 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002033}
2034/* END_CASE */
2035
2036/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002037void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002038 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002039 int alg_arg,
2040 data_t *input,
2041 data_t *expected_mac )
2042{
Ronald Cron5425a212020-08-04 14:58:35 +02002043 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002044 psa_key_type_t key_type = key_type_arg;
2045 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002046 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002047 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002048 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002049 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002050 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002051 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002052 const size_t output_sizes_to_test[] = {
2053 0,
2054 1,
2055 expected_mac->len - 1,
2056 expected_mac->len,
2057 expected_mac->len + 1,
2058 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002059
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002060 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002061 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002062 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002063
Gilles Peskine8817f612018-12-18 00:18:46 +01002064 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002065
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002066 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002067 psa_set_key_algorithm( &attributes, alg );
2068 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002069
Ronald Cron5425a212020-08-04 14:58:35 +02002070 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2071 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002072
Gilles Peskine8b356b52020-08-25 23:44:59 +02002073 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2074 {
2075 const size_t output_size = output_sizes_to_test[i];
2076 psa_status_t expected_status =
2077 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2078 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002079
Chris Jones9634bb12021-01-20 15:56:42 +00002080 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002081 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002082
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002083 /* Calculate the MAC, one-shot case. */
2084 TEST_EQUAL( psa_mac_compute( key, alg,
2085 input->x, input->len,
2086 actual_mac, output_size, &mac_length ),
2087 expected_status );
2088 if( expected_status == PSA_SUCCESS )
2089 {
2090 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2091 actual_mac, mac_length );
2092 }
2093
2094 if( output_size > 0 )
2095 memset( actual_mac, 0, output_size );
2096
2097 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002098 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002099 PSA_ASSERT( psa_mac_update( &operation,
2100 input->x, input->len ) );
2101 TEST_EQUAL( psa_mac_sign_finish( &operation,
2102 actual_mac, output_size,
2103 &mac_length ),
2104 expected_status );
2105 PSA_ASSERT( psa_mac_abort( &operation ) );
2106
2107 if( expected_status == PSA_SUCCESS )
2108 {
2109 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2110 actual_mac, mac_length );
2111 }
2112 mbedtls_free( actual_mac );
2113 actual_mac = NULL;
2114 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002115
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002116exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002117 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002118 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002119 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002120 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002121}
2122/* END_CASE */
2123
2124/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002125void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002126 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002127 int alg_arg,
2128 data_t *input,
2129 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002130{
Ronald Cron5425a212020-08-04 14:58:35 +02002131 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002132 psa_key_type_t key_type = key_type_arg;
2133 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002134 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002135 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002136 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002137
Gilles Peskine69c12672018-06-28 00:07:19 +02002138 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2139
Gilles Peskine8817f612018-12-18 00:18:46 +01002140 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002141
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002142 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002143 psa_set_key_algorithm( &attributes, alg );
2144 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002145
Ronald Cron5425a212020-08-04 14:58:35 +02002146 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2147 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002148
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002149 /* Verify correct MAC, one-shot case. */
2150 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2151 expected_mac->x, expected_mac->len ) );
2152
2153 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002154 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002155 PSA_ASSERT( psa_mac_update( &operation,
2156 input->x, input->len ) );
2157 PSA_ASSERT( psa_mac_verify_finish( &operation,
2158 expected_mac->x,
2159 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002160
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002161 /* Test a MAC that's too short, one-shot case. */
2162 TEST_EQUAL( psa_mac_verify( key, alg,
2163 input->x, input->len,
2164 expected_mac->x,
2165 expected_mac->len - 1 ),
2166 PSA_ERROR_INVALID_SIGNATURE );
2167
2168 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002169 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002170 PSA_ASSERT( psa_mac_update( &operation,
2171 input->x, input->len ) );
2172 TEST_EQUAL( psa_mac_verify_finish( &operation,
2173 expected_mac->x,
2174 expected_mac->len - 1 ),
2175 PSA_ERROR_INVALID_SIGNATURE );
2176
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002177 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002178 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2179 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002180 TEST_EQUAL( psa_mac_verify( key, alg,
2181 input->x, input->len,
2182 perturbed_mac, expected_mac->len + 1 ),
2183 PSA_ERROR_INVALID_SIGNATURE );
2184
2185 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002186 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002187 PSA_ASSERT( psa_mac_update( &operation,
2188 input->x, input->len ) );
2189 TEST_EQUAL( psa_mac_verify_finish( &operation,
2190 perturbed_mac,
2191 expected_mac->len + 1 ),
2192 PSA_ERROR_INVALID_SIGNATURE );
2193
2194 /* Test changing one byte. */
2195 for( size_t i = 0; i < expected_mac->len; i++ )
2196 {
Chris Jones9634bb12021-01-20 15:56:42 +00002197 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002198 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002199
2200 TEST_EQUAL( psa_mac_verify( key, alg,
2201 input->x, input->len,
2202 perturbed_mac, expected_mac->len ),
2203 PSA_ERROR_INVALID_SIGNATURE );
2204
Ronald Cron5425a212020-08-04 14:58:35 +02002205 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002206 PSA_ASSERT( psa_mac_update( &operation,
2207 input->x, input->len ) );
2208 TEST_EQUAL( psa_mac_verify_finish( &operation,
2209 perturbed_mac,
2210 expected_mac->len ),
2211 PSA_ERROR_INVALID_SIGNATURE );
2212 perturbed_mac[i] ^= 1;
2213 }
2214
Gilles Peskine8c9def32018-02-08 10:02:12 +01002215exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002216 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002217 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002218 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002219 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002220}
2221/* END_CASE */
2222
2223/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002224void cipher_operation_init( )
2225{
Jaeden Ameroab439972019-02-15 14:12:05 +00002226 const uint8_t input[1] = { 0 };
2227 unsigned char output[1] = { 0 };
2228 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002229 /* Test each valid way of initializing the object, except for `= {0}`, as
2230 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2231 * though it's OK by the C standard. We could test for this, but we'd need
2232 * to supress the Clang warning for the test. */
2233 psa_cipher_operation_t func = psa_cipher_operation_init( );
2234 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2235 psa_cipher_operation_t zero;
2236
2237 memset( &zero, 0, sizeof( zero ) );
2238
Jaeden Ameroab439972019-02-15 14:12:05 +00002239 /* A freshly-initialized cipher operation should not be usable. */
2240 TEST_EQUAL( psa_cipher_update( &func,
2241 input, sizeof( input ),
2242 output, sizeof( output ),
2243 &output_length ),
2244 PSA_ERROR_BAD_STATE );
2245 TEST_EQUAL( psa_cipher_update( &init,
2246 input, sizeof( input ),
2247 output, sizeof( output ),
2248 &output_length ),
2249 PSA_ERROR_BAD_STATE );
2250 TEST_EQUAL( psa_cipher_update( &zero,
2251 input, sizeof( input ),
2252 output, sizeof( output ),
2253 &output_length ),
2254 PSA_ERROR_BAD_STATE );
2255
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002256 /* A default cipher operation should be abortable without error. */
2257 PSA_ASSERT( psa_cipher_abort( &func ) );
2258 PSA_ASSERT( psa_cipher_abort( &init ) );
2259 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002260}
2261/* END_CASE */
2262
2263/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002264void cipher_setup( int key_type_arg,
2265 data_t *key,
2266 int alg_arg,
2267 int expected_status_arg )
2268{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002269 psa_key_type_t key_type = key_type_arg;
2270 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002271 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002272 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002273 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002274#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002275 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2276#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002277
Gilles Peskine8817f612018-12-18 00:18:46 +01002278 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002279
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002280 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2281 &operation, &status ) )
2282 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002283 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002284
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002285 /* The operation object should be reusable. */
2286#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2287 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2288 smoke_test_key_data,
2289 sizeof( smoke_test_key_data ),
2290 KNOWN_SUPPORTED_CIPHER_ALG,
2291 &operation, &status ) )
2292 goto exit;
2293 TEST_EQUAL( status, PSA_SUCCESS );
2294#endif
2295
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002296exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002297 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002298 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002299}
2300/* END_CASE */
2301
Ronald Cronee414c72021-03-18 18:50:08 +01002302/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002303void cipher_bad_order( )
2304{
Ronald Cron5425a212020-08-04 14:58:35 +02002305 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002306 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2307 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002308 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002309 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002310 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002311 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002312 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2313 0xaa, 0xaa, 0xaa, 0xaa };
2314 const uint8_t text[] = {
2315 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2316 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002317 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002318 size_t length = 0;
2319
2320 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002321 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2322 psa_set_key_algorithm( &attributes, alg );
2323 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002324 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2325 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002326
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002327 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002328 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002329 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002330 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002331 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002332 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002333 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002334 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002335
2336 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002337 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002338 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002339 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002340 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002341 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002342 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002343 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002344
Jaeden Ameroab439972019-02-15 14:12:05 +00002345 /* Generate an IV without calling setup beforehand. */
2346 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2347 buffer, sizeof( buffer ),
2348 &length ),
2349 PSA_ERROR_BAD_STATE );
2350 PSA_ASSERT( psa_cipher_abort( &operation ) );
2351
2352 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002353 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002354 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2355 buffer, sizeof( buffer ),
2356 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002357 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002358 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2359 buffer, sizeof( buffer ),
2360 &length ),
2361 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002362 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002363 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002364 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002365
2366 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002367 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002368 PSA_ASSERT( psa_cipher_set_iv( &operation,
2369 iv, sizeof( iv ) ) );
2370 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2371 buffer, sizeof( buffer ),
2372 &length ),
2373 PSA_ERROR_BAD_STATE );
2374 PSA_ASSERT( psa_cipher_abort( &operation ) );
2375
2376 /* Set an IV without calling setup beforehand. */
2377 TEST_EQUAL( psa_cipher_set_iv( &operation,
2378 iv, sizeof( iv ) ),
2379 PSA_ERROR_BAD_STATE );
2380 PSA_ASSERT( psa_cipher_abort( &operation ) );
2381
2382 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002383 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002384 PSA_ASSERT( psa_cipher_set_iv( &operation,
2385 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002386 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002387 TEST_EQUAL( psa_cipher_set_iv( &operation,
2388 iv, sizeof( iv ) ),
2389 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002390 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002391 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002392 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002393
2394 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002395 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002396 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2397 buffer, sizeof( buffer ),
2398 &length ) );
2399 TEST_EQUAL( psa_cipher_set_iv( &operation,
2400 iv, sizeof( iv ) ),
2401 PSA_ERROR_BAD_STATE );
2402 PSA_ASSERT( psa_cipher_abort( &operation ) );
2403
2404 /* Call update without calling setup beforehand. */
2405 TEST_EQUAL( psa_cipher_update( &operation,
2406 text, sizeof( text ),
2407 buffer, sizeof( buffer ),
2408 &length ),
2409 PSA_ERROR_BAD_STATE );
2410 PSA_ASSERT( psa_cipher_abort( &operation ) );
2411
2412 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01002413 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002414 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002415 TEST_EQUAL( psa_cipher_update( &operation,
2416 text, sizeof( text ),
2417 buffer, sizeof( buffer ),
2418 &length ),
2419 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002420 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002421 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002422 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002423
2424 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002425 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002426 PSA_ASSERT( psa_cipher_set_iv( &operation,
2427 iv, sizeof( iv ) ) );
2428 PSA_ASSERT( psa_cipher_finish( &operation,
2429 buffer, sizeof( buffer ), &length ) );
2430 TEST_EQUAL( psa_cipher_update( &operation,
2431 text, sizeof( text ),
2432 buffer, sizeof( buffer ),
2433 &length ),
2434 PSA_ERROR_BAD_STATE );
2435 PSA_ASSERT( psa_cipher_abort( &operation ) );
2436
2437 /* Call finish without calling setup beforehand. */
2438 TEST_EQUAL( psa_cipher_finish( &operation,
2439 buffer, sizeof( buffer ), &length ),
2440 PSA_ERROR_BAD_STATE );
2441 PSA_ASSERT( psa_cipher_abort( &operation ) );
2442
2443 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002444 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002445 /* Not calling update means we are encrypting an empty buffer, which is OK
2446 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01002447 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002448 TEST_EQUAL( psa_cipher_finish( &operation,
2449 buffer, sizeof( buffer ), &length ),
2450 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002451 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002452 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002453 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002454
2455 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002456 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002457 PSA_ASSERT( psa_cipher_set_iv( &operation,
2458 iv, sizeof( iv ) ) );
2459 PSA_ASSERT( psa_cipher_finish( &operation,
2460 buffer, sizeof( buffer ), &length ) );
2461 TEST_EQUAL( psa_cipher_finish( &operation,
2462 buffer, sizeof( buffer ), &length ),
2463 PSA_ERROR_BAD_STATE );
2464 PSA_ASSERT( psa_cipher_abort( &operation ) );
2465
Ronald Cron5425a212020-08-04 14:58:35 +02002466 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002467
Jaeden Ameroab439972019-02-15 14:12:05 +00002468exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002469 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002470 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002471}
2472/* END_CASE */
2473
2474/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02002475void cipher_encrypt_fail( int alg_arg,
2476 int key_type_arg,
2477 data_t *key_data,
2478 data_t *input,
2479 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002480{
Ronald Cron5425a212020-08-04 14:58:35 +02002481 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002482 psa_status_t status;
2483 psa_key_type_t key_type = key_type_arg;
2484 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002485 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002486 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002487 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002488 size_t output_length = 0;
2489 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2490
2491 if ( PSA_ERROR_BAD_STATE != expected_status )
2492 {
2493 PSA_ASSERT( psa_crypto_init( ) );
2494
2495 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2496 psa_set_key_algorithm( &attributes, alg );
2497 psa_set_key_type( &attributes, key_type );
2498
2499 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
2500 input->len );
2501 ASSERT_ALLOC( output, output_buffer_size );
2502
2503 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2504 &key ) );
2505 }
2506
2507 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
2508 output_buffer_size, &output_length );
2509
2510 TEST_EQUAL( status, expected_status );
2511
2512exit:
2513 mbedtls_free( output );
2514 psa_destroy_key( key );
2515 PSA_DONE( );
2516}
2517/* END_CASE */
2518
2519/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02002520void cipher_encrypt_alg_without_iv( int alg_arg,
2521 int key_type_arg,
2522 data_t *key_data,
2523 data_t *input,
2524 data_t *expected_output )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002525{
2526 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2527 psa_key_type_t key_type = key_type_arg;
2528 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02002529 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2530 uint8_t iv[1] = { 0x5a };
2531 size_t iv_length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002532 unsigned char *output = NULL;
2533 size_t output_buffer_size = 0;
2534 size_t output_length = 0;
2535 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2536
2537 PSA_ASSERT( psa_crypto_init( ) );
2538
2539 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2540 psa_set_key_algorithm( &attributes, alg );
2541 psa_set_key_type( &attributes, key_type );
2542
2543 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2544 ASSERT_ALLOC( output, output_buffer_size );
2545
2546 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2547 &key ) );
2548
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02002549 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2550 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
2551 PSA_ERROR_BAD_STATE );
2552 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2553 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
2554 &iv_length ),
2555 PSA_ERROR_BAD_STATE );
2556
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002557 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
2558 output_buffer_size, &output_length ) );
2559 TEST_ASSERT( output_length <=
2560 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2561 TEST_ASSERT( output_length <=
2562 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
2563
2564 ASSERT_COMPARE( expected_output->x, expected_output->len,
2565 output, output_length );
2566exit:
2567 mbedtls_free( output );
2568 psa_destroy_key( key );
2569 PSA_DONE( );
2570}
2571/* END_CASE */
2572
2573/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002574void cipher_encrypt_validation( int alg_arg,
2575 int key_type_arg,
2576 data_t *key_data,
2577 data_t *input )
2578{
2579 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2580 psa_key_type_t key_type = key_type_arg;
2581 psa_algorithm_t alg = alg_arg;
2582 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
2583 unsigned char *output1 = NULL;
2584 size_t output1_buffer_size = 0;
2585 size_t output1_length = 0;
2586 unsigned char *output2 = NULL;
2587 size_t output2_buffer_size = 0;
2588 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002589 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002590 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002591 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002592
Gilles Peskine8817f612018-12-18 00:18:46 +01002593 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002594
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002595 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2596 psa_set_key_algorithm( &attributes, alg );
2597 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002598
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002599 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2600 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2601 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
2602 ASSERT_ALLOC( output1, output1_buffer_size );
2603 ASSERT_ALLOC( output2, output2_buffer_size );
2604
Ronald Cron5425a212020-08-04 14:58:35 +02002605 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2606 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002607
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02002608 /* The one-shot cipher encryption uses generated iv so validating
2609 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002610 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
2611 output1_buffer_size, &output1_length ) );
2612 TEST_ASSERT( output1_length <=
2613 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2614 TEST_ASSERT( output1_length <=
gabor-mezei-armceface22021-01-21 12:26:17 +01002615 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002616
2617 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2618 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002619
Gilles Peskine8817f612018-12-18 00:18:46 +01002620 PSA_ASSERT( psa_cipher_update( &operation,
2621 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002622 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01002623 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002624 TEST_ASSERT( function_output_length <=
2625 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2626 TEST_ASSERT( function_output_length <=
2627 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002628 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002629
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002630 PSA_ASSERT( psa_cipher_finish( &operation,
2631 output2 + output2_length,
2632 output2_buffer_size - output2_length,
2633 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002634 TEST_ASSERT( function_output_length <=
2635 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2636 TEST_ASSERT( function_output_length <=
2637 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002638 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002639
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002640 PSA_ASSERT( psa_cipher_abort( &operation ) );
2641 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
2642 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002643
Gilles Peskine50e586b2018-06-08 14:28:46 +02002644exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002645 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002646 mbedtls_free( output1 );
2647 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002648 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002649 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002650}
2651/* END_CASE */
2652
2653/* BEGIN_CASE */
2654void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002655 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002656 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002657 int first_part_size_arg,
2658 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002659 data_t *expected_output,
2660 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002661{
Ronald Cron5425a212020-08-04 14:58:35 +02002662 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002663 psa_key_type_t key_type = key_type_arg;
2664 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002665 psa_status_t status;
2666 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002667 size_t first_part_size = first_part_size_arg;
2668 size_t output1_length = output1_length_arg;
2669 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002670 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002671 size_t output_buffer_size = 0;
2672 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002673 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002674 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002675 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002676
Gilles Peskine8817f612018-12-18 00:18:46 +01002677 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002678
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002679 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2680 psa_set_key_algorithm( &attributes, alg );
2681 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002682
Ronald Cron5425a212020-08-04 14:58:35 +02002683 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2684 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002685
Ronald Cron5425a212020-08-04 14:58:35 +02002686 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002687
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002688 if( iv->len > 0 )
2689 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002690 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002691 }
2692
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002693 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2694 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002695 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002696
Gilles Peskinee0866522019-02-19 19:44:00 +01002697 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002698 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2699 output, output_buffer_size,
2700 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002701 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002702 TEST_ASSERT( function_output_length <=
2703 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2704 TEST_ASSERT( function_output_length <=
2705 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002706 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002707
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002708 if( first_part_size < input->len )
2709 {
2710 PSA_ASSERT( psa_cipher_update( &operation,
2711 input->x + first_part_size,
2712 input->len - first_part_size,
2713 ( output_buffer_size == 0 ? NULL :
2714 output + total_output_length ),
2715 output_buffer_size - total_output_length,
2716 &function_output_length ) );
2717 TEST_ASSERT( function_output_length == output2_length );
2718 TEST_ASSERT( function_output_length <=
2719 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2720 alg,
2721 input->len - first_part_size ) );
2722 TEST_ASSERT( function_output_length <=
2723 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2724 total_output_length += function_output_length;
2725 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002726
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002727 status = psa_cipher_finish( &operation,
2728 ( output_buffer_size == 0 ? NULL :
2729 output + total_output_length ),
2730 output_buffer_size - total_output_length,
2731 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002732 TEST_ASSERT( function_output_length <=
2733 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2734 TEST_ASSERT( function_output_length <=
2735 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002736 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002737 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002738
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002739 if( expected_status == PSA_SUCCESS )
2740 {
2741 PSA_ASSERT( psa_cipher_abort( &operation ) );
2742
2743 ASSERT_COMPARE( expected_output->x, expected_output->len,
2744 output, total_output_length );
2745 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002746
2747exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002748 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002749 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002750 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002751 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002752}
2753/* END_CASE */
2754
2755/* BEGIN_CASE */
2756void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002757 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002758 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002759 int first_part_size_arg,
2760 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002761 data_t *expected_output,
2762 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002763{
Ronald Cron5425a212020-08-04 14:58:35 +02002764 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002765 psa_key_type_t key_type = key_type_arg;
2766 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002767 psa_status_t status;
2768 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002769 size_t first_part_size = first_part_size_arg;
2770 size_t output1_length = output1_length_arg;
2771 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002772 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002773 size_t output_buffer_size = 0;
2774 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002775 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002776 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002777 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002778
Gilles Peskine8817f612018-12-18 00:18:46 +01002779 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002780
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002781 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2782 psa_set_key_algorithm( &attributes, alg );
2783 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002784
Ronald Cron5425a212020-08-04 14:58:35 +02002785 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2786 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002787
Ronald Cron5425a212020-08-04 14:58:35 +02002788 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002789
Steven Cooreman177deba2020-09-07 17:14:14 +02002790 if( iv->len > 0 )
2791 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002792 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002793 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002794
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002795 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2796 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002797 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002798
Gilles Peskinee0866522019-02-19 19:44:00 +01002799 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002800 PSA_ASSERT( psa_cipher_update( &operation,
2801 input->x, first_part_size,
2802 output, output_buffer_size,
2803 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002804 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002805 TEST_ASSERT( function_output_length <=
2806 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2807 TEST_ASSERT( function_output_length <=
2808 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002809 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002810
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002811 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02002812 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002813 PSA_ASSERT( psa_cipher_update( &operation,
2814 input->x + first_part_size,
2815 input->len - first_part_size,
2816 ( output_buffer_size == 0 ? NULL :
2817 output + total_output_length ),
2818 output_buffer_size - total_output_length,
2819 &function_output_length ) );
2820 TEST_ASSERT( function_output_length == output2_length );
2821 TEST_ASSERT( function_output_length <=
2822 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2823 alg,
2824 input->len - first_part_size ) );
2825 TEST_ASSERT( function_output_length <=
2826 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2827 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002828 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002829
Gilles Peskine50e586b2018-06-08 14:28:46 +02002830 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002831 ( output_buffer_size == 0 ? NULL :
2832 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002833 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002834 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002835 TEST_ASSERT( function_output_length <=
2836 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2837 TEST_ASSERT( function_output_length <=
2838 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002839 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002840 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002841
2842 if( expected_status == PSA_SUCCESS )
2843 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002844 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002845
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002846 ASSERT_COMPARE( expected_output->x, expected_output->len,
2847 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002848 }
2849
Gilles Peskine50e586b2018-06-08 14:28:46 +02002850exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002851 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002852 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002853 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002854 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002855}
2856/* END_CASE */
2857
Gilles Peskine50e586b2018-06-08 14:28:46 +02002858/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02002859void cipher_decrypt_fail( int alg_arg,
2860 int key_type_arg,
2861 data_t *key_data,
2862 data_t *iv,
2863 data_t *input_arg,
2864 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002865{
2866 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2867 psa_status_t status;
2868 psa_key_type_t key_type = key_type_arg;
2869 psa_algorithm_t alg = alg_arg;
2870 psa_status_t expected_status = expected_status_arg;
2871 unsigned char *input = NULL;
2872 size_t input_buffer_size = 0;
2873 unsigned char *output = NULL;
2874 size_t output_buffer_size = 0;
2875 size_t output_length = 0;
2876 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2877
2878 if ( PSA_ERROR_BAD_STATE != expected_status )
2879 {
2880 PSA_ASSERT( psa_crypto_init( ) );
2881
2882 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2883 psa_set_key_algorithm( &attributes, alg );
2884 psa_set_key_type( &attributes, key_type );
2885
2886 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2887 &key ) );
2888 }
2889
2890 /* Allocate input buffer and copy the iv and the plaintext */
2891 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
2892 if ( input_buffer_size > 0 )
2893 {
2894 ASSERT_ALLOC( input, input_buffer_size );
2895 memcpy( input, iv->x, iv->len );
2896 memcpy( input + iv->len, input_arg->x, input_arg->len );
2897 }
2898
2899 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
2900 ASSERT_ALLOC( output, output_buffer_size );
2901
2902 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
2903 output_buffer_size, &output_length );
2904 TEST_EQUAL( status, expected_status );
2905
2906exit:
2907 mbedtls_free( input );
2908 mbedtls_free( output );
2909 psa_destroy_key( key );
2910 PSA_DONE( );
2911}
2912/* END_CASE */
2913
2914/* BEGIN_CASE */
2915void cipher_decrypt( int alg_arg,
2916 int key_type_arg,
2917 data_t *key_data,
2918 data_t *iv,
2919 data_t *input_arg,
2920 data_t *expected_output )
2921{
2922 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2923 psa_key_type_t key_type = key_type_arg;
2924 psa_algorithm_t alg = alg_arg;
2925 unsigned char *input = NULL;
2926 size_t input_buffer_size = 0;
2927 unsigned char *output = NULL;
2928 size_t output_buffer_size = 0;
2929 size_t output_length = 0;
2930 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2931
2932 PSA_ASSERT( psa_crypto_init( ) );
2933
2934 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2935 psa_set_key_algorithm( &attributes, alg );
2936 psa_set_key_type( &attributes, key_type );
2937
2938 /* Allocate input buffer and copy the iv and the plaintext */
2939 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
2940 if ( input_buffer_size > 0 )
2941 {
2942 ASSERT_ALLOC( input, input_buffer_size );
2943 memcpy( input, iv->x, iv->len );
2944 memcpy( input + iv->len, input_arg->x, input_arg->len );
2945 }
2946
2947 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
2948 ASSERT_ALLOC( output, output_buffer_size );
2949
2950 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2951 &key ) );
2952
2953 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
2954 output_buffer_size, &output_length ) );
2955 TEST_ASSERT( output_length <=
2956 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
2957 TEST_ASSERT( output_length <=
2958 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
2959
2960 ASSERT_COMPARE( expected_output->x, expected_output->len,
2961 output, output_length );
2962exit:
2963 mbedtls_free( input );
2964 mbedtls_free( output );
2965 psa_destroy_key( key );
2966 PSA_DONE( );
2967}
2968/* END_CASE */
2969
2970/* BEGIN_CASE */
2971void cipher_verify_output( int alg_arg,
2972 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002973 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002974 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002975{
Ronald Cron5425a212020-08-04 14:58:35 +02002976 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002977 psa_key_type_t key_type = key_type_arg;
2978 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002979 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002980 size_t output1_size = 0;
2981 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002982 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002983 size_t output2_size = 0;
2984 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002985 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002986
Gilles Peskine8817f612018-12-18 00:18:46 +01002987 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002988
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002989 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2990 psa_set_key_algorithm( &attributes, alg );
2991 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002992
Ronald Cron5425a212020-08-04 14:58:35 +02002993 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2994 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002995 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002996 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002997
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002998 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
2999 output1, output1_size,
3000 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003001 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003002 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003003 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003004 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003005
3006 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003007 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003008
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003009 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3010 output2, output2_size,
3011 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003012 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003013 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003014 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003015 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003016
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003017 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003018
3019exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003020 mbedtls_free( output1 );
3021 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003022 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003023 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003024}
3025/* END_CASE */
3026
3027/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003028void cipher_verify_output_multipart( int alg_arg,
3029 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003030 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003031 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003032 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003033{
Ronald Cron5425a212020-08-04 14:58:35 +02003034 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003035 psa_key_type_t key_type = key_type_arg;
3036 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003037 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003038 unsigned char iv[16] = {0};
3039 size_t iv_size = 16;
3040 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003041 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003042 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003043 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003044 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003045 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003046 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003047 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003048 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3049 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003050 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003051
Gilles Peskine8817f612018-12-18 00:18:46 +01003052 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003053
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003054 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3055 psa_set_key_algorithm( &attributes, alg );
3056 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003057
Ronald Cron5425a212020-08-04 14:58:35 +02003058 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3059 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003060
Ronald Cron5425a212020-08-04 14:58:35 +02003061 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3062 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003063
Steven Cooreman177deba2020-09-07 17:14:14 +02003064 if( alg != PSA_ALG_ECB_NO_PADDING )
3065 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003066 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3067 iv, iv_size,
3068 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003069 }
3070
gabor-mezei-armceface22021-01-21 12:26:17 +01003071 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3072 TEST_ASSERT( output1_buffer_size <=
3073 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003074 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003075
Gilles Peskinee0866522019-02-19 19:44:00 +01003076 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003077
Gilles Peskine8817f612018-12-18 00:18:46 +01003078 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3079 output1, output1_buffer_size,
3080 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003081 TEST_ASSERT( function_output_length <=
3082 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3083 TEST_ASSERT( function_output_length <=
3084 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003085 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003086
Gilles Peskine8817f612018-12-18 00:18:46 +01003087 PSA_ASSERT( psa_cipher_update( &operation1,
3088 input->x + first_part_size,
3089 input->len - first_part_size,
3090 output1, output1_buffer_size,
3091 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003092 TEST_ASSERT( function_output_length <=
3093 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3094 alg,
3095 input->len - first_part_size ) );
3096 TEST_ASSERT( function_output_length <=
3097 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003098 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003099
Gilles Peskine8817f612018-12-18 00:18:46 +01003100 PSA_ASSERT( psa_cipher_finish( &operation1,
3101 output1 + output1_length,
3102 output1_buffer_size - output1_length,
3103 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003104 TEST_ASSERT( function_output_length <=
3105 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3106 TEST_ASSERT( function_output_length <=
3107 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003108 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003109
Gilles Peskine8817f612018-12-18 00:18:46 +01003110 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003111
Gilles Peskine048b7f02018-06-08 14:20:49 +02003112 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003113 TEST_ASSERT( output2_buffer_size <=
3114 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3115 TEST_ASSERT( output2_buffer_size <=
3116 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003117 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003118
Steven Cooreman177deba2020-09-07 17:14:14 +02003119 if( iv_length > 0 )
3120 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003121 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3122 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003123 }
Moran Pekerded84402018-06-06 16:36:50 +03003124
Gilles Peskine8817f612018-12-18 00:18:46 +01003125 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3126 output2, output2_buffer_size,
3127 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003128 TEST_ASSERT( function_output_length <=
3129 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3130 TEST_ASSERT( function_output_length <=
3131 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003132 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003133
Gilles Peskine8817f612018-12-18 00:18:46 +01003134 PSA_ASSERT( psa_cipher_update( &operation2,
3135 output1 + first_part_size,
3136 output1_length - first_part_size,
3137 output2, output2_buffer_size,
3138 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003139 TEST_ASSERT( function_output_length <=
3140 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3141 alg,
3142 output1_length - first_part_size ) );
3143 TEST_ASSERT( function_output_length <=
3144 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003145 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003146
Gilles Peskine8817f612018-12-18 00:18:46 +01003147 PSA_ASSERT( psa_cipher_finish( &operation2,
3148 output2 + output2_length,
3149 output2_buffer_size - output2_length,
3150 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003151 TEST_ASSERT( function_output_length <=
3152 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3153 TEST_ASSERT( function_output_length <=
3154 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003155 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003156
Gilles Peskine8817f612018-12-18 00:18:46 +01003157 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003158
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003159 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003160
3161exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003162 psa_cipher_abort( &operation1 );
3163 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003164 mbedtls_free( output1 );
3165 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003166 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003167 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003168}
3169/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003170
Gilles Peskine20035e32018-02-03 22:44:14 +01003171/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003172void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003173 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003174 data_t *nonce,
3175 data_t *additional_data,
3176 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003177 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003178{
Ronald Cron5425a212020-08-04 14:58:35 +02003179 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003180 psa_key_type_t key_type = key_type_arg;
3181 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003182 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003183 unsigned char *output_data = NULL;
3184 size_t output_size = 0;
3185 size_t output_length = 0;
3186 unsigned char *output_data2 = NULL;
3187 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003188 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003189 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003190 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003191
Gilles Peskine8817f612018-12-18 00:18:46 +01003192 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003193
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003194 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3195 psa_set_key_algorithm( &attributes, alg );
3196 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003197
Gilles Peskine049c7532019-05-15 20:22:09 +02003198 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003199 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003200 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3201 key_bits = psa_get_key_bits( &attributes );
3202
3203 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3204 alg );
3205 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3206 * should be exact. */
3207 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3208 expected_result != PSA_ERROR_NOT_SUPPORTED )
3209 {
3210 TEST_EQUAL( output_size,
3211 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3212 TEST_ASSERT( output_size <=
3213 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3214 }
3215 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003216
Steven Cooremanf49478b2021-02-15 15:19:25 +01003217 status = psa_aead_encrypt( key, alg,
3218 nonce->x, nonce->len,
3219 additional_data->x,
3220 additional_data->len,
3221 input_data->x, input_data->len,
3222 output_data, output_size,
3223 &output_length );
3224
3225 /* If the operation is not supported, just skip and not fail in case the
3226 * encryption involves a common limitation of cryptography hardwares and
3227 * an alternative implementation. */
3228 if( status == PSA_ERROR_NOT_SUPPORTED )
3229 {
3230 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3231 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3232 }
3233
3234 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003235
3236 if( PSA_SUCCESS == expected_result )
3237 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003238 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003239
Gilles Peskine003a4a92019-05-14 16:09:40 +02003240 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3241 * should be exact. */
3242 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003243 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003244
gabor-mezei-armceface22021-01-21 12:26:17 +01003245 TEST_ASSERT( input_data->len <=
3246 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3247
Ronald Cron5425a212020-08-04 14:58:35 +02003248 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003249 nonce->x, nonce->len,
3250 additional_data->x,
3251 additional_data->len,
3252 output_data, output_length,
3253 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003254 &output_length2 ),
3255 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003256
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003257 ASSERT_COMPARE( input_data->x, input_data->len,
3258 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003259 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003260
Gilles Peskinea1cac842018-06-11 19:33:02 +02003261exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003262 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003263 mbedtls_free( output_data );
3264 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003265 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003266}
3267/* END_CASE */
3268
3269/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003270void aead_encrypt( int key_type_arg, data_t *key_data,
3271 int alg_arg,
3272 data_t *nonce,
3273 data_t *additional_data,
3274 data_t *input_data,
3275 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003276{
Ronald Cron5425a212020-08-04 14:58:35 +02003277 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003278 psa_key_type_t key_type = key_type_arg;
3279 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003280 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003281 unsigned char *output_data = NULL;
3282 size_t output_size = 0;
3283 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003284 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003285 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003286
Gilles Peskine8817f612018-12-18 00:18:46 +01003287 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003288
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003289 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3290 psa_set_key_algorithm( &attributes, alg );
3291 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003292
Gilles Peskine049c7532019-05-15 20:22:09 +02003293 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003294 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003295 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3296 key_bits = psa_get_key_bits( &attributes );
3297
3298 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3299 alg );
3300 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3301 * should be exact. */
3302 TEST_EQUAL( output_size,
3303 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3304 TEST_ASSERT( output_size <=
3305 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3306 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003307
Steven Cooremand588ea12021-01-11 19:36:04 +01003308 status = psa_aead_encrypt( key, alg,
3309 nonce->x, nonce->len,
3310 additional_data->x, additional_data->len,
3311 input_data->x, input_data->len,
3312 output_data, output_size,
3313 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003314
Ronald Cron28a45ed2021-02-09 20:35:42 +01003315 /* If the operation is not supported, just skip and not fail in case the
3316 * encryption involves a common limitation of cryptography hardwares and
3317 * an alternative implementation. */
3318 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003319 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003320 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3321 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003322 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003323
3324 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003325 ASSERT_COMPARE( expected_result->x, expected_result->len,
3326 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003327
Gilles Peskinea1cac842018-06-11 19:33:02 +02003328exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003329 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003330 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003331 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003332}
3333/* END_CASE */
3334
3335/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003336void aead_decrypt( int key_type_arg, data_t *key_data,
3337 int alg_arg,
3338 data_t *nonce,
3339 data_t *additional_data,
3340 data_t *input_data,
3341 data_t *expected_data,
3342 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003343{
Ronald Cron5425a212020-08-04 14:58:35 +02003344 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003345 psa_key_type_t key_type = key_type_arg;
3346 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003347 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003348 unsigned char *output_data = NULL;
3349 size_t output_size = 0;
3350 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003351 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003352 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003353 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003354
Gilles Peskine8817f612018-12-18 00:18:46 +01003355 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003356
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003357 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3358 psa_set_key_algorithm( &attributes, alg );
3359 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003360
Gilles Peskine049c7532019-05-15 20:22:09 +02003361 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003362 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003363 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3364 key_bits = psa_get_key_bits( &attributes );
3365
3366 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3367 alg );
3368 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3369 expected_result != PSA_ERROR_NOT_SUPPORTED )
3370 {
3371 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3372 * should be exact. */
3373 TEST_EQUAL( output_size,
3374 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3375 TEST_ASSERT( output_size <=
3376 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3377 }
3378 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003379
Steven Cooremand588ea12021-01-11 19:36:04 +01003380 status = psa_aead_decrypt( key, alg,
3381 nonce->x, nonce->len,
3382 additional_data->x,
3383 additional_data->len,
3384 input_data->x, input_data->len,
3385 output_data, output_size,
3386 &output_length );
3387
Ronald Cron28a45ed2021-02-09 20:35:42 +01003388 /* If the operation is not supported, just skip and not fail in case the
3389 * decryption involves a common limitation of cryptography hardwares and
3390 * an alternative implementation. */
3391 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003392 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003393 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3394 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003395 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003396
3397 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003398
Gilles Peskine2d277862018-06-18 15:41:12 +02003399 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003400 ASSERT_COMPARE( expected_data->x, expected_data->len,
3401 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003402
Gilles Peskinea1cac842018-06-11 19:33:02 +02003403exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003404 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003405 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003406 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003407}
3408/* END_CASE */
3409
3410/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003411void signature_size( int type_arg,
3412 int bits,
3413 int alg_arg,
3414 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003415{
3416 psa_key_type_t type = type_arg;
3417 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003418 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003419
Gilles Peskinefe11b722018-12-18 00:24:04 +01003420 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003421
Gilles Peskinee59236f2018-01-27 23:32:46 +01003422exit:
3423 ;
3424}
3425/* END_CASE */
3426
3427/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003428void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3429 int alg_arg, data_t *input_data,
3430 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003431{
Ronald Cron5425a212020-08-04 14:58:35 +02003432 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003433 psa_key_type_t key_type = key_type_arg;
3434 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003435 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003436 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 Peskine20035e32018-02-03 22:44:14 +01003440
Gilles Peskine8817f612018-12-18 00:18:46 +01003441 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003442
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003443 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003444 psa_set_key_algorithm( &attributes, alg );
3445 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003446
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 Peskine20035e32018-02-03 22:44:14 +01003451
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003452 /* 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 Peskinec1bb6c82018-06-18 16:04:39 +02003455 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003456 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 Peskine20035e32018-02-03 22:44:14 +01003459
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003460 /* 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 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003466 ASSERT_COMPARE( output_data->x, output_data->len,
3467 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003468
3469exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003470 /*
3471 * Key attributes may have been returned by psa_get_key_attributes()
3472 * thus reset them as required.
3473 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003474 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003475
Ronald Cron5425a212020-08-04 14:58:35 +02003476 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003477 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003478 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003479}
3480/* END_CASE */
3481
3482/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003483void sign_hash_fail( int key_type_arg, data_t *key_data,
3484 int alg_arg, data_t *input_data,
3485 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003486{
Ronald Cron5425a212020-08-04 14:58:35 +02003487 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003488 psa_key_type_t key_type = key_type_arg;
3489 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003490 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003491 psa_status_t actual_status;
3492 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003493 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003494 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003495 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003496
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003497 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003498
Gilles Peskine8817f612018-12-18 00:18:46 +01003499 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003500
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003501 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003502 psa_set_key_algorithm( &attributes, alg );
3503 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003504
Gilles Peskine049c7532019-05-15 20:22:09 +02003505 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003506 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003507
Ronald Cron5425a212020-08-04 14:58:35 +02003508 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003509 input_data->x, input_data->len,
3510 signature, signature_size,
3511 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003512 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003513 /* The value of *signature_length is unspecified on error, but
3514 * whatever it is, it should be less than signature_size, so that
3515 * if the caller tries to read *signature_length bytes without
3516 * checking the error code then they don't overflow a buffer. */
3517 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003518
3519exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003520 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003521 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003522 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003523 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003524}
3525/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003526
3527/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003528void sign_verify_hash( int key_type_arg, data_t *key_data,
3529 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003530{
Ronald Cron5425a212020-08-04 14:58:35 +02003531 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003532 psa_key_type_t key_type = key_type_arg;
3533 psa_algorithm_t alg = alg_arg;
3534 size_t key_bits;
3535 unsigned char *signature = NULL;
3536 size_t signature_size;
3537 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003538 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003539
Gilles Peskine8817f612018-12-18 00:18:46 +01003540 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003541
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003542 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003543 psa_set_key_algorithm( &attributes, alg );
3544 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003545
Gilles Peskine049c7532019-05-15 20:22:09 +02003546 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003547 &key ) );
3548 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003549 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003550
3551 /* Allocate a buffer which has the size advertized by the
3552 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003553 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003554 key_bits, alg );
3555 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003556 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003557 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003558
3559 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003560 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003561 input_data->x, input_data->len,
3562 signature, signature_size,
3563 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003564 /* Check that the signature length looks sensible. */
3565 TEST_ASSERT( signature_length <= signature_size );
3566 TEST_ASSERT( signature_length > 0 );
3567
3568 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003569 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003570 input_data->x, input_data->len,
3571 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003572
3573 if( input_data->len != 0 )
3574 {
3575 /* Flip a bit in the input and verify that the signature is now
3576 * detected as invalid. Flip a bit at the beginning, not at the end,
3577 * because ECDSA may ignore the last few bits of the input. */
3578 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003579 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003580 input_data->x, input_data->len,
3581 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003582 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003583 }
3584
3585exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003586 /*
3587 * Key attributes may have been returned by psa_get_key_attributes()
3588 * thus reset them as required.
3589 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003590 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003591
Ronald Cron5425a212020-08-04 14:58:35 +02003592 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003593 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003594 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003595}
3596/* END_CASE */
3597
3598/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003599void verify_hash( int key_type_arg, data_t *key_data,
3600 int alg_arg, data_t *hash_data,
3601 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003602{
Ronald Cron5425a212020-08-04 14:58:35 +02003603 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003604 psa_key_type_t key_type = key_type_arg;
3605 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003606 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003607
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003608 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003609
Gilles Peskine8817f612018-12-18 00:18:46 +01003610 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003611
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003612 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003613 psa_set_key_algorithm( &attributes, alg );
3614 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003615
Gilles Peskine049c7532019-05-15 20:22:09 +02003616 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003617 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003618
Ronald Cron5425a212020-08-04 14:58:35 +02003619 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003620 hash_data->x, hash_data->len,
3621 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003622
itayzafrir5c753392018-05-08 11:18:38 +03003623exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003624 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003625 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003626 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003627}
3628/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003629
3630/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003631void verify_hash_fail( int key_type_arg, data_t *key_data,
3632 int alg_arg, data_t *hash_data,
3633 data_t *signature_data,
3634 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003635{
Ronald Cron5425a212020-08-04 14:58:35 +02003636 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003637 psa_key_type_t key_type = key_type_arg;
3638 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003639 psa_status_t actual_status;
3640 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003641 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003642
Gilles Peskine8817f612018-12-18 00:18:46 +01003643 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003644
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003645 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003646 psa_set_key_algorithm( &attributes, alg );
3647 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003648
Gilles Peskine049c7532019-05-15 20:22:09 +02003649 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003650 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003651
Ronald Cron5425a212020-08-04 14:58:35 +02003652 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003653 hash_data->x, hash_data->len,
3654 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003655 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003656
3657exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003658 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003659 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003660 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003661}
3662/* END_CASE */
3663
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003664/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02003665void sign_message_deterministic( int key_type_arg,
3666 data_t *key_data,
3667 int alg_arg,
3668 data_t *input_data,
3669 data_t *output_data )
3670{
3671 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3672 psa_key_type_t key_type = key_type_arg;
3673 psa_algorithm_t alg = alg_arg;
3674 size_t key_bits;
3675 unsigned char *signature = NULL;
3676 size_t signature_size;
3677 size_t signature_length = 0xdeadbeef;
3678 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3679
3680 PSA_ASSERT( psa_crypto_init( ) );
3681
3682 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3683 psa_set_key_algorithm( &attributes, alg );
3684 psa_set_key_type( &attributes, key_type );
3685
3686 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3687 &key ) );
3688 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3689 key_bits = psa_get_key_bits( &attributes );
3690
3691 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3692 TEST_ASSERT( signature_size != 0 );
3693 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3694 ASSERT_ALLOC( signature, signature_size );
3695
3696 PSA_ASSERT( psa_sign_message( key, alg,
3697 input_data->x, input_data->len,
3698 signature, signature_size,
3699 &signature_length ) );
3700
3701 ASSERT_COMPARE( output_data->x, output_data->len,
3702 signature, signature_length );
3703
3704exit:
3705 psa_reset_key_attributes( &attributes );
3706
3707 psa_destroy_key( key );
3708 mbedtls_free( signature );
3709 PSA_DONE( );
3710
3711}
3712/* END_CASE */
3713
3714/* BEGIN_CASE */
3715void sign_message_fail( int key_type_arg,
3716 data_t *key_data,
3717 int alg_arg,
3718 data_t *input_data,
3719 int signature_size_arg,
3720 int expected_status_arg )
3721{
3722 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3723 psa_key_type_t key_type = key_type_arg;
3724 psa_algorithm_t alg = alg_arg;
3725 size_t signature_size = signature_size_arg;
3726 psa_status_t actual_status;
3727 psa_status_t expected_status = expected_status_arg;
3728 unsigned char *signature = NULL;
3729 size_t signature_length = 0xdeadbeef;
3730 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3731
3732 ASSERT_ALLOC( signature, signature_size );
3733
3734 PSA_ASSERT( psa_crypto_init( ) );
3735
3736 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3737 psa_set_key_algorithm( &attributes, alg );
3738 psa_set_key_type( &attributes, key_type );
3739
3740 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3741 &key ) );
3742
3743 actual_status = psa_sign_message( key, alg,
3744 input_data->x, input_data->len,
3745 signature, signature_size,
3746 &signature_length );
3747 TEST_EQUAL( actual_status, expected_status );
3748 /* The value of *signature_length is unspecified on error, but
3749 * whatever it is, it should be less than signature_size, so that
3750 * if the caller tries to read *signature_length bytes without
3751 * checking the error code then they don't overflow a buffer. */
3752 TEST_ASSERT( signature_length <= signature_size );
3753
3754exit:
3755 psa_reset_key_attributes( &attributes );
3756 psa_destroy_key( key );
3757 mbedtls_free( signature );
3758 PSA_DONE( );
3759}
3760/* END_CASE */
3761
3762/* BEGIN_CASE */
3763void sign_verify_message( int key_type_arg,
3764 data_t *key_data,
3765 int alg_arg,
3766 data_t *input_data )
3767{
3768 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3769 psa_key_type_t key_type = key_type_arg;
3770 psa_algorithm_t alg = alg_arg;
3771 size_t key_bits;
3772 unsigned char *signature = NULL;
3773 size_t signature_size;
3774 size_t signature_length = 0xdeadbeef;
3775 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3776
3777 PSA_ASSERT( psa_crypto_init( ) );
3778
3779 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3780 PSA_KEY_USAGE_VERIFY_MESSAGE );
3781 psa_set_key_algorithm( &attributes, alg );
3782 psa_set_key_type( &attributes, key_type );
3783
3784 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3785 &key ) );
3786 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3787 key_bits = psa_get_key_bits( &attributes );
3788
3789 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3790 TEST_ASSERT( signature_size != 0 );
3791 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3792 ASSERT_ALLOC( signature, signature_size );
3793
3794 PSA_ASSERT( psa_sign_message( key, alg,
3795 input_data->x, input_data->len,
3796 signature, signature_size,
3797 &signature_length ) );
3798 TEST_ASSERT( signature_length <= signature_size );
3799 TEST_ASSERT( signature_length > 0 );
3800
3801 PSA_ASSERT( psa_verify_message( key, alg,
3802 input_data->x, input_data->len,
3803 signature, signature_length ) );
3804
3805 if( input_data->len != 0 )
3806 {
3807 /* Flip a bit in the input and verify that the signature is now
3808 * detected as invalid. Flip a bit at the beginning, not at the end,
3809 * because ECDSA may ignore the last few bits of the input. */
3810 input_data->x[0] ^= 1;
3811 TEST_EQUAL( psa_verify_message( key, alg,
3812 input_data->x, input_data->len,
3813 signature, signature_length ),
3814 PSA_ERROR_INVALID_SIGNATURE );
3815 }
3816
3817exit:
3818 psa_reset_key_attributes( &attributes );
3819
3820 psa_destroy_key( key );
3821 mbedtls_free( signature );
3822 PSA_DONE( );
3823}
3824/* END_CASE */
3825
3826/* BEGIN_CASE */
3827void verify_message( int key_type_arg,
3828 data_t *key_data,
3829 int alg_arg,
3830 data_t *input_data,
3831 data_t *signature_data )
3832{
3833 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3834 psa_key_type_t key_type = key_type_arg;
3835 psa_algorithm_t alg = alg_arg;
3836 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3837
3838 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3839
3840 PSA_ASSERT( psa_crypto_init( ) );
3841
3842 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3843 psa_set_key_algorithm( &attributes, alg );
3844 psa_set_key_type( &attributes, key_type );
3845
3846 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3847 &key ) );
3848
3849 PSA_ASSERT( psa_verify_message( key, alg,
3850 input_data->x, input_data->len,
3851 signature_data->x, signature_data->len ) );
3852
3853exit:
3854 psa_reset_key_attributes( &attributes );
3855 psa_destroy_key( key );
3856 PSA_DONE( );
3857}
3858/* END_CASE */
3859
3860/* BEGIN_CASE */
3861void verify_message_fail( int key_type_arg,
3862 data_t *key_data,
3863 int alg_arg,
3864 data_t *hash_data,
3865 data_t *signature_data,
3866 int expected_status_arg )
3867{
3868 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3869 psa_key_type_t key_type = key_type_arg;
3870 psa_algorithm_t alg = alg_arg;
3871 psa_status_t actual_status;
3872 psa_status_t expected_status = expected_status_arg;
3873 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3874
3875 PSA_ASSERT( psa_crypto_init( ) );
3876
3877 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3878 psa_set_key_algorithm( &attributes, alg );
3879 psa_set_key_type( &attributes, key_type );
3880
3881 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3882 &key ) );
3883
3884 actual_status = psa_verify_message( key, alg,
3885 hash_data->x, hash_data->len,
3886 signature_data->x,
3887 signature_data->len );
3888 TEST_EQUAL( actual_status, expected_status );
3889
3890exit:
3891 psa_reset_key_attributes( &attributes );
3892 psa_destroy_key( key );
3893 PSA_DONE( );
3894}
3895/* END_CASE */
3896
3897/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003898void asymmetric_encrypt( int key_type_arg,
3899 data_t *key_data,
3900 int alg_arg,
3901 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003902 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003903 int expected_output_length_arg,
3904 int expected_status_arg )
3905{
Ronald Cron5425a212020-08-04 14:58:35 +02003906 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003907 psa_key_type_t key_type = key_type_arg;
3908 psa_algorithm_t alg = alg_arg;
3909 size_t expected_output_length = expected_output_length_arg;
3910 size_t key_bits;
3911 unsigned char *output = NULL;
3912 size_t output_size;
3913 size_t output_length = ~0;
3914 psa_status_t actual_status;
3915 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003916 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003917
Gilles Peskine8817f612018-12-18 00:18:46 +01003918 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003919
Gilles Peskine656896e2018-06-29 19:12:28 +02003920 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003921 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3922 psa_set_key_algorithm( &attributes, alg );
3923 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003924 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003925 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003926
3927 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003928 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003929 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003930
Gilles Peskine656896e2018-06-29 19:12:28 +02003931 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003932 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003933 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003934
3935 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003936 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003937 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003938 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003939 output, output_size,
3940 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003941 TEST_EQUAL( actual_status, expected_status );
3942 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003943
Gilles Peskine68428122018-06-30 18:42:41 +02003944 /* If the label is empty, the test framework puts a non-null pointer
3945 * in label->x. Test that a null pointer works as well. */
3946 if( label->len == 0 )
3947 {
3948 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003949 if( output_size != 0 )
3950 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003951 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003952 input_data->x, input_data->len,
3953 NULL, label->len,
3954 output, output_size,
3955 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003956 TEST_EQUAL( actual_status, expected_status );
3957 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003958 }
3959
Gilles Peskine656896e2018-06-29 19:12:28 +02003960exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003961 /*
3962 * Key attributes may have been returned by psa_get_key_attributes()
3963 * thus reset them as required.
3964 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003965 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003966
Ronald Cron5425a212020-08-04 14:58:35 +02003967 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02003968 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003969 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003970}
3971/* END_CASE */
3972
3973/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003974void asymmetric_encrypt_decrypt( int key_type_arg,
3975 data_t *key_data,
3976 int alg_arg,
3977 data_t *input_data,
3978 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003979{
Ronald Cron5425a212020-08-04 14:58:35 +02003980 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003981 psa_key_type_t key_type = key_type_arg;
3982 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003983 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003984 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003985 size_t output_size;
3986 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003987 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003988 size_t output2_size;
3989 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003990 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003991
Gilles Peskine8817f612018-12-18 00:18:46 +01003992 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003993
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003994 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3995 psa_set_key_algorithm( &attributes, alg );
3996 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003997
Gilles Peskine049c7532019-05-15 20:22:09 +02003998 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003999 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004000
4001 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004002 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004003 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004004
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004005 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004006 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004007 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01004008
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004009 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01004010 TEST_ASSERT( output2_size <=
4011 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
4012 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004013 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004014
Gilles Peskineeebd7382018-06-08 18:11:54 +02004015 /* We test encryption by checking that encrypt-then-decrypt gives back
4016 * the original plaintext because of the non-optional random
4017 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004018 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004019 input_data->x, input_data->len,
4020 label->x, label->len,
4021 output, output_size,
4022 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004023 /* We don't know what ciphertext length to expect, but check that
4024 * it looks sensible. */
4025 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004026
Ronald Cron5425a212020-08-04 14:58:35 +02004027 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004028 output, output_length,
4029 label->x, label->len,
4030 output2, output2_size,
4031 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004032 ASSERT_COMPARE( input_data->x, input_data->len,
4033 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004034
4035exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004036 /*
4037 * Key attributes may have been returned by psa_get_key_attributes()
4038 * thus reset them as required.
4039 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004040 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004041
Ronald Cron5425a212020-08-04 14:58:35 +02004042 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004043 mbedtls_free( output );
4044 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004045 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004046}
4047/* END_CASE */
4048
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004049/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004050void asymmetric_decrypt( int key_type_arg,
4051 data_t *key_data,
4052 int alg_arg,
4053 data_t *input_data,
4054 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004055 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004056{
Ronald Cron5425a212020-08-04 14:58:35 +02004057 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004058 psa_key_type_t key_type = key_type_arg;
4059 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004060 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004061 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004062 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004063 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004064 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004065
Gilles Peskine8817f612018-12-18 00:18:46 +01004066 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004067
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004068 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4069 psa_set_key_algorithm( &attributes, alg );
4070 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004071
Gilles Peskine049c7532019-05-15 20:22:09 +02004072 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004073 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004074
gabor-mezei-armceface22021-01-21 12:26:17 +01004075 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4076 key_bits = psa_get_key_bits( &attributes );
4077
4078 /* Determine the maximum ciphertext length */
4079 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
4080 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
4081 ASSERT_ALLOC( output, output_size );
4082
Ronald Cron5425a212020-08-04 14:58:35 +02004083 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004084 input_data->x, input_data->len,
4085 label->x, label->len,
4086 output,
4087 output_size,
4088 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004089 ASSERT_COMPARE( expected_data->x, expected_data->len,
4090 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004091
Gilles Peskine68428122018-06-30 18:42:41 +02004092 /* If the label is empty, the test framework puts a non-null pointer
4093 * in label->x. Test that a null pointer works as well. */
4094 if( label->len == 0 )
4095 {
4096 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004097 if( output_size != 0 )
4098 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004099 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004100 input_data->x, input_data->len,
4101 NULL, label->len,
4102 output,
4103 output_size,
4104 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004105 ASSERT_COMPARE( expected_data->x, expected_data->len,
4106 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004107 }
4108
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004109exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004110 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004111 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004112 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004113 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004114}
4115/* END_CASE */
4116
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004117/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004118void asymmetric_decrypt_fail( int key_type_arg,
4119 data_t *key_data,
4120 int alg_arg,
4121 data_t *input_data,
4122 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004123 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004124 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004125{
Ronald Cron5425a212020-08-04 14:58:35 +02004126 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004127 psa_key_type_t key_type = key_type_arg;
4128 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004129 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004130 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004131 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004132 psa_status_t actual_status;
4133 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004134 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004135
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004136 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004137
Gilles Peskine8817f612018-12-18 00:18:46 +01004138 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004139
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004140 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4141 psa_set_key_algorithm( &attributes, alg );
4142 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004143
Gilles Peskine049c7532019-05-15 20:22:09 +02004144 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004145 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004146
Ronald Cron5425a212020-08-04 14:58:35 +02004147 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004148 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004149 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004150 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004151 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004152 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004153 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004154
Gilles Peskine68428122018-06-30 18:42:41 +02004155 /* If the label is empty, the test framework puts a non-null pointer
4156 * in label->x. Test that a null pointer works as well. */
4157 if( label->len == 0 )
4158 {
4159 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004160 if( output_size != 0 )
4161 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004162 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004163 input_data->x, input_data->len,
4164 NULL, label->len,
4165 output, output_size,
4166 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004167 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004168 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004169 }
4170
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004171exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004172 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004173 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004174 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004175 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004176}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004177/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004178
4179/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004180void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004181{
4182 /* Test each valid way of initializing the object, except for `= {0}`, as
4183 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4184 * though it's OK by the C standard. We could test for this, but we'd need
4185 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004186 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004187 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4188 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4189 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004190
4191 memset( &zero, 0, sizeof( zero ) );
4192
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004193 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004194 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004195 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004196 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004197 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004198 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004199 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004200
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004201 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004202 PSA_ASSERT( psa_key_derivation_abort(&func) );
4203 PSA_ASSERT( psa_key_derivation_abort(&init) );
4204 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004205}
4206/* END_CASE */
4207
Janos Follath16de4a42019-06-13 16:32:24 +01004208/* BEGIN_CASE */
4209void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004210{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004211 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004212 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004213 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004214
Gilles Peskine8817f612018-12-18 00:18:46 +01004215 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004216
Janos Follath16de4a42019-06-13 16:32:24 +01004217 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004218 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004219
4220exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004221 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004222 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004223}
4224/* END_CASE */
4225
Janos Follathaf3c2a02019-06-12 12:34:34 +01004226/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004227void derive_set_capacity( int alg_arg, int capacity_arg,
4228 int expected_status_arg )
4229{
4230 psa_algorithm_t alg = alg_arg;
4231 size_t capacity = capacity_arg;
4232 psa_status_t expected_status = expected_status_arg;
4233 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4234
4235 PSA_ASSERT( psa_crypto_init( ) );
4236
4237 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4238
4239 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4240 expected_status );
4241
4242exit:
4243 psa_key_derivation_abort( &operation );
4244 PSA_DONE( );
4245}
4246/* END_CASE */
4247
4248/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004249void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004250 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004251 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004252 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004253 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004254 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004255 int expected_status_arg3,
4256 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004257{
4258 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004259 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4260 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004261 psa_status_t expected_statuses[] = {expected_status_arg1,
4262 expected_status_arg2,
4263 expected_status_arg3};
4264 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004265 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4266 MBEDTLS_SVC_KEY_ID_INIT,
4267 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004268 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4269 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4270 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004271 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004272 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004273 psa_status_t expected_output_status = expected_output_status_arg;
4274 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004275
4276 PSA_ASSERT( psa_crypto_init( ) );
4277
4278 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4279 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004280
4281 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4282
4283 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4284 {
Gilles Peskine4023c012021-05-27 13:21:20 +02004285 mbedtls_test_set_step( i );
4286 if( steps[i] == 0 )
4287 {
4288 /* Skip this step */
4289 }
4290 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004291 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004292 psa_set_key_type( &attributes, key_types[i] );
4293 PSA_ASSERT( psa_import_key( &attributes,
4294 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004295 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004296 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4297 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4298 {
4299 // When taking a private key as secret input, use key agreement
4300 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004301 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4302 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004303 expected_statuses[i] );
4304 }
4305 else
4306 {
4307 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004308 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004309 expected_statuses[i] );
4310 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004311 }
4312 else
4313 {
4314 TEST_EQUAL( psa_key_derivation_input_bytes(
4315 &operation, steps[i],
4316 inputs[i]->x, inputs[i]->len ),
4317 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004318 }
4319 }
4320
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004321 if( output_key_type != PSA_KEY_TYPE_NONE )
4322 {
4323 psa_reset_key_attributes( &attributes );
4324 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4325 psa_set_key_bits( &attributes, 8 );
4326 actual_output_status =
4327 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004328 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004329 }
4330 else
4331 {
4332 uint8_t buffer[1];
4333 actual_output_status =
4334 psa_key_derivation_output_bytes( &operation,
4335 buffer, sizeof( buffer ) );
4336 }
4337 TEST_EQUAL( actual_output_status, expected_output_status );
4338
Janos Follathaf3c2a02019-06-12 12:34:34 +01004339exit:
4340 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004341 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4342 psa_destroy_key( keys[i] );
4343 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004344 PSA_DONE( );
4345}
4346/* END_CASE */
4347
Janos Follathd958bb72019-07-03 15:02:16 +01004348/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02004349void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004350{
Janos Follathd958bb72019-07-03 15:02:16 +01004351 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004352 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004353 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004354 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004355 unsigned char input1[] = "Input 1";
4356 size_t input1_length = sizeof( input1 );
4357 unsigned char input2[] = "Input 2";
4358 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004359 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004360 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004361 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4362 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4363 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004364 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004365
Gilles Peskine8817f612018-12-18 00:18:46 +01004366 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004367
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004368 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4369 psa_set_key_algorithm( &attributes, alg );
4370 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004371
Gilles Peskine73676cb2019-05-15 20:15:10 +02004372 PSA_ASSERT( psa_import_key( &attributes,
4373 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004374 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004375
4376 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004377 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4378 input1, input1_length,
4379 input2, input2_length,
4380 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004381 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004382
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004383 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004384 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004385 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004386
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004387 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004388
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004389 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004390 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004391
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004392exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004393 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004394 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004395 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004396}
4397/* END_CASE */
4398
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004399/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02004400void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004401{
4402 uint8_t output_buffer[16];
4403 size_t buffer_size = 16;
4404 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004405 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004406
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004407 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4408 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004409 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004410
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004411 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004412 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004413
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004414 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004415
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004416 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4417 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004418 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004419
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004420 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004421 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004422
4423exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004424 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004425}
4426/* END_CASE */
4427
4428/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004429void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004430 int step1_arg, data_t *input1,
4431 int step2_arg, data_t *input2,
4432 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004433 int requested_capacity_arg,
4434 data_t *expected_output1,
4435 data_t *expected_output2 )
4436{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004437 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004438 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4439 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004440 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4441 MBEDTLS_SVC_KEY_ID_INIT,
4442 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004443 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004444 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004445 uint8_t *expected_outputs[2] =
4446 {expected_output1->x, expected_output2->x};
4447 size_t output_sizes[2] =
4448 {expected_output1->len, expected_output2->len};
4449 size_t output_buffer_size = 0;
4450 uint8_t *output_buffer = NULL;
4451 size_t expected_capacity;
4452 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004453 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004454 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004455 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004456
4457 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4458 {
4459 if( output_sizes[i] > output_buffer_size )
4460 output_buffer_size = output_sizes[i];
4461 if( output_sizes[i] == 0 )
4462 expected_outputs[i] = NULL;
4463 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004464 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004465 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004466
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004467 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4468 psa_set_key_algorithm( &attributes, alg );
4469 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004470
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004471 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004472 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4473 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4474 requested_capacity ) );
4475 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004476 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004477 switch( steps[i] )
4478 {
4479 case 0:
4480 break;
4481 case PSA_KEY_DERIVATION_INPUT_SECRET:
4482 PSA_ASSERT( psa_import_key( &attributes,
4483 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004484 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004485
4486 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4487 {
4488 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4489 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4490 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4491 }
4492
Gilles Peskine1468da72019-05-29 17:35:49 +02004493 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004494 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004495 break;
4496 default:
4497 PSA_ASSERT( psa_key_derivation_input_bytes(
4498 &operation, steps[i],
4499 inputs[i]->x, inputs[i]->len ) );
4500 break;
4501 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004502 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004503
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004504 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004505 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004506 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004507 expected_capacity = requested_capacity;
4508
4509 /* Expansion phase. */
4510 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4511 {
4512 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004513 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004514 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004515 if( expected_capacity == 0 && output_sizes[i] == 0 )
4516 {
4517 /* Reading 0 bytes when 0 bytes are available can go either way. */
4518 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004519 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004520 continue;
4521 }
4522 else if( expected_capacity == 0 ||
4523 output_sizes[i] > expected_capacity )
4524 {
4525 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004526 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004527 expected_capacity = 0;
4528 continue;
4529 }
4530 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004531 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004532 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004533 ASSERT_COMPARE( output_buffer, output_sizes[i],
4534 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004535 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004536 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004537 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004538 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004539 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004540 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004541 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004542
4543exit:
4544 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004545 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004546 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4547 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004548 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004549}
4550/* END_CASE */
4551
4552/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004553void derive_full( int alg_arg,
4554 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004555 data_t *input1,
4556 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004557 int requested_capacity_arg )
4558{
Ronald Cron5425a212020-08-04 14:58:35 +02004559 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004560 psa_algorithm_t alg = alg_arg;
4561 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004562 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004563 unsigned char output_buffer[16];
4564 size_t expected_capacity = requested_capacity;
4565 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004566 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004567
Gilles Peskine8817f612018-12-18 00:18:46 +01004568 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004569
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004570 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4571 psa_set_key_algorithm( &attributes, alg );
4572 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004573
Gilles Peskine049c7532019-05-15 20:22:09 +02004574 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004575 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004576
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004577 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4578 input1->x, input1->len,
4579 input2->x, input2->len,
4580 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004581 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004582
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004583 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004584 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004585 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004586
4587 /* Expansion phase. */
4588 while( current_capacity > 0 )
4589 {
4590 size_t read_size = sizeof( output_buffer );
4591 if( read_size > current_capacity )
4592 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004593 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004594 output_buffer,
4595 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004596 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004597 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004598 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004599 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004600 }
4601
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004602 /* Check that the operation refuses to go over capacity. */
4603 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004604 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004605
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004606 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004607
4608exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004609 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004610 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004611 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004612}
4613/* END_CASE */
4614
Janos Follathe60c9052019-07-03 13:51:30 +01004615/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004616void derive_key_exercise( int alg_arg,
4617 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004618 data_t *input1,
4619 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004620 int derived_type_arg,
4621 int derived_bits_arg,
4622 int derived_usage_arg,
4623 int derived_alg_arg )
4624{
Ronald Cron5425a212020-08-04 14:58:35 +02004625 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4626 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004627 psa_algorithm_t alg = alg_arg;
4628 psa_key_type_t derived_type = derived_type_arg;
4629 size_t derived_bits = derived_bits_arg;
4630 psa_key_usage_t derived_usage = derived_usage_arg;
4631 psa_algorithm_t derived_alg = derived_alg_arg;
4632 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004633 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004634 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004635 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004636
Gilles Peskine8817f612018-12-18 00:18:46 +01004637 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004638
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004639 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4640 psa_set_key_algorithm( &attributes, alg );
4641 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004642 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004643 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004644
4645 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004646 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4647 input1->x, input1->len,
4648 input2->x, input2->len,
4649 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004650 goto exit;
4651
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004652 psa_set_key_usage_flags( &attributes, derived_usage );
4653 psa_set_key_algorithm( &attributes, derived_alg );
4654 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004655 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004656 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004657 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004658
4659 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004660 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004661 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4662 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004663
4664 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004665 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004666 goto exit;
4667
4668exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004669 /*
4670 * Key attributes may have been returned by psa_get_key_attributes()
4671 * thus reset them as required.
4672 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004673 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004674
4675 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004676 psa_destroy_key( base_key );
4677 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004678 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004679}
4680/* END_CASE */
4681
Janos Follath42fd8882019-07-03 14:17:09 +01004682/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004683void derive_key_export( int alg_arg,
4684 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004685 data_t *input1,
4686 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004687 int bytes1_arg,
4688 int bytes2_arg )
4689{
Ronald Cron5425a212020-08-04 14:58:35 +02004690 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4691 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004692 psa_algorithm_t alg = alg_arg;
4693 size_t bytes1 = bytes1_arg;
4694 size_t bytes2 = bytes2_arg;
4695 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004696 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004697 uint8_t *output_buffer = NULL;
4698 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004699 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4700 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004701 size_t length;
4702
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004703 ASSERT_ALLOC( output_buffer, capacity );
4704 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004705 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004706
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004707 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4708 psa_set_key_algorithm( &base_attributes, alg );
4709 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004710 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004711 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004712
4713 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004714 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4715 input1->x, input1->len,
4716 input2->x, input2->len,
4717 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004718 goto exit;
4719
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004720 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004721 output_buffer,
4722 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004723 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004724
4725 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004726 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4727 input1->x, input1->len,
4728 input2->x, input2->len,
4729 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004730 goto exit;
4731
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004732 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4733 psa_set_key_algorithm( &derived_attributes, 0 );
4734 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004735 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004736 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004737 &derived_key ) );
4738 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004739 export_buffer, bytes1,
4740 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004741 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004742 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004743 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004744 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004745 &derived_key ) );
4746 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004747 export_buffer + bytes1, bytes2,
4748 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004749 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004750
4751 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004752 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4753 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004754
4755exit:
4756 mbedtls_free( output_buffer );
4757 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004758 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004759 psa_destroy_key( base_key );
4760 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004761 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004762}
4763/* END_CASE */
4764
4765/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004766void derive_key( int alg_arg,
4767 data_t *key_data, data_t *input1, data_t *input2,
4768 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004769 int expected_status_arg,
4770 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004771{
Ronald Cron5425a212020-08-04 14:58:35 +02004772 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4773 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004774 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004775 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004776 size_t bits = bits_arg;
4777 psa_status_t expected_status = expected_status_arg;
4778 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4779 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4780 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4781
4782 PSA_ASSERT( psa_crypto_init( ) );
4783
4784 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4785 psa_set_key_algorithm( &base_attributes, alg );
4786 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4787 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004788 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004789
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004790 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4791 input1->x, input1->len,
4792 input2->x, input2->len,
4793 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004794 goto exit;
4795
4796 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4797 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004798 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004799 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004800
4801 psa_status_t status =
4802 psa_key_derivation_output_key( &derived_attributes,
4803 &operation,
4804 &derived_key );
4805 if( is_large_output > 0 )
4806 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4807 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004808
4809exit:
4810 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004811 psa_destroy_key( base_key );
4812 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004813 PSA_DONE( );
4814}
4815/* END_CASE */
4816
4817/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004818void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004819 int our_key_type_arg, int our_key_alg_arg,
4820 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004821 int expected_status_arg )
4822{
Ronald Cron5425a212020-08-04 14:58:35 +02004823 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004824 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004825 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004826 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004827 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004828 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004829 psa_status_t expected_status = expected_status_arg;
4830 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004831
Gilles Peskine8817f612018-12-18 00:18:46 +01004832 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004833
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004834 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004835 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004836 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004837 PSA_ASSERT( psa_import_key( &attributes,
4838 our_key_data->x, our_key_data->len,
4839 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004840
Gilles Peskine77f40d82019-04-11 21:27:06 +02004841 /* The tests currently include inputs that should fail at either step.
4842 * Test cases that fail at the setup step should be changed to call
4843 * key_derivation_setup instead, and this function should be renamed
4844 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004845 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004846 if( status == PSA_SUCCESS )
4847 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004848 TEST_EQUAL( psa_key_derivation_key_agreement(
4849 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4850 our_key,
4851 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004852 expected_status );
4853 }
4854 else
4855 {
4856 TEST_ASSERT( status == expected_status );
4857 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004858
4859exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004860 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004861 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004862 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004863}
4864/* END_CASE */
4865
4866/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004867void raw_key_agreement( int alg_arg,
4868 int our_key_type_arg, data_t *our_key_data,
4869 data_t *peer_key_data,
4870 data_t *expected_output )
4871{
Ronald Cron5425a212020-08-04 14:58:35 +02004872 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004873 psa_algorithm_t alg = alg_arg;
4874 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004875 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004876 unsigned char *output = NULL;
4877 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004878 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004879
4880 ASSERT_ALLOC( output, expected_output->len );
4881 PSA_ASSERT( psa_crypto_init( ) );
4882
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004883 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4884 psa_set_key_algorithm( &attributes, alg );
4885 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004886 PSA_ASSERT( psa_import_key( &attributes,
4887 our_key_data->x, our_key_data->len,
4888 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004889
gabor-mezei-armceface22021-01-21 12:26:17 +01004890 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4891 key_bits = psa_get_key_bits( &attributes );
4892
Gilles Peskinebe697d82019-05-16 18:00:41 +02004893 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4894 peer_key_data->x, peer_key_data->len,
4895 output, expected_output->len,
4896 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004897 ASSERT_COMPARE( output, output_length,
4898 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01004899 TEST_ASSERT( output_length <=
4900 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
4901 TEST_ASSERT( output_length <=
4902 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004903
4904exit:
4905 mbedtls_free( output );
4906 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004907 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004908}
4909/* END_CASE */
4910
4911/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004912void key_agreement_capacity( int alg_arg,
4913 int our_key_type_arg, data_t *our_key_data,
4914 data_t *peer_key_data,
4915 int expected_capacity_arg )
4916{
Ronald Cron5425a212020-08-04 14:58:35 +02004917 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004918 psa_algorithm_t alg = alg_arg;
4919 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004920 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004921 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004922 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004923 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004924
Gilles Peskine8817f612018-12-18 00:18:46 +01004925 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004926
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004927 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4928 psa_set_key_algorithm( &attributes, alg );
4929 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004930 PSA_ASSERT( psa_import_key( &attributes,
4931 our_key_data->x, our_key_data->len,
4932 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004933
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004934 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004935 PSA_ASSERT( psa_key_derivation_key_agreement(
4936 &operation,
4937 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4938 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004939 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4940 {
4941 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004942 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004943 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004944 NULL, 0 ) );
4945 }
Gilles Peskine59685592018-09-18 12:11:34 +02004946
Gilles Peskinebf491972018-10-25 22:36:12 +02004947 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004948 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004949 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004950 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004951
Gilles Peskinebf491972018-10-25 22:36:12 +02004952 /* Test the actual capacity by reading the output. */
4953 while( actual_capacity > sizeof( output ) )
4954 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004955 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004956 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004957 actual_capacity -= sizeof( output );
4958 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004959 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004960 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004961 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004962 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004963
Gilles Peskine59685592018-09-18 12:11:34 +02004964exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004965 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004966 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004967 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004968}
4969/* END_CASE */
4970
4971/* BEGIN_CASE */
4972void key_agreement_output( int alg_arg,
4973 int our_key_type_arg, data_t *our_key_data,
4974 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004975 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004976{
Ronald Cron5425a212020-08-04 14:58:35 +02004977 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004978 psa_algorithm_t alg = alg_arg;
4979 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004980 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004981 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004982 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004983
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004984 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4985 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004986
Gilles Peskine8817f612018-12-18 00:18:46 +01004987 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004988
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004989 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4990 psa_set_key_algorithm( &attributes, alg );
4991 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004992 PSA_ASSERT( psa_import_key( &attributes,
4993 our_key_data->x, our_key_data->len,
4994 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004995
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004996 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004997 PSA_ASSERT( psa_key_derivation_key_agreement(
4998 &operation,
4999 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5000 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005001 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5002 {
5003 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005004 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005005 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005006 NULL, 0 ) );
5007 }
Gilles Peskine59685592018-09-18 12:11:34 +02005008
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005009 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005010 actual_output,
5011 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005012 ASSERT_COMPARE( actual_output, expected_output1->len,
5013 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005014 if( expected_output2->len != 0 )
5015 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005016 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005017 actual_output,
5018 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005019 ASSERT_COMPARE( actual_output, expected_output2->len,
5020 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005021 }
Gilles Peskine59685592018-09-18 12:11:34 +02005022
5023exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005024 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005025 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005026 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005027 mbedtls_free( actual_output );
5028}
5029/* END_CASE */
5030
5031/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005032void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005033{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005034 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005035 unsigned char *output = NULL;
5036 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005037 size_t i;
5038 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005039
Simon Butcher49f8e312020-03-03 15:51:50 +00005040 TEST_ASSERT( bytes_arg >= 0 );
5041
Gilles Peskine91892022021-02-08 19:50:26 +01005042 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005043 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005044
Gilles Peskine8817f612018-12-18 00:18:46 +01005045 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005046
Gilles Peskinea50d7392018-06-21 10:22:13 +02005047 /* Run several times, to ensure that every output byte will be
5048 * nonzero at least once with overwhelming probability
5049 * (2^(-8*number_of_runs)). */
5050 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005051 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005052 if( bytes != 0 )
5053 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005054 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005055
Gilles Peskinea50d7392018-06-21 10:22:13 +02005056 for( i = 0; i < bytes; i++ )
5057 {
5058 if( output[i] != 0 )
5059 ++changed[i];
5060 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005061 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005062
5063 /* Check that every byte was changed to nonzero at least once. This
5064 * validates that psa_generate_random is overwriting every byte of
5065 * the output buffer. */
5066 for( i = 0; i < bytes; i++ )
5067 {
5068 TEST_ASSERT( changed[i] != 0 );
5069 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005070
5071exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005072 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005073 mbedtls_free( output );
5074 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005075}
5076/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005077
5078/* BEGIN_CASE */
5079void generate_key( int type_arg,
5080 int bits_arg,
5081 int usage_arg,
5082 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005083 int expected_status_arg,
5084 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005085{
Ronald Cron5425a212020-08-04 14:58:35 +02005086 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005087 psa_key_type_t type = type_arg;
5088 psa_key_usage_t usage = usage_arg;
5089 size_t bits = bits_arg;
5090 psa_algorithm_t alg = alg_arg;
5091 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005092 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005093 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005094
Gilles Peskine8817f612018-12-18 00:18:46 +01005095 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005096
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005097 psa_set_key_usage_flags( &attributes, usage );
5098 psa_set_key_algorithm( &attributes, alg );
5099 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005100 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005101
5102 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005103 psa_status_t status = psa_generate_key( &attributes, &key );
5104
5105 if( is_large_key > 0 )
5106 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5107 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005108 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005109 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005110
5111 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005112 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005113 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5114 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005115
Gilles Peskine818ca122018-06-20 18:16:48 +02005116 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005117 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005118 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005119
5120exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005121 /*
5122 * Key attributes may have been returned by psa_get_key_attributes()
5123 * thus reset them as required.
5124 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005125 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005126
Ronald Cron5425a212020-08-04 14:58:35 +02005127 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005128 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005129}
5130/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005131
Ronald Cronee414c72021-03-18 18:50:08 +01005132/* 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 +02005133void generate_key_rsa( int bits_arg,
5134 data_t *e_arg,
5135 int expected_status_arg )
5136{
Ronald Cron5425a212020-08-04 14:58:35 +02005137 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005138 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005139 size_t bits = bits_arg;
5140 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5141 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5142 psa_status_t expected_status = expected_status_arg;
5143 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5144 uint8_t *exported = NULL;
5145 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005146 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005147 size_t exported_length = SIZE_MAX;
5148 uint8_t *e_read_buffer = NULL;
5149 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005150 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005151 size_t e_read_length = SIZE_MAX;
5152
5153 if( e_arg->len == 0 ||
5154 ( e_arg->len == 3 &&
5155 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5156 {
5157 is_default_public_exponent = 1;
5158 e_read_size = 0;
5159 }
5160 ASSERT_ALLOC( e_read_buffer, e_read_size );
5161 ASSERT_ALLOC( exported, exported_size );
5162
5163 PSA_ASSERT( psa_crypto_init( ) );
5164
5165 psa_set_key_usage_flags( &attributes, usage );
5166 psa_set_key_algorithm( &attributes, alg );
5167 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5168 e_arg->x, e_arg->len ) );
5169 psa_set_key_bits( &attributes, bits );
5170
5171 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005172 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005173 if( expected_status != PSA_SUCCESS )
5174 goto exit;
5175
5176 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005177 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005178 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5179 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5180 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5181 e_read_buffer, e_read_size,
5182 &e_read_length ) );
5183 if( is_default_public_exponent )
5184 TEST_EQUAL( e_read_length, 0 );
5185 else
5186 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5187
5188 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005189 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005190 goto exit;
5191
5192 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005193 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005194 exported, exported_size,
5195 &exported_length ) );
5196 {
5197 uint8_t *p = exported;
5198 uint8_t *end = exported + exported_length;
5199 size_t len;
5200 /* RSAPublicKey ::= SEQUENCE {
5201 * modulus INTEGER, -- n
5202 * publicExponent INTEGER } -- e
5203 */
5204 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005205 MBEDTLS_ASN1_SEQUENCE |
5206 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005207 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005208 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5209 MBEDTLS_ASN1_INTEGER ) );
5210 if( len >= 1 && p[0] == 0 )
5211 {
5212 ++p;
5213 --len;
5214 }
5215 if( e_arg->len == 0 )
5216 {
5217 TEST_EQUAL( len, 3 );
5218 TEST_EQUAL( p[0], 1 );
5219 TEST_EQUAL( p[1], 0 );
5220 TEST_EQUAL( p[2], 1 );
5221 }
5222 else
5223 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5224 }
5225
5226exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005227 /*
5228 * Key attributes may have been returned by psa_get_key_attributes() or
5229 * set by psa_set_key_domain_parameters() thus reset them as required.
5230 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005231 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005232
Ronald Cron5425a212020-08-04 14:58:35 +02005233 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005234 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005235 mbedtls_free( e_read_buffer );
5236 mbedtls_free( exported );
5237}
5238/* END_CASE */
5239
Darryl Greend49a4992018-06-18 17:27:26 +01005240/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005241void persistent_key_load_key_from_storage( data_t *data,
5242 int type_arg, int bits_arg,
5243 int usage_flags_arg, int alg_arg,
5244 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005245{
Ronald Cron71016a92020-08-28 19:01:50 +02005246 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005247 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005248 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5249 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005250 psa_key_type_t type = type_arg;
5251 size_t bits = bits_arg;
5252 psa_key_usage_t usage_flags = usage_flags_arg;
5253 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005254 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005255 unsigned char *first_export = NULL;
5256 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005257 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005258 size_t first_exported_length;
5259 size_t second_exported_length;
5260
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005261 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5262 {
5263 ASSERT_ALLOC( first_export, export_size );
5264 ASSERT_ALLOC( second_export, export_size );
5265 }
Darryl Greend49a4992018-06-18 17:27:26 +01005266
Gilles Peskine8817f612018-12-18 00:18:46 +01005267 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005268
Gilles Peskinec87af662019-05-15 16:12:22 +02005269 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005270 psa_set_key_usage_flags( &attributes, usage_flags );
5271 psa_set_key_algorithm( &attributes, alg );
5272 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005273 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005274
Darryl Green0c6575a2018-11-07 16:05:30 +00005275 switch( generation_method )
5276 {
5277 case IMPORT_KEY:
5278 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005279 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005280 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005281 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005282
Darryl Green0c6575a2018-11-07 16:05:30 +00005283 case GENERATE_KEY:
5284 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005285 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005286 break;
5287
5288 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005289#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005290 {
5291 /* Create base key */
5292 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5293 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5294 psa_set_key_usage_flags( &base_attributes,
5295 PSA_KEY_USAGE_DERIVE );
5296 psa_set_key_algorithm( &base_attributes, derive_alg );
5297 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005298 PSA_ASSERT( psa_import_key( &base_attributes,
5299 data->x, data->len,
5300 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005301 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005302 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005303 PSA_ASSERT( psa_key_derivation_input_key(
5304 &operation,
5305 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005306 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005307 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005308 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005309 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5310 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005311 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005312 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005313 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005314 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005315 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005316#else
5317 TEST_ASSUME( ! "KDF not supported in this configuration" );
5318#endif
5319 break;
5320
5321 default:
5322 TEST_ASSERT( ! "generation_method not implemented in test" );
5323 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005324 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005325 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005326
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005327 /* Export the key if permitted by the key policy. */
5328 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5329 {
Ronald Cron5425a212020-08-04 14:58:35 +02005330 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005331 first_export, export_size,
5332 &first_exported_length ) );
5333 if( generation_method == IMPORT_KEY )
5334 ASSERT_COMPARE( data->x, data->len,
5335 first_export, first_exported_length );
5336 }
Darryl Greend49a4992018-06-18 17:27:26 +01005337
5338 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005339 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005340 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005341 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005342
Darryl Greend49a4992018-06-18 17:27:26 +01005343 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005344 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005345 TEST_ASSERT( mbedtls_svc_key_id_equal(
5346 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005347 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5348 PSA_KEY_LIFETIME_PERSISTENT );
5349 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5350 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02005351 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02005352 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005353 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005354
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005355 /* Export the key again if permitted by the key policy. */
5356 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005357 {
Ronald Cron5425a212020-08-04 14:58:35 +02005358 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005359 second_export, export_size,
5360 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005361 ASSERT_COMPARE( first_export, first_exported_length,
5362 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005363 }
5364
5365 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005366 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005367 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005368
5369exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005370 /*
5371 * Key attributes may have been returned by psa_get_key_attributes()
5372 * thus reset them as required.
5373 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005374 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005375
Darryl Greend49a4992018-06-18 17:27:26 +01005376 mbedtls_free( first_export );
5377 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005378 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005379 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005380 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005381 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005382}
5383/* END_CASE */