blob: df2c919dd87f0d95fe0c49a82caf9fcfed3e8a2d [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,
795 int expected_usage_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100796 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200797{
Ronald Cron5425a212020-08-04 14:58:35 +0200798 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200799 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000800 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200801 psa_key_type_t key_type = key_type_arg;
802 psa_algorithm_t policy_alg = policy_alg_arg;
803 psa_algorithm_t exercise_alg = exercise_alg_arg;
804 psa_key_usage_t policy_usage = policy_usage_arg;
805 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200806 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100807 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200808 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200809
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200810 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200811 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200812 TEST_EQUAL( expected_usage,
813 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200814
Gilles Peskine8817f612018-12-18 00:18:46 +0100815 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200816
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200817 psa_set_key_usage_flags( &attributes, policy_usage );
818 psa_set_key_algorithm( &attributes, policy_alg );
819 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200820
Gilles Peskine049c7532019-05-15 20:22:09 +0200821 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200822 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200823
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200824 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
825
Ronald Cron5425a212020-08-04 14:58:35 +0200826 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100827 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100828 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100829 else
830 TEST_EQUAL( status, expected_status );
831
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200832 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200833
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200834 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200835 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100836 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100837 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100838 else
839 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200840
841exit:
842 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200843 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200844 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200845}
846/* END_CASE */
847
848/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200849void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200850 int policy_alg,
851 int key_type,
852 data_t *key_data,
853 int exercise_alg )
854{
Ronald Cron5425a212020-08-04 14:58:35 +0200855 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200856 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000857 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200858 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200859 psa_status_t status;
860
Gilles Peskine8817f612018-12-18 00:18:46 +0100861 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200862
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200863 psa_set_key_usage_flags( &attributes, policy_usage );
864 psa_set_key_algorithm( &attributes, policy_alg );
865 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200866
Gilles Peskine049c7532019-05-15 20:22:09 +0200867 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200868 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200869
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200870 /* Check if no key usage flag implication is done */
871 TEST_EQUAL( policy_usage,
872 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200873
Ronald Cron5425a212020-08-04 14:58:35 +0200874 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200875 if( policy_alg == exercise_alg &&
876 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100877 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200878 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100879 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200880 psa_cipher_abort( &operation );
881
Ronald Cron5425a212020-08-04 14:58:35 +0200882 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200883 if( policy_alg == exercise_alg &&
884 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100885 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200886 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100887 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200888
889exit:
890 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200891 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200892 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200893}
894/* END_CASE */
895
896/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200897void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200898 int policy_alg,
899 int key_type,
900 data_t *key_data,
901 int nonce_length_arg,
902 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100903 int exercise_alg,
904 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200905{
Ronald Cron5425a212020-08-04 14:58:35 +0200906 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200907 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200908 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200909 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100910 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200911 unsigned char nonce[16] = {0};
912 size_t nonce_length = nonce_length_arg;
913 unsigned char tag[16];
914 size_t tag_length = tag_length_arg;
915 size_t output_length;
916
917 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
918 TEST_ASSERT( tag_length <= sizeof( tag ) );
919
Gilles Peskine8817f612018-12-18 00:18:46 +0100920 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200921
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200922 psa_set_key_usage_flags( &attributes, policy_usage );
923 psa_set_key_algorithm( &attributes, policy_alg );
924 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200925
Gilles Peskine049c7532019-05-15 20:22:09 +0200926 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200927 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200928
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200929 /* Check if no key usage implication is done */
930 TEST_EQUAL( policy_usage,
931 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200932
Ronald Cron5425a212020-08-04 14:58:35 +0200933 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200934 nonce, nonce_length,
935 NULL, 0,
936 NULL, 0,
937 tag, tag_length,
938 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100939 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
940 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200941 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100942 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200943
944 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200945 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200946 nonce, nonce_length,
947 NULL, 0,
948 tag, tag_length,
949 NULL, 0,
950 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100951 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
952 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
953 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100954 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200955 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100956 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200957
958exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200959 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200960 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200961}
962/* END_CASE */
963
964/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200965void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200966 int policy_alg,
967 int key_type,
968 data_t *key_data,
969 int exercise_alg )
970{
Ronald Cron5425a212020-08-04 14:58:35 +0200971 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200972 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200973 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200974 psa_status_t status;
975 size_t key_bits;
976 size_t buffer_length;
977 unsigned char *buffer = NULL;
978 size_t output_length;
979
Gilles Peskine8817f612018-12-18 00:18:46 +0100980 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200981
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200982 psa_set_key_usage_flags( &attributes, policy_usage );
983 psa_set_key_algorithm( &attributes, policy_alg );
984 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200985
Gilles Peskine049c7532019-05-15 20:22:09 +0200986 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200987 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200988
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200989 /* Check if no key usage implication is done */
990 TEST_EQUAL( policy_usage,
991 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200992
Ronald Cron5425a212020-08-04 14:58:35 +0200993 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200994 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200995 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
996 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200997 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200998
Ronald Cron5425a212020-08-04 14:58:35 +0200999 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001000 NULL, 0,
1001 NULL, 0,
1002 buffer, buffer_length,
1003 &output_length );
1004 if( policy_alg == exercise_alg &&
1005 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001006 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001007 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001008 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001009
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001010 if( buffer_length != 0 )
1011 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001012 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001013 buffer, buffer_length,
1014 NULL, 0,
1015 buffer, buffer_length,
1016 &output_length );
1017 if( policy_alg == exercise_alg &&
1018 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001019 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001020 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001021 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001022
1023exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001024 /*
1025 * Key attributes may have been returned by psa_get_key_attributes()
1026 * thus reset them as required.
1027 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001028 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001029
1030 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001031 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001032 mbedtls_free( buffer );
1033}
1034/* END_CASE */
1035
1036/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001037void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001038 int policy_alg,
1039 int key_type,
1040 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001041 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001042 int payload_length_arg,
1043 int hashing_permitted,
1044 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001045{
Ronald Cron5425a212020-08-04 14:58:35 +02001046 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001047 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001048 psa_key_usage_t policy_usage = policy_usage_arg;
1049 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001050 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001051 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1052 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1053 * compatible with the policy and `payload_length_arg` is supposed to be
1054 * a valid input length to sign. If `payload_length_arg <= 0`,
1055 * `exercise_alg` is supposed to be forbidden by the policy. */
1056 int compatible_alg = payload_length_arg > 0;
1057 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001058 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001059 size_t signature_length;
1060
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001061 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001062 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001063 TEST_EQUAL( expected_usage,
1064 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001065
Gilles Peskine8817f612018-12-18 00:18:46 +01001066 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001067
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001068 psa_set_key_usage_flags( &attributes, policy_usage );
1069 psa_set_key_algorithm( &attributes, policy_alg );
1070 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001071
Gilles Peskine049c7532019-05-15 20:22:09 +02001072 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001073 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001074
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001075 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1076
Ronald Cron5425a212020-08-04 14:58:35 +02001077 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001078 payload, payload_length,
1079 signature, sizeof( signature ),
1080 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001081 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001082 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001083 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001084 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001085
1086 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001087 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001088 payload, payload_length,
1089 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001090 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001091 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001092 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001093 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001094
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001095 if( hashing_permitted )
1096 {
1097 status = psa_sign_message( key, exercise_alg,
1098 payload, payload_length,
1099 signature, sizeof( signature ),
1100 &signature_length );
1101 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1102 PSA_ASSERT( status );
1103 else
1104 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1105
1106 memset( signature, 0, sizeof( signature ) );
1107 status = psa_verify_message( key, exercise_alg,
1108 payload, payload_length,
1109 signature, sizeof( signature ) );
1110 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1111 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1112 else
1113 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1114 }
1115
Gilles Peskined5b33222018-06-18 22:20:03 +02001116exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001117 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001118 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001119}
1120/* END_CASE */
1121
Janos Follathba3fab92019-06-11 14:50:16 +01001122/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001123void derive_key_policy( int policy_usage,
1124 int policy_alg,
1125 int key_type,
1126 data_t *key_data,
1127 int exercise_alg )
1128{
Ronald Cron5425a212020-08-04 14:58:35 +02001129 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001130 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001131 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001132 psa_status_t status;
1133
Gilles Peskine8817f612018-12-18 00:18:46 +01001134 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001135
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001136 psa_set_key_usage_flags( &attributes, policy_usage );
1137 psa_set_key_algorithm( &attributes, policy_alg );
1138 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001139
Gilles Peskine049c7532019-05-15 20:22:09 +02001140 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001141 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001142
Janos Follathba3fab92019-06-11 14:50:16 +01001143 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1144
1145 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1146 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001147 {
Janos Follathba3fab92019-06-11 14:50:16 +01001148 PSA_ASSERT( psa_key_derivation_input_bytes(
1149 &operation,
1150 PSA_KEY_DERIVATION_INPUT_SEED,
1151 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001152 }
Janos Follathba3fab92019-06-11 14:50:16 +01001153
1154 status = psa_key_derivation_input_key( &operation,
1155 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001156 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001157
Gilles Peskineea0fb492018-07-12 17:17:20 +02001158 if( policy_alg == exercise_alg &&
1159 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001160 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001161 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001162 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001163
1164exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001165 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001166 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001167 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001168}
1169/* END_CASE */
1170
1171/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001172void agreement_key_policy( int policy_usage,
1173 int policy_alg,
1174 int key_type_arg,
1175 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001176 int exercise_alg,
1177 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001178{
Ronald Cron5425a212020-08-04 14:58:35 +02001179 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001180 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001181 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001182 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001183 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001184 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001185
Gilles Peskine8817f612018-12-18 00:18:46 +01001186 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001187
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001188 psa_set_key_usage_flags( &attributes, policy_usage );
1189 psa_set_key_algorithm( &attributes, policy_alg );
1190 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001191
Gilles Peskine049c7532019-05-15 20:22:09 +02001192 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001193 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001194
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001195 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001196 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001197
Steven Cooremance48e852020-10-05 16:02:45 +02001198 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001199
1200exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001201 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001202 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001203 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001204}
1205/* END_CASE */
1206
1207/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001208void key_policy_alg2( int key_type_arg, data_t *key_data,
1209 int usage_arg, int alg_arg, int alg2_arg )
1210{
Ronald Cron5425a212020-08-04 14:58:35 +02001211 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001212 psa_key_type_t key_type = key_type_arg;
1213 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1214 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1215 psa_key_usage_t usage = usage_arg;
1216 psa_algorithm_t alg = alg_arg;
1217 psa_algorithm_t alg2 = alg2_arg;
1218
1219 PSA_ASSERT( psa_crypto_init( ) );
1220
1221 psa_set_key_usage_flags( &attributes, usage );
1222 psa_set_key_algorithm( &attributes, alg );
1223 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1224 psa_set_key_type( &attributes, key_type );
1225 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001226 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001227
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001228 /* Update the usage flags to obtain implicit usage flags */
1229 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001230 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001231 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1232 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1233 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1234
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001235 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001236 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001237 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001238 goto exit;
1239
1240exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001241 /*
1242 * Key attributes may have been returned by psa_get_key_attributes()
1243 * thus reset them as required.
1244 */
1245 psa_reset_key_attributes( &got_attributes );
1246
Ronald Cron5425a212020-08-04 14:58:35 +02001247 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001248 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001249}
1250/* END_CASE */
1251
1252/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001253void raw_agreement_key_policy( int policy_usage,
1254 int policy_alg,
1255 int key_type_arg,
1256 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001257 int exercise_alg,
1258 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001259{
Ronald Cron5425a212020-08-04 14:58:35 +02001260 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001261 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001262 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001263 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001264 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001265 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001266
1267 PSA_ASSERT( psa_crypto_init( ) );
1268
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001269 psa_set_key_usage_flags( &attributes, policy_usage );
1270 psa_set_key_algorithm( &attributes, policy_alg );
1271 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001272
Gilles Peskine049c7532019-05-15 20:22:09 +02001273 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001274 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001275
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001276 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001277
Steven Cooremance48e852020-10-05 16:02:45 +02001278 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001279
1280exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001281 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001282 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001283 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001284}
1285/* END_CASE */
1286
1287/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001288void copy_success( int source_usage_arg,
1289 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001290 int type_arg, data_t *material,
1291 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001292 int target_usage_arg,
1293 int target_alg_arg, int target_alg2_arg,
1294 int expected_usage_arg,
1295 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001296{
Gilles Peskineca25db92019-04-19 11:43:08 +02001297 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1298 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001299 psa_key_usage_t expected_usage = expected_usage_arg;
1300 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001301 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001302 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1303 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001304 uint8_t *export_buffer = NULL;
1305
Gilles Peskine57ab7212019-01-28 13:03:09 +01001306 PSA_ASSERT( psa_crypto_init( ) );
1307
Gilles Peskineca25db92019-04-19 11:43:08 +02001308 /* Prepare the source key. */
1309 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1310 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001311 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001312 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001313 PSA_ASSERT( psa_import_key( &source_attributes,
1314 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001315 &source_key ) );
1316 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001317
Gilles Peskineca25db92019-04-19 11:43:08 +02001318 /* Prepare the target attributes. */
1319 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001320 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001321 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001322 /* Set volatile lifetime to reset the key identifier to 0. */
1323 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1324 }
1325
Gilles Peskineca25db92019-04-19 11:43:08 +02001326 if( target_usage_arg != -1 )
1327 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1328 if( target_alg_arg != -1 )
1329 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001330 if( target_alg2_arg != -1 )
1331 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001332
1333 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001334 PSA_ASSERT( psa_copy_key( source_key,
1335 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001336
1337 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001338 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001339
1340 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001341 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001342 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1343 psa_get_key_type( &target_attributes ) );
1344 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1345 psa_get_key_bits( &target_attributes ) );
1346 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1347 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001348 TEST_EQUAL( expected_alg2,
1349 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001350 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1351 {
1352 size_t length;
1353 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001354 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001355 material->len, &length ) );
1356 ASSERT_COMPARE( material->x, material->len,
1357 export_buffer, length );
1358 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001359
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001360 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001361 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001362 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001363 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001364
Ronald Cron5425a212020-08-04 14:58:35 +02001365 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001366
1367exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001368 /*
1369 * Source and target key attributes may have been returned by
1370 * psa_get_key_attributes() thus reset them as required.
1371 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001372 psa_reset_key_attributes( &source_attributes );
1373 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001374
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001375 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001376 mbedtls_free( export_buffer );
1377}
1378/* END_CASE */
1379
1380/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001381void copy_fail( int source_usage_arg,
1382 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001383 int type_arg, data_t *material,
1384 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001385 int target_usage_arg,
1386 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001387 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001388 int expected_status_arg )
1389{
1390 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1391 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001392 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1393 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001394 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001395
1396 PSA_ASSERT( psa_crypto_init( ) );
1397
1398 /* Prepare the source key. */
1399 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1400 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001401 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001402 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001403 PSA_ASSERT( psa_import_key( &source_attributes,
1404 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001405 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001406
1407 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001408 psa_set_key_id( &target_attributes, key_id );
1409 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001410 psa_set_key_type( &target_attributes, target_type_arg );
1411 psa_set_key_bits( &target_attributes, target_bits_arg );
1412 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1413 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001414 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001415
1416 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001417 TEST_EQUAL( psa_copy_key( source_key,
1418 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001419 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001420
Ronald Cron5425a212020-08-04 14:58:35 +02001421 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001422
Gilles Peskine4a644642019-05-03 17:14:08 +02001423exit:
1424 psa_reset_key_attributes( &source_attributes );
1425 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001426 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001427}
1428/* END_CASE */
1429
1430/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001431void hash_operation_init( )
1432{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001433 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001434 /* Test each valid way of initializing the object, except for `= {0}`, as
1435 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1436 * though it's OK by the C standard. We could test for this, but we'd need
1437 * to supress the Clang warning for the test. */
1438 psa_hash_operation_t func = psa_hash_operation_init( );
1439 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1440 psa_hash_operation_t zero;
1441
1442 memset( &zero, 0, sizeof( zero ) );
1443
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001444 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001445 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1446 PSA_ERROR_BAD_STATE );
1447 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1448 PSA_ERROR_BAD_STATE );
1449 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1450 PSA_ERROR_BAD_STATE );
1451
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001452 /* A default hash operation should be abortable without error. */
1453 PSA_ASSERT( psa_hash_abort( &func ) );
1454 PSA_ASSERT( psa_hash_abort( &init ) );
1455 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001456}
1457/* END_CASE */
1458
1459/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001460void hash_setup( int alg_arg,
1461 int expected_status_arg )
1462{
1463 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001464 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001465 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001466 psa_status_t status;
1467
Gilles Peskine8817f612018-12-18 00:18:46 +01001468 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001469
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001470 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001471 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001472
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001473 /* Whether setup succeeded or failed, abort must succeed. */
1474 PSA_ASSERT( psa_hash_abort( &operation ) );
1475
1476 /* If setup failed, reproduce the failure, so as to
1477 * test the resulting state of the operation object. */
1478 if( status != PSA_SUCCESS )
1479 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1480
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001481 /* Now the operation object should be reusable. */
1482#if defined(KNOWN_SUPPORTED_HASH_ALG)
1483 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1484 PSA_ASSERT( psa_hash_abort( &operation ) );
1485#endif
1486
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001487exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001488 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001489}
1490/* END_CASE */
1491
1492/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001493void hash_compute_fail( int alg_arg, data_t *input,
1494 int output_size_arg, int expected_status_arg )
1495{
1496 psa_algorithm_t alg = alg_arg;
1497 uint8_t *output = NULL;
1498 size_t output_size = output_size_arg;
1499 size_t output_length = INVALID_EXPORT_LENGTH;
1500 psa_status_t expected_status = expected_status_arg;
1501 psa_status_t status;
1502
1503 ASSERT_ALLOC( output, output_size );
1504
1505 PSA_ASSERT( psa_crypto_init( ) );
1506
1507 status = psa_hash_compute( alg, input->x, input->len,
1508 output, output_size, &output_length );
1509 TEST_EQUAL( status, expected_status );
1510 TEST_ASSERT( output_length <= output_size );
1511
1512exit:
1513 mbedtls_free( output );
1514 PSA_DONE( );
1515}
1516/* END_CASE */
1517
1518/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001519void hash_compare_fail( int alg_arg, data_t *input,
1520 data_t *reference_hash,
1521 int expected_status_arg )
1522{
1523 psa_algorithm_t alg = alg_arg;
1524 psa_status_t expected_status = expected_status_arg;
1525 psa_status_t status;
1526
1527 PSA_ASSERT( psa_crypto_init( ) );
1528
1529 status = psa_hash_compare( alg, input->x, input->len,
1530 reference_hash->x, reference_hash->len );
1531 TEST_EQUAL( status, expected_status );
1532
1533exit:
1534 PSA_DONE( );
1535}
1536/* END_CASE */
1537
1538/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001539void hash_compute_compare( int alg_arg, data_t *input,
1540 data_t *expected_output )
1541{
1542 psa_algorithm_t alg = alg_arg;
1543 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1544 size_t output_length = INVALID_EXPORT_LENGTH;
1545 size_t i;
1546
1547 PSA_ASSERT( psa_crypto_init( ) );
1548
1549 /* Compute with tight buffer */
1550 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001551 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001552 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001553 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001554 ASSERT_COMPARE( output, output_length,
1555 expected_output->x, expected_output->len );
1556
1557 /* Compute with larger buffer */
1558 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1559 output, sizeof( output ),
1560 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001561 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001562 ASSERT_COMPARE( output, output_length,
1563 expected_output->x, expected_output->len );
1564
1565 /* Compare with correct hash */
1566 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1567 output, output_length ) );
1568
1569 /* Compare with trailing garbage */
1570 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1571 output, output_length + 1 ),
1572 PSA_ERROR_INVALID_SIGNATURE );
1573
1574 /* Compare with truncated hash */
1575 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1576 output, output_length - 1 ),
1577 PSA_ERROR_INVALID_SIGNATURE );
1578
1579 /* Compare with corrupted value */
1580 for( i = 0; i < output_length; i++ )
1581 {
Chris Jones9634bb12021-01-20 15:56:42 +00001582 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001583 output[i] ^= 1;
1584 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1585 output, output_length ),
1586 PSA_ERROR_INVALID_SIGNATURE );
1587 output[i] ^= 1;
1588 }
1589
1590exit:
1591 PSA_DONE( );
1592}
1593/* END_CASE */
1594
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001595/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001596void hash_bad_order( )
1597{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001598 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001599 unsigned char input[] = "";
1600 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001601 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001602 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1603 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1604 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001605 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001606 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001607 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001608
Gilles Peskine8817f612018-12-18 00:18:46 +01001609 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001610
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001611 /* Call setup twice in a row. */
1612 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001613 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001614 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1615 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001616 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001617 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001618 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001619
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001620 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001621 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001622 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001623 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001624
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001625 /* Check that update calls abort on error. */
1626 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01001627 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001628 ASSERT_OPERATION_IS_ACTIVE( operation );
1629 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
1630 PSA_ERROR_BAD_STATE );
1631 ASSERT_OPERATION_IS_INACTIVE( operation );
1632 PSA_ASSERT( psa_hash_abort( &operation ) );
1633 ASSERT_OPERATION_IS_INACTIVE( operation );
1634
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001635 /* Call update after finish. */
1636 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1637 PSA_ASSERT( psa_hash_finish( &operation,
1638 hash, sizeof( hash ), &hash_len ) );
1639 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001640 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001641 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001642
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001643 /* Call verify without calling setup beforehand. */
1644 TEST_EQUAL( psa_hash_verify( &operation,
1645 valid_hash, sizeof( valid_hash ) ),
1646 PSA_ERROR_BAD_STATE );
1647 PSA_ASSERT( psa_hash_abort( &operation ) );
1648
1649 /* Call verify after finish. */
1650 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1651 PSA_ASSERT( psa_hash_finish( &operation,
1652 hash, sizeof( hash ), &hash_len ) );
1653 TEST_EQUAL( psa_hash_verify( &operation,
1654 valid_hash, sizeof( valid_hash ) ),
1655 PSA_ERROR_BAD_STATE );
1656 PSA_ASSERT( psa_hash_abort( &operation ) );
1657
1658 /* Call verify twice in a row. */
1659 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001660 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001661 PSA_ASSERT( psa_hash_verify( &operation,
1662 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001663 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001664 TEST_EQUAL( psa_hash_verify( &operation,
1665 valid_hash, sizeof( valid_hash ) ),
1666 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001667 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001668 PSA_ASSERT( psa_hash_abort( &operation ) );
1669
1670 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001671 TEST_EQUAL( psa_hash_finish( &operation,
1672 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001673 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001674 PSA_ASSERT( psa_hash_abort( &operation ) );
1675
1676 /* Call finish twice in a row. */
1677 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1678 PSA_ASSERT( psa_hash_finish( &operation,
1679 hash, sizeof( hash ), &hash_len ) );
1680 TEST_EQUAL( psa_hash_finish( &operation,
1681 hash, sizeof( hash ), &hash_len ),
1682 PSA_ERROR_BAD_STATE );
1683 PSA_ASSERT( psa_hash_abort( &operation ) );
1684
1685 /* Call finish after calling verify. */
1686 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1687 PSA_ASSERT( psa_hash_verify( &operation,
1688 valid_hash, sizeof( valid_hash ) ) );
1689 TEST_EQUAL( psa_hash_finish( &operation,
1690 hash, sizeof( hash ), &hash_len ),
1691 PSA_ERROR_BAD_STATE );
1692 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001693
1694exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001695 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001696}
1697/* END_CASE */
1698
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001699/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001700void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001701{
1702 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001703 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1704 * appended to it */
1705 unsigned char hash[] = {
1706 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1707 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1708 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001709 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001710 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001711
Gilles Peskine8817f612018-12-18 00:18:46 +01001712 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001713
itayzafrir27e69452018-11-01 14:26:34 +02001714 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001715 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001716 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001717 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001718 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001719 ASSERT_OPERATION_IS_INACTIVE( operation );
1720 PSA_ASSERT( psa_hash_abort( &operation ) );
1721 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03001722
itayzafrir27e69452018-11-01 14:26:34 +02001723 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001724 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001725 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001726 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001727
itayzafrir27e69452018-11-01 14:26:34 +02001728 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001729 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001730 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001731 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001732
itayzafrirec93d302018-10-18 18:01:10 +03001733exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001734 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001735}
1736/* END_CASE */
1737
Ronald Cronee414c72021-03-18 18:50:08 +01001738/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001739void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001740{
1741 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001742 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001743 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001744 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001745 size_t hash_len;
1746
Gilles Peskine8817f612018-12-18 00:18:46 +01001747 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001748
itayzafrir58028322018-10-25 10:22:01 +03001749 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001750 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001751 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001752 hash, expected_size - 1, &hash_len ),
1753 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001754
1755exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001756 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001757}
1758/* END_CASE */
1759
Ronald Cronee414c72021-03-18 18:50:08 +01001760/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001761void hash_clone_source_state( )
1762{
1763 psa_algorithm_t alg = PSA_ALG_SHA_256;
1764 unsigned char hash[PSA_HASH_MAX_SIZE];
1765 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1766 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1767 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1768 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1769 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1770 size_t hash_len;
1771
1772 PSA_ASSERT( psa_crypto_init( ) );
1773 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1774
1775 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1776 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1777 PSA_ASSERT( psa_hash_finish( &op_finished,
1778 hash, sizeof( hash ), &hash_len ) );
1779 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1780 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1781
1782 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1783 PSA_ERROR_BAD_STATE );
1784
1785 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1786 PSA_ASSERT( psa_hash_finish( &op_init,
1787 hash, sizeof( hash ), &hash_len ) );
1788 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1789 PSA_ASSERT( psa_hash_finish( &op_finished,
1790 hash, sizeof( hash ), &hash_len ) );
1791 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1792 PSA_ASSERT( psa_hash_finish( &op_aborted,
1793 hash, sizeof( hash ), &hash_len ) );
1794
1795exit:
1796 psa_hash_abort( &op_source );
1797 psa_hash_abort( &op_init );
1798 psa_hash_abort( &op_setup );
1799 psa_hash_abort( &op_finished );
1800 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001801 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001802}
1803/* END_CASE */
1804
Ronald Cronee414c72021-03-18 18:50:08 +01001805/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001806void hash_clone_target_state( )
1807{
1808 psa_algorithm_t alg = PSA_ALG_SHA_256;
1809 unsigned char hash[PSA_HASH_MAX_SIZE];
1810 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1811 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1812 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1813 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1814 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1815 size_t hash_len;
1816
1817 PSA_ASSERT( psa_crypto_init( ) );
1818
1819 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1820 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1821 PSA_ASSERT( psa_hash_finish( &op_finished,
1822 hash, sizeof( hash ), &hash_len ) );
1823 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1824 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1825
1826 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1827 PSA_ASSERT( psa_hash_finish( &op_target,
1828 hash, sizeof( hash ), &hash_len ) );
1829
1830 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1831 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1832 PSA_ERROR_BAD_STATE );
1833 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1834 PSA_ERROR_BAD_STATE );
1835
1836exit:
1837 psa_hash_abort( &op_target );
1838 psa_hash_abort( &op_init );
1839 psa_hash_abort( &op_setup );
1840 psa_hash_abort( &op_finished );
1841 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001842 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001843}
1844/* END_CASE */
1845
itayzafrir58028322018-10-25 10:22:01 +03001846/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001847void mac_operation_init( )
1848{
Jaeden Amero252ef282019-02-15 14:05:35 +00001849 const uint8_t input[1] = { 0 };
1850
Jaeden Amero769ce272019-01-04 11:48:03 +00001851 /* Test each valid way of initializing the object, except for `= {0}`, as
1852 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1853 * though it's OK by the C standard. We could test for this, but we'd need
1854 * to supress the Clang warning for the test. */
1855 psa_mac_operation_t func = psa_mac_operation_init( );
1856 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1857 psa_mac_operation_t zero;
1858
1859 memset( &zero, 0, sizeof( zero ) );
1860
Jaeden Amero252ef282019-02-15 14:05:35 +00001861 /* A freshly-initialized MAC operation should not be usable. */
1862 TEST_EQUAL( psa_mac_update( &func,
1863 input, sizeof( input ) ),
1864 PSA_ERROR_BAD_STATE );
1865 TEST_EQUAL( psa_mac_update( &init,
1866 input, sizeof( input ) ),
1867 PSA_ERROR_BAD_STATE );
1868 TEST_EQUAL( psa_mac_update( &zero,
1869 input, sizeof( input ) ),
1870 PSA_ERROR_BAD_STATE );
1871
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001872 /* A default MAC operation should be abortable without error. */
1873 PSA_ASSERT( psa_mac_abort( &func ) );
1874 PSA_ASSERT( psa_mac_abort( &init ) );
1875 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001876}
1877/* END_CASE */
1878
1879/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001880void mac_setup( int key_type_arg,
1881 data_t *key,
1882 int alg_arg,
1883 int expected_status_arg )
1884{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001885 psa_key_type_t key_type = key_type_arg;
1886 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001887 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001888 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001889 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1890#if defined(KNOWN_SUPPORTED_MAC_ALG)
1891 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1892#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001893
Gilles Peskine8817f612018-12-18 00:18:46 +01001894 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001895
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001896 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1897 &operation, &status ) )
1898 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001899 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001900
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001901 /* The operation object should be reusable. */
1902#if defined(KNOWN_SUPPORTED_MAC_ALG)
1903 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1904 smoke_test_key_data,
1905 sizeof( smoke_test_key_data ),
1906 KNOWN_SUPPORTED_MAC_ALG,
1907 &operation, &status ) )
1908 goto exit;
1909 TEST_EQUAL( status, PSA_SUCCESS );
1910#endif
1911
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001912exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001913 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001914}
1915/* END_CASE */
1916
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001917/* 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 +00001918void mac_bad_order( )
1919{
Ronald Cron5425a212020-08-04 14:58:35 +02001920 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001921 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1922 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001923 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001924 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1925 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1926 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001927 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001928 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1929 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1930 size_t sign_mac_length = 0;
1931 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1932 const uint8_t verify_mac[] = {
1933 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1934 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1935 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1936
1937 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001938 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001939 psa_set_key_algorithm( &attributes, alg );
1940 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001941
Ronald Cron5425a212020-08-04 14:58:35 +02001942 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1943 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001944
Jaeden Amero252ef282019-02-15 14:05:35 +00001945 /* Call update without calling setup beforehand. */
1946 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1947 PSA_ERROR_BAD_STATE );
1948 PSA_ASSERT( psa_mac_abort( &operation ) );
1949
1950 /* Call sign finish without calling setup beforehand. */
1951 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1952 &sign_mac_length),
1953 PSA_ERROR_BAD_STATE );
1954 PSA_ASSERT( psa_mac_abort( &operation ) );
1955
1956 /* Call verify finish without calling setup beforehand. */
1957 TEST_EQUAL( psa_mac_verify_finish( &operation,
1958 verify_mac, sizeof( verify_mac ) ),
1959 PSA_ERROR_BAD_STATE );
1960 PSA_ASSERT( psa_mac_abort( &operation ) );
1961
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001962 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001963 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001964 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001965 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001966 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001967 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001968 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001969 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001970
Jaeden Amero252ef282019-02-15 14:05:35 +00001971 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001972 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001973 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1974 PSA_ASSERT( psa_mac_sign_finish( &operation,
1975 sign_mac, sizeof( sign_mac ),
1976 &sign_mac_length ) );
1977 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1978 PSA_ERROR_BAD_STATE );
1979 PSA_ASSERT( psa_mac_abort( &operation ) );
1980
1981 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001982 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001983 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1984 PSA_ASSERT( psa_mac_verify_finish( &operation,
1985 verify_mac, sizeof( verify_mac ) ) );
1986 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1987 PSA_ERROR_BAD_STATE );
1988 PSA_ASSERT( psa_mac_abort( &operation ) );
1989
1990 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001991 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001992 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1993 PSA_ASSERT( psa_mac_sign_finish( &operation,
1994 sign_mac, sizeof( sign_mac ),
1995 &sign_mac_length ) );
1996 TEST_EQUAL( psa_mac_sign_finish( &operation,
1997 sign_mac, sizeof( sign_mac ),
1998 &sign_mac_length ),
1999 PSA_ERROR_BAD_STATE );
2000 PSA_ASSERT( psa_mac_abort( &operation ) );
2001
2002 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002003 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002004 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2005 PSA_ASSERT( psa_mac_verify_finish( &operation,
2006 verify_mac, sizeof( verify_mac ) ) );
2007 TEST_EQUAL( psa_mac_verify_finish( &operation,
2008 verify_mac, sizeof( verify_mac ) ),
2009 PSA_ERROR_BAD_STATE );
2010 PSA_ASSERT( psa_mac_abort( &operation ) );
2011
2012 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002013 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002014 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002015 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002016 TEST_EQUAL( psa_mac_verify_finish( &operation,
2017 verify_mac, sizeof( verify_mac ) ),
2018 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002019 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002020 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002021 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002022
2023 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002024 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002025 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002026 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002027 TEST_EQUAL( psa_mac_sign_finish( &operation,
2028 sign_mac, sizeof( sign_mac ),
2029 &sign_mac_length ),
2030 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002031 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002032 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002033 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002034
Ronald Cron5425a212020-08-04 14:58:35 +02002035 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002036
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002037exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002038 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002039}
2040/* END_CASE */
2041
2042/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002043void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002044 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002045 int alg_arg,
2046 data_t *input,
2047 data_t *expected_mac )
2048{
Ronald Cron5425a212020-08-04 14:58:35 +02002049 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002050 psa_key_type_t key_type = key_type_arg;
2051 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002052 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002053 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002054 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002055 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002056 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002057 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002058 const size_t output_sizes_to_test[] = {
2059 0,
2060 1,
2061 expected_mac->len - 1,
2062 expected_mac->len,
2063 expected_mac->len + 1,
2064 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002065
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002066 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002067 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002068 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002069
Gilles Peskine8817f612018-12-18 00:18:46 +01002070 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002071
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002072 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002073 psa_set_key_algorithm( &attributes, alg );
2074 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002075
Ronald Cron5425a212020-08-04 14:58:35 +02002076 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2077 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002078
Gilles Peskine8b356b52020-08-25 23:44:59 +02002079 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2080 {
2081 const size_t output_size = output_sizes_to_test[i];
2082 psa_status_t expected_status =
2083 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2084 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002085
Chris Jones9634bb12021-01-20 15:56:42 +00002086 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002087 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002088
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002089 /* Calculate the MAC, one-shot case. */
2090 TEST_EQUAL( psa_mac_compute( key, alg,
2091 input->x, input->len,
2092 actual_mac, output_size, &mac_length ),
2093 expected_status );
2094 if( expected_status == PSA_SUCCESS )
2095 {
2096 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2097 actual_mac, mac_length );
2098 }
2099
2100 if( output_size > 0 )
2101 memset( actual_mac, 0, output_size );
2102
2103 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002104 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002105 PSA_ASSERT( psa_mac_update( &operation,
2106 input->x, input->len ) );
2107 TEST_EQUAL( psa_mac_sign_finish( &operation,
2108 actual_mac, output_size,
2109 &mac_length ),
2110 expected_status );
2111 PSA_ASSERT( psa_mac_abort( &operation ) );
2112
2113 if( expected_status == PSA_SUCCESS )
2114 {
2115 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2116 actual_mac, mac_length );
2117 }
2118 mbedtls_free( actual_mac );
2119 actual_mac = NULL;
2120 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002121
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002122exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002123 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002124 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002125 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002126 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002127}
2128/* END_CASE */
2129
2130/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002131void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002132 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002133 int alg_arg,
2134 data_t *input,
2135 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002136{
Ronald Cron5425a212020-08-04 14:58:35 +02002137 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002138 psa_key_type_t key_type = key_type_arg;
2139 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002140 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002141 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002142 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002143
Gilles Peskine69c12672018-06-28 00:07:19 +02002144 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2145
Gilles Peskine8817f612018-12-18 00:18:46 +01002146 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002147
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002148 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002149 psa_set_key_algorithm( &attributes, alg );
2150 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002151
Ronald Cron5425a212020-08-04 14:58:35 +02002152 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2153 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002154
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002155 /* Verify correct MAC, one-shot case. */
2156 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2157 expected_mac->x, expected_mac->len ) );
2158
2159 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002160 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002161 PSA_ASSERT( psa_mac_update( &operation,
2162 input->x, input->len ) );
2163 PSA_ASSERT( psa_mac_verify_finish( &operation,
2164 expected_mac->x,
2165 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002166
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002167 /* Test a MAC that's too short, one-shot case. */
2168 TEST_EQUAL( psa_mac_verify( key, alg,
2169 input->x, input->len,
2170 expected_mac->x,
2171 expected_mac->len - 1 ),
2172 PSA_ERROR_INVALID_SIGNATURE );
2173
2174 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002175 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002176 PSA_ASSERT( psa_mac_update( &operation,
2177 input->x, input->len ) );
2178 TEST_EQUAL( psa_mac_verify_finish( &operation,
2179 expected_mac->x,
2180 expected_mac->len - 1 ),
2181 PSA_ERROR_INVALID_SIGNATURE );
2182
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002183 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002184 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2185 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002186 TEST_EQUAL( psa_mac_verify( key, alg,
2187 input->x, input->len,
2188 perturbed_mac, expected_mac->len + 1 ),
2189 PSA_ERROR_INVALID_SIGNATURE );
2190
2191 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002192 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002193 PSA_ASSERT( psa_mac_update( &operation,
2194 input->x, input->len ) );
2195 TEST_EQUAL( psa_mac_verify_finish( &operation,
2196 perturbed_mac,
2197 expected_mac->len + 1 ),
2198 PSA_ERROR_INVALID_SIGNATURE );
2199
2200 /* Test changing one byte. */
2201 for( size_t i = 0; i < expected_mac->len; i++ )
2202 {
Chris Jones9634bb12021-01-20 15:56:42 +00002203 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002204 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002205
2206 TEST_EQUAL( psa_mac_verify( key, alg,
2207 input->x, input->len,
2208 perturbed_mac, expected_mac->len ),
2209 PSA_ERROR_INVALID_SIGNATURE );
2210
Ronald Cron5425a212020-08-04 14:58:35 +02002211 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002212 PSA_ASSERT( psa_mac_update( &operation,
2213 input->x, input->len ) );
2214 TEST_EQUAL( psa_mac_verify_finish( &operation,
2215 perturbed_mac,
2216 expected_mac->len ),
2217 PSA_ERROR_INVALID_SIGNATURE );
2218 perturbed_mac[i] ^= 1;
2219 }
2220
Gilles Peskine8c9def32018-02-08 10:02:12 +01002221exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002222 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002223 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002224 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002225 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002226}
2227/* END_CASE */
2228
2229/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002230void cipher_operation_init( )
2231{
Jaeden Ameroab439972019-02-15 14:12:05 +00002232 const uint8_t input[1] = { 0 };
2233 unsigned char output[1] = { 0 };
2234 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002235 /* Test each valid way of initializing the object, except for `= {0}`, as
2236 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2237 * though it's OK by the C standard. We could test for this, but we'd need
2238 * to supress the Clang warning for the test. */
2239 psa_cipher_operation_t func = psa_cipher_operation_init( );
2240 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2241 psa_cipher_operation_t zero;
2242
2243 memset( &zero, 0, sizeof( zero ) );
2244
Jaeden Ameroab439972019-02-15 14:12:05 +00002245 /* A freshly-initialized cipher operation should not be usable. */
2246 TEST_EQUAL( psa_cipher_update( &func,
2247 input, sizeof( input ),
2248 output, sizeof( output ),
2249 &output_length ),
2250 PSA_ERROR_BAD_STATE );
2251 TEST_EQUAL( psa_cipher_update( &init,
2252 input, sizeof( input ),
2253 output, sizeof( output ),
2254 &output_length ),
2255 PSA_ERROR_BAD_STATE );
2256 TEST_EQUAL( psa_cipher_update( &zero,
2257 input, sizeof( input ),
2258 output, sizeof( output ),
2259 &output_length ),
2260 PSA_ERROR_BAD_STATE );
2261
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002262 /* A default cipher operation should be abortable without error. */
2263 PSA_ASSERT( psa_cipher_abort( &func ) );
2264 PSA_ASSERT( psa_cipher_abort( &init ) );
2265 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002266}
2267/* END_CASE */
2268
2269/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002270void cipher_setup( int key_type_arg,
2271 data_t *key,
2272 int alg_arg,
2273 int expected_status_arg )
2274{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002275 psa_key_type_t key_type = key_type_arg;
2276 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002277 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002278 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002279 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002280#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002281 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2282#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002283
Gilles Peskine8817f612018-12-18 00:18:46 +01002284 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002285
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002286 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2287 &operation, &status ) )
2288 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002289 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002290
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002291 /* The operation object should be reusable. */
2292#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2293 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2294 smoke_test_key_data,
2295 sizeof( smoke_test_key_data ),
2296 KNOWN_SUPPORTED_CIPHER_ALG,
2297 &operation, &status ) )
2298 goto exit;
2299 TEST_EQUAL( status, PSA_SUCCESS );
2300#endif
2301
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002302exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002303 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002304 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002305}
2306/* END_CASE */
2307
Ronald Cronee414c72021-03-18 18:50:08 +01002308/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002309void cipher_bad_order( )
2310{
Ronald Cron5425a212020-08-04 14:58:35 +02002311 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002312 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2313 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002314 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002315 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002316 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002317 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002318 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2319 0xaa, 0xaa, 0xaa, 0xaa };
2320 const uint8_t text[] = {
2321 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2322 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002323 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002324 size_t length = 0;
2325
2326 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002327 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2328 psa_set_key_algorithm( &attributes, alg );
2329 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002330 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2331 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002332
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002333 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002334 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002335 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002336 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002337 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002338 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002339 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002340 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002341
2342 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002343 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002344 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002345 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002346 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002347 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002348 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002349 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002350
Jaeden Ameroab439972019-02-15 14:12:05 +00002351 /* Generate an IV without calling setup beforehand. */
2352 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2353 buffer, sizeof( buffer ),
2354 &length ),
2355 PSA_ERROR_BAD_STATE );
2356 PSA_ASSERT( psa_cipher_abort( &operation ) );
2357
2358 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002359 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002360 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2361 buffer, sizeof( buffer ),
2362 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002363 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002364 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2365 buffer, sizeof( buffer ),
2366 &length ),
2367 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002368 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002369 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002370 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002371
2372 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002373 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002374 PSA_ASSERT( psa_cipher_set_iv( &operation,
2375 iv, sizeof( iv ) ) );
2376 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2377 buffer, sizeof( buffer ),
2378 &length ),
2379 PSA_ERROR_BAD_STATE );
2380 PSA_ASSERT( psa_cipher_abort( &operation ) );
2381
2382 /* Set an IV without calling setup beforehand. */
2383 TEST_EQUAL( psa_cipher_set_iv( &operation,
2384 iv, sizeof( iv ) ),
2385 PSA_ERROR_BAD_STATE );
2386 PSA_ASSERT( psa_cipher_abort( &operation ) );
2387
2388 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002389 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002390 PSA_ASSERT( psa_cipher_set_iv( &operation,
2391 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002392 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002393 TEST_EQUAL( psa_cipher_set_iv( &operation,
2394 iv, sizeof( iv ) ),
2395 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002396 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002397 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002398 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002399
2400 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002401 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002402 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2403 buffer, sizeof( buffer ),
2404 &length ) );
2405 TEST_EQUAL( psa_cipher_set_iv( &operation,
2406 iv, sizeof( iv ) ),
2407 PSA_ERROR_BAD_STATE );
2408 PSA_ASSERT( psa_cipher_abort( &operation ) );
2409
2410 /* Call update without calling setup beforehand. */
2411 TEST_EQUAL( psa_cipher_update( &operation,
2412 text, sizeof( text ),
2413 buffer, sizeof( buffer ),
2414 &length ),
2415 PSA_ERROR_BAD_STATE );
2416 PSA_ASSERT( psa_cipher_abort( &operation ) );
2417
2418 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01002419 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002420 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002421 TEST_EQUAL( psa_cipher_update( &operation,
2422 text, sizeof( text ),
2423 buffer, sizeof( buffer ),
2424 &length ),
2425 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002426 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002427 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002428 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002429
2430 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002431 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002432 PSA_ASSERT( psa_cipher_set_iv( &operation,
2433 iv, sizeof( iv ) ) );
2434 PSA_ASSERT( psa_cipher_finish( &operation,
2435 buffer, sizeof( buffer ), &length ) );
2436 TEST_EQUAL( psa_cipher_update( &operation,
2437 text, sizeof( text ),
2438 buffer, sizeof( buffer ),
2439 &length ),
2440 PSA_ERROR_BAD_STATE );
2441 PSA_ASSERT( psa_cipher_abort( &operation ) );
2442
2443 /* Call finish without calling setup beforehand. */
2444 TEST_EQUAL( psa_cipher_finish( &operation,
2445 buffer, sizeof( buffer ), &length ),
2446 PSA_ERROR_BAD_STATE );
2447 PSA_ASSERT( psa_cipher_abort( &operation ) );
2448
2449 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002450 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002451 /* Not calling update means we are encrypting an empty buffer, which is OK
2452 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01002453 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002454 TEST_EQUAL( psa_cipher_finish( &operation,
2455 buffer, sizeof( buffer ), &length ),
2456 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002457 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002458 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002459 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002460
2461 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002462 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002463 PSA_ASSERT( psa_cipher_set_iv( &operation,
2464 iv, sizeof( iv ) ) );
2465 PSA_ASSERT( psa_cipher_finish( &operation,
2466 buffer, sizeof( buffer ), &length ) );
2467 TEST_EQUAL( psa_cipher_finish( &operation,
2468 buffer, sizeof( buffer ), &length ),
2469 PSA_ERROR_BAD_STATE );
2470 PSA_ASSERT( psa_cipher_abort( &operation ) );
2471
Ronald Cron5425a212020-08-04 14:58:35 +02002472 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002473
Jaeden Ameroab439972019-02-15 14:12:05 +00002474exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002475 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002476 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002477}
2478/* END_CASE */
2479
2480/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002481void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002482 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002483 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002484 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002485{
Ronald Cron5425a212020-08-04 14:58:35 +02002486 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002487 psa_status_t status;
2488 psa_key_type_t key_type = key_type_arg;
2489 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002490 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002491 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002492 size_t output_buffer_size = 0;
2493 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002494 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002495 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002496 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002497
Gilles Peskine8817f612018-12-18 00:18:46 +01002498 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002499
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002500 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2501 psa_set_key_algorithm( &attributes, alg );
2502 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002503
Ronald Cron5425a212020-08-04 14:58:35 +02002504 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2505 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002506
Ronald Cron5425a212020-08-04 14:58:35 +02002507 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002508
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002509 if( iv->len > 0 )
2510 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002511 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002512 }
2513
gabor-mezei-armceface22021-01-21 12:26:17 +01002514 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2515 TEST_ASSERT( output_buffer_size <=
2516 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002517 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002518
Gilles Peskine8817f612018-12-18 00:18:46 +01002519 PSA_ASSERT( psa_cipher_update( &operation,
2520 input->x, input->len,
2521 output, output_buffer_size,
2522 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002523 TEST_ASSERT( function_output_length <=
2524 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2525 TEST_ASSERT( function_output_length <=
2526 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002527 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002528
Gilles Peskine50e586b2018-06-08 14:28:46 +02002529 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002530 ( output_buffer_size == 0 ? NULL :
2531 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002532 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002533 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002534 TEST_ASSERT( function_output_length <=
2535 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2536 TEST_ASSERT( function_output_length <=
2537 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002538 total_output_length += function_output_length;
2539
Gilles Peskinefe11b722018-12-18 00:24:04 +01002540 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002541 if( expected_status == PSA_SUCCESS )
2542 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002543 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002544 ASSERT_COMPARE( expected_output->x, expected_output->len,
2545 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002546 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002547
Gilles Peskine50e586b2018-06-08 14:28:46 +02002548exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002549 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002550 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002551 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002552 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002553}
2554/* END_CASE */
2555
2556/* BEGIN_CASE */
2557void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002558 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002559 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002560 int first_part_size_arg,
2561 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002562 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002563{
Ronald Cron5425a212020-08-04 14:58:35 +02002564 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002565 psa_key_type_t key_type = key_type_arg;
2566 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002567 size_t first_part_size = first_part_size_arg;
2568 size_t output1_length = output1_length_arg;
2569 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002570 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002571 size_t output_buffer_size = 0;
2572 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002573 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002574 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002575 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002576
Gilles Peskine8817f612018-12-18 00:18:46 +01002577 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002578
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002579 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2580 psa_set_key_algorithm( &attributes, alg );
2581 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002582
Ronald Cron5425a212020-08-04 14:58:35 +02002583 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2584 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002585
Ronald Cron5425a212020-08-04 14:58:35 +02002586 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002587
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002588 if( iv->len > 0 )
2589 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002590 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002591 }
2592
gabor-mezei-armceface22021-01-21 12:26:17 +01002593 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2594 TEST_ASSERT( output_buffer_size <=
2595 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002596 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002597
Gilles Peskinee0866522019-02-19 19:44:00 +01002598 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002599 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2600 output, output_buffer_size,
2601 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002602 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002603 TEST_ASSERT( function_output_length <=
2604 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2605 TEST_ASSERT( function_output_length <=
2606 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002607 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002608
Gilles Peskine8817f612018-12-18 00:18:46 +01002609 PSA_ASSERT( psa_cipher_update( &operation,
2610 input->x + first_part_size,
2611 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002612 ( output_buffer_size == 0 ? NULL :
2613 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002614 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002615 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002616 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002617 TEST_ASSERT( function_output_length <=
2618 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2619 alg,
2620 input->len - first_part_size ) );
2621 TEST_ASSERT( function_output_length <=
2622 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002623 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002624
Gilles Peskine8817f612018-12-18 00:18:46 +01002625 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002626 ( output_buffer_size == 0 ? NULL :
2627 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002628 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002629 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002630 TEST_ASSERT( function_output_length <=
2631 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2632 TEST_ASSERT( function_output_length <=
2633 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002634 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002635 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002636
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002637 ASSERT_COMPARE( expected_output->x, expected_output->len,
2638 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002639
2640exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002641 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002642 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002643 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002644 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002645}
2646/* END_CASE */
2647
2648/* BEGIN_CASE */
2649void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002650 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002651 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002652 int first_part_size_arg,
2653 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002654 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002655{
Ronald Cron5425a212020-08-04 14:58:35 +02002656 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002657 psa_key_type_t key_type = key_type_arg;
2658 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002659 size_t first_part_size = first_part_size_arg;
2660 size_t output1_length = output1_length_arg;
2661 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002662 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002663 size_t output_buffer_size = 0;
2664 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002665 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002666 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002667 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002668
Gilles Peskine8817f612018-12-18 00:18:46 +01002669 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002670
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002671 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2672 psa_set_key_algorithm( &attributes, alg );
2673 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002674
Ronald Cron5425a212020-08-04 14:58:35 +02002675 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2676 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002677
Ronald Cron5425a212020-08-04 14:58:35 +02002678 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002679
Steven Cooreman177deba2020-09-07 17:14:14 +02002680 if( iv->len > 0 )
2681 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002682 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002683 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002684
gabor-mezei-armceface22021-01-21 12:26:17 +01002685 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2686 TEST_ASSERT( output_buffer_size <=
2687 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002688 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002689
Gilles Peskinee0866522019-02-19 19:44:00 +01002690 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002691 PSA_ASSERT( psa_cipher_update( &operation,
2692 input->x, first_part_size,
2693 output, output_buffer_size,
2694 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002695 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002696 TEST_ASSERT( function_output_length <=
2697 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2698 TEST_ASSERT( function_output_length <=
2699 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002700 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002701
Gilles Peskine8817f612018-12-18 00:18:46 +01002702 PSA_ASSERT( psa_cipher_update( &operation,
2703 input->x + first_part_size,
2704 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002705 ( output_buffer_size == 0 ? NULL :
2706 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002707 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002708 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002709 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002710 TEST_ASSERT( function_output_length <=
2711 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2712 alg,
2713 input->len - first_part_size ) );
2714 TEST_ASSERT( function_output_length <=
2715 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002716 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002717
Gilles Peskine8817f612018-12-18 00:18:46 +01002718 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002719 ( output_buffer_size == 0 ? NULL :
2720 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002721 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002722 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002723 TEST_ASSERT( function_output_length <=
2724 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2725 TEST_ASSERT( function_output_length <=
2726 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002727 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002728 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002729
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002730 ASSERT_COMPARE( expected_output->x, expected_output->len,
2731 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002732
2733exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002734 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002735 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002736 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002737 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002738}
2739/* END_CASE */
2740
Gilles Peskine50e586b2018-06-08 14:28:46 +02002741/* BEGIN_CASE */
2742void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002743 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002744 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002745 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002746{
Ronald Cron5425a212020-08-04 14:58:35 +02002747 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002748 psa_status_t status;
2749 psa_key_type_t key_type = key_type_arg;
2750 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002751 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002752 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002753 size_t output_buffer_size = 0;
2754 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002755 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002756 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002757 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002758
Gilles Peskine8817f612018-12-18 00:18:46 +01002759 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002760
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002761 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2762 psa_set_key_algorithm( &attributes, alg );
2763 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002764
Ronald Cron5425a212020-08-04 14:58:35 +02002765 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2766 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002767
Ronald Cron5425a212020-08-04 14:58:35 +02002768 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002769
Steven Cooreman177deba2020-09-07 17:14:14 +02002770 if( iv->len > 0 )
2771 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002772 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002773 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002774
gabor-mezei-armceface22021-01-21 12:26:17 +01002775 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2776 TEST_ASSERT( output_buffer_size <=
2777 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002778 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002779
Gilles Peskine8817f612018-12-18 00:18:46 +01002780 PSA_ASSERT( psa_cipher_update( &operation,
2781 input->x, input->len,
2782 output, output_buffer_size,
2783 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002784 TEST_ASSERT( function_output_length <=
2785 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2786 TEST_ASSERT( function_output_length <=
2787 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002788 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002789
Gilles Peskine50e586b2018-06-08 14:28:46 +02002790 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002791 ( output_buffer_size == 0 ? NULL :
2792 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002793 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002794 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002795 TEST_ASSERT( function_output_length <=
2796 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2797 TEST_ASSERT( function_output_length <=
2798 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002799 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002800 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002801
2802 if( expected_status == PSA_SUCCESS )
2803 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002804 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002805 ASSERT_COMPARE( expected_output->x, expected_output->len,
2806 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002807 }
2808
Gilles Peskine50e586b2018-06-08 14:28:46 +02002809exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002810 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002811 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002812 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002813 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002814}
2815/* END_CASE */
2816
Gilles Peskine50e586b2018-06-08 14:28:46 +02002817/* BEGIN_CASE */
2818void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002819 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002820 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002821{
Ronald Cron5425a212020-08-04 14:58:35 +02002822 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002823 psa_key_type_t key_type = key_type_arg;
2824 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002825 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002826 size_t iv_size = 16;
2827 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002828 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002829 size_t output1_size = 0;
2830 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002831 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002832 size_t output2_size = 0;
2833 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002834 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002835 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2836 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002837 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002838
Gilles Peskine8817f612018-12-18 00:18:46 +01002839 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002840
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002841 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2842 psa_set_key_algorithm( &attributes, alg );
2843 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002844
Ronald Cron5425a212020-08-04 14:58:35 +02002845 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2846 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002847
Ronald Cron5425a212020-08-04 14:58:35 +02002848 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2849 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002850
Steven Cooreman177deba2020-09-07 17:14:14 +02002851 if( alg != PSA_ALG_ECB_NO_PADDING )
2852 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002853 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2854 iv, iv_size,
2855 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002856 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002857 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2858 TEST_ASSERT( output1_size <=
2859 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002860 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002861
Gilles Peskine8817f612018-12-18 00:18:46 +01002862 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2863 output1, output1_size,
2864 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002865 TEST_ASSERT( output1_length <=
2866 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2867 TEST_ASSERT( output1_length <=
2868 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2869
Gilles Peskine8817f612018-12-18 00:18:46 +01002870 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002871 output1 + output1_length,
2872 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002873 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002874 TEST_ASSERT( function_output_length <=
2875 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2876 TEST_ASSERT( function_output_length <=
2877 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002878
Gilles Peskine048b7f02018-06-08 14:20:49 +02002879 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002880
Gilles Peskine8817f612018-12-18 00:18:46 +01002881 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002882
2883 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002884 TEST_ASSERT( output2_size <=
2885 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2886 TEST_ASSERT( output2_size <=
2887 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002888 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002889
Steven Cooreman177deba2020-09-07 17:14:14 +02002890 if( iv_length > 0 )
2891 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002892 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2893 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002894 }
2895
Gilles Peskine8817f612018-12-18 00:18:46 +01002896 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2897 output2, output2_size,
2898 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002899 TEST_ASSERT( output2_length <=
2900 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
2901 TEST_ASSERT( output2_length <=
2902 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
2903
Gilles Peskine048b7f02018-06-08 14:20:49 +02002904 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002905 PSA_ASSERT( psa_cipher_finish( &operation2,
2906 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002907 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002908 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002909 TEST_ASSERT( function_output_length <=
2910 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2911 TEST_ASSERT( function_output_length <=
2912 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03002913
Gilles Peskine048b7f02018-06-08 14:20:49 +02002914 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002915
Gilles Peskine8817f612018-12-18 00:18:46 +01002916 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002917
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002918 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002919
2920exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002921 psa_cipher_abort( &operation1 );
2922 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002923 mbedtls_free( output1 );
2924 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002925 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002926 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03002927}
2928/* END_CASE */
2929
2930/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002931void cipher_verify_output_multipart( int alg_arg,
2932 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002933 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002934 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002935 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03002936{
Ronald Cron5425a212020-08-04 14:58:35 +02002937 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002938 psa_key_type_t key_type = key_type_arg;
2939 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002940 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002941 unsigned char iv[16] = {0};
2942 size_t iv_size = 16;
2943 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002944 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002945 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002946 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002947 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002948 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002949 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002950 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002951 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2952 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002953 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002954
Gilles Peskine8817f612018-12-18 00:18:46 +01002955 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002956
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002957 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2958 psa_set_key_algorithm( &attributes, alg );
2959 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002960
Ronald Cron5425a212020-08-04 14:58:35 +02002961 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2962 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03002963
Ronald Cron5425a212020-08-04 14:58:35 +02002964 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2965 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002966
Steven Cooreman177deba2020-09-07 17:14:14 +02002967 if( alg != PSA_ALG_ECB_NO_PADDING )
2968 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002969 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2970 iv, iv_size,
2971 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002972 }
2973
gabor-mezei-armceface22021-01-21 12:26:17 +01002974 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2975 TEST_ASSERT( output1_buffer_size <=
2976 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002977 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002978
Gilles Peskinee0866522019-02-19 19:44:00 +01002979 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002980
Gilles Peskine8817f612018-12-18 00:18:46 +01002981 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2982 output1, output1_buffer_size,
2983 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002984 TEST_ASSERT( function_output_length <=
2985 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2986 TEST_ASSERT( function_output_length <=
2987 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002988 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002989
Gilles Peskine8817f612018-12-18 00:18:46 +01002990 PSA_ASSERT( psa_cipher_update( &operation1,
2991 input->x + first_part_size,
2992 input->len - first_part_size,
2993 output1, output1_buffer_size,
2994 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002995 TEST_ASSERT( function_output_length <=
2996 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2997 alg,
2998 input->len - first_part_size ) );
2999 TEST_ASSERT( function_output_length <=
3000 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003001 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003002
Gilles Peskine8817f612018-12-18 00:18:46 +01003003 PSA_ASSERT( psa_cipher_finish( &operation1,
3004 output1 + output1_length,
3005 output1_buffer_size - output1_length,
3006 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003007 TEST_ASSERT( function_output_length <=
3008 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3009 TEST_ASSERT( function_output_length <=
3010 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003011 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003012
Gilles Peskine8817f612018-12-18 00:18:46 +01003013 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003014
Gilles Peskine048b7f02018-06-08 14:20:49 +02003015 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003016 TEST_ASSERT( output2_buffer_size <=
3017 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3018 TEST_ASSERT( output2_buffer_size <=
3019 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003020 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003021
Steven Cooreman177deba2020-09-07 17:14:14 +02003022 if( iv_length > 0 )
3023 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003024 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3025 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003026 }
Moran Pekerded84402018-06-06 16:36:50 +03003027
Gilles Peskine8817f612018-12-18 00:18:46 +01003028 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3029 output2, output2_buffer_size,
3030 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003031 TEST_ASSERT( function_output_length <=
3032 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3033 TEST_ASSERT( function_output_length <=
3034 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003035 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003036
Gilles Peskine8817f612018-12-18 00:18:46 +01003037 PSA_ASSERT( psa_cipher_update( &operation2,
3038 output1 + first_part_size,
3039 output1_length - first_part_size,
3040 output2, output2_buffer_size,
3041 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003042 TEST_ASSERT( function_output_length <=
3043 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3044 alg,
3045 output1_length - first_part_size ) );
3046 TEST_ASSERT( function_output_length <=
3047 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003048 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003049
Gilles Peskine8817f612018-12-18 00:18:46 +01003050 PSA_ASSERT( psa_cipher_finish( &operation2,
3051 output2 + output2_length,
3052 output2_buffer_size - output2_length,
3053 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003054 TEST_ASSERT( function_output_length <=
3055 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3056 TEST_ASSERT( function_output_length <=
3057 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003058 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003059
Gilles Peskine8817f612018-12-18 00:18:46 +01003060 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003061
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003062 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003063
3064exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003065 psa_cipher_abort( &operation1 );
3066 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003067 mbedtls_free( output1 );
3068 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003069 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003070 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003071}
3072/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003073
Gilles Peskine20035e32018-02-03 22:44:14 +01003074/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003075void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003076 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003077 data_t *nonce,
3078 data_t *additional_data,
3079 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003080 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003081{
Ronald Cron5425a212020-08-04 14:58:35 +02003082 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003083 psa_key_type_t key_type = key_type_arg;
3084 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003085 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003086 unsigned char *output_data = NULL;
3087 size_t output_size = 0;
3088 size_t output_length = 0;
3089 unsigned char *output_data2 = NULL;
3090 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003091 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003092 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003093 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003094
Gilles Peskine8817f612018-12-18 00:18:46 +01003095 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003096
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003097 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3098 psa_set_key_algorithm( &attributes, alg );
3099 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003100
Gilles Peskine049c7532019-05-15 20:22:09 +02003101 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003102 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003103 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3104 key_bits = psa_get_key_bits( &attributes );
3105
3106 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3107 alg );
3108 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3109 * should be exact. */
3110 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3111 expected_result != PSA_ERROR_NOT_SUPPORTED )
3112 {
3113 TEST_EQUAL( output_size,
3114 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3115 TEST_ASSERT( output_size <=
3116 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3117 }
3118 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003119
Steven Cooremanf49478b2021-02-15 15:19:25 +01003120 status = psa_aead_encrypt( key, alg,
3121 nonce->x, nonce->len,
3122 additional_data->x,
3123 additional_data->len,
3124 input_data->x, input_data->len,
3125 output_data, output_size,
3126 &output_length );
3127
3128 /* If the operation is not supported, just skip and not fail in case the
3129 * encryption involves a common limitation of cryptography hardwares and
3130 * an alternative implementation. */
3131 if( status == PSA_ERROR_NOT_SUPPORTED )
3132 {
3133 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3134 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3135 }
3136
3137 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003138
3139 if( PSA_SUCCESS == expected_result )
3140 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003141 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003142
Gilles Peskine003a4a92019-05-14 16:09:40 +02003143 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3144 * should be exact. */
3145 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003146 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003147
gabor-mezei-armceface22021-01-21 12:26:17 +01003148 TEST_ASSERT( input_data->len <=
3149 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3150
Ronald Cron5425a212020-08-04 14:58:35 +02003151 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003152 nonce->x, nonce->len,
3153 additional_data->x,
3154 additional_data->len,
3155 output_data, output_length,
3156 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003157 &output_length2 ),
3158 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003159
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003160 ASSERT_COMPARE( input_data->x, input_data->len,
3161 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003162 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003163
Gilles Peskinea1cac842018-06-11 19:33:02 +02003164exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003165 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003166 mbedtls_free( output_data );
3167 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003168 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003169}
3170/* END_CASE */
3171
3172/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003173void aead_encrypt( int key_type_arg, data_t *key_data,
3174 int alg_arg,
3175 data_t *nonce,
3176 data_t *additional_data,
3177 data_t *input_data,
3178 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003179{
Ronald Cron5425a212020-08-04 14:58:35 +02003180 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003181 psa_key_type_t key_type = key_type_arg;
3182 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003183 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003184 unsigned char *output_data = NULL;
3185 size_t output_size = 0;
3186 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003187 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003188 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003189
Gilles Peskine8817f612018-12-18 00:18:46 +01003190 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003191
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003192 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3193 psa_set_key_algorithm( &attributes, alg );
3194 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003195
Gilles Peskine049c7532019-05-15 20:22:09 +02003196 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003197 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003198 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3199 key_bits = psa_get_key_bits( &attributes );
3200
3201 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3202 alg );
3203 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3204 * should be exact. */
3205 TEST_EQUAL( output_size,
3206 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3207 TEST_ASSERT( output_size <=
3208 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3209 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003210
Steven Cooremand588ea12021-01-11 19:36:04 +01003211 status = psa_aead_encrypt( key, alg,
3212 nonce->x, nonce->len,
3213 additional_data->x, additional_data->len,
3214 input_data->x, input_data->len,
3215 output_data, output_size,
3216 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003217
Ronald Cron28a45ed2021-02-09 20:35:42 +01003218 /* If the operation is not supported, just skip and not fail in case the
3219 * encryption involves a common limitation of cryptography hardwares and
3220 * an alternative implementation. */
3221 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003222 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003223 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3224 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003225 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003226
3227 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003228 ASSERT_COMPARE( expected_result->x, expected_result->len,
3229 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003230
Gilles Peskinea1cac842018-06-11 19:33:02 +02003231exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003232 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003233 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003234 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003235}
3236/* END_CASE */
3237
3238/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003239void aead_decrypt( int key_type_arg, data_t *key_data,
3240 int alg_arg,
3241 data_t *nonce,
3242 data_t *additional_data,
3243 data_t *input_data,
3244 data_t *expected_data,
3245 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003246{
Ronald Cron5425a212020-08-04 14:58:35 +02003247 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003248 psa_key_type_t key_type = key_type_arg;
3249 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003250 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003251 unsigned char *output_data = NULL;
3252 size_t output_size = 0;
3253 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003254 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003255 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003256 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003257
Gilles Peskine8817f612018-12-18 00:18:46 +01003258 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003259
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003260 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3261 psa_set_key_algorithm( &attributes, alg );
3262 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003263
Gilles Peskine049c7532019-05-15 20:22:09 +02003264 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003265 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003266 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3267 key_bits = psa_get_key_bits( &attributes );
3268
3269 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3270 alg );
3271 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3272 expected_result != PSA_ERROR_NOT_SUPPORTED )
3273 {
3274 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3275 * should be exact. */
3276 TEST_EQUAL( output_size,
3277 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3278 TEST_ASSERT( output_size <=
3279 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3280 }
3281 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003282
Steven Cooremand588ea12021-01-11 19:36:04 +01003283 status = psa_aead_decrypt( key, alg,
3284 nonce->x, nonce->len,
3285 additional_data->x,
3286 additional_data->len,
3287 input_data->x, input_data->len,
3288 output_data, output_size,
3289 &output_length );
3290
Ronald Cron28a45ed2021-02-09 20:35:42 +01003291 /* If the operation is not supported, just skip and not fail in case the
3292 * decryption involves a common limitation of cryptography hardwares and
3293 * an alternative implementation. */
3294 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003295 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003296 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3297 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003298 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003299
3300 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003301
Gilles Peskine2d277862018-06-18 15:41:12 +02003302 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003303 ASSERT_COMPARE( expected_data->x, expected_data->len,
3304 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003305
Gilles Peskinea1cac842018-06-11 19:33:02 +02003306exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003307 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003308 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003309 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003310}
3311/* END_CASE */
3312
3313/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003314void signature_size( int type_arg,
3315 int bits,
3316 int alg_arg,
3317 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003318{
3319 psa_key_type_t type = type_arg;
3320 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003321 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003322
Gilles Peskinefe11b722018-12-18 00:24:04 +01003323 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003324
Gilles Peskinee59236f2018-01-27 23:32:46 +01003325exit:
3326 ;
3327}
3328/* END_CASE */
3329
3330/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003331void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3332 int alg_arg, data_t *input_data,
3333 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003334{
Ronald Cron5425a212020-08-04 14:58:35 +02003335 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003336 psa_key_type_t key_type = key_type_arg;
3337 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003338 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003339 unsigned char *signature = NULL;
3340 size_t signature_size;
3341 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003342 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003343
Gilles Peskine8817f612018-12-18 00:18:46 +01003344 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003345
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003346 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003347 psa_set_key_algorithm( &attributes, alg );
3348 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003349
Gilles Peskine049c7532019-05-15 20:22:09 +02003350 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003351 &key ) );
3352 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003353 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003354
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003355 /* Allocate a buffer which has the size advertized by the
3356 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003357 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003358 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003359 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003360 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003361 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003362
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003363 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003364 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003365 input_data->x, input_data->len,
3366 signature, signature_size,
3367 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003368 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003369 ASSERT_COMPARE( output_data->x, output_data->len,
3370 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003371
3372exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003373 /*
3374 * Key attributes may have been returned by psa_get_key_attributes()
3375 * thus reset them as required.
3376 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003377 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003378
Ronald Cron5425a212020-08-04 14:58:35 +02003379 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003380 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003381 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003382}
3383/* END_CASE */
3384
3385/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003386void sign_hash_fail( int key_type_arg, data_t *key_data,
3387 int alg_arg, data_t *input_data,
3388 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003389{
Ronald Cron5425a212020-08-04 14:58:35 +02003390 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003391 psa_key_type_t key_type = key_type_arg;
3392 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003393 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003394 psa_status_t actual_status;
3395 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003396 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003397 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003398 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003399
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003400 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003401
Gilles Peskine8817f612018-12-18 00:18:46 +01003402 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003403
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003404 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003405 psa_set_key_algorithm( &attributes, alg );
3406 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003407
Gilles Peskine049c7532019-05-15 20:22:09 +02003408 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003409 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003410
Ronald Cron5425a212020-08-04 14:58:35 +02003411 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003412 input_data->x, input_data->len,
3413 signature, signature_size,
3414 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003415 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003416 /* The value of *signature_length is unspecified on error, but
3417 * whatever it is, it should be less than signature_size, so that
3418 * if the caller tries to read *signature_length bytes without
3419 * checking the error code then they don't overflow a buffer. */
3420 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003421
3422exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003423 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003424 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003425 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003426 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003427}
3428/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003429
3430/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003431void sign_verify_hash( int key_type_arg, data_t *key_data,
3432 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003433{
Ronald Cron5425a212020-08-04 14:58:35 +02003434 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003435 psa_key_type_t key_type = key_type_arg;
3436 psa_algorithm_t alg = alg_arg;
3437 size_t key_bits;
3438 unsigned char *signature = NULL;
3439 size_t signature_size;
3440 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003441 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003442
Gilles Peskine8817f612018-12-18 00:18:46 +01003443 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003444
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003445 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003446 psa_set_key_algorithm( &attributes, alg );
3447 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003448
Gilles Peskine049c7532019-05-15 20:22:09 +02003449 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003450 &key ) );
3451 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003452 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003453
3454 /* Allocate a buffer which has the size advertized by the
3455 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003456 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003457 key_bits, alg );
3458 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003459 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003460 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003461
3462 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003463 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003464 input_data->x, input_data->len,
3465 signature, signature_size,
3466 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003467 /* Check that the signature length looks sensible. */
3468 TEST_ASSERT( signature_length <= signature_size );
3469 TEST_ASSERT( signature_length > 0 );
3470
3471 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003472 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003473 input_data->x, input_data->len,
3474 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003475
3476 if( input_data->len != 0 )
3477 {
3478 /* Flip a bit in the input and verify that the signature is now
3479 * detected as invalid. Flip a bit at the beginning, not at the end,
3480 * because ECDSA may ignore the last few bits of the input. */
3481 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003482 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003483 input_data->x, input_data->len,
3484 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003485 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003486 }
3487
3488exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003489 /*
3490 * Key attributes may have been returned by psa_get_key_attributes()
3491 * thus reset them as required.
3492 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003493 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003494
Ronald Cron5425a212020-08-04 14:58:35 +02003495 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003496 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003497 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003498}
3499/* END_CASE */
3500
3501/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003502void verify_hash( int key_type_arg, data_t *key_data,
3503 int alg_arg, data_t *hash_data,
3504 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003505{
Ronald Cron5425a212020-08-04 14:58:35 +02003506 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003507 psa_key_type_t key_type = key_type_arg;
3508 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003509 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003510
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003511 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003512
Gilles Peskine8817f612018-12-18 00:18:46 +01003513 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003514
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003515 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003516 psa_set_key_algorithm( &attributes, alg );
3517 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003518
Gilles Peskine049c7532019-05-15 20:22:09 +02003519 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003520 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003521
Ronald Cron5425a212020-08-04 14:58:35 +02003522 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003523 hash_data->x, hash_data->len,
3524 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003525
itayzafrir5c753392018-05-08 11:18:38 +03003526exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003527 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003528 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003529 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003530}
3531/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003532
3533/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003534void verify_hash_fail( int key_type_arg, data_t *key_data,
3535 int alg_arg, data_t *hash_data,
3536 data_t *signature_data,
3537 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003538{
Ronald Cron5425a212020-08-04 14:58:35 +02003539 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003540 psa_key_type_t key_type = key_type_arg;
3541 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003542 psa_status_t actual_status;
3543 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003544 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003545
Gilles Peskine8817f612018-12-18 00:18:46 +01003546 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003547
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003548 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003549 psa_set_key_algorithm( &attributes, alg );
3550 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003551
Gilles Peskine049c7532019-05-15 20:22:09 +02003552 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003553 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003554
Ronald Cron5425a212020-08-04 14:58:35 +02003555 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003556 hash_data->x, hash_data->len,
3557 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003558 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003559
3560exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003561 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003562 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003563 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003564}
3565/* END_CASE */
3566
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003567/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02003568void sign_message_deterministic( int key_type_arg,
3569 data_t *key_data,
3570 int alg_arg,
3571 data_t *input_data,
3572 data_t *output_data )
3573{
3574 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3575 psa_key_type_t key_type = key_type_arg;
3576 psa_algorithm_t alg = alg_arg;
3577 size_t key_bits;
3578 unsigned char *signature = NULL;
3579 size_t signature_size;
3580 size_t signature_length = 0xdeadbeef;
3581 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3582
3583 PSA_ASSERT( psa_crypto_init( ) );
3584
3585 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3586 psa_set_key_algorithm( &attributes, alg );
3587 psa_set_key_type( &attributes, key_type );
3588
3589 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3590 &key ) );
3591 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3592 key_bits = psa_get_key_bits( &attributes );
3593
3594 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3595 TEST_ASSERT( signature_size != 0 );
3596 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3597 ASSERT_ALLOC( signature, signature_size );
3598
3599 PSA_ASSERT( psa_sign_message( key, alg,
3600 input_data->x, input_data->len,
3601 signature, signature_size,
3602 &signature_length ) );
3603
3604 ASSERT_COMPARE( output_data->x, output_data->len,
3605 signature, signature_length );
3606
3607exit:
3608 psa_reset_key_attributes( &attributes );
3609
3610 psa_destroy_key( key );
3611 mbedtls_free( signature );
3612 PSA_DONE( );
3613
3614}
3615/* END_CASE */
3616
3617/* BEGIN_CASE */
3618void sign_message_fail( int key_type_arg,
3619 data_t *key_data,
3620 int alg_arg,
3621 data_t *input_data,
3622 int signature_size_arg,
3623 int expected_status_arg )
3624{
3625 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3626 psa_key_type_t key_type = key_type_arg;
3627 psa_algorithm_t alg = alg_arg;
3628 size_t signature_size = signature_size_arg;
3629 psa_status_t actual_status;
3630 psa_status_t expected_status = expected_status_arg;
3631 unsigned char *signature = NULL;
3632 size_t signature_length = 0xdeadbeef;
3633 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3634
3635 ASSERT_ALLOC( signature, signature_size );
3636
3637 PSA_ASSERT( psa_crypto_init( ) );
3638
3639 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3640 psa_set_key_algorithm( &attributes, alg );
3641 psa_set_key_type( &attributes, key_type );
3642
3643 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3644 &key ) );
3645
3646 actual_status = psa_sign_message( key, alg,
3647 input_data->x, input_data->len,
3648 signature, signature_size,
3649 &signature_length );
3650 TEST_EQUAL( actual_status, expected_status );
3651 /* The value of *signature_length is unspecified on error, but
3652 * whatever it is, it should be less than signature_size, so that
3653 * if the caller tries to read *signature_length bytes without
3654 * checking the error code then they don't overflow a buffer. */
3655 TEST_ASSERT( signature_length <= signature_size );
3656
3657exit:
3658 psa_reset_key_attributes( &attributes );
3659 psa_destroy_key( key );
3660 mbedtls_free( signature );
3661 PSA_DONE( );
3662}
3663/* END_CASE */
3664
3665/* BEGIN_CASE */
3666void sign_verify_message( int key_type_arg,
3667 data_t *key_data,
3668 int alg_arg,
3669 data_t *input_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_KEY_USAGE_VERIFY_MESSAGE );
3684 psa_set_key_algorithm( &attributes, alg );
3685 psa_set_key_type( &attributes, key_type );
3686
3687 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3688 &key ) );
3689 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3690 key_bits = psa_get_key_bits( &attributes );
3691
3692 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3693 TEST_ASSERT( signature_size != 0 );
3694 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3695 ASSERT_ALLOC( signature, signature_size );
3696
3697 PSA_ASSERT( psa_sign_message( key, alg,
3698 input_data->x, input_data->len,
3699 signature, signature_size,
3700 &signature_length ) );
3701 TEST_ASSERT( signature_length <= signature_size );
3702 TEST_ASSERT( signature_length > 0 );
3703
3704 PSA_ASSERT( psa_verify_message( key, alg,
3705 input_data->x, input_data->len,
3706 signature, signature_length ) );
3707
3708 if( input_data->len != 0 )
3709 {
3710 /* Flip a bit in the input and verify that the signature is now
3711 * detected as invalid. Flip a bit at the beginning, not at the end,
3712 * because ECDSA may ignore the last few bits of the input. */
3713 input_data->x[0] ^= 1;
3714 TEST_EQUAL( psa_verify_message( key, alg,
3715 input_data->x, input_data->len,
3716 signature, signature_length ),
3717 PSA_ERROR_INVALID_SIGNATURE );
3718 }
3719
3720exit:
3721 psa_reset_key_attributes( &attributes );
3722
3723 psa_destroy_key( key );
3724 mbedtls_free( signature );
3725 PSA_DONE( );
3726}
3727/* END_CASE */
3728
3729/* BEGIN_CASE */
3730void verify_message( int key_type_arg,
3731 data_t *key_data,
3732 int alg_arg,
3733 data_t *input_data,
3734 data_t *signature_data )
3735{
3736 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3737 psa_key_type_t key_type = key_type_arg;
3738 psa_algorithm_t alg = alg_arg;
3739 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3740
3741 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3742
3743 PSA_ASSERT( psa_crypto_init( ) );
3744
3745 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3746 psa_set_key_algorithm( &attributes, alg );
3747 psa_set_key_type( &attributes, key_type );
3748
3749 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3750 &key ) );
3751
3752 PSA_ASSERT( psa_verify_message( key, alg,
3753 input_data->x, input_data->len,
3754 signature_data->x, signature_data->len ) );
3755
3756exit:
3757 psa_reset_key_attributes( &attributes );
3758 psa_destroy_key( key );
3759 PSA_DONE( );
3760}
3761/* END_CASE */
3762
3763/* BEGIN_CASE */
3764void verify_message_fail( int key_type_arg,
3765 data_t *key_data,
3766 int alg_arg,
3767 data_t *hash_data,
3768 data_t *signature_data,
3769 int expected_status_arg )
3770{
3771 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3772 psa_key_type_t key_type = key_type_arg;
3773 psa_algorithm_t alg = alg_arg;
3774 psa_status_t actual_status;
3775 psa_status_t expected_status = expected_status_arg;
3776 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3777
3778 PSA_ASSERT( psa_crypto_init( ) );
3779
3780 psa_set_key_usage_flags( &attributes, 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
3787 actual_status = psa_verify_message( key, alg,
3788 hash_data->x, hash_data->len,
3789 signature_data->x,
3790 signature_data->len );
3791 TEST_EQUAL( actual_status, expected_status );
3792
3793exit:
3794 psa_reset_key_attributes( &attributes );
3795 psa_destroy_key( key );
3796 PSA_DONE( );
3797}
3798/* END_CASE */
3799
3800/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003801void asymmetric_encrypt( int key_type_arg,
3802 data_t *key_data,
3803 int alg_arg,
3804 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003805 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003806 int expected_output_length_arg,
3807 int expected_status_arg )
3808{
Ronald Cron5425a212020-08-04 14:58:35 +02003809 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003810 psa_key_type_t key_type = key_type_arg;
3811 psa_algorithm_t alg = alg_arg;
3812 size_t expected_output_length = expected_output_length_arg;
3813 size_t key_bits;
3814 unsigned char *output = NULL;
3815 size_t output_size;
3816 size_t output_length = ~0;
3817 psa_status_t actual_status;
3818 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003819 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003820
Gilles Peskine8817f612018-12-18 00:18:46 +01003821 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003822
Gilles Peskine656896e2018-06-29 19:12:28 +02003823 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003824 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3825 psa_set_key_algorithm( &attributes, alg );
3826 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003827 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003828 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003829
3830 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003831 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003832 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003833
Gilles Peskine656896e2018-06-29 19:12:28 +02003834 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003835 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003836 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003837
3838 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003839 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003840 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003841 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003842 output, output_size,
3843 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003844 TEST_EQUAL( actual_status, expected_status );
3845 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003846
Gilles Peskine68428122018-06-30 18:42:41 +02003847 /* If the label is empty, the test framework puts a non-null pointer
3848 * in label->x. Test that a null pointer works as well. */
3849 if( label->len == 0 )
3850 {
3851 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003852 if( output_size != 0 )
3853 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003854 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003855 input_data->x, input_data->len,
3856 NULL, label->len,
3857 output, output_size,
3858 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003859 TEST_EQUAL( actual_status, expected_status );
3860 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003861 }
3862
Gilles Peskine656896e2018-06-29 19:12:28 +02003863exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003864 /*
3865 * Key attributes may have been returned by psa_get_key_attributes()
3866 * thus reset them as required.
3867 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003868 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003869
Ronald Cron5425a212020-08-04 14:58:35 +02003870 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02003871 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003872 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003873}
3874/* END_CASE */
3875
3876/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003877void asymmetric_encrypt_decrypt( int key_type_arg,
3878 data_t *key_data,
3879 int alg_arg,
3880 data_t *input_data,
3881 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003882{
Ronald Cron5425a212020-08-04 14:58:35 +02003883 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003884 psa_key_type_t key_type = key_type_arg;
3885 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003886 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003887 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003888 size_t output_size;
3889 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003890 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003891 size_t output2_size;
3892 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003893 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003894
Gilles Peskine8817f612018-12-18 00:18:46 +01003895 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003896
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003897 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3898 psa_set_key_algorithm( &attributes, alg );
3899 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003900
Gilles Peskine049c7532019-05-15 20:22:09 +02003901 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003902 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003903
3904 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02003905 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003906 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003907
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003908 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003909 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003910 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01003911
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003912 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01003913 TEST_ASSERT( output2_size <=
3914 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
3915 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003916 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003917
Gilles Peskineeebd7382018-06-08 18:11:54 +02003918 /* We test encryption by checking that encrypt-then-decrypt gives back
3919 * the original plaintext because of the non-optional random
3920 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02003921 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003922 input_data->x, input_data->len,
3923 label->x, label->len,
3924 output, output_size,
3925 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003926 /* We don't know what ciphertext length to expect, but check that
3927 * it looks sensible. */
3928 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003929
Ronald Cron5425a212020-08-04 14:58:35 +02003930 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003931 output, output_length,
3932 label->x, label->len,
3933 output2, output2_size,
3934 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003935 ASSERT_COMPARE( input_data->x, input_data->len,
3936 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003937
3938exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003939 /*
3940 * Key attributes may have been returned by psa_get_key_attributes()
3941 * thus reset them as required.
3942 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003943 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003944
Ronald Cron5425a212020-08-04 14:58:35 +02003945 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003946 mbedtls_free( output );
3947 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003948 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003949}
3950/* END_CASE */
3951
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003952/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003953void asymmetric_decrypt( int key_type_arg,
3954 data_t *key_data,
3955 int alg_arg,
3956 data_t *input_data,
3957 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003958 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003959{
Ronald Cron5425a212020-08-04 14:58:35 +02003960 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003961 psa_key_type_t key_type = key_type_arg;
3962 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01003963 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003964 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003965 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003966 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003967 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003968
Gilles Peskine8817f612018-12-18 00:18:46 +01003969 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003970
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003971 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3972 psa_set_key_algorithm( &attributes, alg );
3973 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003974
Gilles Peskine049c7532019-05-15 20:22:09 +02003975 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003976 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003977
gabor-mezei-armceface22021-01-21 12:26:17 +01003978 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3979 key_bits = psa_get_key_bits( &attributes );
3980
3981 /* Determine the maximum ciphertext length */
3982 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
3983 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
3984 ASSERT_ALLOC( output, output_size );
3985
Ronald Cron5425a212020-08-04 14:58:35 +02003986 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003987 input_data->x, input_data->len,
3988 label->x, label->len,
3989 output,
3990 output_size,
3991 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003992 ASSERT_COMPARE( expected_data->x, expected_data->len,
3993 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003994
Gilles Peskine68428122018-06-30 18:42:41 +02003995 /* If the label is empty, the test framework puts a non-null pointer
3996 * in label->x. Test that a null pointer works as well. */
3997 if( label->len == 0 )
3998 {
3999 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004000 if( output_size != 0 )
4001 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004002 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004003 input_data->x, input_data->len,
4004 NULL, label->len,
4005 output,
4006 output_size,
4007 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004008 ASSERT_COMPARE( expected_data->x, expected_data->len,
4009 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004010 }
4011
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004012exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004013 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004014 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004015 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004016 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004017}
4018/* END_CASE */
4019
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004020/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004021void asymmetric_decrypt_fail( int key_type_arg,
4022 data_t *key_data,
4023 int alg_arg,
4024 data_t *input_data,
4025 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004026 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004027 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004028{
Ronald Cron5425a212020-08-04 14:58:35 +02004029 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004030 psa_key_type_t key_type = key_type_arg;
4031 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004032 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004033 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004034 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004035 psa_status_t actual_status;
4036 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004037 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004038
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004039 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004040
Gilles Peskine8817f612018-12-18 00:18:46 +01004041 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004042
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004043 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4044 psa_set_key_algorithm( &attributes, alg );
4045 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004046
Gilles Peskine049c7532019-05-15 20:22:09 +02004047 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004048 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004049
Ronald Cron5425a212020-08-04 14:58:35 +02004050 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004051 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004052 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004053 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004054 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004055 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004056 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004057
Gilles Peskine68428122018-06-30 18:42:41 +02004058 /* If the label is empty, the test framework puts a non-null pointer
4059 * in label->x. Test that a null pointer works as well. */
4060 if( label->len == 0 )
4061 {
4062 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004063 if( output_size != 0 )
4064 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004065 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004066 input_data->x, input_data->len,
4067 NULL, label->len,
4068 output, output_size,
4069 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004070 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004071 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004072 }
4073
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004074exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004075 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004076 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004077 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004078 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004079}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004080/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004081
4082/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004083void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004084{
4085 /* Test each valid way of initializing the object, except for `= {0}`, as
4086 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4087 * though it's OK by the C standard. We could test for this, but we'd need
4088 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004089 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004090 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4091 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4092 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004093
4094 memset( &zero, 0, sizeof( zero ) );
4095
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004096 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004097 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004098 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004099 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004100 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004101 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004102 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004103
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004104 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004105 PSA_ASSERT( psa_key_derivation_abort(&func) );
4106 PSA_ASSERT( psa_key_derivation_abort(&init) );
4107 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004108}
4109/* END_CASE */
4110
Janos Follath16de4a42019-06-13 16:32:24 +01004111/* BEGIN_CASE */
4112void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004113{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004114 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004115 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004116 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004117
Gilles Peskine8817f612018-12-18 00:18:46 +01004118 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004119
Janos Follath16de4a42019-06-13 16:32:24 +01004120 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004121 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004122
4123exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004124 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004125 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004126}
4127/* END_CASE */
4128
Janos Follathaf3c2a02019-06-12 12:34:34 +01004129/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004130void derive_set_capacity( int alg_arg, int capacity_arg,
4131 int expected_status_arg )
4132{
4133 psa_algorithm_t alg = alg_arg;
4134 size_t capacity = capacity_arg;
4135 psa_status_t expected_status = expected_status_arg;
4136 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4137
4138 PSA_ASSERT( psa_crypto_init( ) );
4139
4140 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4141
4142 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4143 expected_status );
4144
4145exit:
4146 psa_key_derivation_abort( &operation );
4147 PSA_DONE( );
4148}
4149/* END_CASE */
4150
4151/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004152void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004153 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004154 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004155 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004156 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004157 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004158 int expected_status_arg3,
4159 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004160{
4161 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004162 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4163 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004164 psa_status_t expected_statuses[] = {expected_status_arg1,
4165 expected_status_arg2,
4166 expected_status_arg3};
4167 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004168 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4169 MBEDTLS_SVC_KEY_ID_INIT,
4170 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004171 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4172 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4173 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004174 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004175 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004176 psa_status_t expected_output_status = expected_output_status_arg;
4177 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004178
4179 PSA_ASSERT( psa_crypto_init( ) );
4180
4181 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4182 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004183
4184 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4185
4186 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4187 {
Gilles Peskine4023c012021-05-27 13:21:20 +02004188 mbedtls_test_set_step( i );
4189 if( steps[i] == 0 )
4190 {
4191 /* Skip this step */
4192 }
4193 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004194 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004195 psa_set_key_type( &attributes, key_types[i] );
4196 PSA_ASSERT( psa_import_key( &attributes,
4197 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004198 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004199 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4200 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4201 {
4202 // When taking a private key as secret input, use key agreement
4203 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004204 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4205 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004206 expected_statuses[i] );
4207 }
4208 else
4209 {
4210 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004211 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004212 expected_statuses[i] );
4213 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004214 }
4215 else
4216 {
4217 TEST_EQUAL( psa_key_derivation_input_bytes(
4218 &operation, steps[i],
4219 inputs[i]->x, inputs[i]->len ),
4220 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004221 }
4222 }
4223
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004224 if( output_key_type != PSA_KEY_TYPE_NONE )
4225 {
4226 psa_reset_key_attributes( &attributes );
4227 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4228 psa_set_key_bits( &attributes, 8 );
4229 actual_output_status =
4230 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004231 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004232 }
4233 else
4234 {
4235 uint8_t buffer[1];
4236 actual_output_status =
4237 psa_key_derivation_output_bytes( &operation,
4238 buffer, sizeof( buffer ) );
4239 }
4240 TEST_EQUAL( actual_output_status, expected_output_status );
4241
Janos Follathaf3c2a02019-06-12 12:34:34 +01004242exit:
4243 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004244 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4245 psa_destroy_key( keys[i] );
4246 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004247 PSA_DONE( );
4248}
4249/* END_CASE */
4250
Janos Follathd958bb72019-07-03 15:02:16 +01004251/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02004252void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004253{
Janos Follathd958bb72019-07-03 15:02:16 +01004254 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004255 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004256 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004257 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004258 unsigned char input1[] = "Input 1";
4259 size_t input1_length = sizeof( input1 );
4260 unsigned char input2[] = "Input 2";
4261 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004262 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004263 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004264 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4265 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4266 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004267 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004268
Gilles Peskine8817f612018-12-18 00:18:46 +01004269 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004270
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004271 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4272 psa_set_key_algorithm( &attributes, alg );
4273 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004274
Gilles Peskine73676cb2019-05-15 20:15:10 +02004275 PSA_ASSERT( psa_import_key( &attributes,
4276 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004277 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004278
4279 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004280 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4281 input1, input1_length,
4282 input2, input2_length,
4283 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004284 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004285
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004286 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004287 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004288 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004289
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004290 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004291
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004292 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004293 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004294
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004295exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004296 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004297 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004298 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004299}
4300/* END_CASE */
4301
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004302/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02004303void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004304{
4305 uint8_t output_buffer[16];
4306 size_t buffer_size = 16;
4307 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004308 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004309
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004310 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4311 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004312 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004313
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004314 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004315 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004316
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004317 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004318
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004319 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4320 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004321 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004322
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004323 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004324 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004325
4326exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004327 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004328}
4329/* END_CASE */
4330
4331/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004332void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004333 int step1_arg, data_t *input1,
4334 int step2_arg, data_t *input2,
4335 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004336 int requested_capacity_arg,
4337 data_t *expected_output1,
4338 data_t *expected_output2 )
4339{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004340 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004341 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4342 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004343 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4344 MBEDTLS_SVC_KEY_ID_INIT,
4345 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004346 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004347 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004348 uint8_t *expected_outputs[2] =
4349 {expected_output1->x, expected_output2->x};
4350 size_t output_sizes[2] =
4351 {expected_output1->len, expected_output2->len};
4352 size_t output_buffer_size = 0;
4353 uint8_t *output_buffer = NULL;
4354 size_t expected_capacity;
4355 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004356 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004357 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004358 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004359
4360 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4361 {
4362 if( output_sizes[i] > output_buffer_size )
4363 output_buffer_size = output_sizes[i];
4364 if( output_sizes[i] == 0 )
4365 expected_outputs[i] = NULL;
4366 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004367 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004368 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004369
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004370 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4371 psa_set_key_algorithm( &attributes, alg );
4372 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004373
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004374 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004375 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4376 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4377 requested_capacity ) );
4378 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004379 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004380 switch( steps[i] )
4381 {
4382 case 0:
4383 break;
4384 case PSA_KEY_DERIVATION_INPUT_SECRET:
4385 PSA_ASSERT( psa_import_key( &attributes,
4386 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004387 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004388
4389 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4390 {
4391 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4392 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4393 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4394 }
4395
Gilles Peskine1468da72019-05-29 17:35:49 +02004396 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004397 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004398 break;
4399 default:
4400 PSA_ASSERT( psa_key_derivation_input_bytes(
4401 &operation, steps[i],
4402 inputs[i]->x, inputs[i]->len ) );
4403 break;
4404 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004405 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004406
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004407 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004408 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004409 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004410 expected_capacity = requested_capacity;
4411
4412 /* Expansion phase. */
4413 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4414 {
4415 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004416 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004417 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004418 if( expected_capacity == 0 && output_sizes[i] == 0 )
4419 {
4420 /* Reading 0 bytes when 0 bytes are available can go either way. */
4421 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004422 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004423 continue;
4424 }
4425 else if( expected_capacity == 0 ||
4426 output_sizes[i] > expected_capacity )
4427 {
4428 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004429 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004430 expected_capacity = 0;
4431 continue;
4432 }
4433 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004434 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004435 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004436 ASSERT_COMPARE( output_buffer, output_sizes[i],
4437 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004438 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004439 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004440 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004441 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004442 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004443 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004444 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004445
4446exit:
4447 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004448 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004449 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4450 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004451 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004452}
4453/* END_CASE */
4454
4455/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004456void derive_full( int alg_arg,
4457 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004458 data_t *input1,
4459 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004460 int requested_capacity_arg )
4461{
Ronald Cron5425a212020-08-04 14:58:35 +02004462 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004463 psa_algorithm_t alg = alg_arg;
4464 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004465 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004466 unsigned char output_buffer[16];
4467 size_t expected_capacity = requested_capacity;
4468 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004469 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004470
Gilles Peskine8817f612018-12-18 00:18:46 +01004471 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004472
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004473 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4474 psa_set_key_algorithm( &attributes, alg );
4475 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004476
Gilles Peskine049c7532019-05-15 20:22:09 +02004477 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004478 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004479
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004480 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4481 input1->x, input1->len,
4482 input2->x, input2->len,
4483 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004484 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004485
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004486 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004487 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004488 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004489
4490 /* Expansion phase. */
4491 while( current_capacity > 0 )
4492 {
4493 size_t read_size = sizeof( output_buffer );
4494 if( read_size > current_capacity )
4495 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004496 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004497 output_buffer,
4498 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004499 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004500 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004501 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004502 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004503 }
4504
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004505 /* Check that the operation refuses to go over capacity. */
4506 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004507 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004508
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004509 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004510
4511exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004512 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004513 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004514 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004515}
4516/* END_CASE */
4517
Janos Follathe60c9052019-07-03 13:51:30 +01004518/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004519void derive_key_exercise( int alg_arg,
4520 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004521 data_t *input1,
4522 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004523 int derived_type_arg,
4524 int derived_bits_arg,
4525 int derived_usage_arg,
4526 int derived_alg_arg )
4527{
Ronald Cron5425a212020-08-04 14:58:35 +02004528 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4529 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004530 psa_algorithm_t alg = alg_arg;
4531 psa_key_type_t derived_type = derived_type_arg;
4532 size_t derived_bits = derived_bits_arg;
4533 psa_key_usage_t derived_usage = derived_usage_arg;
4534 psa_algorithm_t derived_alg = derived_alg_arg;
4535 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004536 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004537 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004538 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004539
Gilles Peskine8817f612018-12-18 00:18:46 +01004540 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004541
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004542 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4543 psa_set_key_algorithm( &attributes, alg );
4544 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004545 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004546 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004547
4548 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004549 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4550 input1->x, input1->len,
4551 input2->x, input2->len,
4552 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004553 goto exit;
4554
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004555 psa_set_key_usage_flags( &attributes, derived_usage );
4556 psa_set_key_algorithm( &attributes, derived_alg );
4557 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004558 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004559 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004560 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004561
4562 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004563 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004564 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4565 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004566
4567 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004568 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004569 goto exit;
4570
4571exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004572 /*
4573 * Key attributes may have been returned by psa_get_key_attributes()
4574 * thus reset them as required.
4575 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004576 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004577
4578 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004579 psa_destroy_key( base_key );
4580 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004581 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004582}
4583/* END_CASE */
4584
Janos Follath42fd8882019-07-03 14:17:09 +01004585/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004586void derive_key_export( int alg_arg,
4587 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004588 data_t *input1,
4589 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004590 int bytes1_arg,
4591 int bytes2_arg )
4592{
Ronald Cron5425a212020-08-04 14:58:35 +02004593 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4594 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004595 psa_algorithm_t alg = alg_arg;
4596 size_t bytes1 = bytes1_arg;
4597 size_t bytes2 = bytes2_arg;
4598 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004599 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004600 uint8_t *output_buffer = NULL;
4601 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004602 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4603 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004604 size_t length;
4605
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004606 ASSERT_ALLOC( output_buffer, capacity );
4607 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004608 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004609
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004610 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4611 psa_set_key_algorithm( &base_attributes, alg );
4612 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004613 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004614 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004615
4616 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004617 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4618 input1->x, input1->len,
4619 input2->x, input2->len,
4620 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004621 goto exit;
4622
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004623 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004624 output_buffer,
4625 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004626 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004627
4628 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004629 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4630 input1->x, input1->len,
4631 input2->x, input2->len,
4632 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004633 goto exit;
4634
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004635 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4636 psa_set_key_algorithm( &derived_attributes, 0 );
4637 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004638 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004639 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004640 &derived_key ) );
4641 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004642 export_buffer, bytes1,
4643 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004644 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004645 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004646 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004647 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004648 &derived_key ) );
4649 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004650 export_buffer + bytes1, bytes2,
4651 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004652 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004653
4654 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004655 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4656 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004657
4658exit:
4659 mbedtls_free( output_buffer );
4660 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004661 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004662 psa_destroy_key( base_key );
4663 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004664 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004665}
4666/* END_CASE */
4667
4668/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004669void derive_key( int alg_arg,
4670 data_t *key_data, data_t *input1, data_t *input2,
4671 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004672 int expected_status_arg,
4673 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004674{
Ronald Cron5425a212020-08-04 14:58:35 +02004675 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4676 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004677 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004678 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004679 size_t bits = bits_arg;
4680 psa_status_t expected_status = expected_status_arg;
4681 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4682 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4683 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4684
4685 PSA_ASSERT( psa_crypto_init( ) );
4686
4687 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4688 psa_set_key_algorithm( &base_attributes, alg );
4689 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4690 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004691 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004692
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004693 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4694 input1->x, input1->len,
4695 input2->x, input2->len,
4696 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004697 goto exit;
4698
4699 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4700 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004701 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004702 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004703
4704 psa_status_t status =
4705 psa_key_derivation_output_key( &derived_attributes,
4706 &operation,
4707 &derived_key );
4708 if( is_large_output > 0 )
4709 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4710 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004711
4712exit:
4713 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004714 psa_destroy_key( base_key );
4715 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004716 PSA_DONE( );
4717}
4718/* END_CASE */
4719
4720/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004721void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004722 int our_key_type_arg, int our_key_alg_arg,
4723 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004724 int expected_status_arg )
4725{
Ronald Cron5425a212020-08-04 14:58:35 +02004726 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004727 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004728 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004729 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004730 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004731 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004732 psa_status_t expected_status = expected_status_arg;
4733 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004734
Gilles Peskine8817f612018-12-18 00:18:46 +01004735 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004736
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004737 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004738 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004739 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004740 PSA_ASSERT( psa_import_key( &attributes,
4741 our_key_data->x, our_key_data->len,
4742 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004743
Gilles Peskine77f40d82019-04-11 21:27:06 +02004744 /* The tests currently include inputs that should fail at either step.
4745 * Test cases that fail at the setup step should be changed to call
4746 * key_derivation_setup instead, and this function should be renamed
4747 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004748 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004749 if( status == PSA_SUCCESS )
4750 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004751 TEST_EQUAL( psa_key_derivation_key_agreement(
4752 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4753 our_key,
4754 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004755 expected_status );
4756 }
4757 else
4758 {
4759 TEST_ASSERT( status == expected_status );
4760 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004761
4762exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004763 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004764 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004765 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004766}
4767/* END_CASE */
4768
4769/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004770void raw_key_agreement( int alg_arg,
4771 int our_key_type_arg, data_t *our_key_data,
4772 data_t *peer_key_data,
4773 data_t *expected_output )
4774{
Ronald Cron5425a212020-08-04 14:58:35 +02004775 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004776 psa_algorithm_t alg = alg_arg;
4777 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004778 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004779 unsigned char *output = NULL;
4780 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004781 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004782
4783 ASSERT_ALLOC( output, expected_output->len );
4784 PSA_ASSERT( psa_crypto_init( ) );
4785
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004786 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4787 psa_set_key_algorithm( &attributes, alg );
4788 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004789 PSA_ASSERT( psa_import_key( &attributes,
4790 our_key_data->x, our_key_data->len,
4791 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004792
gabor-mezei-armceface22021-01-21 12:26:17 +01004793 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4794 key_bits = psa_get_key_bits( &attributes );
4795
Gilles Peskinebe697d82019-05-16 18:00:41 +02004796 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4797 peer_key_data->x, peer_key_data->len,
4798 output, expected_output->len,
4799 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004800 ASSERT_COMPARE( output, output_length,
4801 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01004802 TEST_ASSERT( output_length <=
4803 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
4804 TEST_ASSERT( output_length <=
4805 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004806
4807exit:
4808 mbedtls_free( output );
4809 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004810 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004811}
4812/* END_CASE */
4813
4814/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004815void key_agreement_capacity( int alg_arg,
4816 int our_key_type_arg, data_t *our_key_data,
4817 data_t *peer_key_data,
4818 int expected_capacity_arg )
4819{
Ronald Cron5425a212020-08-04 14:58:35 +02004820 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004821 psa_algorithm_t alg = alg_arg;
4822 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004823 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004824 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004825 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004826 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004827
Gilles Peskine8817f612018-12-18 00:18:46 +01004828 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004829
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004830 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4831 psa_set_key_algorithm( &attributes, alg );
4832 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004833 PSA_ASSERT( psa_import_key( &attributes,
4834 our_key_data->x, our_key_data->len,
4835 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004836
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004837 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004838 PSA_ASSERT( psa_key_derivation_key_agreement(
4839 &operation,
4840 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4841 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004842 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4843 {
4844 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004845 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004846 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004847 NULL, 0 ) );
4848 }
Gilles Peskine59685592018-09-18 12:11:34 +02004849
Gilles Peskinebf491972018-10-25 22:36:12 +02004850 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004851 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004852 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004853 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004854
Gilles Peskinebf491972018-10-25 22:36:12 +02004855 /* Test the actual capacity by reading the output. */
4856 while( actual_capacity > sizeof( output ) )
4857 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004858 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004859 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004860 actual_capacity -= sizeof( output );
4861 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004862 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004863 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004864 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004865 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004866
Gilles Peskine59685592018-09-18 12:11:34 +02004867exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004868 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004869 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004870 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004871}
4872/* END_CASE */
4873
4874/* BEGIN_CASE */
4875void key_agreement_output( int alg_arg,
4876 int our_key_type_arg, data_t *our_key_data,
4877 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004878 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004879{
Ronald Cron5425a212020-08-04 14:58:35 +02004880 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004881 psa_algorithm_t alg = alg_arg;
4882 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004883 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004884 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004885 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004886
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004887 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4888 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004889
Gilles Peskine8817f612018-12-18 00:18:46 +01004890 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004891
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004892 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4893 psa_set_key_algorithm( &attributes, alg );
4894 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004895 PSA_ASSERT( psa_import_key( &attributes,
4896 our_key_data->x, our_key_data->len,
4897 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004898
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004899 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004900 PSA_ASSERT( psa_key_derivation_key_agreement(
4901 &operation,
4902 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4903 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004904 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4905 {
4906 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004907 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004908 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004909 NULL, 0 ) );
4910 }
Gilles Peskine59685592018-09-18 12:11:34 +02004911
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004912 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004913 actual_output,
4914 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004915 ASSERT_COMPARE( actual_output, expected_output1->len,
4916 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004917 if( expected_output2->len != 0 )
4918 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004919 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004920 actual_output,
4921 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004922 ASSERT_COMPARE( actual_output, expected_output2->len,
4923 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004924 }
Gilles Peskine59685592018-09-18 12:11:34 +02004925
4926exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004927 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004928 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004929 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004930 mbedtls_free( actual_output );
4931}
4932/* END_CASE */
4933
4934/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004935void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004936{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004937 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004938 unsigned char *output = NULL;
4939 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004940 size_t i;
4941 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004942
Simon Butcher49f8e312020-03-03 15:51:50 +00004943 TEST_ASSERT( bytes_arg >= 0 );
4944
Gilles Peskine91892022021-02-08 19:50:26 +01004945 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004946 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02004947
Gilles Peskine8817f612018-12-18 00:18:46 +01004948 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004949
Gilles Peskinea50d7392018-06-21 10:22:13 +02004950 /* Run several times, to ensure that every output byte will be
4951 * nonzero at least once with overwhelming probability
4952 * (2^(-8*number_of_runs)). */
4953 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004954 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004955 if( bytes != 0 )
4956 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004957 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004958
Gilles Peskinea50d7392018-06-21 10:22:13 +02004959 for( i = 0; i < bytes; i++ )
4960 {
4961 if( output[i] != 0 )
4962 ++changed[i];
4963 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004964 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004965
4966 /* Check that every byte was changed to nonzero at least once. This
4967 * validates that psa_generate_random is overwriting every byte of
4968 * the output buffer. */
4969 for( i = 0; i < bytes; i++ )
4970 {
4971 TEST_ASSERT( changed[i] != 0 );
4972 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004973
4974exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004975 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004976 mbedtls_free( output );
4977 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004978}
4979/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004980
4981/* BEGIN_CASE */
4982void generate_key( int type_arg,
4983 int bits_arg,
4984 int usage_arg,
4985 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004986 int expected_status_arg,
4987 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004988{
Ronald Cron5425a212020-08-04 14:58:35 +02004989 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004990 psa_key_type_t type = type_arg;
4991 psa_key_usage_t usage = usage_arg;
4992 size_t bits = bits_arg;
4993 psa_algorithm_t alg = alg_arg;
4994 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004995 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004996 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004997
Gilles Peskine8817f612018-12-18 00:18:46 +01004998 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004999
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005000 psa_set_key_usage_flags( &attributes, usage );
5001 psa_set_key_algorithm( &attributes, alg );
5002 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005003 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005004
5005 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005006 psa_status_t status = psa_generate_key( &attributes, &key );
5007
5008 if( is_large_key > 0 )
5009 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5010 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005011 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005012 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005013
5014 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005015 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005016 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5017 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005018
Gilles Peskine818ca122018-06-20 18:16:48 +02005019 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005020 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005021 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005022
5023exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005024 /*
5025 * Key attributes may have been returned by psa_get_key_attributes()
5026 * thus reset them as required.
5027 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005028 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005029
Ronald Cron5425a212020-08-04 14:58:35 +02005030 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005031 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005032}
5033/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005034
Ronald Cronee414c72021-03-18 18:50:08 +01005035/* 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 +02005036void generate_key_rsa( int bits_arg,
5037 data_t *e_arg,
5038 int expected_status_arg )
5039{
Ronald Cron5425a212020-08-04 14:58:35 +02005040 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005041 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005042 size_t bits = bits_arg;
5043 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5044 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5045 psa_status_t expected_status = expected_status_arg;
5046 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5047 uint8_t *exported = NULL;
5048 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005049 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005050 size_t exported_length = SIZE_MAX;
5051 uint8_t *e_read_buffer = NULL;
5052 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005053 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005054 size_t e_read_length = SIZE_MAX;
5055
5056 if( e_arg->len == 0 ||
5057 ( e_arg->len == 3 &&
5058 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5059 {
5060 is_default_public_exponent = 1;
5061 e_read_size = 0;
5062 }
5063 ASSERT_ALLOC( e_read_buffer, e_read_size );
5064 ASSERT_ALLOC( exported, exported_size );
5065
5066 PSA_ASSERT( psa_crypto_init( ) );
5067
5068 psa_set_key_usage_flags( &attributes, usage );
5069 psa_set_key_algorithm( &attributes, alg );
5070 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5071 e_arg->x, e_arg->len ) );
5072 psa_set_key_bits( &attributes, bits );
5073
5074 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005075 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005076 if( expected_status != PSA_SUCCESS )
5077 goto exit;
5078
5079 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005080 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005081 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5082 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5083 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5084 e_read_buffer, e_read_size,
5085 &e_read_length ) );
5086 if( is_default_public_exponent )
5087 TEST_EQUAL( e_read_length, 0 );
5088 else
5089 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5090
5091 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005092 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005093 goto exit;
5094
5095 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005096 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005097 exported, exported_size,
5098 &exported_length ) );
5099 {
5100 uint8_t *p = exported;
5101 uint8_t *end = exported + exported_length;
5102 size_t len;
5103 /* RSAPublicKey ::= SEQUENCE {
5104 * modulus INTEGER, -- n
5105 * publicExponent INTEGER } -- e
5106 */
5107 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005108 MBEDTLS_ASN1_SEQUENCE |
5109 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005110 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005111 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5112 MBEDTLS_ASN1_INTEGER ) );
5113 if( len >= 1 && p[0] == 0 )
5114 {
5115 ++p;
5116 --len;
5117 }
5118 if( e_arg->len == 0 )
5119 {
5120 TEST_EQUAL( len, 3 );
5121 TEST_EQUAL( p[0], 1 );
5122 TEST_EQUAL( p[1], 0 );
5123 TEST_EQUAL( p[2], 1 );
5124 }
5125 else
5126 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5127 }
5128
5129exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005130 /*
5131 * Key attributes may have been returned by psa_get_key_attributes() or
5132 * set by psa_set_key_domain_parameters() thus reset them as required.
5133 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005134 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005135
Ronald Cron5425a212020-08-04 14:58:35 +02005136 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005137 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005138 mbedtls_free( e_read_buffer );
5139 mbedtls_free( exported );
5140}
5141/* END_CASE */
5142
Darryl Greend49a4992018-06-18 17:27:26 +01005143/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005144void persistent_key_load_key_from_storage( data_t *data,
5145 int type_arg, int bits_arg,
5146 int usage_flags_arg, int alg_arg,
5147 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005148{
Ronald Cron71016a92020-08-28 19:01:50 +02005149 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005150 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005151 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5152 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005153 psa_key_type_t type = type_arg;
5154 size_t bits = bits_arg;
5155 psa_key_usage_t usage_flags = usage_flags_arg;
5156 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005157 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005158 unsigned char *first_export = NULL;
5159 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005160 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005161 size_t first_exported_length;
5162 size_t second_exported_length;
5163
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005164 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5165 {
5166 ASSERT_ALLOC( first_export, export_size );
5167 ASSERT_ALLOC( second_export, export_size );
5168 }
Darryl Greend49a4992018-06-18 17:27:26 +01005169
Gilles Peskine8817f612018-12-18 00:18:46 +01005170 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005171
Gilles Peskinec87af662019-05-15 16:12:22 +02005172 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005173 psa_set_key_usage_flags( &attributes, usage_flags );
5174 psa_set_key_algorithm( &attributes, alg );
5175 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005176 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005177
Darryl Green0c6575a2018-11-07 16:05:30 +00005178 switch( generation_method )
5179 {
5180 case IMPORT_KEY:
5181 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005182 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005183 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005184 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005185
Darryl Green0c6575a2018-11-07 16:05:30 +00005186 case GENERATE_KEY:
5187 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005188 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005189 break;
5190
5191 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005192#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005193 {
5194 /* Create base key */
5195 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5196 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5197 psa_set_key_usage_flags( &base_attributes,
5198 PSA_KEY_USAGE_DERIVE );
5199 psa_set_key_algorithm( &base_attributes, derive_alg );
5200 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005201 PSA_ASSERT( psa_import_key( &base_attributes,
5202 data->x, data->len,
5203 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005204 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005205 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005206 PSA_ASSERT( psa_key_derivation_input_key(
5207 &operation,
5208 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005209 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005210 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005211 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005212 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5213 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005214 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005215 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005216 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005217 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005218 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005219#else
5220 TEST_ASSUME( ! "KDF not supported in this configuration" );
5221#endif
5222 break;
5223
5224 default:
5225 TEST_ASSERT( ! "generation_method not implemented in test" );
5226 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005227 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005228 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005229
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005230 /* Export the key if permitted by the key policy. */
5231 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5232 {
Ronald Cron5425a212020-08-04 14:58:35 +02005233 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005234 first_export, export_size,
5235 &first_exported_length ) );
5236 if( generation_method == IMPORT_KEY )
5237 ASSERT_COMPARE( data->x, data->len,
5238 first_export, first_exported_length );
5239 }
Darryl Greend49a4992018-06-18 17:27:26 +01005240
5241 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005242 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005243 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005244 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005245
Darryl Greend49a4992018-06-18 17:27:26 +01005246 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005247 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005248 TEST_ASSERT( mbedtls_svc_key_id_equal(
5249 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005250 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5251 PSA_KEY_LIFETIME_PERSISTENT );
5252 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5253 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02005254 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02005255 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005256 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005257
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005258 /* Export the key again if permitted by the key policy. */
5259 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005260 {
Ronald Cron5425a212020-08-04 14:58:35 +02005261 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005262 second_export, export_size,
5263 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005264 ASSERT_COMPARE( first_export, first_exported_length,
5265 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005266 }
5267
5268 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005269 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005270 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005271
5272exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005273 /*
5274 * Key attributes may have been returned by psa_get_key_attributes()
5275 * thus reset them as required.
5276 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005277 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005278
Darryl Greend49a4992018-06-18 17:27:26 +01005279 mbedtls_free( first_export );
5280 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005281 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005282 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005283 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005284 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005285}
5286/* END_CASE */