blob: 01e5d5939b838125bc6f1da956bbc0fbf891b387 [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"
Archana4d7ae1d2021-07-07 02:50:22 +053018#if defined(PSA_CRYPTO_DRIVER_TEST)
19#include "test/drivers/test_driver.h"
Archana8a180362021-07-05 02:18:48 +053020#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
21#else
22#define TEST_DRIVER_LOCATION 0x7fffff
Archana4d7ae1d2021-07-07 02:50:22 +053023#endif
Ronald Cron28a45ed2021-02-09 20:35:42 +010024
Gilles Peskine4023c012021-05-27 13:21:20 +020025/* If this comes up, it's a bug in the test code or in the test data. */
26#define UNUSED 0xdeadbeef
27
Dave Rodgman647791d2021-06-23 12:49:59 +010028/* Assert that an operation is (not) active.
29 * This serves as a proxy for checking if the operation is aborted. */
30#define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 )
31#define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 )
32
Jaeden Amerof24c7f82018-06-27 17:20:43 +010033/** An invalid export length that will never be set by psa_export_key(). */
34static const size_t INVALID_EXPORT_LENGTH = ~0U;
35
Gilles Peskinea7aa4422018-08-14 15:17:54 +020036/** Test if a buffer contains a constant byte value.
37 *
38 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020039 *
40 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042 * \param size Size of the buffer in bytes.
43 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020044 * \return 1 if the buffer is all-bits-zero.
45 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020046 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020047static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020048{
49 size_t i;
50 for( i = 0; i < size; i++ )
51 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020052 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020053 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020054 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020055 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020056}
Gilles Peskine818ca122018-06-20 18:16:48 +020057
Gilles Peskine0b352bc2018-06-28 00:16:11 +020058/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
59static int asn1_write_10x( unsigned char **p,
60 unsigned char *start,
61 size_t bits,
62 unsigned char x )
63{
64 int ret;
65 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020066 if( bits == 0 )
67 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
68 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020069 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030070 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020071 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
72 *p -= len;
73 ( *p )[len-1] = x;
74 if( bits % 8 == 0 )
75 ( *p )[1] |= 1;
76 else
77 ( *p )[0] |= 1 << ( bits % 8 );
78 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
79 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
80 MBEDTLS_ASN1_INTEGER ) );
81 return( len );
82}
83
84static int construct_fake_rsa_key( unsigned char *buffer,
85 size_t buffer_size,
86 unsigned char **p,
87 size_t bits,
88 int keypair )
89{
90 size_t half_bits = ( bits + 1 ) / 2;
91 int ret;
92 int len = 0;
93 /* Construct something that looks like a DER encoding of
94 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
95 * RSAPrivateKey ::= SEQUENCE {
96 * version Version,
97 * modulus INTEGER, -- n
98 * publicExponent INTEGER, -- e
99 * privateExponent INTEGER, -- d
100 * prime1 INTEGER, -- p
101 * prime2 INTEGER, -- q
102 * exponent1 INTEGER, -- d mod (p-1)
103 * exponent2 INTEGER, -- d mod (q-1)
104 * coefficient INTEGER, -- (inverse of q) mod p
105 * otherPrimeInfos OtherPrimeInfos OPTIONAL
106 * }
107 * Or, for a public key, the same structure with only
108 * version, modulus and publicExponent.
109 */
110 *p = buffer + buffer_size;
111 if( keypair )
112 {
113 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
114 asn1_write_10x( p, buffer, half_bits, 1 ) );
115 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
116 asn1_write_10x( p, buffer, half_bits, 1 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
118 asn1_write_10x( p, buffer, half_bits, 1 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, /* q */
120 asn1_write_10x( p, buffer, half_bits, 1 ) );
121 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
122 asn1_write_10x( p, buffer, half_bits, 3 ) );
123 MBEDTLS_ASN1_CHK_ADD( len, /* d */
124 asn1_write_10x( p, buffer, bits, 1 ) );
125 }
126 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
127 asn1_write_10x( p, buffer, 17, 1 ) );
128 MBEDTLS_ASN1_CHK_ADD( len, /* n */
129 asn1_write_10x( p, buffer, bits, 1 ) );
130 if( keypair )
131 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
132 mbedtls_asn1_write_int( p, buffer, 0 ) );
133 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
134 {
135 const unsigned char tag =
136 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
137 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
138 }
139 return( len );
140}
141
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100142int exercise_mac_setup( psa_key_type_t key_type,
143 const unsigned char *key_bytes,
144 size_t key_length,
145 psa_algorithm_t alg,
146 psa_mac_operation_t *operation,
147 psa_status_t *status )
148{
Ronald Cron5425a212020-08-04 14:58:35 +0200149 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200150 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100151
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100152 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200153 psa_set_key_algorithm( &attributes, alg );
154 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200155 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100156
Ronald Cron5425a212020-08-04 14:58:35 +0200157 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100158 /* Whether setup succeeded or failed, abort must succeed. */
159 PSA_ASSERT( psa_mac_abort( operation ) );
160 /* If setup failed, reproduce the failure, so that the caller can
161 * test the resulting state of the operation object. */
162 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100163 {
Ronald Cron5425a212020-08-04 14:58:35 +0200164 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100165 }
166
Ronald Cron5425a212020-08-04 14:58:35 +0200167 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100168 return( 1 );
169
170exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200171 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100172 return( 0 );
173}
174
175int exercise_cipher_setup( psa_key_type_t key_type,
176 const unsigned char *key_bytes,
177 size_t key_length,
178 psa_algorithm_t alg,
179 psa_cipher_operation_t *operation,
180 psa_status_t *status )
181{
Ronald Cron5425a212020-08-04 14:58:35 +0200182 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200183 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100184
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200185 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
186 psa_set_key_algorithm( &attributes, alg );
187 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200188 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100189
Ronald Cron5425a212020-08-04 14:58:35 +0200190 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100191 /* Whether setup succeeded or failed, abort must succeed. */
192 PSA_ASSERT( psa_cipher_abort( operation ) );
193 /* If setup failed, reproduce the failure, so that the caller can
194 * test the resulting state of the operation object. */
195 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100196 {
Ronald Cron5425a212020-08-04 14:58:35 +0200197 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100198 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100199 }
200
Ronald Cron5425a212020-08-04 14:58:35 +0200201 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100202 return( 1 );
203
204exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200205 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100206 return( 0 );
207}
208
Ronald Cron5425a212020-08-04 14:58:35 +0200209static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200210{
211 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200212 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200213 uint8_t buffer[1];
214 size_t length;
215 int ok = 0;
216
Ronald Cronecfb2372020-07-23 17:13:42 +0200217 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200218 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
219 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
220 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200221 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000222 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200223 TEST_EQUAL(
224 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
225 TEST_EQUAL(
226 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200227 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200228 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
229 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
230 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
231 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
232
Ronald Cron5425a212020-08-04 14:58:35 +0200233 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000234 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200235 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200236 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000237 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200238
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200239 ok = 1;
240
241exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100242 /*
243 * Key attributes may have been returned by psa_get_key_attributes()
244 * thus reset them as required.
245 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200246 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100247
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200248 return( ok );
249}
250
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200251/* Assert that a key isn't reported as having a slot number. */
252#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
253#define ASSERT_NO_SLOT_NUMBER( attributes ) \
254 do \
255 { \
256 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
257 TEST_EQUAL( psa_get_key_slot_number( \
258 attributes, \
259 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
260 PSA_ERROR_INVALID_ARGUMENT ); \
261 } \
262 while( 0 )
263#else /* MBEDTLS_PSA_CRYPTO_SE_C */
264#define ASSERT_NO_SLOT_NUMBER( attributes ) \
265 ( (void) 0 )
266#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
267
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100268/* An overapproximation of the amount of storage needed for a key of the
269 * given type and with the given content. The API doesn't make it easy
270 * to find a good value for the size. The current implementation doesn't
271 * care about the value anyway. */
272#define KEY_BITS_FROM_DATA( type, data ) \
273 ( data )->len
274
Darryl Green0c6575a2018-11-07 16:05:30 +0000275typedef enum {
276 IMPORT_KEY = 0,
277 GENERATE_KEY = 1,
278 DERIVE_KEY = 2
279} generate_method;
280
Gilles Peskinee59236f2018-01-27 23:32:46 +0100281/* END_HEADER */
282
283/* BEGIN_DEPENDENCIES
284 * depends_on:MBEDTLS_PSA_CRYPTO_C
285 * END_DEPENDENCIES
286 */
287
288/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200289void static_checks( )
290{
291 size_t max_truncated_mac_size =
292 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
293
294 /* Check that the length for a truncated MAC always fits in the algorithm
295 * encoding. The shifted mask is the maximum truncated value. The
296 * untruncated algorithm may be one byte larger. */
297 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
298}
299/* END_CASE */
300
301/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200302void import_with_policy( int type_arg,
303 int usage_arg, int alg_arg,
304 int expected_status_arg )
305{
306 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
307 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200308 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200309 psa_key_type_t type = type_arg;
310 psa_key_usage_t usage = usage_arg;
311 psa_algorithm_t alg = alg_arg;
312 psa_status_t expected_status = expected_status_arg;
313 const uint8_t key_material[16] = {0};
314 psa_status_t status;
315
316 PSA_ASSERT( psa_crypto_init( ) );
317
318 psa_set_key_type( &attributes, type );
319 psa_set_key_usage_flags( &attributes, usage );
320 psa_set_key_algorithm( &attributes, alg );
321
322 status = psa_import_key( &attributes,
323 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200324 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200325 TEST_EQUAL( status, expected_status );
326 if( status != PSA_SUCCESS )
327 goto exit;
328
Ronald Cron5425a212020-08-04 14:58:35 +0200329 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200330 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200331 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200332 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200333 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200334 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200335
Ronald Cron5425a212020-08-04 14:58:35 +0200336 PSA_ASSERT( psa_destroy_key( key ) );
337 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200338
339exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100340 /*
341 * Key attributes may have been returned by psa_get_key_attributes()
342 * thus reset them as required.
343 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200344 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100345
346 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200347 PSA_DONE( );
348}
349/* END_CASE */
350
351/* BEGIN_CASE */
352void import_with_data( data_t *data, int type_arg,
353 int attr_bits_arg,
354 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200355{
356 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
357 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200358 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200359 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200360 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200361 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100362 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100363
Gilles Peskine8817f612018-12-18 00:18:46 +0100364 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100365
Gilles Peskine4747d192019-04-17 15:05:45 +0200366 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200367 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200368
Ronald Cron5425a212020-08-04 14:58:35 +0200369 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100370 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200371 if( status != PSA_SUCCESS )
372 goto exit;
373
Ronald Cron5425a212020-08-04 14:58:35 +0200374 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200375 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200376 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200377 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200378 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200379
Ronald Cron5425a212020-08-04 14:58:35 +0200380 PSA_ASSERT( psa_destroy_key( key ) );
381 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100382
383exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100384 /*
385 * Key attributes may have been returned by psa_get_key_attributes()
386 * thus reset them as required.
387 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200388 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100389
390 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200391 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100392}
393/* END_CASE */
394
395/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200396void import_large_key( int type_arg, int byte_size_arg,
397 int expected_status_arg )
398{
399 psa_key_type_t type = type_arg;
400 size_t byte_size = byte_size_arg;
401 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
402 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200403 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200404 psa_status_t status;
405 uint8_t *buffer = NULL;
406 size_t buffer_size = byte_size + 1;
407 size_t n;
408
Steven Cooreman69967ce2021-01-18 18:01:08 +0100409 /* Skip the test case if the target running the test cannot
410 * accomodate large keys due to heap size constraints */
411 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200412 memset( buffer, 'K', byte_size );
413
414 PSA_ASSERT( psa_crypto_init( ) );
415
416 /* Try importing the key */
417 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
418 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200419 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100420 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200421 TEST_EQUAL( status, expected_status );
422
423 if( status == PSA_SUCCESS )
424 {
Ronald Cron5425a212020-08-04 14:58:35 +0200425 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200426 TEST_EQUAL( psa_get_key_type( &attributes ), type );
427 TEST_EQUAL( psa_get_key_bits( &attributes ),
428 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200429 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200430 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200431 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200432 for( n = 0; n < byte_size; n++ )
433 TEST_EQUAL( buffer[n], 'K' );
434 for( n = byte_size; n < buffer_size; n++ )
435 TEST_EQUAL( buffer[n], 0 );
436 }
437
438exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100439 /*
440 * Key attributes may have been returned by psa_get_key_attributes()
441 * thus reset them as required.
442 */
443 psa_reset_key_attributes( &attributes );
444
Ronald Cron5425a212020-08-04 14:58:35 +0200445 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200446 PSA_DONE( );
447 mbedtls_free( buffer );
448}
449/* END_CASE */
450
451/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200452void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
453{
Ronald Cron5425a212020-08-04 14:58:35 +0200454 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200455 size_t bits = bits_arg;
456 psa_status_t expected_status = expected_status_arg;
457 psa_status_t status;
458 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200459 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200460 size_t buffer_size = /* Slight overapproximations */
461 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200462 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200463 unsigned char *p;
464 int ret;
465 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200466 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200467
Gilles Peskine8817f612018-12-18 00:18:46 +0100468 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200469 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200470
471 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
472 bits, keypair ) ) >= 0 );
473 length = ret;
474
475 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200476 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200477 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100478 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200479
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200480 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200481 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200482
483exit:
484 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200485 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200486}
487/* END_CASE */
488
489/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300490void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300491 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200492 int usage_arg, int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +0530493 int lifetime_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100494 int expected_bits,
495 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200496 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100497 int canonical_input )
498{
Ronald Cron5425a212020-08-04 14:58:35 +0200499 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100500 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200501 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200502 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100503 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +0530504 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100505 unsigned char *exported = NULL;
506 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100507 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100508 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100509 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200510 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200511 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100512
Moran Pekercb088e72018-07-17 17:36:59 +0300513 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200514 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100515 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200516 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100517 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100518
Archana4d7ae1d2021-07-07 02:50:22 +0530519 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +0200520 psa_set_key_usage_flags( &attributes, usage_arg );
521 psa_set_key_algorithm( &attributes, alg );
522 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700523
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100524 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200525 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100526
527 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200528 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200529 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
530 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200531 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100532
533 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200534 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100535 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100536
537 /* The exported length must be set by psa_export_key() to a value between 0
538 * and export_size. On errors, the exported length must be 0. */
539 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
540 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
541 TEST_ASSERT( exported_length <= export_size );
542
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200543 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200544 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100545 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200546 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100547 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100548 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200549 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100550
Gilles Peskineea38a922021-02-13 00:05:16 +0100551 /* Run sanity checks on the exported key. For non-canonical inputs,
552 * this validates the canonical representations. For canonical inputs,
553 * this doesn't directly validate the implementation, but it still helps
554 * by cross-validating the test data with the sanity check code. */
Archana4d7ae1d2021-07-07 02:50:22 +0530555 if( !psa_key_lifetime_is_external( lifetime ) )
556 {
557 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
558 goto exit;
559 }
Gilles Peskine8f609232018-08-11 01:24:55 +0200560
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100561 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200562 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100563 else
564 {
Ronald Cron5425a212020-08-04 14:58:35 +0200565 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200566 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Archana9d17bf42021-09-10 06:22:44 +0530567 &key2 ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200568 PSA_ASSERT( psa_export_key( key2,
Archana9d17bf42021-09-10 06:22:44 +0530569 reexported,
570 export_size,
571 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200572 ASSERT_COMPARE( exported, exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +0530573 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200574 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100575 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100576 TEST_ASSERT( exported_length <=
Archana4d7ae1d2021-07-07 02:50:22 +0530577 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
Archana9d17bf42021-09-10 06:22:44 +0530578 psa_get_key_bits( &got_attributes ) ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100579 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100580
581destroy:
582 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200583 PSA_ASSERT( psa_destroy_key( key ) );
584 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100585
586exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100587 /*
588 * Key attributes may have been returned by psa_get_key_attributes()
589 * thus reset them as required.
590 */
591 psa_reset_key_attributes( &got_attributes );
Archana4d7ae1d2021-07-07 02:50:22 +0530592 psa_destroy_key( key ) ;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300593 mbedtls_free( exported );
594 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200595 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100596}
597/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100598
Moran Pekerf709f4a2018-06-06 17:26:04 +0300599/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300600void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200601 int type_arg,
602 int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +0530603 int lifetime_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100604 int export_size_delta,
605 int expected_export_status_arg,
606 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300607{
Ronald Cron5425a212020-08-04 14:58:35 +0200608 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300609 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200610 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200611 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300612 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +0530613 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300614 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100615 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100616 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200617 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300618
Gilles Peskine8817f612018-12-18 00:18:46 +0100619 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300620
Archana4d7ae1d2021-07-07 02:50:22 +0530621 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +0200622 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
623 psa_set_key_algorithm( &attributes, alg );
624 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300625
626 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200627 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300628
Gilles Peskine49c25912018-10-29 15:15:31 +0100629 /* Export the public key */
630 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200631 status = psa_export_public_key( key,
Archana9d17bf42021-09-10 06:22:44 +0530632 exported, export_size,
633 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100634 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100635 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100636 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200637 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100638 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200639 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200640 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100641 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100642 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100643 TEST_ASSERT( expected_public_key->len <=
644 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
645 TEST_ASSERT( expected_public_key->len <=
646 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100647 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
648 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100649 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300650exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100651 /*
652 * Key attributes may have been returned by psa_get_key_attributes()
653 * thus reset them as required.
654 */
655 psa_reset_key_attributes( &attributes );
656
itayzafrir3e02b3b2018-06-12 17:06:52 +0300657 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200658 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200659 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300660}
661/* END_CASE */
662
Gilles Peskine20035e32018-02-03 22:44:14 +0100663/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200664void import_and_exercise_key( data_t *data,
665 int type_arg,
666 int bits_arg,
667 int alg_arg )
668{
Ronald Cron5425a212020-08-04 14:58:35 +0200669 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200670 psa_key_type_t type = type_arg;
671 size_t bits = bits_arg;
672 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100673 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200674 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200675 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200676
Gilles Peskine8817f612018-12-18 00:18:46 +0100677 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200678
Gilles Peskine4747d192019-04-17 15:05:45 +0200679 psa_set_key_usage_flags( &attributes, usage );
680 psa_set_key_algorithm( &attributes, alg );
681 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200682
683 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200684 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200685
686 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200687 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200688 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
689 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200690
691 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100692 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200693 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200694
Ronald Cron5425a212020-08-04 14:58:35 +0200695 PSA_ASSERT( psa_destroy_key( key ) );
696 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200697
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200698exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100699 /*
700 * Key attributes may have been returned by psa_get_key_attributes()
701 * thus reset them as required.
702 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200703 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100704
705 psa_reset_key_attributes( &attributes );
706 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200707 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200708}
709/* END_CASE */
710
711/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100712void effective_key_attributes( int type_arg, int expected_type_arg,
713 int bits_arg, int expected_bits_arg,
714 int usage_arg, int expected_usage_arg,
715 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200716{
Ronald Cron5425a212020-08-04 14:58:35 +0200717 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100718 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100719 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100720 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100721 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200722 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100723 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200724 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100725 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200726 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200727
Gilles Peskine8817f612018-12-18 00:18:46 +0100728 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200729
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200730 psa_set_key_usage_flags( &attributes, usage );
731 psa_set_key_algorithm( &attributes, alg );
732 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +0100733 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +0200734
Ronald Cron5425a212020-08-04 14:58:35 +0200735 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +0100736 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +0200737
Ronald Cron5425a212020-08-04 14:58:35 +0200738 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +0100739 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
740 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
741 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
742 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200743
744exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100745 /*
746 * Key attributes may have been returned by psa_get_key_attributes()
747 * thus reset them as required.
748 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200749 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100750
751 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200752 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +0200753}
754/* END_CASE */
755
756/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100757void check_key_policy( int type_arg, int bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +0200758 int usage_arg, int alg_arg )
Gilles Peskine06c28892019-11-26 18:07:46 +0100759{
760 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +0200761 usage_arg,
762 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200763 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +0100764 goto exit;
765}
766/* END_CASE */
767
768/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200769void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +0000770{
771 /* Test each valid way of initializing the object, except for `= {0}`, as
772 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
773 * though it's OK by the C standard. We could test for this, but we'd need
774 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200775 psa_key_attributes_t func = psa_key_attributes_init( );
776 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
777 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000778
779 memset( &zero, 0, sizeof( zero ) );
780
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200781 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
782 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
783 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000784
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200785 TEST_EQUAL( psa_get_key_type( &func ), 0 );
786 TEST_EQUAL( psa_get_key_type( &init ), 0 );
787 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
788
789 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
790 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
791 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
792
793 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
794 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
795 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
796
797 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
798 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
799 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +0000800}
801/* END_CASE */
802
803/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200804void mac_key_policy( int policy_usage_arg,
805 int policy_alg_arg,
806 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200807 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200808 int exercise_alg_arg,
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +0200809 int expected_status_sign_arg,
810 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200811{
Ronald Cron5425a212020-08-04 14:58:35 +0200812 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200813 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000814 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200815 psa_key_type_t key_type = key_type_arg;
816 psa_algorithm_t policy_alg = policy_alg_arg;
817 psa_algorithm_t exercise_alg = exercise_alg_arg;
818 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200819 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +0200820 psa_status_t expected_status_sign = expected_status_sign_arg;
821 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200822 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200823
Gilles Peskine8817f612018-12-18 00:18:46 +0100824 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200825
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200826 psa_set_key_usage_flags( &attributes, policy_usage );
827 psa_set_key_algorithm( &attributes, policy_alg );
828 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200829
Gilles Peskine049c7532019-05-15 20:22:09 +0200830 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200831 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200832
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +0200833 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
834 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200835
Ronald Cron5425a212020-08-04 14:58:35 +0200836 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +0200837 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100838
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +0200839 /* Calculate the MAC, one-shot case. */
840 uint8_t input[128] = {0};
841 size_t mac_len;
842 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
843 input, 128,
844 mac, PSA_MAC_MAX_SIZE, &mac_len ),
845 expected_status_sign );
846
847 /* Verify correct MAC, one-shot case. */
848 status = psa_mac_verify( key, exercise_alg, input, 128,
849 mac, mac_len );
850
851 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
852 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
853 else
854 TEST_EQUAL( status, expected_status_verify );
855
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200856 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200857
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200858 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200859 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +0200860 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200861
862exit:
863 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200864 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200865 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200866}
867/* END_CASE */
868
869/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200870void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200871 int policy_alg,
872 int key_type,
873 data_t *key_data,
874 int exercise_alg )
875{
Ronald Cron5425a212020-08-04 14:58:35 +0200876 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200877 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000878 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200879 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200880 psa_status_t status;
881
Gilles Peskine8817f612018-12-18 00:18:46 +0100882 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200883
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200884 psa_set_key_usage_flags( &attributes, policy_usage );
885 psa_set_key_algorithm( &attributes, policy_alg );
886 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200887
Gilles Peskine049c7532019-05-15 20:22:09 +0200888 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200889 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200890
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200891 /* Check if no key usage flag implication is done */
892 TEST_EQUAL( policy_usage,
893 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200894
Ronald Cron5425a212020-08-04 14:58:35 +0200895 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200896 if( policy_alg == exercise_alg &&
897 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100898 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200899 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100900 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200901 psa_cipher_abort( &operation );
902
Ronald Cron5425a212020-08-04 14:58:35 +0200903 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200904 if( policy_alg == exercise_alg &&
905 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100906 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200907 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100908 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200909
910exit:
911 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200912 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200913 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200914}
915/* END_CASE */
916
917/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200918void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200919 int policy_alg,
920 int key_type,
921 data_t *key_data,
922 int nonce_length_arg,
923 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100924 int exercise_alg,
925 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200926{
Ronald Cron5425a212020-08-04 14:58:35 +0200927 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200928 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200929 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200930 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100931 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200932 unsigned char nonce[16] = {0};
933 size_t nonce_length = nonce_length_arg;
934 unsigned char tag[16];
935 size_t tag_length = tag_length_arg;
936 size_t output_length;
937
938 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
939 TEST_ASSERT( tag_length <= sizeof( tag ) );
940
Gilles Peskine8817f612018-12-18 00:18:46 +0100941 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200942
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200943 psa_set_key_usage_flags( &attributes, policy_usage );
944 psa_set_key_algorithm( &attributes, policy_alg );
945 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200946
Gilles Peskine049c7532019-05-15 20:22:09 +0200947 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200948 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200949
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200950 /* Check if no key usage implication is done */
951 TEST_EQUAL( policy_usage,
952 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200953
Ronald Cron5425a212020-08-04 14:58:35 +0200954 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200955 nonce, nonce_length,
956 NULL, 0,
957 NULL, 0,
958 tag, tag_length,
959 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100960 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
961 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200962 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100963 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200964
965 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200966 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200967 nonce, nonce_length,
968 NULL, 0,
969 tag, tag_length,
970 NULL, 0,
971 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100972 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
973 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
974 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100975 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200976 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100977 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200978
979exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200980 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200981 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200982}
983/* END_CASE */
984
985/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200986void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200987 int policy_alg,
988 int key_type,
989 data_t *key_data,
990 int exercise_alg )
991{
Ronald Cron5425a212020-08-04 14:58:35 +0200992 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200993 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200994 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200995 psa_status_t status;
996 size_t key_bits;
997 size_t buffer_length;
998 unsigned char *buffer = NULL;
999 size_t output_length;
1000
Gilles Peskine8817f612018-12-18 00:18:46 +01001001 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001002
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001003 psa_set_key_usage_flags( &attributes, policy_usage );
1004 psa_set_key_algorithm( &attributes, policy_alg );
1005 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001006
Gilles Peskine049c7532019-05-15 20:22:09 +02001007 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001008 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001009
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001010 /* Check if no key usage implication is done */
1011 TEST_EQUAL( policy_usage,
1012 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001013
Ronald Cron5425a212020-08-04 14:58:35 +02001014 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001015 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001016 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1017 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001018 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001019
Ronald Cron5425a212020-08-04 14:58:35 +02001020 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001021 NULL, 0,
1022 NULL, 0,
1023 buffer, buffer_length,
1024 &output_length );
1025 if( policy_alg == exercise_alg &&
1026 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001027 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001028 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001029 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001030
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001031 if( buffer_length != 0 )
1032 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001033 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001034 buffer, buffer_length,
1035 NULL, 0,
1036 buffer, buffer_length,
1037 &output_length );
1038 if( policy_alg == exercise_alg &&
1039 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001040 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001041 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001042 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001043
1044exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001045 /*
1046 * Key attributes may have been returned by psa_get_key_attributes()
1047 * thus reset them as required.
1048 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001049 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001050
1051 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001052 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001053 mbedtls_free( buffer );
1054}
1055/* END_CASE */
1056
1057/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001058void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001059 int policy_alg,
1060 int key_type,
1061 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001062 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001063 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001064 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001065{
Ronald Cron5425a212020-08-04 14:58:35 +02001066 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001067 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001068 psa_key_usage_t policy_usage = policy_usage_arg;
1069 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001070 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001071 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1072 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1073 * compatible with the policy and `payload_length_arg` is supposed to be
1074 * a valid input length to sign. If `payload_length_arg <= 0`,
1075 * `exercise_alg` is supposed to be forbidden by the policy. */
1076 int compatible_alg = payload_length_arg > 0;
1077 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001078 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001079 size_t signature_length;
1080
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001081 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001082 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001083 TEST_EQUAL( expected_usage,
1084 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001085
Gilles Peskine8817f612018-12-18 00:18:46 +01001086 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001087
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001088 psa_set_key_usage_flags( &attributes, policy_usage );
1089 psa_set_key_algorithm( &attributes, policy_alg );
1090 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001091
Gilles Peskine049c7532019-05-15 20:22:09 +02001092 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001093 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001094
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001095 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1096
Ronald Cron5425a212020-08-04 14:58:35 +02001097 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001098 payload, payload_length,
1099 signature, sizeof( signature ),
1100 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001101 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001102 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001103 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001104 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001105
1106 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001107 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001108 payload, payload_length,
1109 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001110 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001111 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001112 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001113 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001114
gabor-mezei-armd851d682021-06-28 14:53:49 +02001115 if( PSA_ALG_IS_HASH_AND_SIGN( exercise_alg ) &&
1116 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001117 {
1118 status = psa_sign_message( key, exercise_alg,
1119 payload, payload_length,
1120 signature, sizeof( signature ),
1121 &signature_length );
1122 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1123 PSA_ASSERT( status );
1124 else
1125 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1126
1127 memset( signature, 0, sizeof( signature ) );
1128 status = psa_verify_message( key, exercise_alg,
1129 payload, payload_length,
1130 signature, sizeof( signature ) );
1131 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1132 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1133 else
1134 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1135 }
1136
Gilles Peskined5b33222018-06-18 22:20:03 +02001137exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001138 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001139 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001140}
1141/* END_CASE */
1142
Janos Follathba3fab92019-06-11 14:50:16 +01001143/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001144void derive_key_policy( int policy_usage,
1145 int policy_alg,
1146 int key_type,
1147 data_t *key_data,
1148 int exercise_alg )
1149{
Ronald Cron5425a212020-08-04 14:58:35 +02001150 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001151 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001152 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001153 psa_status_t status;
1154
Gilles Peskine8817f612018-12-18 00:18:46 +01001155 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001156
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001157 psa_set_key_usage_flags( &attributes, policy_usage );
1158 psa_set_key_algorithm( &attributes, policy_alg );
1159 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001160
Gilles Peskine049c7532019-05-15 20:22:09 +02001161 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001162 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001163
Janos Follathba3fab92019-06-11 14:50:16 +01001164 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1165
1166 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1167 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001168 {
Janos Follathba3fab92019-06-11 14:50:16 +01001169 PSA_ASSERT( psa_key_derivation_input_bytes(
1170 &operation,
1171 PSA_KEY_DERIVATION_INPUT_SEED,
1172 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001173 }
Janos Follathba3fab92019-06-11 14:50:16 +01001174
1175 status = psa_key_derivation_input_key( &operation,
1176 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001177 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001178
Gilles Peskineea0fb492018-07-12 17:17:20 +02001179 if( policy_alg == exercise_alg &&
1180 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001181 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001182 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001183 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001184
1185exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001186 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001187 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001188 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001189}
1190/* END_CASE */
1191
1192/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001193void agreement_key_policy( int policy_usage,
1194 int policy_alg,
1195 int key_type_arg,
1196 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001197 int exercise_alg,
1198 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001199{
Ronald Cron5425a212020-08-04 14:58:35 +02001200 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001201 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001202 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001203 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001204 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001205 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001206
Gilles Peskine8817f612018-12-18 00:18:46 +01001207 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001208
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001209 psa_set_key_usage_flags( &attributes, policy_usage );
1210 psa_set_key_algorithm( &attributes, policy_alg );
1211 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001212
Gilles Peskine049c7532019-05-15 20:22:09 +02001213 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001214 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001215
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001216 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001217 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001218
Steven Cooremance48e852020-10-05 16:02:45 +02001219 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001220
1221exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001222 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001223 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001224 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001225}
1226/* END_CASE */
1227
1228/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001229void key_policy_alg2( int key_type_arg, data_t *key_data,
1230 int usage_arg, int alg_arg, int alg2_arg )
1231{
Ronald Cron5425a212020-08-04 14:58:35 +02001232 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001233 psa_key_type_t key_type = key_type_arg;
1234 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1235 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1236 psa_key_usage_t usage = usage_arg;
1237 psa_algorithm_t alg = alg_arg;
1238 psa_algorithm_t alg2 = alg2_arg;
1239
1240 PSA_ASSERT( psa_crypto_init( ) );
1241
1242 psa_set_key_usage_flags( &attributes, usage );
1243 psa_set_key_algorithm( &attributes, alg );
1244 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1245 psa_set_key_type( &attributes, key_type );
1246 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001247 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001248
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001249 /* Update the usage flags to obtain implicit usage flags */
1250 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001251 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001252 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1253 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1254 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1255
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001256 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001257 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001258 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001259 goto exit;
1260
1261exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001262 /*
1263 * Key attributes may have been returned by psa_get_key_attributes()
1264 * thus reset them as required.
1265 */
1266 psa_reset_key_attributes( &got_attributes );
1267
Ronald Cron5425a212020-08-04 14:58:35 +02001268 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001269 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001270}
1271/* END_CASE */
1272
1273/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001274void raw_agreement_key_policy( int policy_usage,
1275 int policy_alg,
1276 int key_type_arg,
1277 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001278 int exercise_alg,
1279 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001280{
Ronald Cron5425a212020-08-04 14:58:35 +02001281 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001282 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001283 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001284 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001285 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001286 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001287
1288 PSA_ASSERT( psa_crypto_init( ) );
1289
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001290 psa_set_key_usage_flags( &attributes, policy_usage );
1291 psa_set_key_algorithm( &attributes, policy_alg );
1292 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001293
Gilles Peskine049c7532019-05-15 20:22:09 +02001294 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001295 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001296
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001297 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001298
Steven Cooremance48e852020-10-05 16:02:45 +02001299 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001300
1301exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001302 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001303 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001304 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001305}
1306/* END_CASE */
1307
1308/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001309void copy_success( int source_usage_arg,
1310 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301311 unsigned int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001312 int type_arg, data_t *material,
1313 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001314 int target_usage_arg,
1315 int target_alg_arg, int target_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301316 unsigned int target_lifetime_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001317 int expected_usage_arg,
1318 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001319{
Gilles Peskineca25db92019-04-19 11:43:08 +02001320 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1321 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001322 psa_key_usage_t expected_usage = expected_usage_arg;
1323 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001324 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05301325 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
1326 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001327 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1328 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001329 uint8_t *export_buffer = NULL;
1330
Gilles Peskine57ab7212019-01-28 13:03:09 +01001331 PSA_ASSERT( psa_crypto_init( ) );
1332
Gilles Peskineca25db92019-04-19 11:43:08 +02001333 /* Prepare the source key. */
1334 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1335 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001336 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001337 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05301338 psa_set_key_lifetime( &source_attributes, source_lifetime);
Gilles Peskine049c7532019-05-15 20:22:09 +02001339 PSA_ASSERT( psa_import_key( &source_attributes,
1340 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001341 &source_key ) );
1342 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001343
Gilles Peskineca25db92019-04-19 11:43:08 +02001344 /* Prepare the target attributes. */
1345 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001346 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001347 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001348 }
Archana8a180362021-07-05 02:18:48 +05301349 psa_set_key_lifetime( &target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02001350
Gilles Peskineca25db92019-04-19 11:43:08 +02001351 if( target_usage_arg != -1 )
1352 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1353 if( target_alg_arg != -1 )
1354 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001355 if( target_alg2_arg != -1 )
1356 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001357
Archana8a180362021-07-05 02:18:48 +05301358
Gilles Peskine57ab7212019-01-28 13:03:09 +01001359 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001360 PSA_ASSERT( psa_copy_key( source_key,
1361 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001362
1363 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001364 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001365
1366 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001367 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001368 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1369 psa_get_key_type( &target_attributes ) );
1370 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1371 psa_get_key_bits( &target_attributes ) );
1372 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1373 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001374 TEST_EQUAL( expected_alg2,
1375 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001376 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1377 {
1378 size_t length;
1379 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001380 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001381 material->len, &length ) );
1382 ASSERT_COMPARE( material->x, material->len,
1383 export_buffer, length );
1384 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001385
Archana8a180362021-07-05 02:18:48 +05301386 if( !psa_key_lifetime_is_external( target_lifetime ) )
1387 {
1388 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
1389 goto exit;
1390 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
1391 goto exit;
1392 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01001393
Ronald Cron5425a212020-08-04 14:58:35 +02001394 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001395
1396exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001397 /*
1398 * Source and target key attributes may have been returned by
1399 * psa_get_key_attributes() thus reset them as required.
1400 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001401 psa_reset_key_attributes( &source_attributes );
1402 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001403
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001404 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001405 mbedtls_free( export_buffer );
1406}
1407/* END_CASE */
1408
1409/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001410void copy_fail( int source_usage_arg,
1411 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301412 int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001413 int type_arg, data_t *material,
1414 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001415 int target_usage_arg,
1416 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001417 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001418 int expected_status_arg )
1419{
1420 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1421 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001422 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1423 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001424 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001425
1426 PSA_ASSERT( psa_crypto_init( ) );
1427
1428 /* Prepare the source key. */
1429 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1430 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001431 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001432 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05301433 psa_set_key_lifetime( &source_attributes, source_lifetime_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001434 PSA_ASSERT( psa_import_key( &source_attributes,
1435 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001436 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001437
1438 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001439 psa_set_key_id( &target_attributes, key_id );
1440 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001441 psa_set_key_type( &target_attributes, target_type_arg );
1442 psa_set_key_bits( &target_attributes, target_bits_arg );
1443 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1444 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001445 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001446
1447 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001448 TEST_EQUAL( psa_copy_key( source_key,
1449 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001450 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001451
Ronald Cron5425a212020-08-04 14:58:35 +02001452 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001453
Gilles Peskine4a644642019-05-03 17:14:08 +02001454exit:
1455 psa_reset_key_attributes( &source_attributes );
1456 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001457 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001458}
1459/* END_CASE */
1460
1461/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001462void hash_operation_init( )
1463{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001464 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001465 /* Test each valid way of initializing the object, except for `= {0}`, as
1466 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1467 * though it's OK by the C standard. We could test for this, but we'd need
1468 * to supress the Clang warning for the test. */
1469 psa_hash_operation_t func = psa_hash_operation_init( );
1470 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1471 psa_hash_operation_t zero;
1472
1473 memset( &zero, 0, sizeof( zero ) );
1474
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001475 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001476 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1477 PSA_ERROR_BAD_STATE );
1478 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1479 PSA_ERROR_BAD_STATE );
1480 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1481 PSA_ERROR_BAD_STATE );
1482
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001483 /* A default hash operation should be abortable without error. */
1484 PSA_ASSERT( psa_hash_abort( &func ) );
1485 PSA_ASSERT( psa_hash_abort( &init ) );
1486 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001487}
1488/* END_CASE */
1489
1490/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001491void hash_setup( int alg_arg,
1492 int expected_status_arg )
1493{
1494 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001495 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001496 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001497 psa_status_t status;
1498
Gilles Peskine8817f612018-12-18 00:18:46 +01001499 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001500
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001501 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001502 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001503
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001504 /* Whether setup succeeded or failed, abort must succeed. */
1505 PSA_ASSERT( psa_hash_abort( &operation ) );
1506
1507 /* If setup failed, reproduce the failure, so as to
1508 * test the resulting state of the operation object. */
1509 if( status != PSA_SUCCESS )
1510 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1511
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001512 /* Now the operation object should be reusable. */
1513#if defined(KNOWN_SUPPORTED_HASH_ALG)
1514 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1515 PSA_ASSERT( psa_hash_abort( &operation ) );
1516#endif
1517
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001518exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001519 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001520}
1521/* END_CASE */
1522
1523/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001524void hash_compute_fail( int alg_arg, data_t *input,
1525 int output_size_arg, int expected_status_arg )
1526{
1527 psa_algorithm_t alg = alg_arg;
1528 uint8_t *output = NULL;
1529 size_t output_size = output_size_arg;
1530 size_t output_length = INVALID_EXPORT_LENGTH;
1531 psa_status_t expected_status = expected_status_arg;
1532 psa_status_t status;
1533
1534 ASSERT_ALLOC( output, output_size );
1535
1536 PSA_ASSERT( psa_crypto_init( ) );
1537
1538 status = psa_hash_compute( alg, input->x, input->len,
1539 output, output_size, &output_length );
1540 TEST_EQUAL( status, expected_status );
1541 TEST_ASSERT( output_length <= output_size );
1542
1543exit:
1544 mbedtls_free( output );
1545 PSA_DONE( );
1546}
1547/* END_CASE */
1548
1549/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001550void hash_compare_fail( int alg_arg, data_t *input,
1551 data_t *reference_hash,
1552 int expected_status_arg )
1553{
1554 psa_algorithm_t alg = alg_arg;
1555 psa_status_t expected_status = expected_status_arg;
1556 psa_status_t status;
1557
1558 PSA_ASSERT( psa_crypto_init( ) );
1559
1560 status = psa_hash_compare( alg, input->x, input->len,
1561 reference_hash->x, reference_hash->len );
1562 TEST_EQUAL( status, expected_status );
1563
1564exit:
1565 PSA_DONE( );
1566}
1567/* END_CASE */
1568
1569/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001570void hash_compute_compare( int alg_arg, data_t *input,
1571 data_t *expected_output )
1572{
1573 psa_algorithm_t alg = alg_arg;
1574 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1575 size_t output_length = INVALID_EXPORT_LENGTH;
1576 size_t i;
1577
1578 PSA_ASSERT( psa_crypto_init( ) );
1579
1580 /* Compute with tight buffer */
1581 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001582 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001583 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001584 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001585 ASSERT_COMPARE( output, output_length,
1586 expected_output->x, expected_output->len );
1587
1588 /* Compute with larger buffer */
1589 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1590 output, sizeof( output ),
1591 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001592 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001593 ASSERT_COMPARE( output, output_length,
1594 expected_output->x, expected_output->len );
1595
1596 /* Compare with correct hash */
1597 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1598 output, output_length ) );
1599
1600 /* Compare with trailing garbage */
1601 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1602 output, output_length + 1 ),
1603 PSA_ERROR_INVALID_SIGNATURE );
1604
1605 /* Compare with truncated hash */
1606 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1607 output, output_length - 1 ),
1608 PSA_ERROR_INVALID_SIGNATURE );
1609
1610 /* Compare with corrupted value */
1611 for( i = 0; i < output_length; i++ )
1612 {
Chris Jones9634bb12021-01-20 15:56:42 +00001613 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001614 output[i] ^= 1;
1615 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1616 output, output_length ),
1617 PSA_ERROR_INVALID_SIGNATURE );
1618 output[i] ^= 1;
1619 }
1620
1621exit:
1622 PSA_DONE( );
1623}
1624/* END_CASE */
1625
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001626/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001627void hash_bad_order( )
1628{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001629 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001630 unsigned char input[] = "";
1631 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001632 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001633 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1634 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1635 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001636 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001637 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001638 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001639
Gilles Peskine8817f612018-12-18 00:18:46 +01001640 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001641
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001642 /* Call setup twice in a row. */
1643 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001644 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001645 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1646 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001647 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001648 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001649 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001650
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001651 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001652 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001653 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001654 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001655
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001656 /* Check that update calls abort on error. */
1657 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01001658 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001659 ASSERT_OPERATION_IS_ACTIVE( operation );
1660 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
1661 PSA_ERROR_BAD_STATE );
1662 ASSERT_OPERATION_IS_INACTIVE( operation );
1663 PSA_ASSERT( psa_hash_abort( &operation ) );
1664 ASSERT_OPERATION_IS_INACTIVE( operation );
1665
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001666 /* Call update after finish. */
1667 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1668 PSA_ASSERT( psa_hash_finish( &operation,
1669 hash, sizeof( hash ), &hash_len ) );
1670 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001671 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001672 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001673
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001674 /* Call verify without calling setup beforehand. */
1675 TEST_EQUAL( psa_hash_verify( &operation,
1676 valid_hash, sizeof( valid_hash ) ),
1677 PSA_ERROR_BAD_STATE );
1678 PSA_ASSERT( psa_hash_abort( &operation ) );
1679
1680 /* Call verify after finish. */
1681 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1682 PSA_ASSERT( psa_hash_finish( &operation,
1683 hash, sizeof( hash ), &hash_len ) );
1684 TEST_EQUAL( psa_hash_verify( &operation,
1685 valid_hash, sizeof( valid_hash ) ),
1686 PSA_ERROR_BAD_STATE );
1687 PSA_ASSERT( psa_hash_abort( &operation ) );
1688
1689 /* Call verify twice in a row. */
1690 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001691 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001692 PSA_ASSERT( psa_hash_verify( &operation,
1693 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001694 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001695 TEST_EQUAL( psa_hash_verify( &operation,
1696 valid_hash, sizeof( valid_hash ) ),
1697 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001698 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001699 PSA_ASSERT( psa_hash_abort( &operation ) );
1700
1701 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001702 TEST_EQUAL( psa_hash_finish( &operation,
1703 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001704 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001705 PSA_ASSERT( psa_hash_abort( &operation ) );
1706
1707 /* Call finish twice in a row. */
1708 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1709 PSA_ASSERT( psa_hash_finish( &operation,
1710 hash, sizeof( hash ), &hash_len ) );
1711 TEST_EQUAL( psa_hash_finish( &operation,
1712 hash, sizeof( hash ), &hash_len ),
1713 PSA_ERROR_BAD_STATE );
1714 PSA_ASSERT( psa_hash_abort( &operation ) );
1715
1716 /* Call finish after calling verify. */
1717 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1718 PSA_ASSERT( psa_hash_verify( &operation,
1719 valid_hash, sizeof( valid_hash ) ) );
1720 TEST_EQUAL( psa_hash_finish( &operation,
1721 hash, sizeof( hash ), &hash_len ),
1722 PSA_ERROR_BAD_STATE );
1723 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001724
1725exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001726 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001727}
1728/* END_CASE */
1729
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001730/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001731void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001732{
1733 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001734 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1735 * appended to it */
1736 unsigned char hash[] = {
1737 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1738 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1739 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001740 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001741 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001742
Gilles Peskine8817f612018-12-18 00:18:46 +01001743 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001744
itayzafrir27e69452018-11-01 14:26:34 +02001745 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001746 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001747 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001748 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001749 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001750 ASSERT_OPERATION_IS_INACTIVE( operation );
1751 PSA_ASSERT( psa_hash_abort( &operation ) );
1752 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03001753
itayzafrir27e69452018-11-01 14:26:34 +02001754 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001755 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001756 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001757 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001758
itayzafrir27e69452018-11-01 14:26:34 +02001759 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001760 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001761 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001762 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001763
itayzafrirec93d302018-10-18 18:01:10 +03001764exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001765 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001766}
1767/* END_CASE */
1768
Ronald Cronee414c72021-03-18 18:50:08 +01001769/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001770void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001771{
1772 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001773 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001774 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001775 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001776 size_t hash_len;
1777
Gilles Peskine8817f612018-12-18 00:18:46 +01001778 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001779
itayzafrir58028322018-10-25 10:22:01 +03001780 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001781 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001782 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001783 hash, expected_size - 1, &hash_len ),
1784 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001785
1786exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001787 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001788}
1789/* END_CASE */
1790
Ronald Cronee414c72021-03-18 18:50:08 +01001791/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001792void hash_clone_source_state( )
1793{
1794 psa_algorithm_t alg = PSA_ALG_SHA_256;
1795 unsigned char hash[PSA_HASH_MAX_SIZE];
1796 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1797 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1798 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1799 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1800 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1801 size_t hash_len;
1802
1803 PSA_ASSERT( psa_crypto_init( ) );
1804 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1805
1806 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1807 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1808 PSA_ASSERT( psa_hash_finish( &op_finished,
1809 hash, sizeof( hash ), &hash_len ) );
1810 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1811 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1812
1813 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1814 PSA_ERROR_BAD_STATE );
1815
1816 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1817 PSA_ASSERT( psa_hash_finish( &op_init,
1818 hash, sizeof( hash ), &hash_len ) );
1819 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1820 PSA_ASSERT( psa_hash_finish( &op_finished,
1821 hash, sizeof( hash ), &hash_len ) );
1822 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1823 PSA_ASSERT( psa_hash_finish( &op_aborted,
1824 hash, sizeof( hash ), &hash_len ) );
1825
1826exit:
1827 psa_hash_abort( &op_source );
1828 psa_hash_abort( &op_init );
1829 psa_hash_abort( &op_setup );
1830 psa_hash_abort( &op_finished );
1831 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001832 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001833}
1834/* END_CASE */
1835
Ronald Cronee414c72021-03-18 18:50:08 +01001836/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001837void hash_clone_target_state( )
1838{
1839 psa_algorithm_t alg = PSA_ALG_SHA_256;
1840 unsigned char hash[PSA_HASH_MAX_SIZE];
1841 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1842 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1843 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1844 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1845 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1846 size_t hash_len;
1847
1848 PSA_ASSERT( psa_crypto_init( ) );
1849
1850 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1851 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1852 PSA_ASSERT( psa_hash_finish( &op_finished,
1853 hash, sizeof( hash ), &hash_len ) );
1854 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1855 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1856
1857 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1858 PSA_ASSERT( psa_hash_finish( &op_target,
1859 hash, sizeof( hash ), &hash_len ) );
1860
1861 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1862 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1863 PSA_ERROR_BAD_STATE );
1864 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1865 PSA_ERROR_BAD_STATE );
1866
1867exit:
1868 psa_hash_abort( &op_target );
1869 psa_hash_abort( &op_init );
1870 psa_hash_abort( &op_setup );
1871 psa_hash_abort( &op_finished );
1872 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001873 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001874}
1875/* END_CASE */
1876
itayzafrir58028322018-10-25 10:22:01 +03001877/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001878void mac_operation_init( )
1879{
Jaeden Amero252ef282019-02-15 14:05:35 +00001880 const uint8_t input[1] = { 0 };
1881
Jaeden Amero769ce272019-01-04 11:48:03 +00001882 /* Test each valid way of initializing the object, except for `= {0}`, as
1883 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1884 * though it's OK by the C standard. We could test for this, but we'd need
1885 * to supress the Clang warning for the test. */
1886 psa_mac_operation_t func = psa_mac_operation_init( );
1887 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1888 psa_mac_operation_t zero;
1889
1890 memset( &zero, 0, sizeof( zero ) );
1891
Jaeden Amero252ef282019-02-15 14:05:35 +00001892 /* A freshly-initialized MAC operation should not be usable. */
1893 TEST_EQUAL( psa_mac_update( &func,
1894 input, sizeof( input ) ),
1895 PSA_ERROR_BAD_STATE );
1896 TEST_EQUAL( psa_mac_update( &init,
1897 input, sizeof( input ) ),
1898 PSA_ERROR_BAD_STATE );
1899 TEST_EQUAL( psa_mac_update( &zero,
1900 input, sizeof( input ) ),
1901 PSA_ERROR_BAD_STATE );
1902
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001903 /* A default MAC operation should be abortable without error. */
1904 PSA_ASSERT( psa_mac_abort( &func ) );
1905 PSA_ASSERT( psa_mac_abort( &init ) );
1906 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001907}
1908/* END_CASE */
1909
1910/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001911void mac_setup( int key_type_arg,
1912 data_t *key,
1913 int alg_arg,
1914 int expected_status_arg )
1915{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001916 psa_key_type_t key_type = key_type_arg;
1917 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001918 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001919 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001920 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1921#if defined(KNOWN_SUPPORTED_MAC_ALG)
1922 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1923#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001924
Gilles Peskine8817f612018-12-18 00:18:46 +01001925 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001926
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001927 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1928 &operation, &status ) )
1929 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001930 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001931
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001932 /* The operation object should be reusable. */
1933#if defined(KNOWN_SUPPORTED_MAC_ALG)
1934 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1935 smoke_test_key_data,
1936 sizeof( smoke_test_key_data ),
1937 KNOWN_SUPPORTED_MAC_ALG,
1938 &operation, &status ) )
1939 goto exit;
1940 TEST_EQUAL( status, PSA_SUCCESS );
1941#endif
1942
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001943exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001944 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001945}
1946/* END_CASE */
1947
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001948/* 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 +00001949void mac_bad_order( )
1950{
Ronald Cron5425a212020-08-04 14:58:35 +02001951 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001952 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1953 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001954 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001955 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1956 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1957 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001958 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001959 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1960 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1961 size_t sign_mac_length = 0;
1962 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1963 const uint8_t verify_mac[] = {
1964 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1965 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1966 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1967
1968 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001969 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001970 psa_set_key_algorithm( &attributes, alg );
1971 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001972
Ronald Cron5425a212020-08-04 14:58:35 +02001973 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1974 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001975
Jaeden Amero252ef282019-02-15 14:05:35 +00001976 /* Call update without calling setup beforehand. */
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 sign finish without calling setup beforehand. */
1982 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1983 &sign_mac_length),
1984 PSA_ERROR_BAD_STATE );
1985 PSA_ASSERT( psa_mac_abort( &operation ) );
1986
1987 /* Call verify finish without calling setup beforehand. */
1988 TEST_EQUAL( psa_mac_verify_finish( &operation,
1989 verify_mac, sizeof( verify_mac ) ),
1990 PSA_ERROR_BAD_STATE );
1991 PSA_ASSERT( psa_mac_abort( &operation ) );
1992
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001993 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001994 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001995 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001996 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001997 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001998 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001999 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002000 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002001
Jaeden Amero252ef282019-02-15 14:05:35 +00002002 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002003 PSA_ASSERT( psa_mac_sign_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_sign_finish( &operation,
2006 sign_mac, sizeof( sign_mac ),
2007 &sign_mac_length ) );
2008 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2009 PSA_ERROR_BAD_STATE );
2010 PSA_ASSERT( psa_mac_abort( &operation ) );
2011
2012 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002013 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002014 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2015 PSA_ASSERT( psa_mac_verify_finish( &operation,
2016 verify_mac, sizeof( verify_mac ) ) );
2017 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2018 PSA_ERROR_BAD_STATE );
2019 PSA_ASSERT( psa_mac_abort( &operation ) );
2020
2021 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002022 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002023 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2024 PSA_ASSERT( psa_mac_sign_finish( &operation,
2025 sign_mac, sizeof( sign_mac ),
2026 &sign_mac_length ) );
2027 TEST_EQUAL( psa_mac_sign_finish( &operation,
2028 sign_mac, sizeof( sign_mac ),
2029 &sign_mac_length ),
2030 PSA_ERROR_BAD_STATE );
2031 PSA_ASSERT( psa_mac_abort( &operation ) );
2032
2033 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002034 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002035 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2036 PSA_ASSERT( psa_mac_verify_finish( &operation,
2037 verify_mac, sizeof( verify_mac ) ) );
2038 TEST_EQUAL( psa_mac_verify_finish( &operation,
2039 verify_mac, sizeof( verify_mac ) ),
2040 PSA_ERROR_BAD_STATE );
2041 PSA_ASSERT( psa_mac_abort( &operation ) );
2042
2043 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002044 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002045 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002046 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002047 TEST_EQUAL( psa_mac_verify_finish( &operation,
2048 verify_mac, sizeof( verify_mac ) ),
2049 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002050 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002051 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002052 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002053
2054 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002055 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002056 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002057 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002058 TEST_EQUAL( psa_mac_sign_finish( &operation,
2059 sign_mac, sizeof( sign_mac ),
2060 &sign_mac_length ),
2061 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002062 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002063 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002064 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002065
Ronald Cron5425a212020-08-04 14:58:35 +02002066 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002067
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002068exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002069 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002070}
2071/* END_CASE */
2072
2073/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002074void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002075 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002076 int alg_arg,
2077 data_t *input,
2078 data_t *expected_mac )
2079{
Ronald Cron5425a212020-08-04 14:58:35 +02002080 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002081 psa_key_type_t key_type = key_type_arg;
2082 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002083 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002084 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002085 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002086 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002087 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002088 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002089 const size_t output_sizes_to_test[] = {
2090 0,
2091 1,
2092 expected_mac->len - 1,
2093 expected_mac->len,
2094 expected_mac->len + 1,
2095 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002096
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002097 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002098 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002099 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002100
Gilles Peskine8817f612018-12-18 00:18:46 +01002101 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002102
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002103 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002104 psa_set_key_algorithm( &attributes, alg );
2105 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002106
Ronald Cron5425a212020-08-04 14:58:35 +02002107 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2108 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002109
Gilles Peskine8b356b52020-08-25 23:44:59 +02002110 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2111 {
2112 const size_t output_size = output_sizes_to_test[i];
2113 psa_status_t expected_status =
2114 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2115 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002116
Chris Jones9634bb12021-01-20 15:56:42 +00002117 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002118 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002119
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002120 /* Calculate the MAC, one-shot case. */
2121 TEST_EQUAL( psa_mac_compute( key, alg,
2122 input->x, input->len,
2123 actual_mac, output_size, &mac_length ),
2124 expected_status );
2125 if( expected_status == PSA_SUCCESS )
2126 {
2127 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2128 actual_mac, mac_length );
2129 }
2130
2131 if( output_size > 0 )
2132 memset( actual_mac, 0, output_size );
2133
2134 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002135 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002136 PSA_ASSERT( psa_mac_update( &operation,
2137 input->x, input->len ) );
2138 TEST_EQUAL( psa_mac_sign_finish( &operation,
2139 actual_mac, output_size,
2140 &mac_length ),
2141 expected_status );
2142 PSA_ASSERT( psa_mac_abort( &operation ) );
2143
2144 if( expected_status == PSA_SUCCESS )
2145 {
2146 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2147 actual_mac, mac_length );
2148 }
2149 mbedtls_free( actual_mac );
2150 actual_mac = NULL;
2151 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002152
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002153exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002154 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002155 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002156 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002157 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002158}
2159/* END_CASE */
2160
2161/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002162void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002163 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002164 int alg_arg,
2165 data_t *input,
2166 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002167{
Ronald Cron5425a212020-08-04 14:58:35 +02002168 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002169 psa_key_type_t key_type = key_type_arg;
2170 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002171 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002172 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002173 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002174
Gilles Peskine69c12672018-06-28 00:07:19 +02002175 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2176
Gilles Peskine8817f612018-12-18 00:18:46 +01002177 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002178
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002179 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002180 psa_set_key_algorithm( &attributes, alg );
2181 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002182
Ronald Cron5425a212020-08-04 14:58:35 +02002183 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2184 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002185
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002186 /* Verify correct MAC, one-shot case. */
2187 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2188 expected_mac->x, expected_mac->len ) );
2189
2190 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002191 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002192 PSA_ASSERT( psa_mac_update( &operation,
2193 input->x, input->len ) );
2194 PSA_ASSERT( psa_mac_verify_finish( &operation,
2195 expected_mac->x,
2196 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002197
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002198 /* Test a MAC that's too short, one-shot case. */
2199 TEST_EQUAL( psa_mac_verify( key, alg,
2200 input->x, input->len,
2201 expected_mac->x,
2202 expected_mac->len - 1 ),
2203 PSA_ERROR_INVALID_SIGNATURE );
2204
2205 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002206 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002207 PSA_ASSERT( psa_mac_update( &operation,
2208 input->x, input->len ) );
2209 TEST_EQUAL( psa_mac_verify_finish( &operation,
2210 expected_mac->x,
2211 expected_mac->len - 1 ),
2212 PSA_ERROR_INVALID_SIGNATURE );
2213
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002214 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002215 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2216 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002217 TEST_EQUAL( psa_mac_verify( key, alg,
2218 input->x, input->len,
2219 perturbed_mac, expected_mac->len + 1 ),
2220 PSA_ERROR_INVALID_SIGNATURE );
2221
2222 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002223 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002224 PSA_ASSERT( psa_mac_update( &operation,
2225 input->x, input->len ) );
2226 TEST_EQUAL( psa_mac_verify_finish( &operation,
2227 perturbed_mac,
2228 expected_mac->len + 1 ),
2229 PSA_ERROR_INVALID_SIGNATURE );
2230
2231 /* Test changing one byte. */
2232 for( size_t i = 0; i < expected_mac->len; i++ )
2233 {
Chris Jones9634bb12021-01-20 15:56:42 +00002234 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002235 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002236
2237 TEST_EQUAL( psa_mac_verify( key, alg,
2238 input->x, input->len,
2239 perturbed_mac, expected_mac->len ),
2240 PSA_ERROR_INVALID_SIGNATURE );
2241
Ronald Cron5425a212020-08-04 14:58:35 +02002242 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002243 PSA_ASSERT( psa_mac_update( &operation,
2244 input->x, input->len ) );
2245 TEST_EQUAL( psa_mac_verify_finish( &operation,
2246 perturbed_mac,
2247 expected_mac->len ),
2248 PSA_ERROR_INVALID_SIGNATURE );
2249 perturbed_mac[i] ^= 1;
2250 }
2251
Gilles Peskine8c9def32018-02-08 10:02:12 +01002252exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002253 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002254 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002255 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002256 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002257}
2258/* END_CASE */
2259
2260/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002261void cipher_operation_init( )
2262{
Jaeden Ameroab439972019-02-15 14:12:05 +00002263 const uint8_t input[1] = { 0 };
2264 unsigned char output[1] = { 0 };
2265 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002266 /* Test each valid way of initializing the object, except for `= {0}`, as
2267 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2268 * though it's OK by the C standard. We could test for this, but we'd need
2269 * to supress the Clang warning for the test. */
2270 psa_cipher_operation_t func = psa_cipher_operation_init( );
2271 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2272 psa_cipher_operation_t zero;
2273
2274 memset( &zero, 0, sizeof( zero ) );
2275
Jaeden Ameroab439972019-02-15 14:12:05 +00002276 /* A freshly-initialized cipher operation should not be usable. */
2277 TEST_EQUAL( psa_cipher_update( &func,
2278 input, sizeof( input ),
2279 output, sizeof( output ),
2280 &output_length ),
2281 PSA_ERROR_BAD_STATE );
2282 TEST_EQUAL( psa_cipher_update( &init,
2283 input, sizeof( input ),
2284 output, sizeof( output ),
2285 &output_length ),
2286 PSA_ERROR_BAD_STATE );
2287 TEST_EQUAL( psa_cipher_update( &zero,
2288 input, sizeof( input ),
2289 output, sizeof( output ),
2290 &output_length ),
2291 PSA_ERROR_BAD_STATE );
2292
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002293 /* A default cipher operation should be abortable without error. */
2294 PSA_ASSERT( psa_cipher_abort( &func ) );
2295 PSA_ASSERT( psa_cipher_abort( &init ) );
2296 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002297}
2298/* END_CASE */
2299
2300/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002301void cipher_setup( int key_type_arg,
2302 data_t *key,
2303 int alg_arg,
2304 int expected_status_arg )
2305{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002306 psa_key_type_t key_type = key_type_arg;
2307 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002308 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002309 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002310 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002311#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002312 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2313#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002314
Gilles Peskine8817f612018-12-18 00:18:46 +01002315 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002316
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002317 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2318 &operation, &status ) )
2319 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002320 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002321
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002322 /* The operation object should be reusable. */
2323#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2324 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2325 smoke_test_key_data,
2326 sizeof( smoke_test_key_data ),
2327 KNOWN_SUPPORTED_CIPHER_ALG,
2328 &operation, &status ) )
2329 goto exit;
2330 TEST_EQUAL( status, PSA_SUCCESS );
2331#endif
2332
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002333exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002334 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002335 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002336}
2337/* END_CASE */
2338
Ronald Cronee414c72021-03-18 18:50:08 +01002339/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002340void cipher_bad_order( )
2341{
Ronald Cron5425a212020-08-04 14:58:35 +02002342 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002343 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2344 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002345 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002346 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002347 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002348 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002349 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2350 0xaa, 0xaa, 0xaa, 0xaa };
2351 const uint8_t text[] = {
2352 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2353 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002354 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002355 size_t length = 0;
2356
2357 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002358 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2359 psa_set_key_algorithm( &attributes, alg );
2360 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002361 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2362 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002363
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002364 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002365 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002366 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002367 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002368 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002369 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002370 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002371 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002372
2373 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002374 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002375 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002376 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002377 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002378 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002379 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002380 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002381
Jaeden Ameroab439972019-02-15 14:12:05 +00002382 /* Generate an IV without calling setup beforehand. */
2383 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2384 buffer, sizeof( buffer ),
2385 &length ),
2386 PSA_ERROR_BAD_STATE );
2387 PSA_ASSERT( psa_cipher_abort( &operation ) );
2388
2389 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002390 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002391 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2392 buffer, sizeof( buffer ),
2393 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002394 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002395 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2396 buffer, sizeof( buffer ),
2397 &length ),
2398 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002399 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002400 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002401 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002402
2403 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002404 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002405 PSA_ASSERT( psa_cipher_set_iv( &operation,
2406 iv, sizeof( iv ) ) );
2407 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2408 buffer, sizeof( buffer ),
2409 &length ),
2410 PSA_ERROR_BAD_STATE );
2411 PSA_ASSERT( psa_cipher_abort( &operation ) );
2412
2413 /* Set an IV without calling setup beforehand. */
2414 TEST_EQUAL( psa_cipher_set_iv( &operation,
2415 iv, sizeof( iv ) ),
2416 PSA_ERROR_BAD_STATE );
2417 PSA_ASSERT( psa_cipher_abort( &operation ) );
2418
2419 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002420 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002421 PSA_ASSERT( psa_cipher_set_iv( &operation,
2422 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002423 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002424 TEST_EQUAL( psa_cipher_set_iv( &operation,
2425 iv, sizeof( iv ) ),
2426 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002427 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002428 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002429 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002430
2431 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002432 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002433 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2434 buffer, sizeof( buffer ),
2435 &length ) );
2436 TEST_EQUAL( psa_cipher_set_iv( &operation,
2437 iv, sizeof( iv ) ),
2438 PSA_ERROR_BAD_STATE );
2439 PSA_ASSERT( psa_cipher_abort( &operation ) );
2440
2441 /* Call update without calling setup beforehand. */
2442 TEST_EQUAL( psa_cipher_update( &operation,
2443 text, sizeof( text ),
2444 buffer, sizeof( buffer ),
2445 &length ),
2446 PSA_ERROR_BAD_STATE );
2447 PSA_ASSERT( psa_cipher_abort( &operation ) );
2448
2449 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01002450 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002451 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002452 TEST_EQUAL( psa_cipher_update( &operation,
2453 text, sizeof( text ),
2454 buffer, sizeof( buffer ),
2455 &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 update after finish. */
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_update( &operation,
2468 text, sizeof( text ),
2469 buffer, sizeof( buffer ),
2470 &length ),
2471 PSA_ERROR_BAD_STATE );
2472 PSA_ASSERT( psa_cipher_abort( &operation ) );
2473
2474 /* Call finish without calling setup beforehand. */
2475 TEST_EQUAL( psa_cipher_finish( &operation,
2476 buffer, sizeof( buffer ), &length ),
2477 PSA_ERROR_BAD_STATE );
2478 PSA_ASSERT( psa_cipher_abort( &operation ) );
2479
2480 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002481 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002482 /* Not calling update means we are encrypting an empty buffer, which is OK
2483 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01002484 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002485 TEST_EQUAL( psa_cipher_finish( &operation,
2486 buffer, sizeof( buffer ), &length ),
2487 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002488 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002489 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002490 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002491
2492 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002493 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002494 PSA_ASSERT( psa_cipher_set_iv( &operation,
2495 iv, sizeof( iv ) ) );
2496 PSA_ASSERT( psa_cipher_finish( &operation,
2497 buffer, sizeof( buffer ), &length ) );
2498 TEST_EQUAL( psa_cipher_finish( &operation,
2499 buffer, sizeof( buffer ), &length ),
2500 PSA_ERROR_BAD_STATE );
2501 PSA_ASSERT( psa_cipher_abort( &operation ) );
2502
Ronald Cron5425a212020-08-04 14:58:35 +02002503 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002504
Jaeden Ameroab439972019-02-15 14:12:05 +00002505exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002506 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002507 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002508}
2509/* END_CASE */
2510
2511/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02002512void cipher_encrypt_fail( int alg_arg,
2513 int key_type_arg,
2514 data_t *key_data,
2515 data_t *input,
2516 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002517{
Ronald Cron5425a212020-08-04 14:58:35 +02002518 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002519 psa_status_t status;
2520 psa_key_type_t key_type = key_type_arg;
2521 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002522 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002523 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002524 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002525 size_t output_length = 0;
2526 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2527
2528 if ( PSA_ERROR_BAD_STATE != expected_status )
2529 {
2530 PSA_ASSERT( psa_crypto_init( ) );
2531
2532 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2533 psa_set_key_algorithm( &attributes, alg );
2534 psa_set_key_type( &attributes, key_type );
2535
2536 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
2537 input->len );
2538 ASSERT_ALLOC( output, output_buffer_size );
2539
2540 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2541 &key ) );
2542 }
2543
2544 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
2545 output_buffer_size, &output_length );
2546
2547 TEST_EQUAL( status, expected_status );
2548
2549exit:
2550 mbedtls_free( output );
2551 psa_destroy_key( key );
2552 PSA_DONE( );
2553}
2554/* END_CASE */
2555
2556/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02002557void cipher_encrypt_alg_without_iv( int alg_arg,
2558 int key_type_arg,
2559 data_t *key_data,
2560 data_t *input,
2561 data_t *expected_output )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002562{
2563 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2564 psa_key_type_t key_type = key_type_arg;
2565 psa_algorithm_t alg = alg_arg;
2566 unsigned char *output = NULL;
2567 size_t output_buffer_size = 0;
2568 size_t output_length = 0;
2569 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2570
2571 PSA_ASSERT( psa_crypto_init( ) );
2572
2573 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2574 psa_set_key_algorithm( &attributes, alg );
2575 psa_set_key_type( &attributes, key_type );
2576
2577 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2578 ASSERT_ALLOC( output, output_buffer_size );
2579
2580 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2581 &key ) );
2582
2583 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
2584 output_buffer_size, &output_length ) );
2585 TEST_ASSERT( output_length <=
2586 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2587 TEST_ASSERT( output_length <=
2588 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
2589
2590 ASSERT_COMPARE( expected_output->x, expected_output->len,
2591 output, output_length );
2592exit:
2593 mbedtls_free( output );
2594 psa_destroy_key( key );
2595 PSA_DONE( );
2596}
2597/* END_CASE */
2598
2599/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01002600void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
2601{
2602 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2603 psa_algorithm_t alg = alg_arg;
2604 psa_key_type_t key_type = key_type_arg;
2605 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2606 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2607 psa_status_t status;
2608
2609 PSA_ASSERT( psa_crypto_init( ) );
2610
2611 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2612 psa_set_key_algorithm( &attributes, alg );
2613 psa_set_key_type( &attributes, key_type );
2614
2615 /* Usage of either of these two size macros would cause divide by zero
2616 * with incorrect key types previously. Input length should be irrelevant
2617 * here. */
2618 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
2619 0 );
2620 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
2621
2622
2623 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2624 &key ) );
2625
2626 /* Should fail due to invalid alg type (to support invalid key type).
2627 * Encrypt or decrypt will end up in the same place. */
2628 status = psa_cipher_encrypt_setup( &operation, key, alg );
2629
2630 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
2631
2632exit:
2633 psa_cipher_abort( &operation );
2634 psa_destroy_key( key );
2635 PSA_DONE( );
2636}
2637/* END_CASE */
2638
2639/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002640void cipher_encrypt_validation( int alg_arg,
2641 int key_type_arg,
2642 data_t *key_data,
2643 data_t *input )
2644{
2645 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2646 psa_key_type_t key_type = key_type_arg;
2647 psa_algorithm_t alg = alg_arg;
2648 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
2649 unsigned char *output1 = NULL;
2650 size_t output1_buffer_size = 0;
2651 size_t output1_length = 0;
2652 unsigned char *output2 = NULL;
2653 size_t output2_buffer_size = 0;
2654 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002655 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002656 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002657 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002658
Gilles Peskine8817f612018-12-18 00:18:46 +01002659 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002660
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002661 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2662 psa_set_key_algorithm( &attributes, alg );
2663 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002664
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002665 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2666 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2667 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
2668 ASSERT_ALLOC( output1, output1_buffer_size );
2669 ASSERT_ALLOC( output2, output2_buffer_size );
2670
Ronald Cron5425a212020-08-04 14:58:35 +02002671 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2672 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002673
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02002674 /* The one-shot cipher encryption uses generated iv so validating
2675 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002676 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
2677 output1_buffer_size, &output1_length ) );
2678 TEST_ASSERT( output1_length <=
2679 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2680 TEST_ASSERT( output1_length <=
gabor-mezei-armceface22021-01-21 12:26:17 +01002681 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002682
2683 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2684 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002685
Gilles Peskine8817f612018-12-18 00:18:46 +01002686 PSA_ASSERT( psa_cipher_update( &operation,
2687 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002688 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01002689 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002690 TEST_ASSERT( function_output_length <=
2691 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2692 TEST_ASSERT( function_output_length <=
2693 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002694 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002695
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002696 PSA_ASSERT( psa_cipher_finish( &operation,
2697 output2 + output2_length,
2698 output2_buffer_size - output2_length,
2699 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002700 TEST_ASSERT( function_output_length <=
2701 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2702 TEST_ASSERT( function_output_length <=
2703 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002704 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002705
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002706 PSA_ASSERT( psa_cipher_abort( &operation ) );
2707 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
2708 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002709
Gilles Peskine50e586b2018-06-08 14:28:46 +02002710exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002711 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002712 mbedtls_free( output1 );
2713 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002714 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002715 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002716}
2717/* END_CASE */
2718
2719/* BEGIN_CASE */
2720void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002721 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002722 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002723 int first_part_size_arg,
2724 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002725 data_t *expected_output,
2726 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002727{
Ronald Cron5425a212020-08-04 14:58:35 +02002728 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002729 psa_key_type_t key_type = key_type_arg;
2730 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002731 psa_status_t status;
2732 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002733 size_t first_part_size = first_part_size_arg;
2734 size_t output1_length = output1_length_arg;
2735 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002736 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002737 size_t output_buffer_size = 0;
2738 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002739 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002740 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002741 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002742
Gilles Peskine8817f612018-12-18 00:18:46 +01002743 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002744
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002745 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2746 psa_set_key_algorithm( &attributes, alg );
2747 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002748
Ronald Cron5425a212020-08-04 14:58:35 +02002749 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2750 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002751
Ronald Cron5425a212020-08-04 14:58:35 +02002752 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002753
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002754 if( iv->len > 0 )
2755 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002756 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002757 }
2758
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002759 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2760 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002761 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002762
Gilles Peskinee0866522019-02-19 19:44:00 +01002763 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002764 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2765 output, output_buffer_size,
2766 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002767 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002768 TEST_ASSERT( function_output_length <=
2769 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2770 TEST_ASSERT( function_output_length <=
2771 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002772 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002773
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002774 if( first_part_size < input->len )
2775 {
2776 PSA_ASSERT( psa_cipher_update( &operation,
2777 input->x + first_part_size,
2778 input->len - first_part_size,
2779 ( output_buffer_size == 0 ? NULL :
2780 output + total_output_length ),
2781 output_buffer_size - total_output_length,
2782 &function_output_length ) );
2783 TEST_ASSERT( function_output_length == output2_length );
2784 TEST_ASSERT( function_output_length <=
2785 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2786 alg,
2787 input->len - first_part_size ) );
2788 TEST_ASSERT( function_output_length <=
2789 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2790 total_output_length += function_output_length;
2791 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002792
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002793 status = psa_cipher_finish( &operation,
2794 ( output_buffer_size == 0 ? NULL :
2795 output + total_output_length ),
2796 output_buffer_size - total_output_length,
2797 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002798 TEST_ASSERT( function_output_length <=
2799 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2800 TEST_ASSERT( function_output_length <=
2801 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002802 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002803 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002804
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002805 if( expected_status == PSA_SUCCESS )
2806 {
2807 PSA_ASSERT( psa_cipher_abort( &operation ) );
2808
2809 ASSERT_COMPARE( expected_output->x, expected_output->len,
2810 output, total_output_length );
2811 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002812
2813exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002814 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002815 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002816 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002817 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002818}
2819/* END_CASE */
2820
2821/* BEGIN_CASE */
2822void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002823 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002824 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002825 int first_part_size_arg,
2826 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002827 data_t *expected_output,
2828 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002829{
Ronald Cron5425a212020-08-04 14:58:35 +02002830 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002831 psa_key_type_t key_type = key_type_arg;
2832 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002833 psa_status_t status;
2834 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002835 size_t first_part_size = first_part_size_arg;
2836 size_t output1_length = output1_length_arg;
2837 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002838 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002839 size_t output_buffer_size = 0;
2840 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002841 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002842 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002843 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002844
Gilles Peskine8817f612018-12-18 00:18:46 +01002845 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002846
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002847 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2848 psa_set_key_algorithm( &attributes, alg );
2849 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002850
Ronald Cron5425a212020-08-04 14:58:35 +02002851 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2852 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002853
Ronald Cron5425a212020-08-04 14:58:35 +02002854 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002855
Steven Cooreman177deba2020-09-07 17:14:14 +02002856 if( iv->len > 0 )
2857 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002858 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002859 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002860
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002861 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2862 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002863 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002864
Gilles Peskinee0866522019-02-19 19:44:00 +01002865 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002866 PSA_ASSERT( psa_cipher_update( &operation,
2867 input->x, first_part_size,
2868 output, output_buffer_size,
2869 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002870 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002871 TEST_ASSERT( function_output_length <=
2872 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2873 TEST_ASSERT( function_output_length <=
2874 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002875 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002876
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002877 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02002878 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002879 PSA_ASSERT( psa_cipher_update( &operation,
2880 input->x + first_part_size,
2881 input->len - first_part_size,
2882 ( output_buffer_size == 0 ? NULL :
2883 output + total_output_length ),
2884 output_buffer_size - total_output_length,
2885 &function_output_length ) );
2886 TEST_ASSERT( function_output_length == output2_length );
2887 TEST_ASSERT( function_output_length <=
2888 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2889 alg,
2890 input->len - first_part_size ) );
2891 TEST_ASSERT( function_output_length <=
2892 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2893 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002894 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002895
Gilles Peskine50e586b2018-06-08 14:28:46 +02002896 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002897 ( output_buffer_size == 0 ? NULL :
2898 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002899 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002900 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002901 TEST_ASSERT( function_output_length <=
2902 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2903 TEST_ASSERT( function_output_length <=
2904 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002905 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002906 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002907
2908 if( expected_status == PSA_SUCCESS )
2909 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002910 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002911
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002912 ASSERT_COMPARE( expected_output->x, expected_output->len,
2913 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002914 }
2915
Gilles Peskine50e586b2018-06-08 14:28:46 +02002916exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002917 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002918 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002919 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002920 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002921}
2922/* END_CASE */
2923
Gilles Peskine50e586b2018-06-08 14:28:46 +02002924/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02002925void cipher_decrypt_fail( int alg_arg,
2926 int key_type_arg,
2927 data_t *key_data,
2928 data_t *iv,
2929 data_t *input_arg,
2930 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002931{
2932 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2933 psa_status_t status;
2934 psa_key_type_t key_type = key_type_arg;
2935 psa_algorithm_t alg = alg_arg;
2936 psa_status_t expected_status = expected_status_arg;
2937 unsigned char *input = NULL;
2938 size_t input_buffer_size = 0;
2939 unsigned char *output = NULL;
2940 size_t output_buffer_size = 0;
2941 size_t output_length = 0;
2942 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2943
2944 if ( PSA_ERROR_BAD_STATE != expected_status )
2945 {
2946 PSA_ASSERT( psa_crypto_init( ) );
2947
2948 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2949 psa_set_key_algorithm( &attributes, alg );
2950 psa_set_key_type( &attributes, key_type );
2951
2952 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2953 &key ) );
2954 }
2955
2956 /* Allocate input buffer and copy the iv and the plaintext */
2957 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
2958 if ( input_buffer_size > 0 )
2959 {
2960 ASSERT_ALLOC( input, input_buffer_size );
2961 memcpy( input, iv->x, iv->len );
2962 memcpy( input + iv->len, input_arg->x, input_arg->len );
2963 }
2964
2965 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
2966 ASSERT_ALLOC( output, output_buffer_size );
2967
2968 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
2969 output_buffer_size, &output_length );
2970 TEST_EQUAL( status, expected_status );
2971
2972exit:
2973 mbedtls_free( input );
2974 mbedtls_free( output );
2975 psa_destroy_key( key );
2976 PSA_DONE( );
2977}
2978/* END_CASE */
2979
2980/* BEGIN_CASE */
2981void cipher_decrypt( int alg_arg,
2982 int key_type_arg,
2983 data_t *key_data,
2984 data_t *iv,
2985 data_t *input_arg,
2986 data_t *expected_output )
2987{
2988 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2989 psa_key_type_t key_type = key_type_arg;
2990 psa_algorithm_t alg = alg_arg;
2991 unsigned char *input = NULL;
2992 size_t input_buffer_size = 0;
2993 unsigned char *output = NULL;
2994 size_t output_buffer_size = 0;
2995 size_t output_length = 0;
2996 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2997
2998 PSA_ASSERT( psa_crypto_init( ) );
2999
3000 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3001 psa_set_key_algorithm( &attributes, alg );
3002 psa_set_key_type( &attributes, key_type );
3003
3004 /* Allocate input buffer and copy the iv and the plaintext */
3005 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3006 if ( input_buffer_size > 0 )
3007 {
3008 ASSERT_ALLOC( input, input_buffer_size );
3009 memcpy( input, iv->x, iv->len );
3010 memcpy( input + iv->len, input_arg->x, input_arg->len );
3011 }
3012
3013 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3014 ASSERT_ALLOC( output, output_buffer_size );
3015
3016 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3017 &key ) );
3018
3019 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3020 output_buffer_size, &output_length ) );
3021 TEST_ASSERT( output_length <=
3022 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
3023 TEST_ASSERT( output_length <=
3024 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
3025
3026 ASSERT_COMPARE( expected_output->x, expected_output->len,
3027 output, output_length );
3028exit:
3029 mbedtls_free( input );
3030 mbedtls_free( output );
3031 psa_destroy_key( key );
3032 PSA_DONE( );
3033}
3034/* END_CASE */
3035
3036/* BEGIN_CASE */
3037void cipher_verify_output( int alg_arg,
3038 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003039 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003040 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003041{
Ronald Cron5425a212020-08-04 14:58:35 +02003042 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003043 psa_key_type_t key_type = key_type_arg;
3044 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003045 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003046 size_t output1_size = 0;
3047 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003048 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003049 size_t output2_size = 0;
3050 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003051 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003052
Gilles Peskine8817f612018-12-18 00:18:46 +01003053 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003054
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003055 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3056 psa_set_key_algorithm( &attributes, alg );
3057 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003058
Ronald Cron5425a212020-08-04 14:58:35 +02003059 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3060 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003061 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003062 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003063
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003064 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3065 output1, output1_size,
3066 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003067 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003068 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003069 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003070 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003071
3072 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003073 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003074
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003075 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3076 output2, output2_size,
3077 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003078 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003079 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003080 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003081 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003082
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003083 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003084
3085exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003086 mbedtls_free( output1 );
3087 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003088 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003089 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003090}
3091/* END_CASE */
3092
3093/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003094void cipher_verify_output_multipart( int alg_arg,
3095 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003096 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003097 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003098 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003099{
Ronald Cron5425a212020-08-04 14:58:35 +02003100 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003101 psa_key_type_t key_type = key_type_arg;
3102 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003103 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003104 unsigned char iv[16] = {0};
3105 size_t iv_size = 16;
3106 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003107 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003108 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003109 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003110 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003111 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003112 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003113 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003114 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3115 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003116 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003117
Gilles Peskine8817f612018-12-18 00:18:46 +01003118 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003119
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003120 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3121 psa_set_key_algorithm( &attributes, alg );
3122 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003123
Ronald Cron5425a212020-08-04 14:58:35 +02003124 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3125 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003126
Ronald Cron5425a212020-08-04 14:58:35 +02003127 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3128 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003129
Steven Cooreman177deba2020-09-07 17:14:14 +02003130 if( alg != PSA_ALG_ECB_NO_PADDING )
3131 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003132 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3133 iv, iv_size,
3134 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003135 }
3136
gabor-mezei-armceface22021-01-21 12:26:17 +01003137 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3138 TEST_ASSERT( output1_buffer_size <=
3139 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003140 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003141
Gilles Peskinee0866522019-02-19 19:44:00 +01003142 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003143
Gilles Peskine8817f612018-12-18 00:18:46 +01003144 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3145 output1, output1_buffer_size,
3146 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003147 TEST_ASSERT( function_output_length <=
3148 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3149 TEST_ASSERT( function_output_length <=
3150 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003151 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003152
Gilles Peskine8817f612018-12-18 00:18:46 +01003153 PSA_ASSERT( psa_cipher_update( &operation1,
3154 input->x + first_part_size,
3155 input->len - first_part_size,
3156 output1, output1_buffer_size,
3157 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003158 TEST_ASSERT( function_output_length <=
3159 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3160 alg,
3161 input->len - first_part_size ) );
3162 TEST_ASSERT( function_output_length <=
3163 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003164 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003165
Gilles Peskine8817f612018-12-18 00:18:46 +01003166 PSA_ASSERT( psa_cipher_finish( &operation1,
3167 output1 + output1_length,
3168 output1_buffer_size - output1_length,
3169 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003170 TEST_ASSERT( function_output_length <=
3171 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3172 TEST_ASSERT( function_output_length <=
3173 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003174 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003175
Gilles Peskine8817f612018-12-18 00:18:46 +01003176 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003177
Gilles Peskine048b7f02018-06-08 14:20:49 +02003178 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003179 TEST_ASSERT( output2_buffer_size <=
3180 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3181 TEST_ASSERT( output2_buffer_size <=
3182 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003183 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003184
Steven Cooreman177deba2020-09-07 17:14:14 +02003185 if( iv_length > 0 )
3186 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003187 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3188 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003189 }
Moran Pekerded84402018-06-06 16:36:50 +03003190
Gilles Peskine8817f612018-12-18 00:18:46 +01003191 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3192 output2, output2_buffer_size,
3193 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003194 TEST_ASSERT( function_output_length <=
3195 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3196 TEST_ASSERT( function_output_length <=
3197 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003198 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003199
Gilles Peskine8817f612018-12-18 00:18:46 +01003200 PSA_ASSERT( psa_cipher_update( &operation2,
3201 output1 + first_part_size,
3202 output1_length - first_part_size,
3203 output2, output2_buffer_size,
3204 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003205 TEST_ASSERT( function_output_length <=
3206 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3207 alg,
3208 output1_length - first_part_size ) );
3209 TEST_ASSERT( function_output_length <=
3210 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003211 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003212
Gilles Peskine8817f612018-12-18 00:18:46 +01003213 PSA_ASSERT( psa_cipher_finish( &operation2,
3214 output2 + output2_length,
3215 output2_buffer_size - output2_length,
3216 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003217 TEST_ASSERT( function_output_length <=
3218 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3219 TEST_ASSERT( function_output_length <=
3220 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003221 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003222
Gilles Peskine8817f612018-12-18 00:18:46 +01003223 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003224
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003225 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003226
3227exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003228 psa_cipher_abort( &operation1 );
3229 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003230 mbedtls_free( output1 );
3231 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003232 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003233 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003234}
3235/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003236
Gilles Peskine20035e32018-02-03 22:44:14 +01003237/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003238void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003239 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003240 data_t *nonce,
3241 data_t *additional_data,
3242 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003243 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003244{
Ronald Cron5425a212020-08-04 14:58:35 +02003245 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003246 psa_key_type_t key_type = key_type_arg;
3247 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003248 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003249 unsigned char *output_data = NULL;
3250 size_t output_size = 0;
3251 size_t output_length = 0;
3252 unsigned char *output_data2 = NULL;
3253 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003254 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003255 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003256 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
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_ENCRYPT | 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 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3272 * should be exact. */
3273 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3274 expected_result != PSA_ERROR_NOT_SUPPORTED )
3275 {
3276 TEST_EQUAL( output_size,
3277 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3278 TEST_ASSERT( output_size <=
3279 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3280 }
3281 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003282
Steven Cooremanf49478b2021-02-15 15:19:25 +01003283 status = psa_aead_encrypt( 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
3291 /* If the operation is not supported, just skip and not fail in case the
3292 * encryption involves a common limitation of cryptography hardwares and
3293 * an alternative implementation. */
3294 if( status == PSA_ERROR_NOT_SUPPORTED )
3295 {
3296 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 );
3298 }
3299
3300 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003301
3302 if( PSA_SUCCESS == expected_result )
3303 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003304 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003305
Gilles Peskine003a4a92019-05-14 16:09:40 +02003306 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3307 * should be exact. */
3308 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003309 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003310
gabor-mezei-armceface22021-01-21 12:26:17 +01003311 TEST_ASSERT( input_data->len <=
3312 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3313
Ronald Cron5425a212020-08-04 14:58:35 +02003314 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003315 nonce->x, nonce->len,
3316 additional_data->x,
3317 additional_data->len,
3318 output_data, output_length,
3319 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003320 &output_length2 ),
3321 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003322
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003323 ASSERT_COMPARE( input_data->x, input_data->len,
3324 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003325 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003326
Gilles Peskinea1cac842018-06-11 19:33:02 +02003327exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003328 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003329 mbedtls_free( output_data );
3330 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003331 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003332}
3333/* END_CASE */
3334
3335/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003336void aead_encrypt( int key_type_arg, data_t *key_data,
3337 int alg_arg,
3338 data_t *nonce,
3339 data_t *additional_data,
3340 data_t *input_data,
3341 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003342{
Ronald Cron5425a212020-08-04 14:58:35 +02003343 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003344 psa_key_type_t key_type = key_type_arg;
3345 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003346 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003347 unsigned char *output_data = NULL;
3348 size_t output_size = 0;
3349 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003350 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003351 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003352
Gilles Peskine8817f612018-12-18 00:18:46 +01003353 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003354
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003355 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3356 psa_set_key_algorithm( &attributes, alg );
3357 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003358
Gilles Peskine049c7532019-05-15 20:22:09 +02003359 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003360 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003361 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3362 key_bits = psa_get_key_bits( &attributes );
3363
3364 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3365 alg );
3366 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3367 * should be exact. */
3368 TEST_EQUAL( output_size,
3369 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3370 TEST_ASSERT( output_size <=
3371 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3372 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003373
Steven Cooremand588ea12021-01-11 19:36:04 +01003374 status = psa_aead_encrypt( key, alg,
3375 nonce->x, nonce->len,
3376 additional_data->x, additional_data->len,
3377 input_data->x, input_data->len,
3378 output_data, output_size,
3379 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003380
Ronald Cron28a45ed2021-02-09 20:35:42 +01003381 /* If the operation is not supported, just skip and not fail in case the
3382 * encryption involves a common limitation of cryptography hardwares and
3383 * an alternative implementation. */
3384 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003385 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003386 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3387 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003388 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003389
3390 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003391 ASSERT_COMPARE( expected_result->x, expected_result->len,
3392 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003393
Gilles Peskinea1cac842018-06-11 19:33:02 +02003394exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003395 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003396 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003397 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003398}
3399/* END_CASE */
3400
3401/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003402void aead_decrypt( int key_type_arg, data_t *key_data,
3403 int alg_arg,
3404 data_t *nonce,
3405 data_t *additional_data,
3406 data_t *input_data,
3407 data_t *expected_data,
3408 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003409{
Ronald Cron5425a212020-08-04 14:58:35 +02003410 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003411 psa_key_type_t key_type = key_type_arg;
3412 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003413 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003414 unsigned char *output_data = NULL;
3415 size_t output_size = 0;
3416 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003417 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003418 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003419 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003420
Gilles Peskine8817f612018-12-18 00:18:46 +01003421 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003422
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003423 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3424 psa_set_key_algorithm( &attributes, alg );
3425 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003426
Gilles Peskine049c7532019-05-15 20:22:09 +02003427 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003428 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003429 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3430 key_bits = psa_get_key_bits( &attributes );
3431
3432 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3433 alg );
3434 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3435 expected_result != PSA_ERROR_NOT_SUPPORTED )
3436 {
3437 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3438 * should be exact. */
3439 TEST_EQUAL( output_size,
3440 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3441 TEST_ASSERT( output_size <=
3442 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3443 }
3444 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003445
Steven Cooremand588ea12021-01-11 19:36:04 +01003446 status = psa_aead_decrypt( key, alg,
3447 nonce->x, nonce->len,
3448 additional_data->x,
3449 additional_data->len,
3450 input_data->x, input_data->len,
3451 output_data, output_size,
3452 &output_length );
3453
Ronald Cron28a45ed2021-02-09 20:35:42 +01003454 /* If the operation is not supported, just skip and not fail in case the
3455 * decryption involves a common limitation of cryptography hardwares and
3456 * an alternative implementation. */
3457 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003458 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003459 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3460 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003461 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003462
3463 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003464
Gilles Peskine2d277862018-06-18 15:41:12 +02003465 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003466 ASSERT_COMPARE( expected_data->x, expected_data->len,
3467 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003468
Gilles Peskinea1cac842018-06-11 19:33:02 +02003469exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003470 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003471 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003472 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003473}
3474/* END_CASE */
3475
3476/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003477void signature_size( int type_arg,
3478 int bits,
3479 int alg_arg,
3480 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003481{
3482 psa_key_type_t type = type_arg;
3483 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003484 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003485
Gilles Peskinefe11b722018-12-18 00:24:04 +01003486 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003487
Gilles Peskinee59236f2018-01-27 23:32:46 +01003488exit:
3489 ;
3490}
3491/* END_CASE */
3492
3493/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003494void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3495 int alg_arg, data_t *input_data,
3496 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003497{
Ronald Cron5425a212020-08-04 14:58:35 +02003498 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003499 psa_key_type_t key_type = key_type_arg;
3500 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003501 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003502 unsigned char *signature = NULL;
3503 size_t signature_size;
3504 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003505 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003506
Gilles Peskine8817f612018-12-18 00:18:46 +01003507 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003508
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003509 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003510 psa_set_key_algorithm( &attributes, alg );
3511 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003512
Gilles Peskine049c7532019-05-15 20:22:09 +02003513 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003514 &key ) );
3515 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003516 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003517
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003518 /* Allocate a buffer which has the size advertized by the
3519 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003520 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003521 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003522 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003523 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003524 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003525
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003526 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003527 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003528 input_data->x, input_data->len,
3529 signature, signature_size,
3530 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003531 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003532 ASSERT_COMPARE( output_data->x, output_data->len,
3533 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003534
3535exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003536 /*
3537 * Key attributes may have been returned by psa_get_key_attributes()
3538 * thus reset them as required.
3539 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003540 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003541
Ronald Cron5425a212020-08-04 14:58:35 +02003542 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003543 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003544 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003545}
3546/* END_CASE */
3547
3548/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003549void sign_hash_fail( int key_type_arg, data_t *key_data,
3550 int alg_arg, data_t *input_data,
3551 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003552{
Ronald Cron5425a212020-08-04 14:58:35 +02003553 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003554 psa_key_type_t key_type = key_type_arg;
3555 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003556 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003557 psa_status_t actual_status;
3558 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003559 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003560 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003561 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003562
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003563 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003564
Gilles Peskine8817f612018-12-18 00:18:46 +01003565 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003566
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003567 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003568 psa_set_key_algorithm( &attributes, alg );
3569 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003570
Gilles Peskine049c7532019-05-15 20:22:09 +02003571 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003572 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003573
Ronald Cron5425a212020-08-04 14:58:35 +02003574 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003575 input_data->x, input_data->len,
3576 signature, signature_size,
3577 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003578 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003579 /* The value of *signature_length is unspecified on error, but
3580 * whatever it is, it should be less than signature_size, so that
3581 * if the caller tries to read *signature_length bytes without
3582 * checking the error code then they don't overflow a buffer. */
3583 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003584
3585exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003586 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003587 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003588 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003589 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003590}
3591/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003592
3593/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003594void sign_verify_hash( int key_type_arg, data_t *key_data,
3595 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003596{
Ronald Cron5425a212020-08-04 14:58:35 +02003597 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003598 psa_key_type_t key_type = key_type_arg;
3599 psa_algorithm_t alg = alg_arg;
3600 size_t key_bits;
3601 unsigned char *signature = NULL;
3602 size_t signature_size;
3603 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003604 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003605
Gilles Peskine8817f612018-12-18 00:18:46 +01003606 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003607
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003608 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003609 psa_set_key_algorithm( &attributes, alg );
3610 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003611
Gilles Peskine049c7532019-05-15 20:22:09 +02003612 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003613 &key ) );
3614 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003615 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003616
3617 /* Allocate a buffer which has the size advertized by the
3618 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003619 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003620 key_bits, alg );
3621 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003622 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003623 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003624
3625 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003626 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003627 input_data->x, input_data->len,
3628 signature, signature_size,
3629 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003630 /* Check that the signature length looks sensible. */
3631 TEST_ASSERT( signature_length <= signature_size );
3632 TEST_ASSERT( signature_length > 0 );
3633
3634 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003635 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003636 input_data->x, input_data->len,
3637 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003638
3639 if( input_data->len != 0 )
3640 {
3641 /* Flip a bit in the input and verify that the signature is now
3642 * detected as invalid. Flip a bit at the beginning, not at the end,
3643 * because ECDSA may ignore the last few bits of the input. */
3644 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003645 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003646 input_data->x, input_data->len,
3647 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003648 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003649 }
3650
3651exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003652 /*
3653 * Key attributes may have been returned by psa_get_key_attributes()
3654 * thus reset them as required.
3655 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003656 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003657
Ronald Cron5425a212020-08-04 14:58:35 +02003658 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003659 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003660 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003661}
3662/* END_CASE */
3663
3664/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003665void verify_hash( int key_type_arg, data_t *key_data,
3666 int alg_arg, data_t *hash_data,
3667 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003668{
Ronald Cron5425a212020-08-04 14:58:35 +02003669 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003670 psa_key_type_t key_type = key_type_arg;
3671 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003672 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003673
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003674 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003675
Gilles Peskine8817f612018-12-18 00:18:46 +01003676 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003677
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003678 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003679 psa_set_key_algorithm( &attributes, alg );
3680 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003681
Gilles Peskine049c7532019-05-15 20:22:09 +02003682 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003683 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003684
Ronald Cron5425a212020-08-04 14:58:35 +02003685 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003686 hash_data->x, hash_data->len,
3687 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003688
itayzafrir5c753392018-05-08 11:18:38 +03003689exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003690 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003691 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003692 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003693}
3694/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003695
3696/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003697void verify_hash_fail( int key_type_arg, data_t *key_data,
3698 int alg_arg, data_t *hash_data,
3699 data_t *signature_data,
3700 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003701{
Ronald Cron5425a212020-08-04 14:58:35 +02003702 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003703 psa_key_type_t key_type = key_type_arg;
3704 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003705 psa_status_t actual_status;
3706 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003707 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003708
Gilles Peskine8817f612018-12-18 00:18:46 +01003709 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003710
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003711 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003712 psa_set_key_algorithm( &attributes, alg );
3713 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003714
Gilles Peskine049c7532019-05-15 20:22:09 +02003715 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003716 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003717
Ronald Cron5425a212020-08-04 14:58:35 +02003718 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003719 hash_data->x, hash_data->len,
3720 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003721 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003722
3723exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003724 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003725 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003726 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003727}
3728/* END_CASE */
3729
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003730/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02003731void sign_message_deterministic( int key_type_arg,
3732 data_t *key_data,
3733 int alg_arg,
3734 data_t *input_data,
3735 data_t *output_data )
3736{
3737 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3738 psa_key_type_t key_type = key_type_arg;
3739 psa_algorithm_t alg = alg_arg;
3740 size_t key_bits;
3741 unsigned char *signature = NULL;
3742 size_t signature_size;
3743 size_t signature_length = 0xdeadbeef;
3744 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3745
3746 PSA_ASSERT( psa_crypto_init( ) );
3747
3748 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3749 psa_set_key_algorithm( &attributes, alg );
3750 psa_set_key_type( &attributes, key_type );
3751
3752 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3753 &key ) );
3754 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3755 key_bits = psa_get_key_bits( &attributes );
3756
3757 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3758 TEST_ASSERT( signature_size != 0 );
3759 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3760 ASSERT_ALLOC( signature, signature_size );
3761
3762 PSA_ASSERT( psa_sign_message( key, alg,
3763 input_data->x, input_data->len,
3764 signature, signature_size,
3765 &signature_length ) );
3766
3767 ASSERT_COMPARE( output_data->x, output_data->len,
3768 signature, signature_length );
3769
3770exit:
3771 psa_reset_key_attributes( &attributes );
3772
3773 psa_destroy_key( key );
3774 mbedtls_free( signature );
3775 PSA_DONE( );
3776
3777}
3778/* END_CASE */
3779
3780/* BEGIN_CASE */
3781void sign_message_fail( int key_type_arg,
3782 data_t *key_data,
3783 int alg_arg,
3784 data_t *input_data,
3785 int signature_size_arg,
3786 int expected_status_arg )
3787{
3788 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3789 psa_key_type_t key_type = key_type_arg;
3790 psa_algorithm_t alg = alg_arg;
3791 size_t signature_size = signature_size_arg;
3792 psa_status_t actual_status;
3793 psa_status_t expected_status = expected_status_arg;
3794 unsigned char *signature = NULL;
3795 size_t signature_length = 0xdeadbeef;
3796 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3797
3798 ASSERT_ALLOC( signature, signature_size );
3799
3800 PSA_ASSERT( psa_crypto_init( ) );
3801
3802 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3803 psa_set_key_algorithm( &attributes, alg );
3804 psa_set_key_type( &attributes, key_type );
3805
3806 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3807 &key ) );
3808
3809 actual_status = psa_sign_message( key, alg,
3810 input_data->x, input_data->len,
3811 signature, signature_size,
3812 &signature_length );
3813 TEST_EQUAL( actual_status, expected_status );
3814 /* The value of *signature_length is unspecified on error, but
3815 * whatever it is, it should be less than signature_size, so that
3816 * if the caller tries to read *signature_length bytes without
3817 * checking the error code then they don't overflow a buffer. */
3818 TEST_ASSERT( signature_length <= signature_size );
3819
3820exit:
3821 psa_reset_key_attributes( &attributes );
3822 psa_destroy_key( key );
3823 mbedtls_free( signature );
3824 PSA_DONE( );
3825}
3826/* END_CASE */
3827
3828/* BEGIN_CASE */
3829void sign_verify_message( int key_type_arg,
3830 data_t *key_data,
3831 int alg_arg,
3832 data_t *input_data )
3833{
3834 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3835 psa_key_type_t key_type = key_type_arg;
3836 psa_algorithm_t alg = alg_arg;
3837 size_t key_bits;
3838 unsigned char *signature = NULL;
3839 size_t signature_size;
3840 size_t signature_length = 0xdeadbeef;
3841 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3842
3843 PSA_ASSERT( psa_crypto_init( ) );
3844
3845 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3846 PSA_KEY_USAGE_VERIFY_MESSAGE );
3847 psa_set_key_algorithm( &attributes, alg );
3848 psa_set_key_type( &attributes, key_type );
3849
3850 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3851 &key ) );
3852 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3853 key_bits = psa_get_key_bits( &attributes );
3854
3855 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3856 TEST_ASSERT( signature_size != 0 );
3857 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3858 ASSERT_ALLOC( signature, signature_size );
3859
3860 PSA_ASSERT( psa_sign_message( key, alg,
3861 input_data->x, input_data->len,
3862 signature, signature_size,
3863 &signature_length ) );
3864 TEST_ASSERT( signature_length <= signature_size );
3865 TEST_ASSERT( signature_length > 0 );
3866
3867 PSA_ASSERT( psa_verify_message( key, alg,
3868 input_data->x, input_data->len,
3869 signature, signature_length ) );
3870
3871 if( input_data->len != 0 )
3872 {
3873 /* Flip a bit in the input and verify that the signature is now
3874 * detected as invalid. Flip a bit at the beginning, not at the end,
3875 * because ECDSA may ignore the last few bits of the input. */
3876 input_data->x[0] ^= 1;
3877 TEST_EQUAL( psa_verify_message( key, alg,
3878 input_data->x, input_data->len,
3879 signature, signature_length ),
3880 PSA_ERROR_INVALID_SIGNATURE );
3881 }
3882
3883exit:
3884 psa_reset_key_attributes( &attributes );
3885
3886 psa_destroy_key( key );
3887 mbedtls_free( signature );
3888 PSA_DONE( );
3889}
3890/* END_CASE */
3891
3892/* BEGIN_CASE */
3893void verify_message( int key_type_arg,
3894 data_t *key_data,
3895 int alg_arg,
3896 data_t *input_data,
3897 data_t *signature_data )
3898{
3899 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3900 psa_key_type_t key_type = key_type_arg;
3901 psa_algorithm_t alg = alg_arg;
3902 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3903
3904 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3905
3906 PSA_ASSERT( psa_crypto_init( ) );
3907
3908 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3909 psa_set_key_algorithm( &attributes, alg );
3910 psa_set_key_type( &attributes, key_type );
3911
3912 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3913 &key ) );
3914
3915 PSA_ASSERT( psa_verify_message( key, alg,
3916 input_data->x, input_data->len,
3917 signature_data->x, signature_data->len ) );
3918
3919exit:
3920 psa_reset_key_attributes( &attributes );
3921 psa_destroy_key( key );
3922 PSA_DONE( );
3923}
3924/* END_CASE */
3925
3926/* BEGIN_CASE */
3927void verify_message_fail( int key_type_arg,
3928 data_t *key_data,
3929 int alg_arg,
3930 data_t *hash_data,
3931 data_t *signature_data,
3932 int expected_status_arg )
3933{
3934 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3935 psa_key_type_t key_type = key_type_arg;
3936 psa_algorithm_t alg = alg_arg;
3937 psa_status_t actual_status;
3938 psa_status_t expected_status = expected_status_arg;
3939 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3940
3941 PSA_ASSERT( psa_crypto_init( ) );
3942
3943 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3944 psa_set_key_algorithm( &attributes, alg );
3945 psa_set_key_type( &attributes, key_type );
3946
3947 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3948 &key ) );
3949
3950 actual_status = psa_verify_message( key, alg,
3951 hash_data->x, hash_data->len,
3952 signature_data->x,
3953 signature_data->len );
3954 TEST_EQUAL( actual_status, expected_status );
3955
3956exit:
3957 psa_reset_key_attributes( &attributes );
3958 psa_destroy_key( key );
3959 PSA_DONE( );
3960}
3961/* END_CASE */
3962
3963/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003964void asymmetric_encrypt( int key_type_arg,
3965 data_t *key_data,
3966 int alg_arg,
3967 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003968 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003969 int expected_output_length_arg,
3970 int expected_status_arg )
3971{
Ronald Cron5425a212020-08-04 14:58:35 +02003972 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003973 psa_key_type_t key_type = key_type_arg;
3974 psa_algorithm_t alg = alg_arg;
3975 size_t expected_output_length = expected_output_length_arg;
3976 size_t key_bits;
3977 unsigned char *output = NULL;
3978 size_t output_size;
3979 size_t output_length = ~0;
3980 psa_status_t actual_status;
3981 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003982 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003983
Gilles Peskine8817f612018-12-18 00:18:46 +01003984 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003985
Gilles Peskine656896e2018-06-29 19:12:28 +02003986 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003987 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3988 psa_set_key_algorithm( &attributes, alg );
3989 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003990 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003991 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003992
3993 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003994 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003995 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003996
Gilles Peskine656896e2018-06-29 19:12:28 +02003997 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003998 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003999 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004000
4001 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004002 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004003 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004004 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004005 output, output_size,
4006 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004007 TEST_EQUAL( actual_status, expected_status );
4008 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004009
Gilles Peskine68428122018-06-30 18:42:41 +02004010 /* If the label is empty, the test framework puts a non-null pointer
4011 * in label->x. Test that a null pointer works as well. */
4012 if( label->len == 0 )
4013 {
4014 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004015 if( output_size != 0 )
4016 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004017 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004018 input_data->x, input_data->len,
4019 NULL, label->len,
4020 output, output_size,
4021 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004022 TEST_EQUAL( actual_status, expected_status );
4023 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004024 }
4025
Gilles Peskine656896e2018-06-29 19:12:28 +02004026exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004027 /*
4028 * Key attributes may have been returned by psa_get_key_attributes()
4029 * thus reset them as required.
4030 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004031 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004032
Ronald Cron5425a212020-08-04 14:58:35 +02004033 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004034 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004035 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004036}
4037/* END_CASE */
4038
4039/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004040void asymmetric_encrypt_decrypt( int key_type_arg,
4041 data_t *key_data,
4042 int alg_arg,
4043 data_t *input_data,
4044 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004045{
Ronald Cron5425a212020-08-04 14:58:35 +02004046 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004047 psa_key_type_t key_type = key_type_arg;
4048 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004049 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004050 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004051 size_t output_size;
4052 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004053 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004054 size_t output2_size;
4055 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004056 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004057
Gilles Peskine8817f612018-12-18 00:18:46 +01004058 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004059
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004060 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4061 psa_set_key_algorithm( &attributes, alg );
4062 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004063
Gilles Peskine049c7532019-05-15 20:22:09 +02004064 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004065 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004066
4067 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004068 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004069 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004070
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004071 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004072 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004073 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01004074
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004075 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01004076 TEST_ASSERT( output2_size <=
4077 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
4078 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004079 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004080
Gilles Peskineeebd7382018-06-08 18:11:54 +02004081 /* We test encryption by checking that encrypt-then-decrypt gives back
4082 * the original plaintext because of the non-optional random
4083 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004084 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004085 input_data->x, input_data->len,
4086 label->x, label->len,
4087 output, output_size,
4088 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004089 /* We don't know what ciphertext length to expect, but check that
4090 * it looks sensible. */
4091 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004092
Ronald Cron5425a212020-08-04 14:58:35 +02004093 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004094 output, output_length,
4095 label->x, label->len,
4096 output2, output2_size,
4097 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004098 ASSERT_COMPARE( input_data->x, input_data->len,
4099 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004100
4101exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004102 /*
4103 * Key attributes may have been returned by psa_get_key_attributes()
4104 * thus reset them as required.
4105 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004106 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004107
Ronald Cron5425a212020-08-04 14:58:35 +02004108 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004109 mbedtls_free( output );
4110 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004111 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004112}
4113/* END_CASE */
4114
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004115/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004116void asymmetric_decrypt( int key_type_arg,
4117 data_t *key_data,
4118 int alg_arg,
4119 data_t *input_data,
4120 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004121 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004122{
Ronald Cron5425a212020-08-04 14:58:35 +02004123 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004124 psa_key_type_t key_type = key_type_arg;
4125 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004126 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004127 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004128 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004129 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004130 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004131
Gilles Peskine8817f612018-12-18 00:18:46 +01004132 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004133
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004134 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4135 psa_set_key_algorithm( &attributes, alg );
4136 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004137
Gilles Peskine049c7532019-05-15 20:22:09 +02004138 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004139 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004140
gabor-mezei-armceface22021-01-21 12:26:17 +01004141 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4142 key_bits = psa_get_key_bits( &attributes );
4143
4144 /* Determine the maximum ciphertext length */
4145 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
4146 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
4147 ASSERT_ALLOC( output, output_size );
4148
Ronald Cron5425a212020-08-04 14:58:35 +02004149 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004150 input_data->x, input_data->len,
4151 label->x, label->len,
4152 output,
4153 output_size,
4154 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004155 ASSERT_COMPARE( expected_data->x, expected_data->len,
4156 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004157
Gilles Peskine68428122018-06-30 18:42:41 +02004158 /* If the label is empty, the test framework puts a non-null pointer
4159 * in label->x. Test that a null pointer works as well. */
4160 if( label->len == 0 )
4161 {
4162 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004163 if( output_size != 0 )
4164 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004165 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004166 input_data->x, input_data->len,
4167 NULL, label->len,
4168 output,
4169 output_size,
4170 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004171 ASSERT_COMPARE( expected_data->x, expected_data->len,
4172 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004173 }
4174
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004175exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004176 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004177 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004178 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004179 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004180}
4181/* END_CASE */
4182
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004183/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004184void asymmetric_decrypt_fail( int key_type_arg,
4185 data_t *key_data,
4186 int alg_arg,
4187 data_t *input_data,
4188 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004189 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004190 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004191{
Ronald Cron5425a212020-08-04 14:58:35 +02004192 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004193 psa_key_type_t key_type = key_type_arg;
4194 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004195 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004196 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004197 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004198 psa_status_t actual_status;
4199 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004200 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004201
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004202 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004203
Gilles Peskine8817f612018-12-18 00:18:46 +01004204 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004205
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004206 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4207 psa_set_key_algorithm( &attributes, alg );
4208 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004209
Gilles Peskine049c7532019-05-15 20:22:09 +02004210 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004211 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004212
Ronald Cron5425a212020-08-04 14:58:35 +02004213 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004214 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004215 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004216 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004217 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004218 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004219 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004220
Gilles Peskine68428122018-06-30 18:42:41 +02004221 /* If the label is empty, the test framework puts a non-null pointer
4222 * in label->x. Test that a null pointer works as well. */
4223 if( label->len == 0 )
4224 {
4225 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004226 if( output_size != 0 )
4227 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004228 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004229 input_data->x, input_data->len,
4230 NULL, label->len,
4231 output, output_size,
4232 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004233 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004234 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004235 }
4236
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004237exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004238 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004239 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004240 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004241 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004242}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004243/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004244
4245/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004246void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004247{
4248 /* Test each valid way of initializing the object, except for `= {0}`, as
4249 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4250 * though it's OK by the C standard. We could test for this, but we'd need
4251 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004252 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004253 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4254 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4255 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004256
4257 memset( &zero, 0, sizeof( zero ) );
4258
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004259 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004260 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004261 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004262 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004263 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004264 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004265 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004266
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004267 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004268 PSA_ASSERT( psa_key_derivation_abort(&func) );
4269 PSA_ASSERT( psa_key_derivation_abort(&init) );
4270 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004271}
4272/* END_CASE */
4273
Janos Follath16de4a42019-06-13 16:32:24 +01004274/* BEGIN_CASE */
4275void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004276{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004277 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004278 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004279 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004280
Gilles Peskine8817f612018-12-18 00:18:46 +01004281 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004282
Janos Follath16de4a42019-06-13 16:32:24 +01004283 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004284 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004285
4286exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004287 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004288 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004289}
4290/* END_CASE */
4291
Janos Follathaf3c2a02019-06-12 12:34:34 +01004292/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004293void derive_set_capacity( int alg_arg, int capacity_arg,
4294 int expected_status_arg )
4295{
4296 psa_algorithm_t alg = alg_arg;
4297 size_t capacity = capacity_arg;
4298 psa_status_t expected_status = expected_status_arg;
4299 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4300
4301 PSA_ASSERT( psa_crypto_init( ) );
4302
4303 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4304
4305 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4306 expected_status );
4307
4308exit:
4309 psa_key_derivation_abort( &operation );
4310 PSA_DONE( );
4311}
4312/* END_CASE */
4313
4314/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004315void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004316 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004317 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004318 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004319 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004320 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004321 int expected_status_arg3,
4322 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004323{
4324 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004325 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4326 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004327 psa_status_t expected_statuses[] = {expected_status_arg1,
4328 expected_status_arg2,
4329 expected_status_arg3};
4330 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004331 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4332 MBEDTLS_SVC_KEY_ID_INIT,
4333 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004334 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4335 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4336 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004337 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004338 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004339 psa_status_t expected_output_status = expected_output_status_arg;
4340 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004341
4342 PSA_ASSERT( psa_crypto_init( ) );
4343
4344 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4345 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004346
4347 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4348
4349 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4350 {
Gilles Peskine4023c012021-05-27 13:21:20 +02004351 mbedtls_test_set_step( i );
4352 if( steps[i] == 0 )
4353 {
4354 /* Skip this step */
4355 }
4356 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004357 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004358 psa_set_key_type( &attributes, key_types[i] );
4359 PSA_ASSERT( psa_import_key( &attributes,
4360 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004361 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004362 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4363 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4364 {
4365 // When taking a private key as secret input, use key agreement
4366 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004367 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4368 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004369 expected_statuses[i] );
4370 }
4371 else
4372 {
4373 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004374 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004375 expected_statuses[i] );
4376 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004377 }
4378 else
4379 {
4380 TEST_EQUAL( psa_key_derivation_input_bytes(
4381 &operation, steps[i],
4382 inputs[i]->x, inputs[i]->len ),
4383 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004384 }
4385 }
4386
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004387 if( output_key_type != PSA_KEY_TYPE_NONE )
4388 {
4389 psa_reset_key_attributes( &attributes );
4390 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4391 psa_set_key_bits( &attributes, 8 );
4392 actual_output_status =
4393 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004394 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004395 }
4396 else
4397 {
4398 uint8_t buffer[1];
4399 actual_output_status =
4400 psa_key_derivation_output_bytes( &operation,
4401 buffer, sizeof( buffer ) );
4402 }
4403 TEST_EQUAL( actual_output_status, expected_output_status );
4404
Janos Follathaf3c2a02019-06-12 12:34:34 +01004405exit:
4406 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004407 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4408 psa_destroy_key( keys[i] );
4409 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004410 PSA_DONE( );
4411}
4412/* END_CASE */
4413
Janos Follathd958bb72019-07-03 15:02:16 +01004414/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02004415void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004416{
Janos Follathd958bb72019-07-03 15:02:16 +01004417 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004418 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004419 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004420 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004421 unsigned char input1[] = "Input 1";
4422 size_t input1_length = sizeof( input1 );
4423 unsigned char input2[] = "Input 2";
4424 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004425 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004426 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004427 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4428 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4429 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004430 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004431
Gilles Peskine8817f612018-12-18 00:18:46 +01004432 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004433
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004434 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4435 psa_set_key_algorithm( &attributes, alg );
4436 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004437
Gilles Peskine73676cb2019-05-15 20:15:10 +02004438 PSA_ASSERT( psa_import_key( &attributes,
4439 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004440 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004441
4442 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004443 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4444 input1, input1_length,
4445 input2, input2_length,
4446 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004447 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004448
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004449 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004450 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004451 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004452
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004453 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004454
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004455 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004456 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004457
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004458exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004459 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004460 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004461 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004462}
4463/* END_CASE */
4464
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004465/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02004466void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004467{
4468 uint8_t output_buffer[16];
4469 size_t buffer_size = 16;
4470 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004471 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004472
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004473 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4474 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004475 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004476
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004477 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004478 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004479
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004480 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004481
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004482 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4483 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004484 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004485
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004486 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004487 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004488
4489exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004490 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004491}
4492/* END_CASE */
4493
4494/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004495void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004496 int step1_arg, data_t *input1,
4497 int step2_arg, data_t *input2,
4498 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004499 int requested_capacity_arg,
4500 data_t *expected_output1,
4501 data_t *expected_output2 )
4502{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004503 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004504 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4505 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004506 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4507 MBEDTLS_SVC_KEY_ID_INIT,
4508 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004509 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004510 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004511 uint8_t *expected_outputs[2] =
4512 {expected_output1->x, expected_output2->x};
4513 size_t output_sizes[2] =
4514 {expected_output1->len, expected_output2->len};
4515 size_t output_buffer_size = 0;
4516 uint8_t *output_buffer = NULL;
4517 size_t expected_capacity;
4518 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004519 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004520 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004521 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004522
4523 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4524 {
4525 if( output_sizes[i] > output_buffer_size )
4526 output_buffer_size = output_sizes[i];
4527 if( output_sizes[i] == 0 )
4528 expected_outputs[i] = NULL;
4529 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004530 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004531 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004532
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004533 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4534 psa_set_key_algorithm( &attributes, alg );
4535 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004536
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004537 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004538 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4539 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4540 requested_capacity ) );
4541 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004542 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004543 switch( steps[i] )
4544 {
4545 case 0:
4546 break;
4547 case PSA_KEY_DERIVATION_INPUT_SECRET:
4548 PSA_ASSERT( psa_import_key( &attributes,
4549 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004550 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004551
4552 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4553 {
4554 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4555 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4556 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4557 }
4558
Gilles Peskine1468da72019-05-29 17:35:49 +02004559 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004560 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004561 break;
4562 default:
4563 PSA_ASSERT( psa_key_derivation_input_bytes(
4564 &operation, steps[i],
4565 inputs[i]->x, inputs[i]->len ) );
4566 break;
4567 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004568 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004569
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004570 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004571 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004572 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004573 expected_capacity = requested_capacity;
4574
4575 /* Expansion phase. */
4576 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4577 {
4578 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004579 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004580 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004581 if( expected_capacity == 0 && output_sizes[i] == 0 )
4582 {
4583 /* Reading 0 bytes when 0 bytes are available can go either way. */
4584 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004585 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004586 continue;
4587 }
4588 else if( expected_capacity == 0 ||
4589 output_sizes[i] > expected_capacity )
4590 {
4591 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004592 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004593 expected_capacity = 0;
4594 continue;
4595 }
4596 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004597 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004598 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004599 ASSERT_COMPARE( output_buffer, output_sizes[i],
4600 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004601 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004602 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004603 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004604 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004605 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004606 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004607 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004608
4609exit:
4610 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004611 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004612 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4613 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004614 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004615}
4616/* END_CASE */
4617
4618/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004619void derive_full( int alg_arg,
4620 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004621 data_t *input1,
4622 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004623 int requested_capacity_arg )
4624{
Ronald Cron5425a212020-08-04 14:58:35 +02004625 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004626 psa_algorithm_t alg = alg_arg;
4627 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004628 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004629 unsigned char output_buffer[16];
4630 size_t expected_capacity = requested_capacity;
4631 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004632 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004633
Gilles Peskine8817f612018-12-18 00:18:46 +01004634 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004635
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004636 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4637 psa_set_key_algorithm( &attributes, alg );
4638 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004639
Gilles Peskine049c7532019-05-15 20:22:09 +02004640 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004641 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004642
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004643 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4644 input1->x, input1->len,
4645 input2->x, input2->len,
4646 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004647 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004648
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004649 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004650 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004651 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004652
4653 /* Expansion phase. */
4654 while( current_capacity > 0 )
4655 {
4656 size_t read_size = sizeof( output_buffer );
4657 if( read_size > current_capacity )
4658 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004659 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004660 output_buffer,
4661 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004662 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004663 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004664 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004665 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004666 }
4667
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004668 /* Check that the operation refuses to go over capacity. */
4669 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004670 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004671
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004672 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004673
4674exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004675 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004676 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004677 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004678}
4679/* END_CASE */
4680
Janos Follathe60c9052019-07-03 13:51:30 +01004681/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004682void derive_key_exercise( int alg_arg,
4683 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004684 data_t *input1,
4685 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004686 int derived_type_arg,
4687 int derived_bits_arg,
4688 int derived_usage_arg,
4689 int derived_alg_arg )
4690{
Ronald Cron5425a212020-08-04 14:58:35 +02004691 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4692 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004693 psa_algorithm_t alg = alg_arg;
4694 psa_key_type_t derived_type = derived_type_arg;
4695 size_t derived_bits = derived_bits_arg;
4696 psa_key_usage_t derived_usage = derived_usage_arg;
4697 psa_algorithm_t derived_alg = derived_alg_arg;
4698 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004699 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004700 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004701 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004702
Gilles Peskine8817f612018-12-18 00:18:46 +01004703 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004704
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004705 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4706 psa_set_key_algorithm( &attributes, alg );
4707 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004708 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004709 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004710
4711 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004712 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4713 input1->x, input1->len,
4714 input2->x, input2->len,
4715 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004716 goto exit;
4717
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004718 psa_set_key_usage_flags( &attributes, derived_usage );
4719 psa_set_key_algorithm( &attributes, derived_alg );
4720 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004721 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004722 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004723 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004724
4725 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004726 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004727 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4728 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004729
4730 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004731 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004732 goto exit;
4733
4734exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004735 /*
4736 * Key attributes may have been returned by psa_get_key_attributes()
4737 * thus reset them as required.
4738 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004739 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004740
4741 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004742 psa_destroy_key( base_key );
4743 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004744 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004745}
4746/* END_CASE */
4747
Janos Follath42fd8882019-07-03 14:17:09 +01004748/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004749void derive_key_export( int alg_arg,
4750 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004751 data_t *input1,
4752 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004753 int bytes1_arg,
4754 int bytes2_arg )
4755{
Ronald Cron5425a212020-08-04 14:58:35 +02004756 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4757 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004758 psa_algorithm_t alg = alg_arg;
4759 size_t bytes1 = bytes1_arg;
4760 size_t bytes2 = bytes2_arg;
4761 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004762 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004763 uint8_t *output_buffer = NULL;
4764 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004765 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4766 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004767 size_t length;
4768
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004769 ASSERT_ALLOC( output_buffer, capacity );
4770 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004771 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004772
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004773 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4774 psa_set_key_algorithm( &base_attributes, alg );
4775 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004776 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004777 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004778
4779 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004780 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4781 input1->x, input1->len,
4782 input2->x, input2->len,
4783 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004784 goto exit;
4785
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004786 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004787 output_buffer,
4788 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004789 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004790
4791 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004792 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4793 input1->x, input1->len,
4794 input2->x, input2->len,
4795 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004796 goto exit;
4797
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004798 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4799 psa_set_key_algorithm( &derived_attributes, 0 );
4800 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004801 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004802 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004803 &derived_key ) );
4804 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004805 export_buffer, bytes1,
4806 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004807 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004808 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004809 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004810 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004811 &derived_key ) );
4812 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004813 export_buffer + bytes1, bytes2,
4814 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004815 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004816
4817 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004818 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4819 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004820
4821exit:
4822 mbedtls_free( output_buffer );
4823 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004824 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004825 psa_destroy_key( base_key );
4826 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004827 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004828}
4829/* END_CASE */
4830
4831/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004832void derive_key( int alg_arg,
4833 data_t *key_data, data_t *input1, data_t *input2,
4834 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004835 int expected_status_arg,
4836 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004837{
Ronald Cron5425a212020-08-04 14:58:35 +02004838 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4839 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004840 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004841 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004842 size_t bits = bits_arg;
4843 psa_status_t expected_status = expected_status_arg;
4844 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4845 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4846 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4847
4848 PSA_ASSERT( psa_crypto_init( ) );
4849
4850 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4851 psa_set_key_algorithm( &base_attributes, alg );
4852 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4853 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004854 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004855
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004856 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4857 input1->x, input1->len,
4858 input2->x, input2->len,
4859 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004860 goto exit;
4861
4862 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4863 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004864 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004865 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004866
4867 psa_status_t status =
4868 psa_key_derivation_output_key( &derived_attributes,
4869 &operation,
4870 &derived_key );
4871 if( is_large_output > 0 )
4872 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4873 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004874
4875exit:
4876 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004877 psa_destroy_key( base_key );
4878 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004879 PSA_DONE( );
4880}
4881/* END_CASE */
4882
4883/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004884void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004885 int our_key_type_arg, int our_key_alg_arg,
4886 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004887 int expected_status_arg )
4888{
Ronald Cron5425a212020-08-04 14:58:35 +02004889 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004890 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004891 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004892 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004893 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004894 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004895 psa_status_t expected_status = expected_status_arg;
4896 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004897
Gilles Peskine8817f612018-12-18 00:18:46 +01004898 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004899
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004900 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004901 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004902 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004903 PSA_ASSERT( psa_import_key( &attributes,
4904 our_key_data->x, our_key_data->len,
4905 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004906
Gilles Peskine77f40d82019-04-11 21:27:06 +02004907 /* The tests currently include inputs that should fail at either step.
4908 * Test cases that fail at the setup step should be changed to call
4909 * key_derivation_setup instead, and this function should be renamed
4910 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004911 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004912 if( status == PSA_SUCCESS )
4913 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004914 TEST_EQUAL( psa_key_derivation_key_agreement(
4915 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4916 our_key,
4917 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004918 expected_status );
4919 }
4920 else
4921 {
4922 TEST_ASSERT( status == expected_status );
4923 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004924
4925exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004926 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004927 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004928 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004929}
4930/* END_CASE */
4931
4932/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004933void raw_key_agreement( int alg_arg,
4934 int our_key_type_arg, data_t *our_key_data,
4935 data_t *peer_key_data,
4936 data_t *expected_output )
4937{
Ronald Cron5425a212020-08-04 14:58:35 +02004938 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004939 psa_algorithm_t alg = alg_arg;
4940 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004941 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004942 unsigned char *output = NULL;
4943 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004944 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004945
4946 ASSERT_ALLOC( output, expected_output->len );
4947 PSA_ASSERT( psa_crypto_init( ) );
4948
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004949 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4950 psa_set_key_algorithm( &attributes, alg );
4951 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004952 PSA_ASSERT( psa_import_key( &attributes,
4953 our_key_data->x, our_key_data->len,
4954 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004955
gabor-mezei-armceface22021-01-21 12:26:17 +01004956 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4957 key_bits = psa_get_key_bits( &attributes );
4958
Gilles Peskinebe697d82019-05-16 18:00:41 +02004959 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4960 peer_key_data->x, peer_key_data->len,
4961 output, expected_output->len,
4962 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004963 ASSERT_COMPARE( output, output_length,
4964 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01004965 TEST_ASSERT( output_length <=
4966 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
4967 TEST_ASSERT( output_length <=
4968 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004969
4970exit:
4971 mbedtls_free( output );
4972 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004973 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004974}
4975/* END_CASE */
4976
4977/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004978void key_agreement_capacity( int alg_arg,
4979 int our_key_type_arg, data_t *our_key_data,
4980 data_t *peer_key_data,
4981 int expected_capacity_arg )
4982{
Ronald Cron5425a212020-08-04 14:58:35 +02004983 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004984 psa_algorithm_t alg = alg_arg;
4985 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004986 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004987 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004988 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004989 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004990
Gilles Peskine8817f612018-12-18 00:18:46 +01004991 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004992
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004993 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4994 psa_set_key_algorithm( &attributes, alg );
4995 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004996 PSA_ASSERT( psa_import_key( &attributes,
4997 our_key_data->x, our_key_data->len,
4998 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004999
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005000 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005001 PSA_ASSERT( psa_key_derivation_key_agreement(
5002 &operation,
5003 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5004 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005005 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5006 {
5007 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005008 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005009 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005010 NULL, 0 ) );
5011 }
Gilles Peskine59685592018-09-18 12:11:34 +02005012
Gilles Peskinebf491972018-10-25 22:36:12 +02005013 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005014 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005015 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005016 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005017
Gilles Peskinebf491972018-10-25 22:36:12 +02005018 /* Test the actual capacity by reading the output. */
5019 while( actual_capacity > sizeof( output ) )
5020 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005021 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005022 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005023 actual_capacity -= sizeof( output );
5024 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005025 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005026 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005027 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005028 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005029
Gilles Peskine59685592018-09-18 12:11:34 +02005030exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005031 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005032 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005033 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005034}
5035/* END_CASE */
5036
5037/* BEGIN_CASE */
5038void key_agreement_output( int alg_arg,
5039 int our_key_type_arg, data_t *our_key_data,
5040 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005041 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005042{
Ronald Cron5425a212020-08-04 14:58:35 +02005043 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005044 psa_algorithm_t alg = alg_arg;
5045 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005046 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005047 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005048 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005049
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005050 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5051 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005052
Gilles Peskine8817f612018-12-18 00:18:46 +01005053 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005054
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005055 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5056 psa_set_key_algorithm( &attributes, alg );
5057 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005058 PSA_ASSERT( psa_import_key( &attributes,
5059 our_key_data->x, our_key_data->len,
5060 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005061
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005062 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005063 PSA_ASSERT( psa_key_derivation_key_agreement(
5064 &operation,
5065 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5066 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005067 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5068 {
5069 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005070 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005071 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005072 NULL, 0 ) );
5073 }
Gilles Peskine59685592018-09-18 12:11:34 +02005074
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005075 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005076 actual_output,
5077 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005078 ASSERT_COMPARE( actual_output, expected_output1->len,
5079 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005080 if( expected_output2->len != 0 )
5081 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005082 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005083 actual_output,
5084 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005085 ASSERT_COMPARE( actual_output, expected_output2->len,
5086 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005087 }
Gilles Peskine59685592018-09-18 12:11:34 +02005088
5089exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005090 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005091 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005092 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005093 mbedtls_free( actual_output );
5094}
5095/* END_CASE */
5096
5097/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005098void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005099{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005100 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005101 unsigned char *output = NULL;
5102 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005103 size_t i;
5104 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005105
Simon Butcher49f8e312020-03-03 15:51:50 +00005106 TEST_ASSERT( bytes_arg >= 0 );
5107
Gilles Peskine91892022021-02-08 19:50:26 +01005108 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005109 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005110
Gilles Peskine8817f612018-12-18 00:18:46 +01005111 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005112
Gilles Peskinea50d7392018-06-21 10:22:13 +02005113 /* Run several times, to ensure that every output byte will be
5114 * nonzero at least once with overwhelming probability
5115 * (2^(-8*number_of_runs)). */
5116 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005117 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005118 if( bytes != 0 )
5119 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005120 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005121
Gilles Peskinea50d7392018-06-21 10:22:13 +02005122 for( i = 0; i < bytes; i++ )
5123 {
5124 if( output[i] != 0 )
5125 ++changed[i];
5126 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005127 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005128
5129 /* Check that every byte was changed to nonzero at least once. This
5130 * validates that psa_generate_random is overwriting every byte of
5131 * the output buffer. */
5132 for( i = 0; i < bytes; i++ )
5133 {
5134 TEST_ASSERT( changed[i] != 0 );
5135 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005136
5137exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005138 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005139 mbedtls_free( output );
5140 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005141}
5142/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005143
5144/* BEGIN_CASE */
5145void generate_key( int type_arg,
5146 int bits_arg,
5147 int usage_arg,
5148 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005149 int expected_status_arg,
5150 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005151{
Ronald Cron5425a212020-08-04 14:58:35 +02005152 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005153 psa_key_type_t type = type_arg;
5154 psa_key_usage_t usage = usage_arg;
5155 size_t bits = bits_arg;
5156 psa_algorithm_t alg = alg_arg;
5157 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005158 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005159 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005160
Gilles Peskine8817f612018-12-18 00:18:46 +01005161 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005162
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005163 psa_set_key_usage_flags( &attributes, usage );
5164 psa_set_key_algorithm( &attributes, alg );
5165 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005166 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005167
5168 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005169 psa_status_t status = psa_generate_key( &attributes, &key );
5170
5171 if( is_large_key > 0 )
5172 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5173 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005174 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005175 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005176
5177 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005178 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005179 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5180 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005181
Gilles Peskine818ca122018-06-20 18:16:48 +02005182 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005183 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005184 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005185
5186exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005187 /*
5188 * Key attributes may have been returned by psa_get_key_attributes()
5189 * thus reset them as required.
5190 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005191 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005192
Ronald Cron5425a212020-08-04 14:58:35 +02005193 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005194 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005195}
5196/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005197
Ronald Cronee414c72021-03-18 18:50:08 +01005198/* 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 +02005199void generate_key_rsa( int bits_arg,
5200 data_t *e_arg,
5201 int expected_status_arg )
5202{
Ronald Cron5425a212020-08-04 14:58:35 +02005203 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005204 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005205 size_t bits = bits_arg;
5206 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5207 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5208 psa_status_t expected_status = expected_status_arg;
5209 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5210 uint8_t *exported = NULL;
5211 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005212 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005213 size_t exported_length = SIZE_MAX;
5214 uint8_t *e_read_buffer = NULL;
5215 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005216 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005217 size_t e_read_length = SIZE_MAX;
5218
5219 if( e_arg->len == 0 ||
5220 ( e_arg->len == 3 &&
5221 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5222 {
5223 is_default_public_exponent = 1;
5224 e_read_size = 0;
5225 }
5226 ASSERT_ALLOC( e_read_buffer, e_read_size );
5227 ASSERT_ALLOC( exported, exported_size );
5228
5229 PSA_ASSERT( psa_crypto_init( ) );
5230
5231 psa_set_key_usage_flags( &attributes, usage );
5232 psa_set_key_algorithm( &attributes, alg );
5233 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5234 e_arg->x, e_arg->len ) );
5235 psa_set_key_bits( &attributes, bits );
5236
5237 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005238 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005239 if( expected_status != PSA_SUCCESS )
5240 goto exit;
5241
5242 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005243 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005244 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5245 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5246 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5247 e_read_buffer, e_read_size,
5248 &e_read_length ) );
5249 if( is_default_public_exponent )
5250 TEST_EQUAL( e_read_length, 0 );
5251 else
5252 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5253
5254 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005255 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005256 goto exit;
5257
5258 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005259 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005260 exported, exported_size,
5261 &exported_length ) );
5262 {
5263 uint8_t *p = exported;
5264 uint8_t *end = exported + exported_length;
5265 size_t len;
5266 /* RSAPublicKey ::= SEQUENCE {
5267 * modulus INTEGER, -- n
5268 * publicExponent INTEGER } -- e
5269 */
5270 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005271 MBEDTLS_ASN1_SEQUENCE |
5272 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005273 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005274 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5275 MBEDTLS_ASN1_INTEGER ) );
5276 if( len >= 1 && p[0] == 0 )
5277 {
5278 ++p;
5279 --len;
5280 }
5281 if( e_arg->len == 0 )
5282 {
5283 TEST_EQUAL( len, 3 );
5284 TEST_EQUAL( p[0], 1 );
5285 TEST_EQUAL( p[1], 0 );
5286 TEST_EQUAL( p[2], 1 );
5287 }
5288 else
5289 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5290 }
5291
5292exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005293 /*
5294 * Key attributes may have been returned by psa_get_key_attributes() or
5295 * set by psa_set_key_domain_parameters() thus reset them as required.
5296 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005297 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005298
Ronald Cron5425a212020-08-04 14:58:35 +02005299 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005300 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005301 mbedtls_free( e_read_buffer );
5302 mbedtls_free( exported );
5303}
5304/* END_CASE */
5305
Darryl Greend49a4992018-06-18 17:27:26 +01005306/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005307void persistent_key_load_key_from_storage( data_t *data,
5308 int type_arg, int bits_arg,
5309 int usage_flags_arg, int alg_arg,
5310 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005311{
Ronald Cron71016a92020-08-28 19:01:50 +02005312 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005313 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005314 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5315 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005316 psa_key_type_t type = type_arg;
5317 size_t bits = bits_arg;
5318 psa_key_usage_t usage_flags = usage_flags_arg;
5319 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005320 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005321 unsigned char *first_export = NULL;
5322 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005323 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005324 size_t first_exported_length;
5325 size_t second_exported_length;
5326
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005327 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5328 {
5329 ASSERT_ALLOC( first_export, export_size );
5330 ASSERT_ALLOC( second_export, export_size );
5331 }
Darryl Greend49a4992018-06-18 17:27:26 +01005332
Gilles Peskine8817f612018-12-18 00:18:46 +01005333 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005334
Gilles Peskinec87af662019-05-15 16:12:22 +02005335 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005336 psa_set_key_usage_flags( &attributes, usage_flags );
5337 psa_set_key_algorithm( &attributes, alg );
5338 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005339 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005340
Darryl Green0c6575a2018-11-07 16:05:30 +00005341 switch( generation_method )
5342 {
5343 case IMPORT_KEY:
5344 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005345 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005346 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005347 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005348
Darryl Green0c6575a2018-11-07 16:05:30 +00005349 case GENERATE_KEY:
5350 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005351 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005352 break;
5353
5354 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005355#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005356 {
5357 /* Create base key */
5358 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5359 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5360 psa_set_key_usage_flags( &base_attributes,
5361 PSA_KEY_USAGE_DERIVE );
5362 psa_set_key_algorithm( &base_attributes, derive_alg );
5363 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005364 PSA_ASSERT( psa_import_key( &base_attributes,
5365 data->x, data->len,
5366 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005367 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005368 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005369 PSA_ASSERT( psa_key_derivation_input_key(
5370 &operation,
5371 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005372 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005373 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005374 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005375 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5376 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005377 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005378 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005379 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005380 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005381 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005382#else
5383 TEST_ASSUME( ! "KDF not supported in this configuration" );
5384#endif
5385 break;
5386
5387 default:
5388 TEST_ASSERT( ! "generation_method not implemented in test" );
5389 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005390 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005391 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005392
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005393 /* Export the key if permitted by the key policy. */
5394 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5395 {
Ronald Cron5425a212020-08-04 14:58:35 +02005396 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005397 first_export, export_size,
5398 &first_exported_length ) );
5399 if( generation_method == IMPORT_KEY )
5400 ASSERT_COMPARE( data->x, data->len,
5401 first_export, first_exported_length );
5402 }
Darryl Greend49a4992018-06-18 17:27:26 +01005403
5404 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005405 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005406 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005407 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005408
Darryl Greend49a4992018-06-18 17:27:26 +01005409 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005410 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005411 TEST_ASSERT( mbedtls_svc_key_id_equal(
5412 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005413 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5414 PSA_KEY_LIFETIME_PERSISTENT );
5415 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5416 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02005417 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02005418 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005419 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005420
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005421 /* Export the key again if permitted by the key policy. */
5422 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005423 {
Ronald Cron5425a212020-08-04 14:58:35 +02005424 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005425 second_export, export_size,
5426 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005427 ASSERT_COMPARE( first_export, first_exported_length,
5428 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005429 }
5430
5431 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005432 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005433 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005434
5435exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005436 /*
5437 * Key attributes may have been returned by psa_get_key_attributes()
5438 * thus reset them as required.
5439 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005440 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005441
Darryl Greend49a4992018-06-18 17:27:26 +01005442 mbedtls_free( first_export );
5443 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005444 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005445 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005446 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005447 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005448}
5449/* END_CASE */