blob: e42015833f1ff39d7533b3a067d69cbd12a7bf7f [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010018
Jaeden Amerof24c7f82018-06-27 17:20:43 +010019/** An invalid export length that will never be set by psa_export_key(). */
20static const size_t INVALID_EXPORT_LENGTH = ~0U;
21
Gilles Peskinea7aa4422018-08-14 15:17:54 +020022/** Test if a buffer contains a constant byte value.
23 *
24 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020025 *
26 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020027 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020028 * \param size Size of the buffer in bytes.
29 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020030 * \return 1 if the buffer is all-bits-zero.
31 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020032 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020033static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020034{
35 size_t i;
36 for( i = 0; i < size; i++ )
37 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020038 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020039 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020040 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020041 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042}
Gilles Peskine818ca122018-06-20 18:16:48 +020043
Gilles Peskine0b352bc2018-06-28 00:16:11 +020044/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
45static int asn1_write_10x( unsigned char **p,
46 unsigned char *start,
47 size_t bits,
48 unsigned char x )
49{
50 int ret;
51 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020052 if( bits == 0 )
53 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
54 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020055 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030056 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020057 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
58 *p -= len;
59 ( *p )[len-1] = x;
60 if( bits % 8 == 0 )
61 ( *p )[1] |= 1;
62 else
63 ( *p )[0] |= 1 << ( bits % 8 );
64 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
65 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
66 MBEDTLS_ASN1_INTEGER ) );
67 return( len );
68}
69
70static int construct_fake_rsa_key( unsigned char *buffer,
71 size_t buffer_size,
72 unsigned char **p,
73 size_t bits,
74 int keypair )
75{
76 size_t half_bits = ( bits + 1 ) / 2;
77 int ret;
78 int len = 0;
79 /* Construct something that looks like a DER encoding of
80 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
81 * RSAPrivateKey ::= SEQUENCE {
82 * version Version,
83 * modulus INTEGER, -- n
84 * publicExponent INTEGER, -- e
85 * privateExponent INTEGER, -- d
86 * prime1 INTEGER, -- p
87 * prime2 INTEGER, -- q
88 * exponent1 INTEGER, -- d mod (p-1)
89 * exponent2 INTEGER, -- d mod (q-1)
90 * coefficient INTEGER, -- (inverse of q) mod p
91 * otherPrimeInfos OtherPrimeInfos OPTIONAL
92 * }
93 * Or, for a public key, the same structure with only
94 * version, modulus and publicExponent.
95 */
96 *p = buffer + buffer_size;
97 if( keypair )
98 {
99 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
100 asn1_write_10x( p, buffer, half_bits, 1 ) );
101 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
102 asn1_write_10x( p, buffer, half_bits, 1 ) );
103 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
104 asn1_write_10x( p, buffer, half_bits, 1 ) );
105 MBEDTLS_ASN1_CHK_ADD( len, /* q */
106 asn1_write_10x( p, buffer, half_bits, 1 ) );
107 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
108 asn1_write_10x( p, buffer, half_bits, 3 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* d */
110 asn1_write_10x( p, buffer, bits, 1 ) );
111 }
112 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
113 asn1_write_10x( p, buffer, 17, 1 ) );
114 MBEDTLS_ASN1_CHK_ADD( len, /* n */
115 asn1_write_10x( p, buffer, bits, 1 ) );
116 if( keypair )
117 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
118 mbedtls_asn1_write_int( p, buffer, 0 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
120 {
121 const unsigned char tag =
122 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
123 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
124 }
125 return( len );
126}
127
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100128int exercise_mac_setup( psa_key_type_t key_type,
129 const unsigned char *key_bytes,
130 size_t key_length,
131 psa_algorithm_t alg,
132 psa_mac_operation_t *operation,
133 psa_status_t *status )
134{
Ronald Cron5425a212020-08-04 14:58:35 +0200135 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200136 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100137
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100138 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200139 psa_set_key_algorithm( &attributes, alg );
140 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200141 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100142
Ronald Cron5425a212020-08-04 14:58:35 +0200143 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100144 /* Whether setup succeeded or failed, abort must succeed. */
145 PSA_ASSERT( psa_mac_abort( operation ) );
146 /* If setup failed, reproduce the failure, so that the caller can
147 * test the resulting state of the operation object. */
148 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100149 {
Ronald Cron5425a212020-08-04 14:58:35 +0200150 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100151 }
152
Ronald Cron5425a212020-08-04 14:58:35 +0200153 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100154 return( 1 );
155
156exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200157 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100158 return( 0 );
159}
160
161int exercise_cipher_setup( psa_key_type_t key_type,
162 const unsigned char *key_bytes,
163 size_t key_length,
164 psa_algorithm_t alg,
165 psa_cipher_operation_t *operation,
166 psa_status_t *status )
167{
Ronald Cron5425a212020-08-04 14:58:35 +0200168 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200169 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100170
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200171 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
172 psa_set_key_algorithm( &attributes, alg );
173 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200174 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100175
Ronald Cron5425a212020-08-04 14:58:35 +0200176 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100177 /* Whether setup succeeded or failed, abort must succeed. */
178 PSA_ASSERT( psa_cipher_abort( operation ) );
179 /* If setup failed, reproduce the failure, so that the caller can
180 * test the resulting state of the operation object. */
181 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100182 {
Ronald Cron5425a212020-08-04 14:58:35 +0200183 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100184 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185 }
186
Ronald Cron5425a212020-08-04 14:58:35 +0200187 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100188 return( 1 );
189
190exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200191 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100192 return( 0 );
193}
194
Ronald Cron5425a212020-08-04 14:58:35 +0200195static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200196{
197 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200198 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200199 uint8_t buffer[1];
200 size_t length;
201 int ok = 0;
202
Ronald Cronecfb2372020-07-23 17:13:42 +0200203 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200204 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
205 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
206 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200207 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000208 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200209 TEST_EQUAL(
210 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
211 TEST_EQUAL(
212 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200213 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200214 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
215 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
216 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
217 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
218
Ronald Cron5425a212020-08-04 14:58:35 +0200219 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000220 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200221 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200222 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000223 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200224
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200225 ok = 1;
226
227exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100228 /*
229 * Key attributes may have been returned by psa_get_key_attributes()
230 * thus reset them as required.
231 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200232 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100233
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200234 return( ok );
235}
236
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200237/* Assert that a key isn't reported as having a slot number. */
238#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
239#define ASSERT_NO_SLOT_NUMBER( attributes ) \
240 do \
241 { \
242 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
243 TEST_EQUAL( psa_get_key_slot_number( \
244 attributes, \
245 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
246 PSA_ERROR_INVALID_ARGUMENT ); \
247 } \
248 while( 0 )
249#else /* MBEDTLS_PSA_CRYPTO_SE_C */
250#define ASSERT_NO_SLOT_NUMBER( attributes ) \
251 ( (void) 0 )
252#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
253
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100254/* An overapproximation of the amount of storage needed for a key of the
255 * given type and with the given content. The API doesn't make it easy
256 * to find a good value for the size. The current implementation doesn't
257 * care about the value anyway. */
258#define KEY_BITS_FROM_DATA( type, data ) \
259 ( data )->len
260
Darryl Green0c6575a2018-11-07 16:05:30 +0000261typedef enum {
262 IMPORT_KEY = 0,
263 GENERATE_KEY = 1,
264 DERIVE_KEY = 2
265} generate_method;
266
Gilles Peskinee59236f2018-01-27 23:32:46 +0100267/* END_HEADER */
268
269/* BEGIN_DEPENDENCIES
270 * depends_on:MBEDTLS_PSA_CRYPTO_C
271 * END_DEPENDENCIES
272 */
273
274/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200275void static_checks( )
276{
277 size_t max_truncated_mac_size =
278 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
279
280 /* Check that the length for a truncated MAC always fits in the algorithm
281 * encoding. The shifted mask is the maximum truncated value. The
282 * untruncated algorithm may be one byte larger. */
283 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
284}
285/* END_CASE */
286
287/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200288void import_with_policy( int type_arg,
289 int usage_arg, int alg_arg,
290 int expected_status_arg )
291{
292 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
293 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200294 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200295 psa_key_type_t type = type_arg;
296 psa_key_usage_t usage = usage_arg;
297 psa_algorithm_t alg = alg_arg;
298 psa_status_t expected_status = expected_status_arg;
299 const uint8_t key_material[16] = {0};
300 psa_status_t status;
301
302 PSA_ASSERT( psa_crypto_init( ) );
303
304 psa_set_key_type( &attributes, type );
305 psa_set_key_usage_flags( &attributes, usage );
306 psa_set_key_algorithm( &attributes, alg );
307
308 status = psa_import_key( &attributes,
309 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200310 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200311 TEST_EQUAL( status, expected_status );
312 if( status != PSA_SUCCESS )
313 goto exit;
314
Ronald Cron5425a212020-08-04 14:58:35 +0200315 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200316 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
317 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
318 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200319 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200320
Ronald Cron5425a212020-08-04 14:58:35 +0200321 PSA_ASSERT( psa_destroy_key( key ) );
322 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200323
324exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100325 /*
326 * Key attributes may have been returned by psa_get_key_attributes()
327 * thus reset them as required.
328 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200329 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100330
331 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200332 PSA_DONE( );
333}
334/* END_CASE */
335
336/* BEGIN_CASE */
337void import_with_data( data_t *data, int type_arg,
338 int attr_bits_arg,
339 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200340{
341 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
342 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200343 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200344 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200345 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200346 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100347 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100348
Gilles Peskine8817f612018-12-18 00:18:46 +0100349 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100350
Gilles Peskine4747d192019-04-17 15:05:45 +0200351 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200352 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200353
Ronald Cron5425a212020-08-04 14:58:35 +0200354 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100355 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200356 if( status != PSA_SUCCESS )
357 goto exit;
358
Ronald Cron5425a212020-08-04 14:58:35 +0200359 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200360 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200361 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200362 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200363 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200364
Ronald Cron5425a212020-08-04 14:58:35 +0200365 PSA_ASSERT( psa_destroy_key( key ) );
366 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100367
368exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100369 /*
370 * Key attributes may have been returned by psa_get_key_attributes()
371 * thus reset them as required.
372 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200373 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100374
375 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200376 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100377}
378/* END_CASE */
379
380/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200381void import_large_key( int type_arg, int byte_size_arg,
382 int expected_status_arg )
383{
384 psa_key_type_t type = type_arg;
385 size_t byte_size = byte_size_arg;
386 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
387 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200388 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200389 psa_status_t status;
390 uint8_t *buffer = NULL;
391 size_t buffer_size = byte_size + 1;
392 size_t n;
393
Steven Cooreman69967ce2021-01-18 18:01:08 +0100394 /* Skip the test case if the target running the test cannot
395 * accomodate large keys due to heap size constraints */
396 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200397 memset( buffer, 'K', byte_size );
398
399 PSA_ASSERT( psa_crypto_init( ) );
400
401 /* Try importing the key */
402 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
403 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200404 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100405 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200406 TEST_EQUAL( status, expected_status );
407
408 if( status == PSA_SUCCESS )
409 {
Ronald Cron5425a212020-08-04 14:58:35 +0200410 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200411 TEST_EQUAL( psa_get_key_type( &attributes ), type );
412 TEST_EQUAL( psa_get_key_bits( &attributes ),
413 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200414 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200415 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200416 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200417 for( n = 0; n < byte_size; n++ )
418 TEST_EQUAL( buffer[n], 'K' );
419 for( n = byte_size; n < buffer_size; n++ )
420 TEST_EQUAL( buffer[n], 0 );
421 }
422
423exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100424 /*
425 * Key attributes may have been returned by psa_get_key_attributes()
426 * thus reset them as required.
427 */
428 psa_reset_key_attributes( &attributes );
429
Ronald Cron5425a212020-08-04 14:58:35 +0200430 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200431 PSA_DONE( );
432 mbedtls_free( buffer );
433}
434/* END_CASE */
435
436/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200437void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
438{
Ronald Cron5425a212020-08-04 14:58:35 +0200439 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200440 size_t bits = bits_arg;
441 psa_status_t expected_status = expected_status_arg;
442 psa_status_t status;
443 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200444 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200445 size_t buffer_size = /* Slight overapproximations */
446 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200447 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200448 unsigned char *p;
449 int ret;
450 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200451 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200452
Gilles Peskine8817f612018-12-18 00:18:46 +0100453 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200454 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200455
456 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
457 bits, keypair ) ) >= 0 );
458 length = ret;
459
460 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200461 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200462 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100463 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200464
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200465 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200466 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200467
468exit:
469 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200470 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200471}
472/* END_CASE */
473
474/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300475void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300476 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200477 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100478 int expected_bits,
479 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200480 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100481 int canonical_input )
482{
Ronald Cron5425a212020-08-04 14:58:35 +0200483 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100484 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200485 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200486 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100487 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100488 unsigned char *exported = NULL;
489 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100490 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100491 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100492 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200493 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200494 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100495
Moran Pekercb088e72018-07-17 17:36:59 +0300496 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200497 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100498 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200499 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100500 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100501
Gilles Peskine4747d192019-04-17 15:05:45 +0200502 psa_set_key_usage_flags( &attributes, usage_arg );
503 psa_set_key_algorithm( &attributes, alg );
504 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700505
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100506 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200507 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100508
509 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200510 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200511 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
512 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200513 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100514
515 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200516 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100517 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100518
519 /* The exported length must be set by psa_export_key() to a value between 0
520 * and export_size. On errors, the exported length must be 0. */
521 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
522 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
523 TEST_ASSERT( exported_length <= export_size );
524
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200525 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200526 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100527 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200528 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100529 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100530 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200531 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100532
Gilles Peskineea38a922021-02-13 00:05:16 +0100533 /* Run sanity checks on the exported key. For non-canonical inputs,
534 * this validates the canonical representations. For canonical inputs,
535 * this doesn't directly validate the implementation, but it still helps
536 * by cross-validating the test data with the sanity check code. */
537 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200538 goto exit;
539
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100540 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200541 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100542 else
543 {
Ronald Cron5425a212020-08-04 14:58:35 +0200544 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200545 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200546 &key2 ) );
547 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100548 reexported,
549 export_size,
550 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200551 ASSERT_COMPARE( exported, exported_length,
552 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200553 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100554 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100555 TEST_ASSERT( exported_length <=
556 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
557 psa_get_key_bits( &got_attributes ) ) );
558 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100559
560destroy:
561 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200562 PSA_ASSERT( psa_destroy_key( key ) );
563 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100564
565exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100566 /*
567 * Key attributes may have been returned by psa_get_key_attributes()
568 * thus reset them as required.
569 */
570 psa_reset_key_attributes( &got_attributes );
571
itayzafrir3e02b3b2018-06-12 17:06:52 +0300572 mbedtls_free( exported );
573 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200574 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100575}
576/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100577
Moran Pekerf709f4a2018-06-06 17:26:04 +0300578/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300579void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200580 int type_arg,
581 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100582 int export_size_delta,
583 int expected_export_status_arg,
584 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300585{
Ronald Cron5425a212020-08-04 14:58:35 +0200586 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300587 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200588 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200589 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300590 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300591 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100592 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100593 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200594 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300595
Gilles Peskine8817f612018-12-18 00:18:46 +0100596 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300597
Gilles Peskine4747d192019-04-17 15:05:45 +0200598 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
599 psa_set_key_algorithm( &attributes, alg );
600 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300601
602 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200603 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300604
Gilles Peskine49c25912018-10-29 15:15:31 +0100605 /* Export the public key */
606 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200607 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200608 exported, export_size,
609 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100610 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100611 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100612 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200613 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100614 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200615 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200616 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100617 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100618 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100619 TEST_ASSERT( expected_public_key->len <=
620 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
621 TEST_ASSERT( expected_public_key->len <=
622 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100623 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
624 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100625 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300626
627exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100628 /*
629 * Key attributes may have been returned by psa_get_key_attributes()
630 * thus reset them as required.
631 */
632 psa_reset_key_attributes( &attributes );
633
itayzafrir3e02b3b2018-06-12 17:06:52 +0300634 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200635 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200636 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300637}
638/* END_CASE */
639
Gilles Peskine20035e32018-02-03 22:44:14 +0100640/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200641void import_and_exercise_key( data_t *data,
642 int type_arg,
643 int bits_arg,
644 int alg_arg )
645{
Ronald Cron5425a212020-08-04 14:58:35 +0200646 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200647 psa_key_type_t type = type_arg;
648 size_t bits = bits_arg;
649 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100650 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200651 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200652 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200653
Gilles Peskine8817f612018-12-18 00:18:46 +0100654 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200655
Gilles Peskine4747d192019-04-17 15:05:45 +0200656 psa_set_key_usage_flags( &attributes, usage );
657 psa_set_key_algorithm( &attributes, alg );
658 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200659
660 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200661 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200662
663 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200664 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200665 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
666 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200667
668 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100669 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200670 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200671
Ronald Cron5425a212020-08-04 14:58:35 +0200672 PSA_ASSERT( psa_destroy_key( key ) );
673 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200674
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200675exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100676 /*
677 * Key attributes may have been returned by psa_get_key_attributes()
678 * thus reset them as required.
679 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200680 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100681
682 psa_reset_key_attributes( &attributes );
683 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200684 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200685}
686/* END_CASE */
687
688/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100689void effective_key_attributes( int type_arg, int expected_type_arg,
690 int bits_arg, int expected_bits_arg,
691 int usage_arg, int expected_usage_arg,
692 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200693{
Ronald Cron5425a212020-08-04 14:58:35 +0200694 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100695 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100696 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100697 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100698 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200699 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100700 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200701 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100702 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200703 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200704
Gilles Peskine8817f612018-12-18 00:18:46 +0100705 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200706
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200707 psa_set_key_usage_flags( &attributes, usage );
708 psa_set_key_algorithm( &attributes, alg );
709 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +0100710 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +0200711
Ronald Cron5425a212020-08-04 14:58:35 +0200712 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +0100713 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +0200714
Ronald Cron5425a212020-08-04 14:58:35 +0200715 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +0100716 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
717 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
718 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
719 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200720
721exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100722 /*
723 * Key attributes may have been returned by psa_get_key_attributes()
724 * thus reset them as required.
725 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200726 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100727
728 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200729 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +0200730}
731/* END_CASE */
732
733/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100734void check_key_policy( int type_arg, int bits_arg,
735 int usage_arg, int alg_arg )
736{
737 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
738 usage_arg, usage_arg, alg_arg, alg_arg );
739 goto exit;
740}
741/* END_CASE */
742
743/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200744void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +0000745{
746 /* Test each valid way of initializing the object, except for `= {0}`, as
747 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
748 * though it's OK by the C standard. We could test for this, but we'd need
749 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200750 psa_key_attributes_t func = psa_key_attributes_init( );
751 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
752 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000753
754 memset( &zero, 0, sizeof( zero ) );
755
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200756 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
757 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
758 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000759
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200760 TEST_EQUAL( psa_get_key_type( &func ), 0 );
761 TEST_EQUAL( psa_get_key_type( &init ), 0 );
762 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
763
764 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
765 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
766 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
767
768 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
769 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
770 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
771
772 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
773 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
774 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +0000775}
776/* END_CASE */
777
778/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200779void mac_key_policy( int policy_usage,
780 int policy_alg,
781 int key_type,
782 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100783 int exercise_alg,
784 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200785{
Ronald Cron5425a212020-08-04 14:58:35 +0200786 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200787 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000788 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200789 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100790 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200791 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200792
Gilles Peskine8817f612018-12-18 00:18:46 +0100793 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200794
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200795 psa_set_key_usage_flags( &attributes, policy_usage );
796 psa_set_key_algorithm( &attributes, policy_alg );
797 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200798
Gilles Peskine049c7532019-05-15 20:22:09 +0200799 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200800 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200801
Ronald Cron5425a212020-08-04 14:58:35 +0200802 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100803 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100804 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100805 else
806 TEST_EQUAL( status, expected_status );
807
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200808 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200809
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200810 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200811 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100812 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100813 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100814 else
815 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200816
817exit:
818 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200819 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200820 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200821}
822/* END_CASE */
823
824/* BEGIN_CASE */
825void cipher_key_policy( int policy_usage,
826 int policy_alg,
827 int key_type,
828 data_t *key_data,
829 int exercise_alg )
830{
Ronald Cron5425a212020-08-04 14:58:35 +0200831 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200832 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000833 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200834 psa_status_t status;
835
Gilles Peskine8817f612018-12-18 00:18:46 +0100836 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200837
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200838 psa_set_key_usage_flags( &attributes, policy_usage );
839 psa_set_key_algorithm( &attributes, policy_alg );
840 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200841
Gilles Peskine049c7532019-05-15 20:22:09 +0200842 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200843 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200844
Ronald Cron5425a212020-08-04 14:58:35 +0200845 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200846 if( policy_alg == exercise_alg &&
847 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100848 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200849 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100850 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200851 psa_cipher_abort( &operation );
852
Ronald Cron5425a212020-08-04 14:58:35 +0200853 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200854 if( policy_alg == exercise_alg &&
855 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100856 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200857 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100858 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200859
860exit:
861 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200862 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200863 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200864}
865/* END_CASE */
866
867/* BEGIN_CASE */
868void aead_key_policy( int policy_usage,
869 int policy_alg,
870 int key_type,
871 data_t *key_data,
872 int nonce_length_arg,
873 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100874 int exercise_alg,
875 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200876{
Ronald Cron5425a212020-08-04 14:58:35 +0200877 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200878 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200879 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100880 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200881 unsigned char nonce[16] = {0};
882 size_t nonce_length = nonce_length_arg;
883 unsigned char tag[16];
884 size_t tag_length = tag_length_arg;
885 size_t output_length;
886
887 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
888 TEST_ASSERT( tag_length <= sizeof( tag ) );
889
Gilles Peskine8817f612018-12-18 00:18:46 +0100890 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200891
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200892 psa_set_key_usage_flags( &attributes, policy_usage );
893 psa_set_key_algorithm( &attributes, policy_alg );
894 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200895
Gilles Peskine049c7532019-05-15 20:22:09 +0200896 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200897 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200898
Ronald Cron5425a212020-08-04 14:58:35 +0200899 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200900 nonce, nonce_length,
901 NULL, 0,
902 NULL, 0,
903 tag, tag_length,
904 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100905 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
906 TEST_EQUAL( status, expected_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
910 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200911 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200912 nonce, nonce_length,
913 NULL, 0,
914 tag, tag_length,
915 NULL, 0,
916 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100917 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
918 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
919 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100920 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200921 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100922 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200923
924exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200925 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200926 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200927}
928/* END_CASE */
929
930/* BEGIN_CASE */
931void asymmetric_encryption_key_policy( int policy_usage,
932 int policy_alg,
933 int key_type,
934 data_t *key_data,
935 int exercise_alg )
936{
Ronald Cron5425a212020-08-04 14:58:35 +0200937 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200938 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200939 psa_status_t status;
940 size_t key_bits;
941 size_t buffer_length;
942 unsigned char *buffer = NULL;
943 size_t output_length;
944
Gilles Peskine8817f612018-12-18 00:18:46 +0100945 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200946
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200947 psa_set_key_usage_flags( &attributes, policy_usage );
948 psa_set_key_algorithm( &attributes, policy_alg );
949 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200950
Gilles Peskine049c7532019-05-15 20:22:09 +0200951 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200952 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200953
Ronald Cron5425a212020-08-04 14:58:35 +0200954 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200955 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200956 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
957 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200958 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200959
Ronald Cron5425a212020-08-04 14:58:35 +0200960 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200961 NULL, 0,
962 NULL, 0,
963 buffer, buffer_length,
964 &output_length );
965 if( policy_alg == exercise_alg &&
966 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100967 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200968 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100969 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200970
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +0200971 if( buffer_length != 0 )
972 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200973 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200974 buffer, buffer_length,
975 NULL, 0,
976 buffer, buffer_length,
977 &output_length );
978 if( policy_alg == exercise_alg &&
979 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100980 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200981 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100982 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200983
984exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100985 /*
986 * Key attributes may have been returned by psa_get_key_attributes()
987 * thus reset them as required.
988 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200989 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100990
991 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200992 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200993 mbedtls_free( buffer );
994}
995/* END_CASE */
996
997/* BEGIN_CASE */
998void asymmetric_signature_key_policy( int policy_usage,
999 int policy_alg,
1000 int key_type,
1001 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001002 int exercise_alg,
1003 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001004{
Ronald Cron5425a212020-08-04 14:58:35 +02001005 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001006 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001007 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001008 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1009 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1010 * compatible with the policy and `payload_length_arg` is supposed to be
1011 * a valid input length to sign. If `payload_length_arg <= 0`,
1012 * `exercise_alg` is supposed to be forbidden by the policy. */
1013 int compatible_alg = payload_length_arg > 0;
1014 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001015 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001016 size_t signature_length;
1017
Gilles Peskine8817f612018-12-18 00:18:46 +01001018 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001019
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001020 psa_set_key_usage_flags( &attributes, policy_usage );
1021 psa_set_key_algorithm( &attributes, policy_alg );
1022 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001023
Gilles Peskine049c7532019-05-15 20:22:09 +02001024 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001025 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001026
Ronald Cron5425a212020-08-04 14:58:35 +02001027 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001028 payload, payload_length,
1029 signature, sizeof( signature ),
1030 &signature_length );
1031 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001032 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001033 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001034 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001035
1036 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001037 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001038 payload, payload_length,
1039 signature, sizeof( signature ) );
1040 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001041 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001042 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001043 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001044
1045exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001046 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001047 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001048}
1049/* END_CASE */
1050
Janos Follathba3fab92019-06-11 14:50:16 +01001051/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001052void derive_key_policy( int policy_usage,
1053 int policy_alg,
1054 int key_type,
1055 data_t *key_data,
1056 int exercise_alg )
1057{
Ronald Cron5425a212020-08-04 14:58:35 +02001058 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001059 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001060 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001061 psa_status_t status;
1062
Gilles Peskine8817f612018-12-18 00:18:46 +01001063 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001064
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001065 psa_set_key_usage_flags( &attributes, policy_usage );
1066 psa_set_key_algorithm( &attributes, policy_alg );
1067 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001068
Gilles Peskine049c7532019-05-15 20:22:09 +02001069 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001070 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001071
Janos Follathba3fab92019-06-11 14:50:16 +01001072 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1073
1074 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1075 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001076 {
Janos Follathba3fab92019-06-11 14:50:16 +01001077 PSA_ASSERT( psa_key_derivation_input_bytes(
1078 &operation,
1079 PSA_KEY_DERIVATION_INPUT_SEED,
1080 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001081 }
Janos Follathba3fab92019-06-11 14:50:16 +01001082
1083 status = psa_key_derivation_input_key( &operation,
1084 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001085 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001086
Gilles Peskineea0fb492018-07-12 17:17:20 +02001087 if( policy_alg == exercise_alg &&
1088 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001089 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001090 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001091 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001092
1093exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001094 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001095 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001096 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001097}
1098/* END_CASE */
1099
1100/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001101void agreement_key_policy( int policy_usage,
1102 int policy_alg,
1103 int key_type_arg,
1104 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001105 int exercise_alg,
1106 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001107{
Ronald Cron5425a212020-08-04 14:58:35 +02001108 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001109 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001110 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001111 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001112 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001113 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001114
Gilles Peskine8817f612018-12-18 00:18:46 +01001115 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001116
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001117 psa_set_key_usage_flags( &attributes, policy_usage );
1118 psa_set_key_algorithm( &attributes, policy_alg );
1119 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001120
Gilles Peskine049c7532019-05-15 20:22:09 +02001121 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001122 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001123
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001124 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001125 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001126
Steven Cooremance48e852020-10-05 16:02:45 +02001127 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001128
1129exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001130 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001131 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001132 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001133}
1134/* END_CASE */
1135
1136/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001137void key_policy_alg2( int key_type_arg, data_t *key_data,
1138 int usage_arg, int alg_arg, int alg2_arg )
1139{
Ronald Cron5425a212020-08-04 14:58:35 +02001140 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001141 psa_key_type_t key_type = key_type_arg;
1142 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1143 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1144 psa_key_usage_t usage = usage_arg;
1145 psa_algorithm_t alg = alg_arg;
1146 psa_algorithm_t alg2 = alg2_arg;
1147
1148 PSA_ASSERT( psa_crypto_init( ) );
1149
1150 psa_set_key_usage_flags( &attributes, usage );
1151 psa_set_key_algorithm( &attributes, alg );
1152 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1153 psa_set_key_type( &attributes, key_type );
1154 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001155 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001156
Ronald Cron5425a212020-08-04 14:58:35 +02001157 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001158 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1159 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1160 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1161
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001162 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001163 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001164 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001165 goto exit;
1166
1167exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001168 /*
1169 * Key attributes may have been returned by psa_get_key_attributes()
1170 * thus reset them as required.
1171 */
1172 psa_reset_key_attributes( &got_attributes );
1173
Ronald Cron5425a212020-08-04 14:58:35 +02001174 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001175 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001176}
1177/* END_CASE */
1178
1179/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001180void raw_agreement_key_policy( int policy_usage,
1181 int policy_alg,
1182 int key_type_arg,
1183 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001184 int exercise_alg,
1185 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001186{
Ronald Cron5425a212020-08-04 14:58:35 +02001187 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001188 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001189 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001190 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001191 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001192 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001193
1194 PSA_ASSERT( psa_crypto_init( ) );
1195
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001196 psa_set_key_usage_flags( &attributes, policy_usage );
1197 psa_set_key_algorithm( &attributes, policy_alg );
1198 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001199
Gilles Peskine049c7532019-05-15 20:22:09 +02001200 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001201 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001202
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001203 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001204
Steven Cooremance48e852020-10-05 16:02:45 +02001205 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001206
1207exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001208 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001209 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001210 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001211}
1212/* END_CASE */
1213
1214/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001215void copy_success( int source_usage_arg,
1216 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001217 int type_arg, data_t *material,
1218 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001219 int target_usage_arg,
1220 int target_alg_arg, int target_alg2_arg,
1221 int expected_usage_arg,
1222 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001223{
Gilles Peskineca25db92019-04-19 11:43:08 +02001224 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1225 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001226 psa_key_usage_t expected_usage = expected_usage_arg;
1227 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001228 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001229 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1230 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001231 uint8_t *export_buffer = NULL;
1232
Gilles Peskine57ab7212019-01-28 13:03:09 +01001233 PSA_ASSERT( psa_crypto_init( ) );
1234
Gilles Peskineca25db92019-04-19 11:43:08 +02001235 /* Prepare the source key. */
1236 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1237 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001238 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001239 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001240 PSA_ASSERT( psa_import_key( &source_attributes,
1241 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001242 &source_key ) );
1243 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001244
Gilles Peskineca25db92019-04-19 11:43:08 +02001245 /* Prepare the target attributes. */
1246 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001247 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001248 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001249 /* Set volatile lifetime to reset the key identifier to 0. */
1250 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1251 }
1252
Gilles Peskineca25db92019-04-19 11:43:08 +02001253 if( target_usage_arg != -1 )
1254 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1255 if( target_alg_arg != -1 )
1256 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001257 if( target_alg2_arg != -1 )
1258 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001259
1260 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001261 PSA_ASSERT( psa_copy_key( source_key,
1262 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001263
1264 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001265 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001266
1267 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001268 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001269 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1270 psa_get_key_type( &target_attributes ) );
1271 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1272 psa_get_key_bits( &target_attributes ) );
1273 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1274 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001275 TEST_EQUAL( expected_alg2,
1276 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001277 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1278 {
1279 size_t length;
1280 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001281 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001282 material->len, &length ) );
1283 ASSERT_COMPARE( material->x, material->len,
1284 export_buffer, length );
1285 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001286
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001287 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001288 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001289 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001290 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001291
Ronald Cron5425a212020-08-04 14:58:35 +02001292 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001293
1294exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001295 /*
1296 * Source and target key attributes may have been returned by
1297 * psa_get_key_attributes() thus reset them as required.
1298 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001299 psa_reset_key_attributes( &source_attributes );
1300 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001301
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001302 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001303 mbedtls_free( export_buffer );
1304}
1305/* END_CASE */
1306
1307/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001308void copy_fail( int source_usage_arg,
1309 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001310 int type_arg, data_t *material,
1311 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001312 int target_usage_arg,
1313 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001314 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001315 int expected_status_arg )
1316{
1317 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1318 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001319 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1320 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001321 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001322
1323 PSA_ASSERT( psa_crypto_init( ) );
1324
1325 /* Prepare the source key. */
1326 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1327 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001328 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001329 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001330 PSA_ASSERT( psa_import_key( &source_attributes,
1331 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001332 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001333
1334 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001335 psa_set_key_id( &target_attributes, key_id );
1336 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001337 psa_set_key_type( &target_attributes, target_type_arg );
1338 psa_set_key_bits( &target_attributes, target_bits_arg );
1339 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1340 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001341 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001342
1343 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001344 TEST_EQUAL( psa_copy_key( source_key,
1345 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001346 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001347
Ronald Cron5425a212020-08-04 14:58:35 +02001348 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001349
Gilles Peskine4a644642019-05-03 17:14:08 +02001350exit:
1351 psa_reset_key_attributes( &source_attributes );
1352 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001353 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001354}
1355/* END_CASE */
1356
1357/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001358void hash_operation_init( )
1359{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001360 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001361 /* Test each valid way of initializing the object, except for `= {0}`, as
1362 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1363 * though it's OK by the C standard. We could test for this, but we'd need
1364 * to supress the Clang warning for the test. */
1365 psa_hash_operation_t func = psa_hash_operation_init( );
1366 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1367 psa_hash_operation_t zero;
1368
1369 memset( &zero, 0, sizeof( zero ) );
1370
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001371 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001372 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1373 PSA_ERROR_BAD_STATE );
1374 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1375 PSA_ERROR_BAD_STATE );
1376 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1377 PSA_ERROR_BAD_STATE );
1378
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001379 /* A default hash operation should be abortable without error. */
1380 PSA_ASSERT( psa_hash_abort( &func ) );
1381 PSA_ASSERT( psa_hash_abort( &init ) );
1382 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001383}
1384/* END_CASE */
1385
1386/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001387void hash_setup( int alg_arg,
1388 int expected_status_arg )
1389{
1390 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001391 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001392 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001393 psa_status_t status;
1394
Gilles Peskine8817f612018-12-18 00:18:46 +01001395 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001396
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001397 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001398 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001399
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001400 /* Whether setup succeeded or failed, abort must succeed. */
1401 PSA_ASSERT( psa_hash_abort( &operation ) );
1402
1403 /* If setup failed, reproduce the failure, so as to
1404 * test the resulting state of the operation object. */
1405 if( status != PSA_SUCCESS )
1406 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1407
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001408 /* Now the operation object should be reusable. */
1409#if defined(KNOWN_SUPPORTED_HASH_ALG)
1410 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1411 PSA_ASSERT( psa_hash_abort( &operation ) );
1412#endif
1413
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001414exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001415 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001416}
1417/* END_CASE */
1418
1419/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001420void hash_compute_fail( int alg_arg, data_t *input,
1421 int output_size_arg, int expected_status_arg )
1422{
1423 psa_algorithm_t alg = alg_arg;
1424 uint8_t *output = NULL;
1425 size_t output_size = output_size_arg;
1426 size_t output_length = INVALID_EXPORT_LENGTH;
1427 psa_status_t expected_status = expected_status_arg;
1428 psa_status_t status;
1429
1430 ASSERT_ALLOC( output, output_size );
1431
1432 PSA_ASSERT( psa_crypto_init( ) );
1433
1434 status = psa_hash_compute( alg, input->x, input->len,
1435 output, output_size, &output_length );
1436 TEST_EQUAL( status, expected_status );
1437 TEST_ASSERT( output_length <= output_size );
1438
1439exit:
1440 mbedtls_free( output );
1441 PSA_DONE( );
1442}
1443/* END_CASE */
1444
1445/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001446void hash_compare_fail( int alg_arg, data_t *input,
1447 data_t *reference_hash,
1448 int expected_status_arg )
1449{
1450 psa_algorithm_t alg = alg_arg;
1451 psa_status_t expected_status = expected_status_arg;
1452 psa_status_t status;
1453
1454 PSA_ASSERT( psa_crypto_init( ) );
1455
1456 status = psa_hash_compare( alg, input->x, input->len,
1457 reference_hash->x, reference_hash->len );
1458 TEST_EQUAL( status, expected_status );
1459
1460exit:
1461 PSA_DONE( );
1462}
1463/* END_CASE */
1464
1465/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001466void hash_compute_compare( int alg_arg, data_t *input,
1467 data_t *expected_output )
1468{
1469 psa_algorithm_t alg = alg_arg;
1470 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1471 size_t output_length = INVALID_EXPORT_LENGTH;
1472 size_t i;
1473
1474 PSA_ASSERT( psa_crypto_init( ) );
1475
1476 /* Compute with tight buffer */
1477 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001478 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001479 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001480 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001481 ASSERT_COMPARE( output, output_length,
1482 expected_output->x, expected_output->len );
1483
1484 /* Compute with larger buffer */
1485 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1486 output, sizeof( output ),
1487 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001488 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001489 ASSERT_COMPARE( output, output_length,
1490 expected_output->x, expected_output->len );
1491
1492 /* Compare with correct hash */
1493 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1494 output, output_length ) );
1495
1496 /* Compare with trailing garbage */
1497 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1498 output, output_length + 1 ),
1499 PSA_ERROR_INVALID_SIGNATURE );
1500
1501 /* Compare with truncated hash */
1502 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1503 output, output_length - 1 ),
1504 PSA_ERROR_INVALID_SIGNATURE );
1505
1506 /* Compare with corrupted value */
1507 for( i = 0; i < output_length; i++ )
1508 {
Chris Jones9634bb12021-01-20 15:56:42 +00001509 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001510 output[i] ^= 1;
1511 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1512 output, output_length ),
1513 PSA_ERROR_INVALID_SIGNATURE );
1514 output[i] ^= 1;
1515 }
1516
1517exit:
1518 PSA_DONE( );
1519}
1520/* END_CASE */
1521
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001522/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001523void hash_bad_order( )
1524{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001525 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001526 unsigned char input[] = "";
1527 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001528 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001529 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1530 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1531 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001532 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001533 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001534 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001535
Gilles Peskine8817f612018-12-18 00:18:46 +01001536 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001537
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001538 /* Call setup twice in a row. */
1539 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1540 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1541 PSA_ERROR_BAD_STATE );
1542 PSA_ASSERT( psa_hash_abort( &operation ) );
1543
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001544 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001545 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001546 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001547 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001548
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001549 /* Call update after finish. */
1550 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1551 PSA_ASSERT( psa_hash_finish( &operation,
1552 hash, sizeof( hash ), &hash_len ) );
1553 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001554 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001555 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001556
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001557 /* Call verify without calling setup beforehand. */
1558 TEST_EQUAL( psa_hash_verify( &operation,
1559 valid_hash, sizeof( valid_hash ) ),
1560 PSA_ERROR_BAD_STATE );
1561 PSA_ASSERT( psa_hash_abort( &operation ) );
1562
1563 /* Call verify after finish. */
1564 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1565 PSA_ASSERT( psa_hash_finish( &operation,
1566 hash, sizeof( hash ), &hash_len ) );
1567 TEST_EQUAL( psa_hash_verify( &operation,
1568 valid_hash, sizeof( valid_hash ) ),
1569 PSA_ERROR_BAD_STATE );
1570 PSA_ASSERT( psa_hash_abort( &operation ) );
1571
1572 /* Call verify twice in a row. */
1573 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1574 PSA_ASSERT( psa_hash_verify( &operation,
1575 valid_hash, sizeof( valid_hash ) ) );
1576 TEST_EQUAL( psa_hash_verify( &operation,
1577 valid_hash, sizeof( valid_hash ) ),
1578 PSA_ERROR_BAD_STATE );
1579 PSA_ASSERT( psa_hash_abort( &operation ) );
1580
1581 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001582 TEST_EQUAL( psa_hash_finish( &operation,
1583 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001584 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001585 PSA_ASSERT( psa_hash_abort( &operation ) );
1586
1587 /* Call finish twice in a row. */
1588 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1589 PSA_ASSERT( psa_hash_finish( &operation,
1590 hash, sizeof( hash ), &hash_len ) );
1591 TEST_EQUAL( psa_hash_finish( &operation,
1592 hash, sizeof( hash ), &hash_len ),
1593 PSA_ERROR_BAD_STATE );
1594 PSA_ASSERT( psa_hash_abort( &operation ) );
1595
1596 /* Call finish after calling verify. */
1597 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1598 PSA_ASSERT( psa_hash_verify( &operation,
1599 valid_hash, sizeof( valid_hash ) ) );
1600 TEST_EQUAL( psa_hash_finish( &operation,
1601 hash, sizeof( hash ), &hash_len ),
1602 PSA_ERROR_BAD_STATE );
1603 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001604
1605exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001606 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001607}
1608/* END_CASE */
1609
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001610/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001611void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001612{
1613 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001614 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1615 * appended to it */
1616 unsigned char hash[] = {
1617 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1618 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1619 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001620 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001621 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001622
Gilles Peskine8817f612018-12-18 00:18:46 +01001623 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001624
itayzafrir27e69452018-11-01 14:26:34 +02001625 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001626 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001627 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001628 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001629
itayzafrir27e69452018-11-01 14:26:34 +02001630 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001631 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001632 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001633 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001634
itayzafrir27e69452018-11-01 14:26:34 +02001635 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001636 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001637 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001638 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001639
itayzafrirec93d302018-10-18 18:01:10 +03001640exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001641 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001642}
1643/* END_CASE */
1644
Ronald Cronee414c72021-03-18 18:50:08 +01001645/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001646void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001647{
1648 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001649 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001650 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001651 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001652 size_t hash_len;
1653
Gilles Peskine8817f612018-12-18 00:18:46 +01001654 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001655
itayzafrir58028322018-10-25 10:22:01 +03001656 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001657 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001658 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001659 hash, expected_size - 1, &hash_len ),
1660 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001661
1662exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001663 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001664}
1665/* END_CASE */
1666
Ronald Cronee414c72021-03-18 18:50:08 +01001667/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001668void hash_clone_source_state( )
1669{
1670 psa_algorithm_t alg = PSA_ALG_SHA_256;
1671 unsigned char hash[PSA_HASH_MAX_SIZE];
1672 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1673 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1674 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1675 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1676 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1677 size_t hash_len;
1678
1679 PSA_ASSERT( psa_crypto_init( ) );
1680 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1681
1682 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1683 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1684 PSA_ASSERT( psa_hash_finish( &op_finished,
1685 hash, sizeof( hash ), &hash_len ) );
1686 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1687 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1688
1689 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1690 PSA_ERROR_BAD_STATE );
1691
1692 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1693 PSA_ASSERT( psa_hash_finish( &op_init,
1694 hash, sizeof( hash ), &hash_len ) );
1695 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1696 PSA_ASSERT( psa_hash_finish( &op_finished,
1697 hash, sizeof( hash ), &hash_len ) );
1698 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1699 PSA_ASSERT( psa_hash_finish( &op_aborted,
1700 hash, sizeof( hash ), &hash_len ) );
1701
1702exit:
1703 psa_hash_abort( &op_source );
1704 psa_hash_abort( &op_init );
1705 psa_hash_abort( &op_setup );
1706 psa_hash_abort( &op_finished );
1707 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001708 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001709}
1710/* END_CASE */
1711
Ronald Cronee414c72021-03-18 18:50:08 +01001712/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001713void hash_clone_target_state( )
1714{
1715 psa_algorithm_t alg = PSA_ALG_SHA_256;
1716 unsigned char hash[PSA_HASH_MAX_SIZE];
1717 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1718 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1719 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1720 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1721 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1722 size_t hash_len;
1723
1724 PSA_ASSERT( psa_crypto_init( ) );
1725
1726 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1727 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1728 PSA_ASSERT( psa_hash_finish( &op_finished,
1729 hash, sizeof( hash ), &hash_len ) );
1730 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1731 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1732
1733 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1734 PSA_ASSERT( psa_hash_finish( &op_target,
1735 hash, sizeof( hash ), &hash_len ) );
1736
1737 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1738 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1739 PSA_ERROR_BAD_STATE );
1740 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1741 PSA_ERROR_BAD_STATE );
1742
1743exit:
1744 psa_hash_abort( &op_target );
1745 psa_hash_abort( &op_init );
1746 psa_hash_abort( &op_setup );
1747 psa_hash_abort( &op_finished );
1748 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001749 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001750}
1751/* END_CASE */
1752
itayzafrir58028322018-10-25 10:22:01 +03001753/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001754void mac_operation_init( )
1755{
Jaeden Amero252ef282019-02-15 14:05:35 +00001756 const uint8_t input[1] = { 0 };
1757
Jaeden Amero769ce272019-01-04 11:48:03 +00001758 /* Test each valid way of initializing the object, except for `= {0}`, as
1759 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1760 * though it's OK by the C standard. We could test for this, but we'd need
1761 * to supress the Clang warning for the test. */
1762 psa_mac_operation_t func = psa_mac_operation_init( );
1763 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1764 psa_mac_operation_t zero;
1765
1766 memset( &zero, 0, sizeof( zero ) );
1767
Jaeden Amero252ef282019-02-15 14:05:35 +00001768 /* A freshly-initialized MAC operation should not be usable. */
1769 TEST_EQUAL( psa_mac_update( &func,
1770 input, sizeof( input ) ),
1771 PSA_ERROR_BAD_STATE );
1772 TEST_EQUAL( psa_mac_update( &init,
1773 input, sizeof( input ) ),
1774 PSA_ERROR_BAD_STATE );
1775 TEST_EQUAL( psa_mac_update( &zero,
1776 input, sizeof( input ) ),
1777 PSA_ERROR_BAD_STATE );
1778
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001779 /* A default MAC operation should be abortable without error. */
1780 PSA_ASSERT( psa_mac_abort( &func ) );
1781 PSA_ASSERT( psa_mac_abort( &init ) );
1782 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001783}
1784/* END_CASE */
1785
1786/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001787void mac_setup( int key_type_arg,
1788 data_t *key,
1789 int alg_arg,
1790 int expected_status_arg )
1791{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001792 psa_key_type_t key_type = key_type_arg;
1793 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001794 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001795 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001796 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1797#if defined(KNOWN_SUPPORTED_MAC_ALG)
1798 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1799#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001800
Gilles Peskine8817f612018-12-18 00:18:46 +01001801 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001802
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001803 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1804 &operation, &status ) )
1805 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001806 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001807
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001808 /* The operation object should be reusable. */
1809#if defined(KNOWN_SUPPORTED_MAC_ALG)
1810 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1811 smoke_test_key_data,
1812 sizeof( smoke_test_key_data ),
1813 KNOWN_SUPPORTED_MAC_ALG,
1814 &operation, &status ) )
1815 goto exit;
1816 TEST_EQUAL( status, PSA_SUCCESS );
1817#endif
1818
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001819exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001820 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001821}
1822/* END_CASE */
1823
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001824/* 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 +00001825void mac_bad_order( )
1826{
Ronald Cron5425a212020-08-04 14:58:35 +02001827 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001828 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1829 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001830 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001831 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1832 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1833 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001834 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001835 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1836 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1837 size_t sign_mac_length = 0;
1838 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1839 const uint8_t verify_mac[] = {
1840 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1841 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1842 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1843
1844 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001845 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001846 psa_set_key_algorithm( &attributes, alg );
1847 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001848
Ronald Cron5425a212020-08-04 14:58:35 +02001849 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1850 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001851
Jaeden Amero252ef282019-02-15 14:05:35 +00001852 /* Call update without calling setup beforehand. */
1853 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1854 PSA_ERROR_BAD_STATE );
1855 PSA_ASSERT( psa_mac_abort( &operation ) );
1856
1857 /* Call sign finish without calling setup beforehand. */
1858 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1859 &sign_mac_length),
1860 PSA_ERROR_BAD_STATE );
1861 PSA_ASSERT( psa_mac_abort( &operation ) );
1862
1863 /* Call verify finish without calling setup beforehand. */
1864 TEST_EQUAL( psa_mac_verify_finish( &operation,
1865 verify_mac, sizeof( verify_mac ) ),
1866 PSA_ERROR_BAD_STATE );
1867 PSA_ASSERT( psa_mac_abort( &operation ) );
1868
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001869 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001870 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
1871 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001872 PSA_ERROR_BAD_STATE );
1873 PSA_ASSERT( psa_mac_abort( &operation ) );
1874
Jaeden Amero252ef282019-02-15 14:05:35 +00001875 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001876 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001877 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1878 PSA_ASSERT( psa_mac_sign_finish( &operation,
1879 sign_mac, sizeof( sign_mac ),
1880 &sign_mac_length ) );
1881 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1882 PSA_ERROR_BAD_STATE );
1883 PSA_ASSERT( psa_mac_abort( &operation ) );
1884
1885 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001886 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001887 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1888 PSA_ASSERT( psa_mac_verify_finish( &operation,
1889 verify_mac, sizeof( verify_mac ) ) );
1890 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1891 PSA_ERROR_BAD_STATE );
1892 PSA_ASSERT( psa_mac_abort( &operation ) );
1893
1894 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001895 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001896 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1897 PSA_ASSERT( psa_mac_sign_finish( &operation,
1898 sign_mac, sizeof( sign_mac ),
1899 &sign_mac_length ) );
1900 TEST_EQUAL( psa_mac_sign_finish( &operation,
1901 sign_mac, sizeof( sign_mac ),
1902 &sign_mac_length ),
1903 PSA_ERROR_BAD_STATE );
1904 PSA_ASSERT( psa_mac_abort( &operation ) );
1905
1906 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001907 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001908 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1909 PSA_ASSERT( psa_mac_verify_finish( &operation,
1910 verify_mac, sizeof( verify_mac ) ) );
1911 TEST_EQUAL( psa_mac_verify_finish( &operation,
1912 verify_mac, sizeof( verify_mac ) ),
1913 PSA_ERROR_BAD_STATE );
1914 PSA_ASSERT( psa_mac_abort( &operation ) );
1915
1916 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02001917 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001918 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1919 TEST_EQUAL( psa_mac_verify_finish( &operation,
1920 verify_mac, sizeof( verify_mac ) ),
1921 PSA_ERROR_BAD_STATE );
1922 PSA_ASSERT( psa_mac_abort( &operation ) );
1923
1924 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02001925 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001926 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1927 TEST_EQUAL( psa_mac_sign_finish( &operation,
1928 sign_mac, sizeof( sign_mac ),
1929 &sign_mac_length ),
1930 PSA_ERROR_BAD_STATE );
1931 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001932
Ronald Cron5425a212020-08-04 14:58:35 +02001933 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001934
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001935exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001936 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001937}
1938/* END_CASE */
1939
1940/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001941void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02001942 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001943 int alg_arg,
1944 data_t *input,
1945 data_t *expected_mac )
1946{
Ronald Cron5425a212020-08-04 14:58:35 +02001947 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001948 psa_key_type_t key_type = key_type_arg;
1949 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001950 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001951 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02001952 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001953 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001954 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001955 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02001956 const size_t output_sizes_to_test[] = {
1957 0,
1958 1,
1959 expected_mac->len - 1,
1960 expected_mac->len,
1961 expected_mac->len + 1,
1962 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001963
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001964 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001965 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02001966 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001967
Gilles Peskine8817f612018-12-18 00:18:46 +01001968 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001969
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001970 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001971 psa_set_key_algorithm( &attributes, alg );
1972 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001973
Ronald Cron5425a212020-08-04 14:58:35 +02001974 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1975 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001976
Gilles Peskine8b356b52020-08-25 23:44:59 +02001977 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
1978 {
1979 const size_t output_size = output_sizes_to_test[i];
1980 psa_status_t expected_status =
1981 ( output_size >= expected_mac->len ? PSA_SUCCESS :
1982 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02001983
Chris Jones9634bb12021-01-20 15:56:42 +00001984 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02001985 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001986
Gilles Peskine8b356b52020-08-25 23:44:59 +02001987 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02001988 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02001989 PSA_ASSERT( psa_mac_update( &operation,
1990 input->x, input->len ) );
1991 TEST_EQUAL( psa_mac_sign_finish( &operation,
1992 actual_mac, output_size,
1993 &mac_length ),
1994 expected_status );
1995 PSA_ASSERT( psa_mac_abort( &operation ) );
1996
1997 if( expected_status == PSA_SUCCESS )
1998 {
1999 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2000 actual_mac, mac_length );
2001 }
2002 mbedtls_free( actual_mac );
2003 actual_mac = NULL;
2004 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002005
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002006exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002007 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002008 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002009 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002010 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002011}
2012/* END_CASE */
2013
2014/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002015void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002016 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002017 int alg_arg,
2018 data_t *input,
2019 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002020{
Ronald Cron5425a212020-08-04 14:58:35 +02002021 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002022 psa_key_type_t key_type = key_type_arg;
2023 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002024 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002025 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002026 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002027
Gilles Peskine69c12672018-06-28 00:07:19 +02002028 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2029
Gilles Peskine8817f612018-12-18 00:18:46 +01002030 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002031
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002032 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002033 psa_set_key_algorithm( &attributes, alg );
2034 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002035
Ronald Cron5425a212020-08-04 14:58:35 +02002036 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2037 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002038
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002039 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002040 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002041 PSA_ASSERT( psa_mac_update( &operation,
2042 input->x, input->len ) );
2043 PSA_ASSERT( psa_mac_verify_finish( &operation,
2044 expected_mac->x,
2045 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002046
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002047 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002048 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002049 PSA_ASSERT( psa_mac_update( &operation,
2050 input->x, input->len ) );
2051 TEST_EQUAL( psa_mac_verify_finish( &operation,
2052 expected_mac->x,
2053 expected_mac->len - 1 ),
2054 PSA_ERROR_INVALID_SIGNATURE );
2055
2056 /* Test a MAC that's too long. */
2057 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2058 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002059 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002060 PSA_ASSERT( psa_mac_update( &operation,
2061 input->x, input->len ) );
2062 TEST_EQUAL( psa_mac_verify_finish( &operation,
2063 perturbed_mac,
2064 expected_mac->len + 1 ),
2065 PSA_ERROR_INVALID_SIGNATURE );
2066
2067 /* Test changing one byte. */
2068 for( size_t i = 0; i < expected_mac->len; i++ )
2069 {
Chris Jones9634bb12021-01-20 15:56:42 +00002070 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002071 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002072 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002073 PSA_ASSERT( psa_mac_update( &operation,
2074 input->x, input->len ) );
2075 TEST_EQUAL( psa_mac_verify_finish( &operation,
2076 perturbed_mac,
2077 expected_mac->len ),
2078 PSA_ERROR_INVALID_SIGNATURE );
2079 perturbed_mac[i] ^= 1;
2080 }
2081
Gilles Peskine8c9def32018-02-08 10:02:12 +01002082exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002083 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002084 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002085 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002086 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002087}
2088/* END_CASE */
2089
2090/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002091void cipher_operation_init( )
2092{
Jaeden Ameroab439972019-02-15 14:12:05 +00002093 const uint8_t input[1] = { 0 };
2094 unsigned char output[1] = { 0 };
2095 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002096 /* Test each valid way of initializing the object, except for `= {0}`, as
2097 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2098 * though it's OK by the C standard. We could test for this, but we'd need
2099 * to supress the Clang warning for the test. */
2100 psa_cipher_operation_t func = psa_cipher_operation_init( );
2101 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2102 psa_cipher_operation_t zero;
2103
2104 memset( &zero, 0, sizeof( zero ) );
2105
Jaeden Ameroab439972019-02-15 14:12:05 +00002106 /* A freshly-initialized cipher operation should not be usable. */
2107 TEST_EQUAL( psa_cipher_update( &func,
2108 input, sizeof( input ),
2109 output, sizeof( output ),
2110 &output_length ),
2111 PSA_ERROR_BAD_STATE );
2112 TEST_EQUAL( psa_cipher_update( &init,
2113 input, sizeof( input ),
2114 output, sizeof( output ),
2115 &output_length ),
2116 PSA_ERROR_BAD_STATE );
2117 TEST_EQUAL( psa_cipher_update( &zero,
2118 input, sizeof( input ),
2119 output, sizeof( output ),
2120 &output_length ),
2121 PSA_ERROR_BAD_STATE );
2122
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002123 /* A default cipher operation should be abortable without error. */
2124 PSA_ASSERT( psa_cipher_abort( &func ) );
2125 PSA_ASSERT( psa_cipher_abort( &init ) );
2126 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002127}
2128/* END_CASE */
2129
2130/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002131void cipher_setup( int key_type_arg,
2132 data_t *key,
2133 int alg_arg,
2134 int expected_status_arg )
2135{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002136 psa_key_type_t key_type = key_type_arg;
2137 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002138 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002139 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002140 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002141#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002142 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2143#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002144
Gilles Peskine8817f612018-12-18 00:18:46 +01002145 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002146
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002147 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2148 &operation, &status ) )
2149 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002150 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002151
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002152 /* The operation object should be reusable. */
2153#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2154 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2155 smoke_test_key_data,
2156 sizeof( smoke_test_key_data ),
2157 KNOWN_SUPPORTED_CIPHER_ALG,
2158 &operation, &status ) )
2159 goto exit;
2160 TEST_EQUAL( status, PSA_SUCCESS );
2161#endif
2162
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002163exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002164 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002165 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002166}
2167/* END_CASE */
2168
Ronald Cronee414c72021-03-18 18:50:08 +01002169/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002170void cipher_bad_order( )
2171{
Ronald Cron5425a212020-08-04 14:58:35 +02002172 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002173 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2174 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002175 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002176 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002177 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002178 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002179 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2180 0xaa, 0xaa, 0xaa, 0xaa };
2181 const uint8_t text[] = {
2182 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2183 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002184 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002185 size_t length = 0;
2186
2187 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002188 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2189 psa_set_key_algorithm( &attributes, alg );
2190 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002191 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2192 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002193
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002194 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002195 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2196 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002197 PSA_ERROR_BAD_STATE );
2198 PSA_ASSERT( psa_cipher_abort( &operation ) );
2199
2200 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002201 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2202 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002203 PSA_ERROR_BAD_STATE );
2204 PSA_ASSERT( psa_cipher_abort( &operation ) );
2205
Jaeden Ameroab439972019-02-15 14:12:05 +00002206 /* Generate an IV without calling setup beforehand. */
2207 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2208 buffer, sizeof( buffer ),
2209 &length ),
2210 PSA_ERROR_BAD_STATE );
2211 PSA_ASSERT( psa_cipher_abort( &operation ) );
2212
2213 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002214 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002215 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2216 buffer, sizeof( buffer ),
2217 &length ) );
2218 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2219 buffer, sizeof( buffer ),
2220 &length ),
2221 PSA_ERROR_BAD_STATE );
2222 PSA_ASSERT( psa_cipher_abort( &operation ) );
2223
2224 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002225 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002226 PSA_ASSERT( psa_cipher_set_iv( &operation,
2227 iv, sizeof( iv ) ) );
2228 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2229 buffer, sizeof( buffer ),
2230 &length ),
2231 PSA_ERROR_BAD_STATE );
2232 PSA_ASSERT( psa_cipher_abort( &operation ) );
2233
2234 /* Set an IV without calling setup beforehand. */
2235 TEST_EQUAL( psa_cipher_set_iv( &operation,
2236 iv, sizeof( iv ) ),
2237 PSA_ERROR_BAD_STATE );
2238 PSA_ASSERT( psa_cipher_abort( &operation ) );
2239
2240 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002241 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002242 PSA_ASSERT( psa_cipher_set_iv( &operation,
2243 iv, sizeof( iv ) ) );
2244 TEST_EQUAL( psa_cipher_set_iv( &operation,
2245 iv, sizeof( iv ) ),
2246 PSA_ERROR_BAD_STATE );
2247 PSA_ASSERT( psa_cipher_abort( &operation ) );
2248
2249 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002250 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002251 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2252 buffer, sizeof( buffer ),
2253 &length ) );
2254 TEST_EQUAL( psa_cipher_set_iv( &operation,
2255 iv, sizeof( iv ) ),
2256 PSA_ERROR_BAD_STATE );
2257 PSA_ASSERT( psa_cipher_abort( &operation ) );
2258
2259 /* Call update without calling setup beforehand. */
2260 TEST_EQUAL( psa_cipher_update( &operation,
2261 text, sizeof( text ),
2262 buffer, sizeof( buffer ),
2263 &length ),
2264 PSA_ERROR_BAD_STATE );
2265 PSA_ASSERT( psa_cipher_abort( &operation ) );
2266
2267 /* Call update without an IV where an IV is required. */
2268 TEST_EQUAL( psa_cipher_update( &operation,
2269 text, sizeof( text ),
2270 buffer, sizeof( buffer ),
2271 &length ),
2272 PSA_ERROR_BAD_STATE );
2273 PSA_ASSERT( psa_cipher_abort( &operation ) );
2274
2275 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002276 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002277 PSA_ASSERT( psa_cipher_set_iv( &operation,
2278 iv, sizeof( iv ) ) );
2279 PSA_ASSERT( psa_cipher_finish( &operation,
2280 buffer, sizeof( buffer ), &length ) );
2281 TEST_EQUAL( psa_cipher_update( &operation,
2282 text, sizeof( text ),
2283 buffer, sizeof( buffer ),
2284 &length ),
2285 PSA_ERROR_BAD_STATE );
2286 PSA_ASSERT( psa_cipher_abort( &operation ) );
2287
2288 /* Call finish without calling setup beforehand. */
2289 TEST_EQUAL( psa_cipher_finish( &operation,
2290 buffer, sizeof( buffer ), &length ),
2291 PSA_ERROR_BAD_STATE );
2292 PSA_ASSERT( psa_cipher_abort( &operation ) );
2293
2294 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002295 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002296 /* Not calling update means we are encrypting an empty buffer, which is OK
2297 * for cipher modes with padding. */
2298 TEST_EQUAL( psa_cipher_finish( &operation,
2299 buffer, sizeof( buffer ), &length ),
2300 PSA_ERROR_BAD_STATE );
2301 PSA_ASSERT( psa_cipher_abort( &operation ) );
2302
2303 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002304 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002305 PSA_ASSERT( psa_cipher_set_iv( &operation,
2306 iv, sizeof( iv ) ) );
2307 PSA_ASSERT( psa_cipher_finish( &operation,
2308 buffer, sizeof( buffer ), &length ) );
2309 TEST_EQUAL( psa_cipher_finish( &operation,
2310 buffer, sizeof( buffer ), &length ),
2311 PSA_ERROR_BAD_STATE );
2312 PSA_ASSERT( psa_cipher_abort( &operation ) );
2313
Ronald Cron5425a212020-08-04 14:58:35 +02002314 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002315
Jaeden Ameroab439972019-02-15 14:12:05 +00002316exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002317 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002318 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002319}
2320/* END_CASE */
2321
2322/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002323void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002324 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002325 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002326 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002327{
Ronald Cron5425a212020-08-04 14:58:35 +02002328 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002329 psa_status_t status;
2330 psa_key_type_t key_type = key_type_arg;
2331 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002332 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002333 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002334 size_t output_buffer_size = 0;
2335 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002336 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002337 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002338 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002339
Gilles Peskine8817f612018-12-18 00:18:46 +01002340 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002341
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002342 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2343 psa_set_key_algorithm( &attributes, alg );
2344 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002345
Ronald Cron5425a212020-08-04 14:58:35 +02002346 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2347 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002348
Ronald Cron5425a212020-08-04 14:58:35 +02002349 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002350
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002351 if( iv->len > 0 )
2352 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002353 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002354 }
2355
gabor-mezei-armceface22021-01-21 12:26:17 +01002356 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2357 TEST_ASSERT( output_buffer_size <=
2358 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002359 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002360
Gilles Peskine8817f612018-12-18 00:18:46 +01002361 PSA_ASSERT( psa_cipher_update( &operation,
2362 input->x, input->len,
2363 output, output_buffer_size,
2364 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002365 TEST_ASSERT( function_output_length <=
2366 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2367 TEST_ASSERT( function_output_length <=
2368 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002369 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002370
Gilles Peskine50e586b2018-06-08 14:28:46 +02002371 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002372 ( output_buffer_size == 0 ? NULL :
2373 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002374 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002375 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002376 TEST_ASSERT( function_output_length <=
2377 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2378 TEST_ASSERT( function_output_length <=
2379 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002380 total_output_length += function_output_length;
2381
Gilles Peskinefe11b722018-12-18 00:24:04 +01002382 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002383 if( expected_status == PSA_SUCCESS )
2384 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002385 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002386 ASSERT_COMPARE( expected_output->x, expected_output->len,
2387 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002388 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002389
Gilles Peskine50e586b2018-06-08 14:28:46 +02002390exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002391 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002392 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002393 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002394 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002395}
2396/* END_CASE */
2397
2398/* BEGIN_CASE */
2399void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002400 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002401 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002402 int first_part_size_arg,
2403 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002404 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002405{
Ronald Cron5425a212020-08-04 14:58:35 +02002406 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002407 psa_key_type_t key_type = key_type_arg;
2408 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002409 size_t first_part_size = first_part_size_arg;
2410 size_t output1_length = output1_length_arg;
2411 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002412 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002413 size_t output_buffer_size = 0;
2414 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002415 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002416 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002417 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002418
Gilles Peskine8817f612018-12-18 00:18:46 +01002419 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002420
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002421 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2422 psa_set_key_algorithm( &attributes, alg );
2423 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002424
Ronald Cron5425a212020-08-04 14:58:35 +02002425 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2426 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002427
Ronald Cron5425a212020-08-04 14:58:35 +02002428 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002429
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002430 if( iv->len > 0 )
2431 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002432 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002433 }
2434
gabor-mezei-armceface22021-01-21 12:26:17 +01002435 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2436 TEST_ASSERT( output_buffer_size <=
2437 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002438 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002439
Gilles Peskinee0866522019-02-19 19:44:00 +01002440 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002441 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2442 output, output_buffer_size,
2443 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002444 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002445 TEST_ASSERT( function_output_length <=
2446 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2447 TEST_ASSERT( function_output_length <=
2448 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002449 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002450
Gilles Peskine8817f612018-12-18 00:18:46 +01002451 PSA_ASSERT( psa_cipher_update( &operation,
2452 input->x + first_part_size,
2453 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002454 ( output_buffer_size == 0 ? NULL :
2455 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002456 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002457 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002458 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002459 TEST_ASSERT( function_output_length <=
2460 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2461 alg,
2462 input->len - first_part_size ) );
2463 TEST_ASSERT( function_output_length <=
2464 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002465 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002466
Gilles Peskine8817f612018-12-18 00:18:46 +01002467 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002468 ( output_buffer_size == 0 ? NULL :
2469 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002470 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002471 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002472 TEST_ASSERT( function_output_length <=
2473 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2474 TEST_ASSERT( function_output_length <=
2475 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002476 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002477 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002478
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002479 ASSERT_COMPARE( expected_output->x, expected_output->len,
2480 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002481
2482exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002483 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002484 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002485 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002486 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002487}
2488/* END_CASE */
2489
2490/* BEGIN_CASE */
2491void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002492 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002493 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002494 int first_part_size_arg,
2495 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002496 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002497{
Ronald Cron5425a212020-08-04 14:58:35 +02002498 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002499 psa_key_type_t key_type = key_type_arg;
2500 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002501 size_t first_part_size = first_part_size_arg;
2502 size_t output1_length = output1_length_arg;
2503 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002504 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002505 size_t output_buffer_size = 0;
2506 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002507 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002508 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002509 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002510
Gilles Peskine8817f612018-12-18 00:18:46 +01002511 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002512
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002513 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2514 psa_set_key_algorithm( &attributes, alg );
2515 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002516
Ronald Cron5425a212020-08-04 14:58:35 +02002517 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2518 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002519
Ronald Cron5425a212020-08-04 14:58:35 +02002520 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002521
Steven Cooreman177deba2020-09-07 17:14:14 +02002522 if( iv->len > 0 )
2523 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002524 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002525 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002526
gabor-mezei-armceface22021-01-21 12:26:17 +01002527 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2528 TEST_ASSERT( output_buffer_size <=
2529 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002530 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002531
Gilles Peskinee0866522019-02-19 19:44:00 +01002532 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002533 PSA_ASSERT( psa_cipher_update( &operation,
2534 input->x, first_part_size,
2535 output, output_buffer_size,
2536 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002537 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002538 TEST_ASSERT( function_output_length <=
2539 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2540 TEST_ASSERT( function_output_length <=
2541 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002542 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002543
Gilles Peskine8817f612018-12-18 00:18:46 +01002544 PSA_ASSERT( psa_cipher_update( &operation,
2545 input->x + first_part_size,
2546 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002547 ( output_buffer_size == 0 ? NULL :
2548 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002549 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002550 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002551 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002552 TEST_ASSERT( function_output_length <=
2553 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2554 alg,
2555 input->len - first_part_size ) );
2556 TEST_ASSERT( function_output_length <=
2557 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002558 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002559
Gilles Peskine8817f612018-12-18 00:18:46 +01002560 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002561 ( output_buffer_size == 0 ? NULL :
2562 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002563 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002564 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002565 TEST_ASSERT( function_output_length <=
2566 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2567 TEST_ASSERT( function_output_length <=
2568 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002569 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002570 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002571
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002572 ASSERT_COMPARE( expected_output->x, expected_output->len,
2573 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002574
2575exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002576 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002577 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002578 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002579 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002580}
2581/* END_CASE */
2582
Gilles Peskine50e586b2018-06-08 14:28:46 +02002583/* BEGIN_CASE */
2584void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002585 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002586 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002587 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002588{
Ronald Cron5425a212020-08-04 14:58:35 +02002589 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002590 psa_status_t status;
2591 psa_key_type_t key_type = key_type_arg;
2592 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002593 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002594 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002595 size_t output_buffer_size = 0;
2596 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002597 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002598 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002599 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002600
Gilles Peskine8817f612018-12-18 00:18:46 +01002601 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002602
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002603 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2604 psa_set_key_algorithm( &attributes, alg );
2605 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002606
Ronald Cron5425a212020-08-04 14:58:35 +02002607 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2608 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002609
Ronald Cron5425a212020-08-04 14:58:35 +02002610 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002611
Steven Cooreman177deba2020-09-07 17:14:14 +02002612 if( iv->len > 0 )
2613 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002614 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002615 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002616
gabor-mezei-armceface22021-01-21 12:26:17 +01002617 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2618 TEST_ASSERT( output_buffer_size <=
2619 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002620 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002621
Gilles Peskine8817f612018-12-18 00:18:46 +01002622 PSA_ASSERT( psa_cipher_update( &operation,
2623 input->x, input->len,
2624 output, output_buffer_size,
2625 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002626 TEST_ASSERT( function_output_length <=
2627 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2628 TEST_ASSERT( function_output_length <=
2629 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002630 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002631
Gilles Peskine50e586b2018-06-08 14:28:46 +02002632 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002633 ( output_buffer_size == 0 ? NULL :
2634 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002635 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002636 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002637 TEST_ASSERT( function_output_length <=
2638 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2639 TEST_ASSERT( function_output_length <=
2640 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002641 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002642 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002643
2644 if( expected_status == PSA_SUCCESS )
2645 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002646 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002647 ASSERT_COMPARE( expected_output->x, expected_output->len,
2648 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002649 }
2650
Gilles Peskine50e586b2018-06-08 14:28:46 +02002651exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002652 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002653 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002654 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002655 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002656}
2657/* END_CASE */
2658
Gilles Peskine50e586b2018-06-08 14:28:46 +02002659/* BEGIN_CASE */
2660void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002661 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002662 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002663{
Ronald Cron5425a212020-08-04 14:58:35 +02002664 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002665 psa_key_type_t key_type = key_type_arg;
2666 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002667 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002668 size_t iv_size = 16;
2669 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002670 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002671 size_t output1_size = 0;
2672 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002673 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002674 size_t output2_size = 0;
2675 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002676 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002677 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2678 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002679 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002680
Gilles Peskine8817f612018-12-18 00:18:46 +01002681 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002682
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002683 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2684 psa_set_key_algorithm( &attributes, alg );
2685 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002686
Ronald Cron5425a212020-08-04 14:58:35 +02002687 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2688 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002689
Ronald Cron5425a212020-08-04 14:58:35 +02002690 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2691 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002692
Steven Cooreman177deba2020-09-07 17:14:14 +02002693 if( alg != PSA_ALG_ECB_NO_PADDING )
2694 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002695 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2696 iv, iv_size,
2697 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002698 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002699 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2700 TEST_ASSERT( output1_size <=
2701 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002702 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002703
Gilles Peskine8817f612018-12-18 00:18:46 +01002704 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2705 output1, output1_size,
2706 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002707 TEST_ASSERT( output1_length <=
2708 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2709 TEST_ASSERT( output1_length <=
2710 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2711
Gilles Peskine8817f612018-12-18 00:18:46 +01002712 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002713 output1 + output1_length,
2714 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002715 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002716 TEST_ASSERT( function_output_length <=
2717 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2718 TEST_ASSERT( function_output_length <=
2719 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002720
Gilles Peskine048b7f02018-06-08 14:20:49 +02002721 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002722
Gilles Peskine8817f612018-12-18 00:18:46 +01002723 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002724
2725 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002726 TEST_ASSERT( output2_size <=
2727 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2728 TEST_ASSERT( output2_size <=
2729 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002730 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002731
Steven Cooreman177deba2020-09-07 17:14:14 +02002732 if( iv_length > 0 )
2733 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002734 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2735 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002736 }
2737
Gilles Peskine8817f612018-12-18 00:18:46 +01002738 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2739 output2, output2_size,
2740 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002741 TEST_ASSERT( output2_length <=
2742 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
2743 TEST_ASSERT( output2_length <=
2744 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
2745
Gilles Peskine048b7f02018-06-08 14:20:49 +02002746 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002747 PSA_ASSERT( psa_cipher_finish( &operation2,
2748 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002749 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002750 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002751 TEST_ASSERT( function_output_length <=
2752 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2753 TEST_ASSERT( function_output_length <=
2754 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03002755
Gilles Peskine048b7f02018-06-08 14:20:49 +02002756 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002757
Gilles Peskine8817f612018-12-18 00:18:46 +01002758 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002759
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002760 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002761
2762exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002763 psa_cipher_abort( &operation1 );
2764 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002765 mbedtls_free( output1 );
2766 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002767 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002768 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03002769}
2770/* END_CASE */
2771
2772/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002773void cipher_verify_output_multipart( int alg_arg,
2774 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002775 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002776 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002777 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03002778{
Ronald Cron5425a212020-08-04 14:58:35 +02002779 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002780 psa_key_type_t key_type = key_type_arg;
2781 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002782 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002783 unsigned char iv[16] = {0};
2784 size_t iv_size = 16;
2785 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002786 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002787 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002788 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002789 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002790 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002791 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002792 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002793 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2794 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002795 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002796
Gilles Peskine8817f612018-12-18 00:18:46 +01002797 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002798
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002799 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2800 psa_set_key_algorithm( &attributes, alg );
2801 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002802
Ronald Cron5425a212020-08-04 14:58:35 +02002803 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2804 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03002805
Ronald Cron5425a212020-08-04 14:58:35 +02002806 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2807 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002808
Steven Cooreman177deba2020-09-07 17:14:14 +02002809 if( alg != PSA_ALG_ECB_NO_PADDING )
2810 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002811 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2812 iv, iv_size,
2813 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002814 }
2815
gabor-mezei-armceface22021-01-21 12:26:17 +01002816 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2817 TEST_ASSERT( output1_buffer_size <=
2818 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002819 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002820
Gilles Peskinee0866522019-02-19 19:44:00 +01002821 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002822
Gilles Peskine8817f612018-12-18 00:18:46 +01002823 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2824 output1, output1_buffer_size,
2825 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002826 TEST_ASSERT( function_output_length <=
2827 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2828 TEST_ASSERT( function_output_length <=
2829 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002830 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002831
Gilles Peskine8817f612018-12-18 00:18:46 +01002832 PSA_ASSERT( psa_cipher_update( &operation1,
2833 input->x + first_part_size,
2834 input->len - first_part_size,
2835 output1, output1_buffer_size,
2836 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002837 TEST_ASSERT( function_output_length <=
2838 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2839 alg,
2840 input->len - first_part_size ) );
2841 TEST_ASSERT( function_output_length <=
2842 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002843 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002844
Gilles Peskine8817f612018-12-18 00:18:46 +01002845 PSA_ASSERT( psa_cipher_finish( &operation1,
2846 output1 + output1_length,
2847 output1_buffer_size - output1_length,
2848 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002849 TEST_ASSERT( function_output_length <=
2850 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2851 TEST_ASSERT( function_output_length <=
2852 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002853 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002854
Gilles Peskine8817f612018-12-18 00:18:46 +01002855 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002856
Gilles Peskine048b7f02018-06-08 14:20:49 +02002857 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002858 TEST_ASSERT( output2_buffer_size <=
2859 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2860 TEST_ASSERT( output2_buffer_size <=
2861 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002862 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002863
Steven Cooreman177deba2020-09-07 17:14:14 +02002864 if( iv_length > 0 )
2865 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002866 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2867 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002868 }
Moran Pekerded84402018-06-06 16:36:50 +03002869
Gilles Peskine8817f612018-12-18 00:18:46 +01002870 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2871 output2, output2_buffer_size,
2872 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002873 TEST_ASSERT( function_output_length <=
2874 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2875 TEST_ASSERT( function_output_length <=
2876 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002877 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002878
Gilles Peskine8817f612018-12-18 00:18:46 +01002879 PSA_ASSERT( psa_cipher_update( &operation2,
2880 output1 + first_part_size,
2881 output1_length - first_part_size,
2882 output2, output2_buffer_size,
2883 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002884 TEST_ASSERT( function_output_length <=
2885 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2886 alg,
2887 output1_length - first_part_size ) );
2888 TEST_ASSERT( function_output_length <=
2889 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002890 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002891
Gilles Peskine8817f612018-12-18 00:18:46 +01002892 PSA_ASSERT( psa_cipher_finish( &operation2,
2893 output2 + output2_length,
2894 output2_buffer_size - output2_length,
2895 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002896 TEST_ASSERT( function_output_length <=
2897 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2898 TEST_ASSERT( function_output_length <=
2899 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002900 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002901
Gilles Peskine8817f612018-12-18 00:18:46 +01002902 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002903
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002904 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002905
2906exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002907 psa_cipher_abort( &operation1 );
2908 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002909 mbedtls_free( output1 );
2910 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002911 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002912 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002913}
2914/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002915
Gilles Peskine20035e32018-02-03 22:44:14 +01002916/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002917void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002918 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002919 data_t *nonce,
2920 data_t *additional_data,
2921 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002922 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002923{
Ronald Cron5425a212020-08-04 14:58:35 +02002924 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002925 psa_key_type_t key_type = key_type_arg;
2926 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01002927 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002928 unsigned char *output_data = NULL;
2929 size_t output_size = 0;
2930 size_t output_length = 0;
2931 unsigned char *output_data2 = NULL;
2932 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01002933 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002934 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002935 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002936
Gilles Peskine8817f612018-12-18 00:18:46 +01002937 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002938
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002939 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2940 psa_set_key_algorithm( &attributes, alg );
2941 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002942
Gilles Peskine049c7532019-05-15 20:22:09 +02002943 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002944 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01002945 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
2946 key_bits = psa_get_key_bits( &attributes );
2947
2948 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
2949 alg );
2950 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
2951 * should be exact. */
2952 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
2953 expected_result != PSA_ERROR_NOT_SUPPORTED )
2954 {
2955 TEST_EQUAL( output_size,
2956 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
2957 TEST_ASSERT( output_size <=
2958 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
2959 }
2960 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002961
Steven Cooremanf49478b2021-02-15 15:19:25 +01002962 status = psa_aead_encrypt( key, alg,
2963 nonce->x, nonce->len,
2964 additional_data->x,
2965 additional_data->len,
2966 input_data->x, input_data->len,
2967 output_data, output_size,
2968 &output_length );
2969
2970 /* If the operation is not supported, just skip and not fail in case the
2971 * encryption involves a common limitation of cryptography hardwares and
2972 * an alternative implementation. */
2973 if( status == PSA_ERROR_NOT_SUPPORTED )
2974 {
2975 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
2976 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
2977 }
2978
2979 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002980
2981 if( PSA_SUCCESS == expected_result )
2982 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002983 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002984
Gilles Peskine003a4a92019-05-14 16:09:40 +02002985 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
2986 * should be exact. */
2987 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01002988 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02002989
gabor-mezei-armceface22021-01-21 12:26:17 +01002990 TEST_ASSERT( input_data->len <=
2991 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
2992
Ronald Cron5425a212020-08-04 14:58:35 +02002993 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01002994 nonce->x, nonce->len,
2995 additional_data->x,
2996 additional_data->len,
2997 output_data, output_length,
2998 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002999 &output_length2 ),
3000 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003001
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003002 ASSERT_COMPARE( input_data->x, input_data->len,
3003 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003004 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003005
Gilles Peskinea1cac842018-06-11 19:33:02 +02003006exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003007 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003008 mbedtls_free( output_data );
3009 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003010 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003011}
3012/* END_CASE */
3013
3014/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003015void aead_encrypt( int key_type_arg, data_t *key_data,
3016 int alg_arg,
3017 data_t *nonce,
3018 data_t *additional_data,
3019 data_t *input_data,
3020 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003021{
Ronald Cron5425a212020-08-04 14:58:35 +02003022 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003023 psa_key_type_t key_type = key_type_arg;
3024 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003025 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003026 unsigned char *output_data = NULL;
3027 size_t output_size = 0;
3028 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003029 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003030 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003031
Gilles Peskine8817f612018-12-18 00:18:46 +01003032 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003033
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003034 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3035 psa_set_key_algorithm( &attributes, alg );
3036 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003037
Gilles Peskine049c7532019-05-15 20:22:09 +02003038 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003039 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003040 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3041 key_bits = psa_get_key_bits( &attributes );
3042
3043 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3044 alg );
3045 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3046 * should be exact. */
3047 TEST_EQUAL( output_size,
3048 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3049 TEST_ASSERT( output_size <=
3050 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3051 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003052
Steven Cooremand588ea12021-01-11 19:36:04 +01003053 status = psa_aead_encrypt( key, alg,
3054 nonce->x, nonce->len,
3055 additional_data->x, additional_data->len,
3056 input_data->x, input_data->len,
3057 output_data, output_size,
3058 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003059
Ronald Cron28a45ed2021-02-09 20:35:42 +01003060 /* If the operation is not supported, just skip and not fail in case the
3061 * encryption involves a common limitation of cryptography hardwares and
3062 * an alternative implementation. */
3063 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003064 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003065 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3066 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003067 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003068
3069 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003070 ASSERT_COMPARE( expected_result->x, expected_result->len,
3071 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003072
Gilles Peskinea1cac842018-06-11 19:33:02 +02003073exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003074 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003075 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003076 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003077}
3078/* END_CASE */
3079
3080/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003081void aead_decrypt( int key_type_arg, data_t *key_data,
3082 int alg_arg,
3083 data_t *nonce,
3084 data_t *additional_data,
3085 data_t *input_data,
3086 data_t *expected_data,
3087 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003088{
Ronald Cron5425a212020-08-04 14:58:35 +02003089 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003090 psa_key_type_t key_type = key_type_arg;
3091 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003092 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003093 unsigned char *output_data = NULL;
3094 size_t output_size = 0;
3095 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003096 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003097 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003098 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003099
Gilles Peskine8817f612018-12-18 00:18:46 +01003100 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003101
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003102 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3103 psa_set_key_algorithm( &attributes, alg );
3104 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003105
Gilles Peskine049c7532019-05-15 20:22:09 +02003106 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003107 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003108 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3109 key_bits = psa_get_key_bits( &attributes );
3110
3111 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3112 alg );
3113 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3114 expected_result != PSA_ERROR_NOT_SUPPORTED )
3115 {
3116 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3117 * should be exact. */
3118 TEST_EQUAL( output_size,
3119 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3120 TEST_ASSERT( output_size <=
3121 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3122 }
3123 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003124
Steven Cooremand588ea12021-01-11 19:36:04 +01003125 status = psa_aead_decrypt( key, alg,
3126 nonce->x, nonce->len,
3127 additional_data->x,
3128 additional_data->len,
3129 input_data->x, input_data->len,
3130 output_data, output_size,
3131 &output_length );
3132
Ronald Cron28a45ed2021-02-09 20:35:42 +01003133 /* If the operation is not supported, just skip and not fail in case the
3134 * decryption involves a common limitation of cryptography hardwares and
3135 * an alternative implementation. */
3136 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003137 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003138 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3139 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003140 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003141
3142 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003143
Gilles Peskine2d277862018-06-18 15:41:12 +02003144 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003145 ASSERT_COMPARE( expected_data->x, expected_data->len,
3146 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003147
Gilles Peskinea1cac842018-06-11 19:33:02 +02003148exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003149 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003150 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003151 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003152}
3153/* END_CASE */
3154
3155/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003156void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3157 int alg_arg,
3158 data_t *nonce,
3159 data_t *additional_data,
3160 int ad_part_len,
3161 data_t *input_data,
3162 int data_part_len,
3163 data_t *expected_result )
3164{
3165 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3166 psa_key_type_t key_type = key_type_arg;
3167 psa_algorithm_t alg = alg_arg;
3168 psa_aead_operation_t operation;
3169 unsigned char *output_data = NULL;
3170 unsigned char *part_data = NULL;
3171 unsigned char *final_data = NULL;
3172 size_t output_size = 0;
Paul Elliott388f6062021-06-03 19:19:49 +01003173 size_t finish_output_size;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003174 size_t part_data_size = 0;
3175 size_t output_length = 0;
3176 size_t key_bits = 0;
3177 size_t tag_length = 0;
3178 size_t tag_size = 0;
3179 size_t nonce_length = 0;
Paul Elliott388f6062021-06-03 19:19:49 +01003180 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3181 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott0023e0a2021-04-27 10:06:22 +01003182 uint32_t part_offset = 0;
3183 size_t part_length = 0;
3184 size_t output_part_length = 0;
3185 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3186 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3187
3188 PSA_ASSERT( psa_crypto_init( ) );
3189
3190 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3191 psa_set_key_algorithm( &attributes, alg );
3192 psa_set_key_type( &attributes, key_type );
3193
3194 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3195 &key ) );
3196
3197 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3198 key_bits = psa_get_key_bits( &attributes );
3199
3200 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
3201
Paul Elliott388f6062021-06-03 19:19:49 +01003202 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003203
3204 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott2df40052021-05-07 17:52:18 +01003205 ( input_data->len +
3206 tag_length ) );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003207
3208 ASSERT_ALLOC( output_data, output_size );
3209
Paul Elliott388f6062021-06-03 19:19:49 +01003210 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003211
Paul Elliott388f6062021-06-03 19:19:49 +01003212 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
3213
3214 ASSERT_ALLOC( final_data, finish_output_size );
3215
3216 operation = psa_aead_operation_init( );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003217
3218 status = psa_aead_encrypt_setup( &operation, key, alg );
3219
3220 /* If the operation is not supported, just skip and not fail in case the
3221 * encryption involves a common limitation of cryptography hardwares and
3222 * an alternative implementation. */
3223 if( status == PSA_ERROR_NOT_SUPPORTED )
3224 {
3225 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3226 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3227 }
3228
3229 PSA_ASSERT( status );
3230
3231 if( nonce->len == 0 )
3232 {
Paul Elliott2df40052021-05-07 17:52:18 +01003233 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
3234 sizeof( nonce_buffer ),
3235 &nonce_length ) );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003236 }
3237 else
3238 {
3239 nonce_length = nonce->len;
3240 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3241 }
3242
3243#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
3244 if( operation.alg == PSA_ALG_GCM )
3245 {
Paul Elliott2df40052021-05-07 17:52:18 +01003246 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
3247 input_data->len ) );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003248 }
3249#endif
3250
3251 if( ad_part_len != -1 )
3252 {
3253 /* Pass addtional data in parts */
3254 part_offset = 0;
3255
3256 while( part_offset <= additional_data->len)
3257 {
3258 if( additional_data->len - part_offset < ( uint32_t ) ad_part_len )
3259 {
3260 part_length = additional_data->len - part_offset;
3261 }
3262 else
3263 {
3264 part_length = ad_part_len;
3265 }
3266
Paul Elliott2df40052021-05-07 17:52:18 +01003267 PSA_ASSERT( psa_aead_update_ad( &operation,
3268 additional_data->x + part_offset,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003269 part_length ) );
3270
3271 part_offset += part_length;
3272 }
3273 }
3274 else
3275 {
3276 /* Pass additional data in one go. */
Paul Elliott2df40052021-05-07 17:52:18 +01003277 PSA_ASSERT( psa_aead_update_ad(&operation, additional_data->x,
3278 additional_data->len) );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003279 }
3280
3281 if( data_part_len != -1 )
3282 {
3283 /* Pass data in parts */
Paul Elliott2df40052021-05-07 17:52:18 +01003284 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
3285 ( size_t ) data_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003286
3287 ASSERT_ALLOC( part_data, part_data_size );
3288
3289 part_offset = 0;
3290
3291 while( part_offset <= input_data->len)
3292 {
3293 if( input_data->len - part_offset < ( uint32_t ) data_part_len )
3294 {
3295 part_length = input_data->len - part_offset;
3296 }
3297 else
3298 {
3299 part_length = data_part_len;
3300 }
3301
Paul Elliott2df40052021-05-07 17:52:18 +01003302 PSA_ASSERT( psa_aead_update( &operation,
3303 ( input_data->x + part_offset ),
Paul Elliott0023e0a2021-04-27 10:06:22 +01003304 part_length, part_data,
Paul Elliott2df40052021-05-07 17:52:18 +01003305 part_data_size,
3306 &output_part_length ) );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003307
Paul Elliottac3c2002021-04-27 19:10:18 +01003308 if( output_data && output_part_length )
3309 {
Paul Elliott2df40052021-05-07 17:52:18 +01003310 memcpy( ( output_data + part_offset ), part_data,
3311 output_part_length );
Paul Elliottac3c2002021-04-27 19:10:18 +01003312 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003313
3314 part_offset += part_length;
3315 output_length += output_part_length;
3316 }
3317 }
3318 else
3319 {
3320 /* Pass whole data in one go */
3321 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
3322 input_data->len, output_data,
3323 output_size, &output_length ) );
3324 }
3325
3326 PSA_ASSERT( psa_aead_finish( &operation, final_data,
Paul Elliott388f6062021-06-03 19:19:49 +01003327 finish_output_size,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003328 &output_part_length,
3329 tag_buffer, tag_length,
3330 &tag_size ) );
3331
Paul Elliottac3c2002021-04-27 19:10:18 +01003332 if( output_data && output_part_length )
3333 {
Paul Elliott2df40052021-05-07 17:52:18 +01003334 memcpy( ( output_data + output_length ), final_data,
3335 output_part_length );
Paul Elliottac3c2002021-04-27 19:10:18 +01003336 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003337
3338 TEST_EQUAL(tag_length, tag_size);
3339
3340 output_length += output_part_length;
3341
Paul Elliottac3c2002021-04-27 19:10:18 +01003342 if( output_data && tag_length )
3343 {
3344 memcpy( ( output_data + output_length ), tag_buffer, tag_length );
3345 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003346
3347 output_length += tag_length;
3348
3349 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3350 * should be exact. */
3351 TEST_EQUAL( output_length,
Paul Elliott2df40052021-05-07 17:52:18 +01003352 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3353 input_data->len ) );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003354 TEST_ASSERT( output_length <=
3355 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3356
3357 ASSERT_COMPARE( expected_result->x, expected_result->len,
3358 output_data, output_length );
3359
3360exit:
3361 psa_destroy_key( key );
Paul Elliott16e6dcd2021-04-28 13:27:39 +01003362 psa_aead_abort( &operation );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003363 mbedtls_free( output_data );
3364 mbedtls_free( part_data );
3365 mbedtls_free( final_data );
3366 PSA_DONE( );
3367}
3368/* END_CASE */
3369
3370/* BEGIN_CASE */
3371void aead_multipart_encrypt_decrypt( int key_type_arg, data_t *key_data,
3372 int alg_arg,
3373 data_t *nonce,
3374 data_t *additional_data,
3375 int ad_part_len,
3376 data_t *input_data,
3377 int data_part_len,
3378 int expected_result_arg )
3379{
3380 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3381 psa_key_type_t key_type = key_type_arg;
3382 psa_algorithm_t alg = alg_arg;
3383 psa_aead_operation_t operation;
3384 unsigned char *output_data = NULL;
3385 unsigned char *part_data = NULL;
3386 unsigned char *final_data = NULL;
3387 size_t part_data_size;
3388 size_t output_size = 0;
Paul Elliott388f6062021-06-03 19:19:49 +01003389 size_t finish_output_size = 0;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003390 size_t output_length = 0;
3391 unsigned char *output_data2 = NULL;
3392 size_t output_size2 = 0;
3393 size_t output_length2 = 0;
3394 size_t key_bits = 0;
3395 size_t tag_length = 0;
3396 size_t tag_size = 0;
3397 size_t nonce_length = 0;
Paul Elliott388f6062021-06-03 19:19:49 +01003398 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3399 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott0023e0a2021-04-27 10:06:22 +01003400 uint32_t part_offset = 0;
3401 size_t part_length = 0;
3402 size_t output_part_length = 0;
3403 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3404 psa_status_t expected_result = expected_result_arg;
3405 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3406
3407 PSA_ASSERT( psa_crypto_init( ) );
3408
Paul Elliott2df40052021-05-07 17:52:18 +01003409 psa_set_key_usage_flags( &attributes,
3410 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003411 psa_set_key_algorithm( &attributes, alg );
3412 psa_set_key_type( &attributes, key_type );
3413
3414 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3415 &key ) );
3416
3417 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3418 key_bits = psa_get_key_bits( &attributes );
3419
3420 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
3421
Paul Elliott388f6062021-06-03 19:19:49 +01003422 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003423
3424 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3425
3426 ASSERT_ALLOC( output_data, output_size );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003427
Paul Elliott388f6062021-06-03 19:19:49 +01003428 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
3429
3430 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
3431
3432 ASSERT_ALLOC( final_data, finish_output_size );
3433
3434 operation = psa_aead_operation_init( );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003435
3436 status = psa_aead_encrypt_setup( &operation, key, alg );
3437
3438 /* If the operation is not supported, just skip and not fail in case the
3439 * encryption involves a common limitation of cryptography hardwares and
3440 * an alternative implementation. */
3441 if( status == PSA_ERROR_NOT_SUPPORTED )
3442 {
3443 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3444 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3445 }
3446
3447 if( status != PSA_SUCCESS )
3448 {
3449 TEST_EQUAL( status, expected_result_arg );
3450 goto exit;
3451 }
3452
3453 if( nonce->len == 0 )
3454 {
Paul Elliott2df40052021-05-07 17:52:18 +01003455 status = psa_aead_generate_nonce( &operation, nonce_buffer,
3456 sizeof( nonce_buffer ),
Paul Elliott0023e0a2021-04-27 10:06:22 +01003457 &nonce_length );
3458 }
3459 else
3460 {
3461 nonce_length = nonce->len;
3462 status = psa_aead_set_nonce( &operation, nonce->x, nonce->len );
3463 }
3464
3465 if( status != PSA_SUCCESS )
3466 {
3467 TEST_EQUAL( status, expected_result_arg );
3468 goto exit;
3469 }
3470
3471#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
3472 if( operation.alg == PSA_ALG_GCM )
3473 {
Paul Elliott2df40052021-05-07 17:52:18 +01003474 status = psa_aead_set_lengths( &operation, additional_data->len,
3475 input_data->len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003476
3477 if( status != PSA_SUCCESS )
3478 {
3479 TEST_EQUAL( status, expected_result_arg );
3480 goto exit;
3481 }
3482 }
3483#endif
3484
3485 if( ad_part_len != -1 )
3486 {
3487 part_offset = 0;
3488
3489 while( part_offset <= additional_data->len)
3490 {
3491 if( additional_data->len - part_offset < ( uint32_t ) ad_part_len )
3492 {
3493 part_length = additional_data->len - part_offset;
3494 }
3495 else
3496 {
3497 part_length = ad_part_len;
3498 }
3499
Paul Elliott2df40052021-05-07 17:52:18 +01003500 status = psa_aead_update_ad( &operation,
3501 additional_data->x + part_offset,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003502 part_length );
3503
3504 if( status != PSA_SUCCESS )
3505 {
3506 TEST_EQUAL( status, expected_result_arg );
3507 goto exit;
3508 }
3509
3510 part_offset += part_length;
3511 }
3512 }
3513 else
3514 {
Paul Elliott2df40052021-05-07 17:52:18 +01003515 status = psa_aead_update_ad(&operation, additional_data->x,
3516 additional_data->len);
Paul Elliott0023e0a2021-04-27 10:06:22 +01003517
3518 if( status != PSA_SUCCESS )
3519 {
3520 TEST_EQUAL( status, expected_result_arg );
3521 goto exit;
3522 }
3523 }
3524
3525 if( data_part_len != -1 )
3526 {
3527 /* Pass data in parts */
3528 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott2df40052021-05-07 17:52:18 +01003529 ( size_t ) data_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003530
3531 ASSERT_ALLOC( part_data, part_data_size );
3532
3533 part_offset = 0;
3534
3535 while( part_offset <= input_data->len)
3536 {
3537 if( input_data->len - part_offset < ( uint32_t ) data_part_len )
3538 {
3539 part_length = input_data->len - part_offset;
3540 }
3541 else
3542 {
3543 part_length = data_part_len;
3544 }
3545
Paul Elliott2df40052021-05-07 17:52:18 +01003546 status = psa_aead_update( &operation,
3547 ( input_data->x + part_offset ),
Paul Elliott0023e0a2021-04-27 10:06:22 +01003548 part_length, part_data,
3549 part_data_size, &output_part_length );
3550
3551 if( status != PSA_SUCCESS )
3552 {
3553 TEST_EQUAL( status, expected_result_arg );
3554 goto exit;
3555 }
3556
Paul Elliottac3c2002021-04-27 19:10:18 +01003557 if( output_data && output_part_length )
3558 {
Paul Elliott2df40052021-05-07 17:52:18 +01003559 memcpy( ( output_data + part_offset ), part_data,
3560 output_part_length );
Paul Elliottac3c2002021-04-27 19:10:18 +01003561 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003562
3563 part_offset += part_length;
3564 output_length += output_part_length;
3565 }
3566 }
3567 else
3568 {
3569 status = psa_aead_update( &operation, input_data->x,
3570 input_data->len, output_data,
3571 output_size, &output_length );
3572
3573 if( status != PSA_SUCCESS )
3574 {
3575 TEST_EQUAL( status, expected_result_arg );
3576 goto exit;
3577 }
3578 }
3579
3580 status = psa_aead_finish( &operation, final_data,
Paul Elliott388f6062021-06-03 19:19:49 +01003581 finish_output_size,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003582 &output_part_length,
3583 tag_buffer, tag_length,
3584 &tag_size );
3585
3586 if( status != PSA_SUCCESS )
3587 {
3588 TEST_EQUAL( status, expected_result_arg );
3589 goto exit;
3590 }
3591
Paul Elliottac3c2002021-04-27 19:10:18 +01003592 if( output_data &&output_part_length )
3593 {
Paul Elliott2df40052021-05-07 17:52:18 +01003594 memcpy( ( output_data + output_length ), final_data,
3595 output_part_length );
Paul Elliottac3c2002021-04-27 19:10:18 +01003596 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003597
3598 output_length += output_part_length;
3599
3600 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3601 * should be exact. */
3602 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3603 TEST_EQUAL( ( output_length + tag_length ),
Paul Elliott2df40052021-05-07 17:52:18 +01003604 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3605 input_data->len ) );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003606
3607 TEST_EQUAL(tag_length, tag_size);
3608
3609 if( PSA_SUCCESS == expected_result )
3610 {
Paul Elliott2df40052021-05-07 17:52:18 +01003611 output_size2 = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
3612 output_length );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003613 ASSERT_ALLOC( output_data2, output_size2 );
3614
3615 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3616 * should be exact. */
3617 TEST_EQUAL( input_data->len,
3618 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
Paul Elliott2df40052021-05-07 17:52:18 +01003619 ( output_length +
3620 tag_length ) ) );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003621
3622 TEST_ASSERT( input_data->len <=
Paul Elliott2df40052021-05-07 17:52:18 +01003623 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length +
3624 tag_length ) );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003625
Paul Elliott388f6062021-06-03 19:19:49 +01003626 operation = psa_aead_operation_init( );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003627
3628 status = psa_aead_decrypt_setup( &operation, key, alg );
3629
3630 /* If the operation is not supported, just skip and not fail in case the
3631 * encryption involves a common limitation of cryptography hardwares and
3632 * an alternative implementation. */
3633 if( status == PSA_ERROR_NOT_SUPPORTED )
3634 {
3635 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott2df40052021-05-07 17:52:18 +01003636 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg,
3637 nonce->len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003638 }
3639
3640 TEST_EQUAL( status, expected_result );
3641
3642 if( nonce->len == 0 )
3643 {
3644 /* Use previously generated nonce. */
Paul Elliott2df40052021-05-07 17:52:18 +01003645 status = psa_aead_set_nonce( &operation, nonce_buffer,
3646 nonce_length );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003647 }
3648 else
3649 {
3650 nonce_length = nonce->len;
3651 status = psa_aead_set_nonce( &operation, nonce->x, nonce->len );
3652 }
3653
3654 if( status != PSA_SUCCESS )
3655 {
3656 TEST_EQUAL( status, expected_result_arg );
3657 }
3658
3659#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
3660 if( operation.alg == PSA_ALG_GCM )
3661 {
Paul Elliott2df40052021-05-07 17:52:18 +01003662 status = psa_aead_set_lengths( &operation, additional_data->len,
3663 output_length );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003664
3665 if( status != PSA_SUCCESS )
3666 {
3667 TEST_EQUAL( status, expected_result_arg );
3668 }
3669 }
3670#endif
3671
3672 if( ad_part_len != -1 )
3673 {
3674 part_offset = 0;
3675
3676 while( part_offset <= additional_data->len)
3677 {
Paul Elliott2df40052021-05-07 17:52:18 +01003678 if( additional_data->len - part_offset <
3679 ( uint32_t ) ad_part_len )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003680 {
3681 part_length = additional_data->len - part_offset;
3682 }
3683 else
3684 {
3685 part_length = ad_part_len;
3686 }
3687
Paul Elliott2df40052021-05-07 17:52:18 +01003688 PSA_ASSERT( psa_aead_update_ad( &operation,
3689 additional_data->x +
3690 part_offset,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003691 part_length ) );
3692
3693 part_offset += part_length;
3694 }
3695 }
3696 else
3697 {
Paul Elliott2df40052021-05-07 17:52:18 +01003698 PSA_ASSERT( psa_aead_update_ad(&operation, additional_data->x,
3699 additional_data->len) );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003700 }
3701
3702 if( data_part_len != -1 )
3703 {
3704 /* Pass data in parts */
Paul Elliott2df40052021-05-07 17:52:18 +01003705 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
3706 ( size_t ) data_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003707
3708 ASSERT_ALLOC( part_data, part_data_size );
3709
3710 part_offset = 0;
3711
3712 while( part_offset <= ( input_data->len - tag_length ) )
3713 {
Paul Elliott2df40052021-05-07 17:52:18 +01003714 if( ( input_data->len - tag_length - part_offset ) <
3715 ( uint32_t ) data_part_len )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003716 {
Paul Elliott2df40052021-05-07 17:52:18 +01003717 part_length =
3718 ( input_data->len - tag_length - part_offset );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003719 }
3720 else
3721 {
3722 part_length = data_part_len;
3723 }
3724
Paul Elliott2df40052021-05-07 17:52:18 +01003725 PSA_ASSERT( psa_aead_update( &operation,
3726 ( input_data->x + part_offset ),
Paul Elliott0023e0a2021-04-27 10:06:22 +01003727 part_length, part_data,
Paul Elliott2df40052021-05-07 17:52:18 +01003728 part_data_size,
3729 &output_part_length ) );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003730
Paul Elliottac3c2002021-04-27 19:10:18 +01003731 if( output_data2 && output_part_length )
3732 {
Paul Elliott2df40052021-05-07 17:52:18 +01003733 memcpy( ( output_data2 + part_offset ),
3734 part_data, output_part_length );
Paul Elliottac3c2002021-04-27 19:10:18 +01003735 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003736
3737 part_offset += part_length;
3738 output_length2 += output_part_length;
3739 }
3740 }
3741 else
3742 {
3743 PSA_ASSERT( psa_aead_update( &operation, output_data,
3744 output_length, output_data2,
3745 output_size2, &output_length2 ) );
3746 }
3747
3748 PSA_ASSERT( psa_aead_verify( &operation, final_data,
Paul Elliott388f6062021-06-03 19:19:49 +01003749 finish_output_size,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003750 &output_part_length,
3751 tag_buffer, tag_length ) );
3752
Paul Elliottac3c2002021-04-27 19:10:18 +01003753 if( output_data2 && output_part_length )
3754 {
Paul Elliott2df40052021-05-07 17:52:18 +01003755 memcpy( ( output_data2 + output_length2 ), final_data,
3756 output_part_length);
Paul Elliottac3c2002021-04-27 19:10:18 +01003757 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003758
3759 output_length2 += output_part_length;
3760
3761 ASSERT_COMPARE( input_data->x, input_data->len,
3762 output_data2, output_length2 );
3763 }
3764
3765exit:
3766 psa_destroy_key( key );
Paul Elliott16e6dcd2021-04-28 13:27:39 +01003767 psa_aead_abort( &operation );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003768 mbedtls_free( output_data );
3769 mbedtls_free( output_data2 );
3770 mbedtls_free( part_data );
3771 mbedtls_free( final_data );
3772 PSA_DONE( );
3773}
3774/* END_CASE */
3775
3776/* BEGIN_CASE */
3777void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3778 int alg_arg,
3779 data_t *nonce,
3780 data_t *additional_data,
3781 int ad_part_len,
3782 data_t *input_data,
3783 int data_part_len,
3784 data_t *expected_data,
3785 int expected_result_arg )
3786{
3787 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3788 psa_key_type_t key_type = key_type_arg;
3789 psa_algorithm_t alg = alg_arg;
3790 psa_aead_operation_t operation;
3791 unsigned char *output_data = NULL;
3792 unsigned char *part_data = NULL;
3793 unsigned char *final_data = NULL;
3794 size_t part_data_size;
3795 size_t output_size = 0;
Paul Elliott388f6062021-06-03 19:19:49 +01003796 size_t verify_output_size = 0;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003797 size_t output_length = 0;
3798 size_t key_bits = 0;
3799 size_t tag_length = 0;
3800 size_t nonce_length = 0;
Paul Elliott388f6062021-06-03 19:19:49 +01003801 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliott0023e0a2021-04-27 10:06:22 +01003802 uint32_t part_offset = 0;
3803 size_t part_length = 0;
3804 size_t output_part_length = 0;
3805 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3806 psa_status_t expected_result = expected_result_arg;
3807 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3808
3809 PSA_ASSERT( psa_crypto_init( ) );
3810
3811 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3812 psa_set_key_algorithm( &attributes, alg );
3813 psa_set_key_type( &attributes, key_type );
3814
3815 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3816 &key ) );
3817
3818 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3819 key_bits = psa_get_key_bits( &attributes );
3820
3821 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
3822
3823 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott2df40052021-05-07 17:52:18 +01003824 ( input_data->len -
3825 tag_length ) );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003826
3827 ASSERT_ALLOC( output_data, output_size );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003828
Paul Elliott388f6062021-06-03 19:19:49 +01003829 verify_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
3830 TEST_ASSERT( verify_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
3831 ASSERT_ALLOC( final_data, verify_output_size );
3832
3833 operation = psa_aead_operation_init( );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003834
3835 status = psa_aead_decrypt_setup( &operation, key, alg );
3836
3837 /* If the operation is not supported, just skip and not fail in case the
3838 * encryption involves a common limitation of cryptography hardwares and
3839 * an alternative implementation. */
3840 if( status == PSA_ERROR_NOT_SUPPORTED )
3841 {
3842 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3843 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3844 }
3845
3846 if( status != PSA_SUCCESS )
3847 {
3848 TEST_EQUAL( status, expected_result_arg );
3849 goto exit;
3850 }
3851
3852 if( nonce->len == 0 )
3853 {
Paul Elliott2df40052021-05-07 17:52:18 +01003854 status = psa_aead_generate_nonce( &operation, nonce_buffer,
3855 sizeof( nonce_buffer ),
Paul Elliott0023e0a2021-04-27 10:06:22 +01003856 &nonce_length );
3857 }
3858 else
3859 {
3860 nonce_length = nonce->len;
3861 status = psa_aead_set_nonce( &operation, nonce->x, nonce->len );
3862 }
3863
3864 if( status != PSA_SUCCESS )
3865 {
3866 TEST_EQUAL( status, expected_result_arg );
3867 goto exit;
3868 }
3869
3870#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
3871 if( operation.alg == PSA_ALG_GCM )
3872 {
3873 status = psa_aead_set_lengths( &operation, additional_data->len,
3874 ( input_data->len - tag_length ) );
3875
3876 if( status != PSA_SUCCESS )
3877 {
3878 TEST_EQUAL( status, expected_result_arg );
3879 goto exit;
3880 }
3881 }
3882#endif
3883
3884 if( ad_part_len != -1 )
3885 {
3886 part_offset = 0;
3887
3888 while( part_offset <= additional_data->len)
3889 {
3890 if( additional_data->len - part_offset < ( uint32_t ) ad_part_len )
3891 {
3892 part_length = additional_data->len - part_offset;
3893 }
3894 else
3895 {
3896 part_length = ad_part_len;
3897 }
3898
Paul Elliott2df40052021-05-07 17:52:18 +01003899 status = psa_aead_update_ad( &operation,
3900 additional_data->x + part_offset,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003901 part_length );
3902
3903 if( status != PSA_SUCCESS )
3904 {
3905 TEST_EQUAL( status, expected_result_arg );
3906 goto exit;
3907 }
3908
3909 part_offset += part_length;
3910 }
3911 }
3912 else
3913 {
Paul Elliott2df40052021-05-07 17:52:18 +01003914 status = psa_aead_update_ad( &operation, additional_data->x,
3915 additional_data->len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003916
3917 if( status != PSA_SUCCESS )
3918 {
3919 TEST_EQUAL( status, expected_result_arg );
3920 goto exit;
3921 }
3922 }
3923
3924 if( data_part_len != -1 )
3925 {
3926 /* Pass data in parts */
Paul Elliott2df40052021-05-07 17:52:18 +01003927 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
3928 ( size_t ) data_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003929
3930 ASSERT_ALLOC( part_data, part_data_size );
3931
3932 part_offset = 0;
3933
3934 while( part_offset <= input_data->len)
3935 {
Paul Elliott2df40052021-05-07 17:52:18 +01003936 if( (input_data->len - tag_length - part_offset ) <
3937 ( uint32_t ) data_part_len )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003938 {
3939 part_length = ( input_data->len - tag_length - part_offset );
3940 }
3941 else
3942 {
3943 part_length = data_part_len;
3944 }
3945
Paul Elliott2df40052021-05-07 17:52:18 +01003946 status = psa_aead_update( &operation,
3947 ( input_data->x + part_offset ),
3948 part_length, part_data,
3949 part_data_size, &output_part_length );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003950
3951 if( status != PSA_SUCCESS )
3952 {
3953 TEST_EQUAL( status, expected_result_arg );
3954 goto exit;
3955 }
3956
Paul Elliottac3c2002021-04-27 19:10:18 +01003957 if( output_data && output_part_length )
3958 {
Paul Elliott2df40052021-05-07 17:52:18 +01003959 memcpy( ( output_data + part_offset ), part_data,
3960 output_part_length );
Paul Elliottac3c2002021-04-27 19:10:18 +01003961 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003962
3963 part_offset += part_length;
3964 output_length += output_part_length;
3965 }
3966 }
3967 else
3968 {
3969 status = psa_aead_update( &operation, input_data->x,
3970 ( input_data->len - tag_length ), output_data,
3971 output_size, &output_length );
3972
3973 if( status != PSA_SUCCESS )
3974 {
3975 TEST_EQUAL( status, expected_result_arg );
3976 goto exit;
3977 }
3978 }
3979
3980 status = psa_aead_verify( &operation, final_data,
Paul Elliott388f6062021-06-03 19:19:49 +01003981 verify_output_size,
3982 &output_part_length,
3983 ( input_data->x + input_data->len - tag_length ),
3984 tag_length );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003985
3986 if( status != PSA_SUCCESS )
3987 {
3988 TEST_EQUAL( status, expected_result_arg );
3989 goto exit;
3990 }
3991
Paul Elliottac3c2002021-04-27 19:10:18 +01003992 if( output_data && output_part_length )
3993 {
Paul Elliott2df40052021-05-07 17:52:18 +01003994 memcpy( ( output_data + output_length ), final_data,
3995 output_part_length );
Paul Elliottac3c2002021-04-27 19:10:18 +01003996 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003997
3998 output_length += output_part_length;
3999
4000 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
4001 {
4002 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4003 * should be exact. */
4004 TEST_EQUAL( output_length,
Paul Elliott2df40052021-05-07 17:52:18 +01004005 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
4006 input_data->len ) );
Paul Elliott0023e0a2021-04-27 10:06:22 +01004007 TEST_ASSERT( output_length <=
4008 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
4009 }
4010
4011 if( expected_result == PSA_SUCCESS )
4012 ASSERT_COMPARE( expected_data->x, expected_data->len,
4013 output_data, output_length );
4014
4015exit:
4016 psa_destroy_key( key );
Paul Elliott16e6dcd2021-04-28 13:27:39 +01004017 psa_aead_abort( &operation );
Paul Elliott0023e0a2021-04-27 10:06:22 +01004018 mbedtls_free( output_data );
4019 mbedtls_free( part_data );
4020 mbedtls_free( final_data );
4021 PSA_DONE( );
4022}
4023/* END_CASE */
4024
4025/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004026void signature_size( int type_arg,
4027 int bits,
4028 int alg_arg,
4029 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004030{
4031 psa_key_type_t type = type_arg;
4032 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004033 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004034
Gilles Peskinefe11b722018-12-18 00:24:04 +01004035 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004036
Gilles Peskinee59236f2018-01-27 23:32:46 +01004037exit:
4038 ;
4039}
4040/* END_CASE */
4041
4042/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004043void sign_hash_deterministic( int key_type_arg, data_t *key_data,
4044 int alg_arg, data_t *input_data,
4045 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004046{
Ronald Cron5425a212020-08-04 14:58:35 +02004047 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004048 psa_key_type_t key_type = key_type_arg;
4049 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004050 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004051 unsigned char *signature = NULL;
4052 size_t signature_size;
4053 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004054 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004055
Gilles Peskine8817f612018-12-18 00:18:46 +01004056 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004057
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004058 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004059 psa_set_key_algorithm( &attributes, alg );
4060 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004061
Gilles Peskine049c7532019-05-15 20:22:09 +02004062 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004063 &key ) );
4064 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004065 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004066
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004067 /* Allocate a buffer which has the size advertized by the
4068 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004069 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004070 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004071 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004072 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004073 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004074
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004075 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004076 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004077 input_data->x, input_data->len,
4078 signature, signature_size,
4079 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004080 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004081 ASSERT_COMPARE( output_data->x, output_data->len,
4082 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004083
4084exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004085 /*
4086 * Key attributes may have been returned by psa_get_key_attributes()
4087 * thus reset them as required.
4088 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004089 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004090
Ronald Cron5425a212020-08-04 14:58:35 +02004091 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004092 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004093 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004094}
4095/* END_CASE */
4096
4097/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004098void sign_hash_fail( int key_type_arg, data_t *key_data,
4099 int alg_arg, data_t *input_data,
4100 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004101{
Ronald Cron5425a212020-08-04 14:58:35 +02004102 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004103 psa_key_type_t key_type = key_type_arg;
4104 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004105 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004106 psa_status_t actual_status;
4107 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004108 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004109 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004110 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004111
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004112 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004113
Gilles Peskine8817f612018-12-18 00:18:46 +01004114 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004115
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004116 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004117 psa_set_key_algorithm( &attributes, alg );
4118 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004119
Gilles Peskine049c7532019-05-15 20:22:09 +02004120 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004121 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004122
Ronald Cron5425a212020-08-04 14:58:35 +02004123 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004124 input_data->x, input_data->len,
4125 signature, signature_size,
4126 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004127 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004128 /* The value of *signature_length is unspecified on error, but
4129 * whatever it is, it should be less than signature_size, so that
4130 * if the caller tries to read *signature_length bytes without
4131 * checking the error code then they don't overflow a buffer. */
4132 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004133
4134exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004135 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004136 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004137 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004138 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004139}
4140/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004141
4142/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004143void sign_verify_hash( int key_type_arg, data_t *key_data,
4144 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02004145{
Ronald Cron5425a212020-08-04 14:58:35 +02004146 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004147 psa_key_type_t key_type = key_type_arg;
4148 psa_algorithm_t alg = alg_arg;
4149 size_t key_bits;
4150 unsigned char *signature = NULL;
4151 size_t signature_size;
4152 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004153 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004154
Gilles Peskine8817f612018-12-18 00:18:46 +01004155 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004156
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004157 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004158 psa_set_key_algorithm( &attributes, alg );
4159 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004160
Gilles Peskine049c7532019-05-15 20:22:09 +02004161 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004162 &key ) );
4163 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004164 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004165
4166 /* Allocate a buffer which has the size advertized by the
4167 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004168 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004169 key_bits, alg );
4170 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004171 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004172 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004173
4174 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004175 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004176 input_data->x, input_data->len,
4177 signature, signature_size,
4178 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004179 /* Check that the signature length looks sensible. */
4180 TEST_ASSERT( signature_length <= signature_size );
4181 TEST_ASSERT( signature_length > 0 );
4182
4183 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004184 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004185 input_data->x, input_data->len,
4186 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004187
4188 if( input_data->len != 0 )
4189 {
4190 /* Flip a bit in the input and verify that the signature is now
4191 * detected as invalid. Flip a bit at the beginning, not at the end,
4192 * because ECDSA may ignore the last few bits of the input. */
4193 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004194 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004195 input_data->x, input_data->len,
4196 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004197 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004198 }
4199
4200exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004201 /*
4202 * Key attributes may have been returned by psa_get_key_attributes()
4203 * thus reset them as required.
4204 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004205 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004206
Ronald Cron5425a212020-08-04 14:58:35 +02004207 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004208 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004209 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004210}
4211/* END_CASE */
4212
4213/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004214void verify_hash( int key_type_arg, data_t *key_data,
4215 int alg_arg, data_t *hash_data,
4216 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004217{
Ronald Cron5425a212020-08-04 14:58:35 +02004218 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004219 psa_key_type_t key_type = key_type_arg;
4220 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004221 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004222
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004223 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004224
Gilles Peskine8817f612018-12-18 00:18:46 +01004225 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004226
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004227 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004228 psa_set_key_algorithm( &attributes, alg );
4229 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004230
Gilles Peskine049c7532019-05-15 20:22:09 +02004231 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004232 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004233
Ronald Cron5425a212020-08-04 14:58:35 +02004234 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004235 hash_data->x, hash_data->len,
4236 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004237
itayzafrir5c753392018-05-08 11:18:38 +03004238exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004239 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004240 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004241 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004242}
4243/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004244
4245/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004246void verify_hash_fail( int key_type_arg, data_t *key_data,
4247 int alg_arg, data_t *hash_data,
4248 data_t *signature_data,
4249 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004250{
Ronald Cron5425a212020-08-04 14:58:35 +02004251 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004252 psa_key_type_t key_type = key_type_arg;
4253 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004254 psa_status_t actual_status;
4255 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004256 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004257
Gilles Peskine8817f612018-12-18 00:18:46 +01004258 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004259
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004260 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004261 psa_set_key_algorithm( &attributes, alg );
4262 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004263
Gilles Peskine049c7532019-05-15 20:22:09 +02004264 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004265 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004266
Ronald Cron5425a212020-08-04 14:58:35 +02004267 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004268 hash_data->x, hash_data->len,
4269 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004270 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004271
4272exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004273 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004274 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004275 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004276}
4277/* END_CASE */
4278
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004279/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02004280void sign_message_deterministic( int key_type_arg,
4281 data_t *key_data,
4282 int alg_arg,
4283 data_t *input_data,
4284 data_t *output_data )
4285{
4286 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4287 psa_key_type_t key_type = key_type_arg;
4288 psa_algorithm_t alg = alg_arg;
4289 size_t key_bits;
4290 unsigned char *signature = NULL;
4291 size_t signature_size;
4292 size_t signature_length = 0xdeadbeef;
4293 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4294
4295 PSA_ASSERT( psa_crypto_init( ) );
4296
4297 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4298 psa_set_key_algorithm( &attributes, alg );
4299 psa_set_key_type( &attributes, key_type );
4300
4301 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4302 &key ) );
4303 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4304 key_bits = psa_get_key_bits( &attributes );
4305
4306 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4307 TEST_ASSERT( signature_size != 0 );
4308 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4309 ASSERT_ALLOC( signature, signature_size );
4310
4311 PSA_ASSERT( psa_sign_message( key, alg,
4312 input_data->x, input_data->len,
4313 signature, signature_size,
4314 &signature_length ) );
4315
4316 ASSERT_COMPARE( output_data->x, output_data->len,
4317 signature, signature_length );
4318
4319exit:
4320 psa_reset_key_attributes( &attributes );
4321
4322 psa_destroy_key( key );
4323 mbedtls_free( signature );
4324 PSA_DONE( );
4325
4326}
4327/* END_CASE */
4328
4329/* BEGIN_CASE */
4330void sign_message_fail( int key_type_arg,
4331 data_t *key_data,
4332 int alg_arg,
4333 data_t *input_data,
4334 int signature_size_arg,
4335 int expected_status_arg )
4336{
4337 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4338 psa_key_type_t key_type = key_type_arg;
4339 psa_algorithm_t alg = alg_arg;
4340 size_t signature_size = signature_size_arg;
4341 psa_status_t actual_status;
4342 psa_status_t expected_status = expected_status_arg;
4343 unsigned char *signature = NULL;
4344 size_t signature_length = 0xdeadbeef;
4345 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4346
4347 ASSERT_ALLOC( signature, signature_size );
4348
4349 PSA_ASSERT( psa_crypto_init( ) );
4350
4351 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4352 psa_set_key_algorithm( &attributes, alg );
4353 psa_set_key_type( &attributes, key_type );
4354
4355 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4356 &key ) );
4357
4358 actual_status = psa_sign_message( key, alg,
4359 input_data->x, input_data->len,
4360 signature, signature_size,
4361 &signature_length );
4362 TEST_EQUAL( actual_status, expected_status );
4363 /* The value of *signature_length is unspecified on error, but
4364 * whatever it is, it should be less than signature_size, so that
4365 * if the caller tries to read *signature_length bytes without
4366 * checking the error code then they don't overflow a buffer. */
4367 TEST_ASSERT( signature_length <= signature_size );
4368
4369exit:
4370 psa_reset_key_attributes( &attributes );
4371 psa_destroy_key( key );
4372 mbedtls_free( signature );
4373 PSA_DONE( );
4374}
4375/* END_CASE */
4376
4377/* BEGIN_CASE */
4378void sign_verify_message( int key_type_arg,
4379 data_t *key_data,
4380 int alg_arg,
4381 data_t *input_data )
4382{
4383 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4384 psa_key_type_t key_type = key_type_arg;
4385 psa_algorithm_t alg = alg_arg;
4386 size_t key_bits;
4387 unsigned char *signature = NULL;
4388 size_t signature_size;
4389 size_t signature_length = 0xdeadbeef;
4390 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4391
4392 PSA_ASSERT( psa_crypto_init( ) );
4393
4394 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
4395 PSA_KEY_USAGE_VERIFY_MESSAGE );
4396 psa_set_key_algorithm( &attributes, alg );
4397 psa_set_key_type( &attributes, key_type );
4398
4399 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4400 &key ) );
4401 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4402 key_bits = psa_get_key_bits( &attributes );
4403
4404 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4405 TEST_ASSERT( signature_size != 0 );
4406 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4407 ASSERT_ALLOC( signature, signature_size );
4408
4409 PSA_ASSERT( psa_sign_message( key, alg,
4410 input_data->x, input_data->len,
4411 signature, signature_size,
4412 &signature_length ) );
4413 TEST_ASSERT( signature_length <= signature_size );
4414 TEST_ASSERT( signature_length > 0 );
4415
4416 PSA_ASSERT( psa_verify_message( key, alg,
4417 input_data->x, input_data->len,
4418 signature, signature_length ) );
4419
4420 if( input_data->len != 0 )
4421 {
4422 /* Flip a bit in the input and verify that the signature is now
4423 * detected as invalid. Flip a bit at the beginning, not at the end,
4424 * because ECDSA may ignore the last few bits of the input. */
4425 input_data->x[0] ^= 1;
4426 TEST_EQUAL( psa_verify_message( key, alg,
4427 input_data->x, input_data->len,
4428 signature, signature_length ),
4429 PSA_ERROR_INVALID_SIGNATURE );
4430 }
4431
4432exit:
4433 psa_reset_key_attributes( &attributes );
4434
4435 psa_destroy_key( key );
4436 mbedtls_free( signature );
4437 PSA_DONE( );
4438}
4439/* END_CASE */
4440
4441/* BEGIN_CASE */
4442void verify_message( int key_type_arg,
4443 data_t *key_data,
4444 int alg_arg,
4445 data_t *input_data,
4446 data_t *signature_data )
4447{
4448 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4449 psa_key_type_t key_type = key_type_arg;
4450 psa_algorithm_t alg = alg_arg;
4451 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4452
4453 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
4454
4455 PSA_ASSERT( psa_crypto_init( ) );
4456
4457 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4458 psa_set_key_algorithm( &attributes, alg );
4459 psa_set_key_type( &attributes, key_type );
4460
4461 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4462 &key ) );
4463
4464 PSA_ASSERT( psa_verify_message( key, alg,
4465 input_data->x, input_data->len,
4466 signature_data->x, signature_data->len ) );
4467
4468exit:
4469 psa_reset_key_attributes( &attributes );
4470 psa_destroy_key( key );
4471 PSA_DONE( );
4472}
4473/* END_CASE */
4474
4475/* BEGIN_CASE */
4476void verify_message_fail( int key_type_arg,
4477 data_t *key_data,
4478 int alg_arg,
4479 data_t *hash_data,
4480 data_t *signature_data,
4481 int expected_status_arg )
4482{
4483 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4484 psa_key_type_t key_type = key_type_arg;
4485 psa_algorithm_t alg = alg_arg;
4486 psa_status_t actual_status;
4487 psa_status_t expected_status = expected_status_arg;
4488 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4489
4490 PSA_ASSERT( psa_crypto_init( ) );
4491
4492 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4493 psa_set_key_algorithm( &attributes, alg );
4494 psa_set_key_type( &attributes, key_type );
4495
4496 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4497 &key ) );
4498
4499 actual_status = psa_verify_message( key, alg,
4500 hash_data->x, hash_data->len,
4501 signature_data->x,
4502 signature_data->len );
4503 TEST_EQUAL( actual_status, expected_status );
4504
4505exit:
4506 psa_reset_key_attributes( &attributes );
4507 psa_destroy_key( key );
4508 PSA_DONE( );
4509}
4510/* END_CASE */
4511
4512/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004513void asymmetric_encrypt( int key_type_arg,
4514 data_t *key_data,
4515 int alg_arg,
4516 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004517 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004518 int expected_output_length_arg,
4519 int expected_status_arg )
4520{
Ronald Cron5425a212020-08-04 14:58:35 +02004521 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004522 psa_key_type_t key_type = key_type_arg;
4523 psa_algorithm_t alg = alg_arg;
4524 size_t expected_output_length = expected_output_length_arg;
4525 size_t key_bits;
4526 unsigned char *output = NULL;
4527 size_t output_size;
4528 size_t output_length = ~0;
4529 psa_status_t actual_status;
4530 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004531 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004532
Gilles Peskine8817f612018-12-18 00:18:46 +01004533 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004534
Gilles Peskine656896e2018-06-29 19:12:28 +02004535 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004536 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4537 psa_set_key_algorithm( &attributes, alg );
4538 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004539 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004540 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004541
4542 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004543 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004544 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004545
Gilles Peskine656896e2018-06-29 19:12:28 +02004546 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004547 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004548 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004549
4550 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004551 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004552 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004553 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004554 output, output_size,
4555 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004556 TEST_EQUAL( actual_status, expected_status );
4557 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004558
Gilles Peskine68428122018-06-30 18:42:41 +02004559 /* If the label is empty, the test framework puts a non-null pointer
4560 * in label->x. Test that a null pointer works as well. */
4561 if( label->len == 0 )
4562 {
4563 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004564 if( output_size != 0 )
4565 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004566 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004567 input_data->x, input_data->len,
4568 NULL, label->len,
4569 output, output_size,
4570 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004571 TEST_EQUAL( actual_status, expected_status );
4572 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004573 }
4574
Gilles Peskine656896e2018-06-29 19:12:28 +02004575exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004576 /*
4577 * Key attributes may have been returned by psa_get_key_attributes()
4578 * thus reset them as required.
4579 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004580 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004581
Ronald Cron5425a212020-08-04 14:58:35 +02004582 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004583 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004584 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004585}
4586/* END_CASE */
4587
4588/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004589void asymmetric_encrypt_decrypt( int key_type_arg,
4590 data_t *key_data,
4591 int alg_arg,
4592 data_t *input_data,
4593 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004594{
Ronald Cron5425a212020-08-04 14:58:35 +02004595 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004596 psa_key_type_t key_type = key_type_arg;
4597 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004598 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004599 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004600 size_t output_size;
4601 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004602 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004603 size_t output2_size;
4604 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004605 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004606
Gilles Peskine8817f612018-12-18 00:18:46 +01004607 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004608
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004609 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4610 psa_set_key_algorithm( &attributes, alg );
4611 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004612
Gilles Peskine049c7532019-05-15 20:22:09 +02004613 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004614 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004615
4616 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004617 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004618 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004619
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004620 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004621 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004622 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01004623
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004624 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01004625 TEST_ASSERT( output2_size <=
4626 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
4627 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004628 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004629
Gilles Peskineeebd7382018-06-08 18:11:54 +02004630 /* We test encryption by checking that encrypt-then-decrypt gives back
4631 * the original plaintext because of the non-optional random
4632 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004633 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004634 input_data->x, input_data->len,
4635 label->x, label->len,
4636 output, output_size,
4637 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004638 /* We don't know what ciphertext length to expect, but check that
4639 * it looks sensible. */
4640 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004641
Ronald Cron5425a212020-08-04 14:58:35 +02004642 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004643 output, output_length,
4644 label->x, label->len,
4645 output2, output2_size,
4646 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004647 ASSERT_COMPARE( input_data->x, input_data->len,
4648 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004649
4650exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004651 /*
4652 * Key attributes may have been returned by psa_get_key_attributes()
4653 * thus reset them as required.
4654 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004655 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004656
Ronald Cron5425a212020-08-04 14:58:35 +02004657 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004658 mbedtls_free( output );
4659 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004660 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004661}
4662/* END_CASE */
4663
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004664/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004665void asymmetric_decrypt( int key_type_arg,
4666 data_t *key_data,
4667 int alg_arg,
4668 data_t *input_data,
4669 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004670 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004671{
Ronald Cron5425a212020-08-04 14:58:35 +02004672 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004673 psa_key_type_t key_type = key_type_arg;
4674 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004675 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004676 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004677 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004678 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004679 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004680
Gilles Peskine8817f612018-12-18 00:18:46 +01004681 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004682
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004683 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4684 psa_set_key_algorithm( &attributes, alg );
4685 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004686
Gilles Peskine049c7532019-05-15 20:22:09 +02004687 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004688 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004689
gabor-mezei-armceface22021-01-21 12:26:17 +01004690 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4691 key_bits = psa_get_key_bits( &attributes );
4692
4693 /* Determine the maximum ciphertext length */
4694 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
4695 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
4696 ASSERT_ALLOC( output, output_size );
4697
Ronald Cron5425a212020-08-04 14:58:35 +02004698 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004699 input_data->x, input_data->len,
4700 label->x, label->len,
4701 output,
4702 output_size,
4703 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004704 ASSERT_COMPARE( expected_data->x, expected_data->len,
4705 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004706
Gilles Peskine68428122018-06-30 18:42:41 +02004707 /* If the label is empty, the test framework puts a non-null pointer
4708 * in label->x. Test that a null pointer works as well. */
4709 if( label->len == 0 )
4710 {
4711 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004712 if( output_size != 0 )
4713 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004714 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004715 input_data->x, input_data->len,
4716 NULL, label->len,
4717 output,
4718 output_size,
4719 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004720 ASSERT_COMPARE( expected_data->x, expected_data->len,
4721 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004722 }
4723
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004724exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004725 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004726 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004727 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004728 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004729}
4730/* END_CASE */
4731
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004732/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004733void asymmetric_decrypt_fail( int key_type_arg,
4734 data_t *key_data,
4735 int alg_arg,
4736 data_t *input_data,
4737 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004738 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004739 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004740{
Ronald Cron5425a212020-08-04 14:58:35 +02004741 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004742 psa_key_type_t key_type = key_type_arg;
4743 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004744 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004745 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004746 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004747 psa_status_t actual_status;
4748 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004749 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004750
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004751 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004752
Gilles Peskine8817f612018-12-18 00:18:46 +01004753 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004754
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004755 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4756 psa_set_key_algorithm( &attributes, alg );
4757 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004758
Gilles Peskine049c7532019-05-15 20:22:09 +02004759 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004760 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004761
Ronald Cron5425a212020-08-04 14:58:35 +02004762 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004763 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004764 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004765 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004766 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004767 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004768 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004769
Gilles Peskine68428122018-06-30 18:42:41 +02004770 /* If the label is empty, the test framework puts a non-null pointer
4771 * in label->x. Test that a null pointer works as well. */
4772 if( label->len == 0 )
4773 {
4774 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004775 if( output_size != 0 )
4776 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004777 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004778 input_data->x, input_data->len,
4779 NULL, label->len,
4780 output, output_size,
4781 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004782 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004783 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004784 }
4785
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004786exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004787 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004788 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004789 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004790 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004791}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004792/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004793
4794/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004795void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004796{
4797 /* Test each valid way of initializing the object, except for `= {0}`, as
4798 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4799 * though it's OK by the C standard. We could test for this, but we'd need
4800 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004801 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004802 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4803 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4804 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004805
4806 memset( &zero, 0, sizeof( zero ) );
4807
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004808 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004809 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004810 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004811 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004812 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004813 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004814 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004815
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004816 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004817 PSA_ASSERT( psa_key_derivation_abort(&func) );
4818 PSA_ASSERT( psa_key_derivation_abort(&init) );
4819 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004820}
4821/* END_CASE */
4822
Janos Follath16de4a42019-06-13 16:32:24 +01004823/* BEGIN_CASE */
4824void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004825{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004826 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004827 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004828 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004829
Gilles Peskine8817f612018-12-18 00:18:46 +01004830 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004831
Janos Follath16de4a42019-06-13 16:32:24 +01004832 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004833 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004834
4835exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004836 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004837 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004838}
4839/* END_CASE */
4840
Janos Follathaf3c2a02019-06-12 12:34:34 +01004841/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004842void derive_set_capacity( int alg_arg, int capacity_arg,
4843 int expected_status_arg )
4844{
4845 psa_algorithm_t alg = alg_arg;
4846 size_t capacity = capacity_arg;
4847 psa_status_t expected_status = expected_status_arg;
4848 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4849
4850 PSA_ASSERT( psa_crypto_init( ) );
4851
4852 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4853
4854 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4855 expected_status );
4856
4857exit:
4858 psa_key_derivation_abort( &operation );
4859 PSA_DONE( );
4860}
4861/* END_CASE */
4862
4863/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004864void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004865 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004866 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004867 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004868 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004869 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004870 int expected_status_arg3,
4871 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004872{
4873 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004874 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4875 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004876 psa_status_t expected_statuses[] = {expected_status_arg1,
4877 expected_status_arg2,
4878 expected_status_arg3};
4879 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004880 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4881 MBEDTLS_SVC_KEY_ID_INIT,
4882 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004883 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4884 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4885 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004886 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004887 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004888 psa_status_t expected_output_status = expected_output_status_arg;
4889 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004890
4891 PSA_ASSERT( psa_crypto_init( ) );
4892
4893 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4894 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004895
4896 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4897
4898 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4899 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004900 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004901 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004902 psa_set_key_type( &attributes, key_types[i] );
4903 PSA_ASSERT( psa_import_key( &attributes,
4904 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004905 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004906 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4907 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4908 {
4909 // When taking a private key as secret input, use key agreement
4910 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004911 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4912 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004913 expected_statuses[i] );
4914 }
4915 else
4916 {
4917 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004918 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004919 expected_statuses[i] );
4920 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004921 }
4922 else
4923 {
4924 TEST_EQUAL( psa_key_derivation_input_bytes(
4925 &operation, steps[i],
4926 inputs[i]->x, inputs[i]->len ),
4927 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004928 }
4929 }
4930
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004931 if( output_key_type != PSA_KEY_TYPE_NONE )
4932 {
4933 psa_reset_key_attributes( &attributes );
4934 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4935 psa_set_key_bits( &attributes, 8 );
4936 actual_output_status =
4937 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004938 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004939 }
4940 else
4941 {
4942 uint8_t buffer[1];
4943 actual_output_status =
4944 psa_key_derivation_output_bytes( &operation,
4945 buffer, sizeof( buffer ) );
4946 }
4947 TEST_EQUAL( actual_output_status, expected_output_status );
4948
Janos Follathaf3c2a02019-06-12 12:34:34 +01004949exit:
4950 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004951 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4952 psa_destroy_key( keys[i] );
4953 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004954 PSA_DONE( );
4955}
4956/* END_CASE */
4957
Janos Follathd958bb72019-07-03 15:02:16 +01004958/* BEGIN_CASE */
4959void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004960{
Janos Follathd958bb72019-07-03 15:02:16 +01004961 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004962 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004963 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004964 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004965 unsigned char input1[] = "Input 1";
4966 size_t input1_length = sizeof( input1 );
4967 unsigned char input2[] = "Input 2";
4968 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004969 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004970 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004971 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4972 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4973 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004974 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004975
Gilles Peskine8817f612018-12-18 00:18:46 +01004976 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004977
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004978 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4979 psa_set_key_algorithm( &attributes, alg );
4980 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004981
Gilles Peskine73676cb2019-05-15 20:15:10 +02004982 PSA_ASSERT( psa_import_key( &attributes,
4983 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004984 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004985
4986 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004987 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4988 input1, input1_length,
4989 input2, input2_length,
4990 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004991 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004992
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004993 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004994 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004995 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004996
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004997 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004998
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004999 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02005000 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005001
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005002exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005003 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005004 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005005 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005006}
5007/* END_CASE */
5008
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005009/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005010void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005011{
5012 uint8_t output_buffer[16];
5013 size_t buffer_size = 16;
5014 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005015 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005016
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005017 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5018 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005019 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005020
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005021 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005022 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005023
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005024 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005025
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005026 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5027 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005028 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005029
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005030 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005031 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005032
5033exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005034 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005035}
5036/* END_CASE */
5037
5038/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005039void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02005040 int step1_arg, data_t *input1,
5041 int step2_arg, data_t *input2,
5042 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005043 int requested_capacity_arg,
5044 data_t *expected_output1,
5045 data_t *expected_output2 )
5046{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005047 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005048 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5049 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005050 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5051 MBEDTLS_SVC_KEY_ID_INIT,
5052 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005053 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005054 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005055 uint8_t *expected_outputs[2] =
5056 {expected_output1->x, expected_output2->x};
5057 size_t output_sizes[2] =
5058 {expected_output1->len, expected_output2->len};
5059 size_t output_buffer_size = 0;
5060 uint8_t *output_buffer = NULL;
5061 size_t expected_capacity;
5062 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005063 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005064 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005065 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005066
5067 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5068 {
5069 if( output_sizes[i] > output_buffer_size )
5070 output_buffer_size = output_sizes[i];
5071 if( output_sizes[i] == 0 )
5072 expected_outputs[i] = NULL;
5073 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005074 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005075 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005076
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005077 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5078 psa_set_key_algorithm( &attributes, alg );
5079 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005080
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005081 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005082 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5083 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5084 requested_capacity ) );
5085 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005086 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005087 switch( steps[i] )
5088 {
5089 case 0:
5090 break;
5091 case PSA_KEY_DERIVATION_INPUT_SECRET:
5092 PSA_ASSERT( psa_import_key( &attributes,
5093 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005094 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01005095
5096 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5097 {
5098 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
5099 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
5100 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
5101 }
5102
Gilles Peskine1468da72019-05-29 17:35:49 +02005103 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005104 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005105 break;
5106 default:
5107 PSA_ASSERT( psa_key_derivation_input_bytes(
5108 &operation, steps[i],
5109 inputs[i]->x, inputs[i]->len ) );
5110 break;
5111 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005112 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005113
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005114 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005115 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005116 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005117 expected_capacity = requested_capacity;
5118
5119 /* Expansion phase. */
5120 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5121 {
5122 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005123 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005124 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005125 if( expected_capacity == 0 && output_sizes[i] == 0 )
5126 {
5127 /* Reading 0 bytes when 0 bytes are available can go either way. */
5128 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005129 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005130 continue;
5131 }
5132 else if( expected_capacity == 0 ||
5133 output_sizes[i] > expected_capacity )
5134 {
5135 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005136 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005137 expected_capacity = 0;
5138 continue;
5139 }
5140 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005141 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005142 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005143 ASSERT_COMPARE( output_buffer, output_sizes[i],
5144 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005145 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005146 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005147 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005148 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005149 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005150 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005151 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005152
5153exit:
5154 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005155 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005156 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5157 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005158 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005159}
5160/* END_CASE */
5161
5162/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005163void derive_full( int alg_arg,
5164 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005165 data_t *input1,
5166 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005167 int requested_capacity_arg )
5168{
Ronald Cron5425a212020-08-04 14:58:35 +02005169 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005170 psa_algorithm_t alg = alg_arg;
5171 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005172 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005173 unsigned char output_buffer[16];
5174 size_t expected_capacity = requested_capacity;
5175 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005176 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005177
Gilles Peskine8817f612018-12-18 00:18:46 +01005178 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005179
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005180 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5181 psa_set_key_algorithm( &attributes, alg );
5182 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005183
Gilles Peskine049c7532019-05-15 20:22:09 +02005184 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005185 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005186
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005187 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5188 input1->x, input1->len,
5189 input2->x, input2->len,
5190 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01005191 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005192
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005193 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005194 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005195 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005196
5197 /* Expansion phase. */
5198 while( current_capacity > 0 )
5199 {
5200 size_t read_size = sizeof( output_buffer );
5201 if( read_size > current_capacity )
5202 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005203 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005204 output_buffer,
5205 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005206 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005207 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005208 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005209 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005210 }
5211
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005212 /* Check that the operation refuses to go over capacity. */
5213 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005214 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005215
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005216 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005217
5218exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005219 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005220 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005221 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005222}
5223/* END_CASE */
5224
Janos Follathe60c9052019-07-03 13:51:30 +01005225/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005226void derive_key_exercise( int alg_arg,
5227 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005228 data_t *input1,
5229 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005230 int derived_type_arg,
5231 int derived_bits_arg,
5232 int derived_usage_arg,
5233 int derived_alg_arg )
5234{
Ronald Cron5425a212020-08-04 14:58:35 +02005235 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5236 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005237 psa_algorithm_t alg = alg_arg;
5238 psa_key_type_t derived_type = derived_type_arg;
5239 size_t derived_bits = derived_bits_arg;
5240 psa_key_usage_t derived_usage = derived_usage_arg;
5241 psa_algorithm_t derived_alg = derived_alg_arg;
5242 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005243 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005244 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005245 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005246
Gilles Peskine8817f612018-12-18 00:18:46 +01005247 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005248
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005249 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5250 psa_set_key_algorithm( &attributes, alg );
5251 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005252 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005253 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005254
5255 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005256 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5257 input1->x, input1->len,
5258 input2->x, input2->len,
5259 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01005260 goto exit;
5261
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005262 psa_set_key_usage_flags( &attributes, derived_usage );
5263 psa_set_key_algorithm( &attributes, derived_alg );
5264 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005265 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005266 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005267 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005268
5269 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005270 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005271 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5272 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005273
5274 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005275 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005276 goto exit;
5277
5278exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005279 /*
5280 * Key attributes may have been returned by psa_get_key_attributes()
5281 * thus reset them as required.
5282 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005283 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005284
5285 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005286 psa_destroy_key( base_key );
5287 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005288 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005289}
5290/* END_CASE */
5291
Janos Follath42fd8882019-07-03 14:17:09 +01005292/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005293void derive_key_export( int alg_arg,
5294 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005295 data_t *input1,
5296 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005297 int bytes1_arg,
5298 int bytes2_arg )
5299{
Ronald Cron5425a212020-08-04 14:58:35 +02005300 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5301 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005302 psa_algorithm_t alg = alg_arg;
5303 size_t bytes1 = bytes1_arg;
5304 size_t bytes2 = bytes2_arg;
5305 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005306 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005307 uint8_t *output_buffer = NULL;
5308 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005309 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5310 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005311 size_t length;
5312
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005313 ASSERT_ALLOC( output_buffer, capacity );
5314 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005315 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005316
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005317 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5318 psa_set_key_algorithm( &base_attributes, alg );
5319 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005320 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005321 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005322
5323 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005324 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5325 input1->x, input1->len,
5326 input2->x, input2->len,
5327 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005328 goto exit;
5329
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005330 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005331 output_buffer,
5332 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005333 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005334
5335 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005336 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5337 input1->x, input1->len,
5338 input2->x, input2->len,
5339 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005340 goto exit;
5341
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005342 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5343 psa_set_key_algorithm( &derived_attributes, 0 );
5344 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005345 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005346 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005347 &derived_key ) );
5348 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005349 export_buffer, bytes1,
5350 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005351 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005352 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005353 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005354 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005355 &derived_key ) );
5356 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005357 export_buffer + bytes1, bytes2,
5358 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005359 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005360
5361 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005362 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5363 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005364
5365exit:
5366 mbedtls_free( output_buffer );
5367 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005368 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005369 psa_destroy_key( base_key );
5370 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005371 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005372}
5373/* END_CASE */
5374
5375/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005376void derive_key( int alg_arg,
5377 data_t *key_data, data_t *input1, data_t *input2,
5378 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005379 int expected_status_arg,
5380 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02005381{
Ronald Cron5425a212020-08-04 14:58:35 +02005382 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5383 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005384 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005385 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005386 size_t bits = bits_arg;
5387 psa_status_t expected_status = expected_status_arg;
5388 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5389 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5390 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5391
5392 PSA_ASSERT( psa_crypto_init( ) );
5393
5394 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5395 psa_set_key_algorithm( &base_attributes, alg );
5396 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5397 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005398 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005399
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005400 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5401 input1->x, input1->len,
5402 input2->x, input2->len,
5403 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02005404 goto exit;
5405
5406 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5407 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005408 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005409 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01005410
5411 psa_status_t status =
5412 psa_key_derivation_output_key( &derived_attributes,
5413 &operation,
5414 &derived_key );
5415 if( is_large_output > 0 )
5416 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5417 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02005418
5419exit:
5420 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005421 psa_destroy_key( base_key );
5422 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005423 PSA_DONE( );
5424}
5425/* END_CASE */
5426
5427/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005428void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005429 int our_key_type_arg, int our_key_alg_arg,
5430 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005431 int expected_status_arg )
5432{
Ronald Cron5425a212020-08-04 14:58:35 +02005433 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005434 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005435 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005436 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005437 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005438 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005439 psa_status_t expected_status = expected_status_arg;
5440 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005441
Gilles Peskine8817f612018-12-18 00:18:46 +01005442 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005443
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005444 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005445 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005446 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005447 PSA_ASSERT( psa_import_key( &attributes,
5448 our_key_data->x, our_key_data->len,
5449 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005450
Gilles Peskine77f40d82019-04-11 21:27:06 +02005451 /* The tests currently include inputs that should fail at either step.
5452 * Test cases that fail at the setup step should be changed to call
5453 * key_derivation_setup instead, and this function should be renamed
5454 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005455 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005456 if( status == PSA_SUCCESS )
5457 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005458 TEST_EQUAL( psa_key_derivation_key_agreement(
5459 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5460 our_key,
5461 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005462 expected_status );
5463 }
5464 else
5465 {
5466 TEST_ASSERT( status == expected_status );
5467 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005468
5469exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005470 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005471 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005472 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005473}
5474/* END_CASE */
5475
5476/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005477void raw_key_agreement( int alg_arg,
5478 int our_key_type_arg, data_t *our_key_data,
5479 data_t *peer_key_data,
5480 data_t *expected_output )
5481{
Ronald Cron5425a212020-08-04 14:58:35 +02005482 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005483 psa_algorithm_t alg = alg_arg;
5484 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005485 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005486 unsigned char *output = NULL;
5487 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01005488 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005489
5490 ASSERT_ALLOC( output, expected_output->len );
5491 PSA_ASSERT( psa_crypto_init( ) );
5492
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005493 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5494 psa_set_key_algorithm( &attributes, alg );
5495 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005496 PSA_ASSERT( psa_import_key( &attributes,
5497 our_key_data->x, our_key_data->len,
5498 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005499
gabor-mezei-armceface22021-01-21 12:26:17 +01005500 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
5501 key_bits = psa_get_key_bits( &attributes );
5502
Gilles Peskinebe697d82019-05-16 18:00:41 +02005503 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5504 peer_key_data->x, peer_key_data->len,
5505 output, expected_output->len,
5506 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005507 ASSERT_COMPARE( output, output_length,
5508 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01005509 TEST_ASSERT( output_length <=
5510 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
5511 TEST_ASSERT( output_length <=
5512 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005513
5514exit:
5515 mbedtls_free( output );
5516 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005517 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005518}
5519/* END_CASE */
5520
5521/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005522void key_agreement_capacity( int alg_arg,
5523 int our_key_type_arg, data_t *our_key_data,
5524 data_t *peer_key_data,
5525 int expected_capacity_arg )
5526{
Ronald Cron5425a212020-08-04 14:58:35 +02005527 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005528 psa_algorithm_t alg = alg_arg;
5529 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005530 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005531 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005532 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005533 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005534
Gilles Peskine8817f612018-12-18 00:18:46 +01005535 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005536
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005537 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5538 psa_set_key_algorithm( &attributes, alg );
5539 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005540 PSA_ASSERT( psa_import_key( &attributes,
5541 our_key_data->x, our_key_data->len,
5542 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005543
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005544 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005545 PSA_ASSERT( psa_key_derivation_key_agreement(
5546 &operation,
5547 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5548 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005549 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5550 {
5551 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005552 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005553 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005554 NULL, 0 ) );
5555 }
Gilles Peskine59685592018-09-18 12:11:34 +02005556
Gilles Peskinebf491972018-10-25 22:36:12 +02005557 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005558 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005559 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005560 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005561
Gilles Peskinebf491972018-10-25 22:36:12 +02005562 /* Test the actual capacity by reading the output. */
5563 while( actual_capacity > sizeof( output ) )
5564 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005565 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005566 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005567 actual_capacity -= sizeof( output );
5568 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005569 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005570 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005571 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005572 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005573
Gilles Peskine59685592018-09-18 12:11:34 +02005574exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005575 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005576 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005577 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005578}
5579/* END_CASE */
5580
5581/* BEGIN_CASE */
5582void key_agreement_output( int alg_arg,
5583 int our_key_type_arg, data_t *our_key_data,
5584 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005585 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005586{
Ronald Cron5425a212020-08-04 14:58:35 +02005587 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005588 psa_algorithm_t alg = alg_arg;
5589 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005590 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005591 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005592 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005593
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005594 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5595 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005596
Gilles Peskine8817f612018-12-18 00:18:46 +01005597 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005598
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005599 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5600 psa_set_key_algorithm( &attributes, alg );
5601 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005602 PSA_ASSERT( psa_import_key( &attributes,
5603 our_key_data->x, our_key_data->len,
5604 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005605
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005606 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005607 PSA_ASSERT( psa_key_derivation_key_agreement(
5608 &operation,
5609 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5610 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005611 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5612 {
5613 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005614 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005615 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005616 NULL, 0 ) );
5617 }
Gilles Peskine59685592018-09-18 12:11:34 +02005618
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005619 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005620 actual_output,
5621 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005622 ASSERT_COMPARE( actual_output, expected_output1->len,
5623 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005624 if( expected_output2->len != 0 )
5625 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005626 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005627 actual_output,
5628 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005629 ASSERT_COMPARE( actual_output, expected_output2->len,
5630 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005631 }
Gilles Peskine59685592018-09-18 12:11:34 +02005632
5633exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005634 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005635 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005636 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005637 mbedtls_free( actual_output );
5638}
5639/* END_CASE */
5640
5641/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005642void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005643{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005644 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005645 unsigned char *output = NULL;
5646 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005647 size_t i;
5648 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005649
Simon Butcher49f8e312020-03-03 15:51:50 +00005650 TEST_ASSERT( bytes_arg >= 0 );
5651
Gilles Peskine91892022021-02-08 19:50:26 +01005652 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005653 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005654
Gilles Peskine8817f612018-12-18 00:18:46 +01005655 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005656
Gilles Peskinea50d7392018-06-21 10:22:13 +02005657 /* Run several times, to ensure that every output byte will be
5658 * nonzero at least once with overwhelming probability
5659 * (2^(-8*number_of_runs)). */
5660 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005661 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005662 if( bytes != 0 )
5663 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005664 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005665
Gilles Peskinea50d7392018-06-21 10:22:13 +02005666 for( i = 0; i < bytes; i++ )
5667 {
5668 if( output[i] != 0 )
5669 ++changed[i];
5670 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005671 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005672
5673 /* Check that every byte was changed to nonzero at least once. This
5674 * validates that psa_generate_random is overwriting every byte of
5675 * the output buffer. */
5676 for( i = 0; i < bytes; i++ )
5677 {
5678 TEST_ASSERT( changed[i] != 0 );
5679 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005680
5681exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005682 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005683 mbedtls_free( output );
5684 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005685}
5686/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005687
5688/* BEGIN_CASE */
5689void generate_key( int type_arg,
5690 int bits_arg,
5691 int usage_arg,
5692 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005693 int expected_status_arg,
5694 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005695{
Ronald Cron5425a212020-08-04 14:58:35 +02005696 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005697 psa_key_type_t type = type_arg;
5698 psa_key_usage_t usage = usage_arg;
5699 size_t bits = bits_arg;
5700 psa_algorithm_t alg = alg_arg;
5701 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005702 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005703 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005704
Gilles Peskine8817f612018-12-18 00:18:46 +01005705 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005706
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005707 psa_set_key_usage_flags( &attributes, usage );
5708 psa_set_key_algorithm( &attributes, alg );
5709 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005710 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005711
5712 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005713 psa_status_t status = psa_generate_key( &attributes, &key );
5714
5715 if( is_large_key > 0 )
5716 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5717 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005718 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005719 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005720
5721 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005722 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005723 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5724 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005725
Gilles Peskine818ca122018-06-20 18:16:48 +02005726 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005727 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005728 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005729
5730exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005731 /*
5732 * Key attributes may have been returned by psa_get_key_attributes()
5733 * thus reset them as required.
5734 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005735 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005736
Ronald Cron5425a212020-08-04 14:58:35 +02005737 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005738 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005739}
5740/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005741
Ronald Cronee414c72021-03-18 18:50:08 +01005742/* 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 +02005743void generate_key_rsa( int bits_arg,
5744 data_t *e_arg,
5745 int expected_status_arg )
5746{
Ronald Cron5425a212020-08-04 14:58:35 +02005747 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005748 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005749 size_t bits = bits_arg;
5750 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5751 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5752 psa_status_t expected_status = expected_status_arg;
5753 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5754 uint8_t *exported = NULL;
5755 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005756 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005757 size_t exported_length = SIZE_MAX;
5758 uint8_t *e_read_buffer = NULL;
5759 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005760 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005761 size_t e_read_length = SIZE_MAX;
5762
5763 if( e_arg->len == 0 ||
5764 ( e_arg->len == 3 &&
5765 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5766 {
5767 is_default_public_exponent = 1;
5768 e_read_size = 0;
5769 }
5770 ASSERT_ALLOC( e_read_buffer, e_read_size );
5771 ASSERT_ALLOC( exported, exported_size );
5772
5773 PSA_ASSERT( psa_crypto_init( ) );
5774
5775 psa_set_key_usage_flags( &attributes, usage );
5776 psa_set_key_algorithm( &attributes, alg );
5777 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5778 e_arg->x, e_arg->len ) );
5779 psa_set_key_bits( &attributes, bits );
5780
5781 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005782 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005783 if( expected_status != PSA_SUCCESS )
5784 goto exit;
5785
5786 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005787 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005788 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5789 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5790 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5791 e_read_buffer, e_read_size,
5792 &e_read_length ) );
5793 if( is_default_public_exponent )
5794 TEST_EQUAL( e_read_length, 0 );
5795 else
5796 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5797
5798 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005799 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005800 goto exit;
5801
5802 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005803 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005804 exported, exported_size,
5805 &exported_length ) );
5806 {
5807 uint8_t *p = exported;
5808 uint8_t *end = exported + exported_length;
5809 size_t len;
5810 /* RSAPublicKey ::= SEQUENCE {
5811 * modulus INTEGER, -- n
5812 * publicExponent INTEGER } -- e
5813 */
5814 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005815 MBEDTLS_ASN1_SEQUENCE |
5816 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005817 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005818 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5819 MBEDTLS_ASN1_INTEGER ) );
5820 if( len >= 1 && p[0] == 0 )
5821 {
5822 ++p;
5823 --len;
5824 }
5825 if( e_arg->len == 0 )
5826 {
5827 TEST_EQUAL( len, 3 );
5828 TEST_EQUAL( p[0], 1 );
5829 TEST_EQUAL( p[1], 0 );
5830 TEST_EQUAL( p[2], 1 );
5831 }
5832 else
5833 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5834 }
5835
5836exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005837 /*
5838 * Key attributes may have been returned by psa_get_key_attributes() or
5839 * set by psa_set_key_domain_parameters() thus reset them as required.
5840 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005841 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005842
Ronald Cron5425a212020-08-04 14:58:35 +02005843 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005844 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005845 mbedtls_free( e_read_buffer );
5846 mbedtls_free( exported );
5847}
5848/* END_CASE */
5849
Darryl Greend49a4992018-06-18 17:27:26 +01005850/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005851void persistent_key_load_key_from_storage( data_t *data,
5852 int type_arg, int bits_arg,
5853 int usage_flags_arg, int alg_arg,
5854 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005855{
Ronald Cron71016a92020-08-28 19:01:50 +02005856 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005857 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005858 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5859 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005860 psa_key_type_t type = type_arg;
5861 size_t bits = bits_arg;
5862 psa_key_usage_t usage_flags = usage_flags_arg;
5863 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005864 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005865 unsigned char *first_export = NULL;
5866 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005867 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005868 size_t first_exported_length;
5869 size_t second_exported_length;
5870
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005871 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5872 {
5873 ASSERT_ALLOC( first_export, export_size );
5874 ASSERT_ALLOC( second_export, export_size );
5875 }
Darryl Greend49a4992018-06-18 17:27:26 +01005876
Gilles Peskine8817f612018-12-18 00:18:46 +01005877 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005878
Gilles Peskinec87af662019-05-15 16:12:22 +02005879 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005880 psa_set_key_usage_flags( &attributes, usage_flags );
5881 psa_set_key_algorithm( &attributes, alg );
5882 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005883 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005884
Darryl Green0c6575a2018-11-07 16:05:30 +00005885 switch( generation_method )
5886 {
5887 case IMPORT_KEY:
5888 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005889 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005890 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005891 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005892
Darryl Green0c6575a2018-11-07 16:05:30 +00005893 case GENERATE_KEY:
5894 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005895 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005896 break;
5897
5898 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005899#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005900 {
5901 /* Create base key */
5902 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5903 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5904 psa_set_key_usage_flags( &base_attributes,
5905 PSA_KEY_USAGE_DERIVE );
5906 psa_set_key_algorithm( &base_attributes, derive_alg );
5907 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005908 PSA_ASSERT( psa_import_key( &base_attributes,
5909 data->x, data->len,
5910 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005911 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005912 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005913 PSA_ASSERT( psa_key_derivation_input_key(
5914 &operation,
5915 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005916 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005917 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005918 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005919 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5920 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005921 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005922 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005923 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005924 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005925 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005926#else
5927 TEST_ASSUME( ! "KDF not supported in this configuration" );
5928#endif
5929 break;
5930
5931 default:
5932 TEST_ASSERT( ! "generation_method not implemented in test" );
5933 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005934 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005935 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005936
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005937 /* Export the key if permitted by the key policy. */
5938 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5939 {
Ronald Cron5425a212020-08-04 14:58:35 +02005940 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005941 first_export, export_size,
5942 &first_exported_length ) );
5943 if( generation_method == IMPORT_KEY )
5944 ASSERT_COMPARE( data->x, data->len,
5945 first_export, first_exported_length );
5946 }
Darryl Greend49a4992018-06-18 17:27:26 +01005947
5948 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005949 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005950 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005951 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005952
Darryl Greend49a4992018-06-18 17:27:26 +01005953 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005954 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005955 TEST_ASSERT( mbedtls_svc_key_id_equal(
5956 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005957 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5958 PSA_KEY_LIFETIME_PERSISTENT );
5959 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5960 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5961 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5962 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005963
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005964 /* Export the key again if permitted by the key policy. */
5965 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005966 {
Ronald Cron5425a212020-08-04 14:58:35 +02005967 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005968 second_export, export_size,
5969 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005970 ASSERT_COMPARE( first_export, first_exported_length,
5971 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005972 }
5973
5974 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005975 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005976 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005977
5978exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005979 /*
5980 * Key attributes may have been returned by psa_get_key_attributes()
5981 * thus reset them as required.
5982 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005983 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005984
Darryl Greend49a4992018-06-18 17:27:26 +01005985 mbedtls_free( first_export );
5986 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005987 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005988 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005989 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005990 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005991}
5992/* END_CASE */