blob: c5fa389c3e29bcb6c0950a5e7a62a256ada62bc9 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010018
Gilles Peskine4023c012021-05-27 13:21:20 +020019/* If this comes up, it's a bug in the test code or in the test data. */
20#define UNUSED 0xdeadbeef
21
Dave Rodgman647791d2021-06-23 12:49:59 +010022/* Assert that an operation is (not) active.
23 * This serves as a proxy for checking if the operation is aborted. */
24#define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 )
25#define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 )
26
Jaeden Amerof24c7f82018-06-27 17:20:43 +010027/** An invalid export length that will never be set by psa_export_key(). */
28static const size_t INVALID_EXPORT_LENGTH = ~0U;
29
Gilles Peskinea7aa4422018-08-14 15:17:54 +020030/** Test if a buffer contains a constant byte value.
31 *
32 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020033 *
34 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020035 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020036 * \param size Size of the buffer in bytes.
37 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020038 * \return 1 if the buffer is all-bits-zero.
39 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020040 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042{
43 size_t i;
44 for( i = 0; i < size; i++ )
45 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020046 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020047 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020048 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020049 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020050}
Gilles Peskine818ca122018-06-20 18:16:48 +020051
Gilles Peskine0b352bc2018-06-28 00:16:11 +020052/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
53static int asn1_write_10x( unsigned char **p,
54 unsigned char *start,
55 size_t bits,
56 unsigned char x )
57{
58 int ret;
59 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020060 if( bits == 0 )
61 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
62 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020063 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030064 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020065 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
66 *p -= len;
67 ( *p )[len-1] = x;
68 if( bits % 8 == 0 )
69 ( *p )[1] |= 1;
70 else
71 ( *p )[0] |= 1 << ( bits % 8 );
72 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
73 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
74 MBEDTLS_ASN1_INTEGER ) );
75 return( len );
76}
77
78static int construct_fake_rsa_key( unsigned char *buffer,
79 size_t buffer_size,
80 unsigned char **p,
81 size_t bits,
82 int keypair )
83{
84 size_t half_bits = ( bits + 1 ) / 2;
85 int ret;
86 int len = 0;
87 /* Construct something that looks like a DER encoding of
88 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
89 * RSAPrivateKey ::= SEQUENCE {
90 * version Version,
91 * modulus INTEGER, -- n
92 * publicExponent INTEGER, -- e
93 * privateExponent INTEGER, -- d
94 * prime1 INTEGER, -- p
95 * prime2 INTEGER, -- q
96 * exponent1 INTEGER, -- d mod (p-1)
97 * exponent2 INTEGER, -- d mod (q-1)
98 * coefficient INTEGER, -- (inverse of q) mod p
99 * otherPrimeInfos OtherPrimeInfos OPTIONAL
100 * }
101 * Or, for a public key, the same structure with only
102 * version, modulus and publicExponent.
103 */
104 *p = buffer + buffer_size;
105 if( keypair )
106 {
107 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
108 asn1_write_10x( p, buffer, half_bits, 1 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
110 asn1_write_10x( p, buffer, half_bits, 1 ) );
111 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
112 asn1_write_10x( p, buffer, half_bits, 1 ) );
113 MBEDTLS_ASN1_CHK_ADD( len, /* q */
114 asn1_write_10x( p, buffer, half_bits, 1 ) );
115 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
116 asn1_write_10x( p, buffer, half_bits, 3 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* d */
118 asn1_write_10x( p, buffer, bits, 1 ) );
119 }
120 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
121 asn1_write_10x( p, buffer, 17, 1 ) );
122 MBEDTLS_ASN1_CHK_ADD( len, /* n */
123 asn1_write_10x( p, buffer, bits, 1 ) );
124 if( keypair )
125 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
126 mbedtls_asn1_write_int( p, buffer, 0 ) );
127 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
128 {
129 const unsigned char tag =
130 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
131 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
132 }
133 return( len );
134}
135
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100136int exercise_mac_setup( psa_key_type_t key_type,
137 const unsigned char *key_bytes,
138 size_t key_length,
139 psa_algorithm_t alg,
140 psa_mac_operation_t *operation,
141 psa_status_t *status )
142{
Ronald Cron5425a212020-08-04 14:58:35 +0200143 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200144 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100145
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100146 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200147 psa_set_key_algorithm( &attributes, alg );
148 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200149 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100150
Ronald Cron5425a212020-08-04 14:58:35 +0200151 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100152 /* Whether setup succeeded or failed, abort must succeed. */
153 PSA_ASSERT( psa_mac_abort( operation ) );
154 /* If setup failed, reproduce the failure, so that the caller can
155 * test the resulting state of the operation object. */
156 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100157 {
Ronald Cron5425a212020-08-04 14:58:35 +0200158 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100159 }
160
Ronald Cron5425a212020-08-04 14:58:35 +0200161 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100162 return( 1 );
163
164exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200165 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100166 return( 0 );
167}
168
169int exercise_cipher_setup( psa_key_type_t key_type,
170 const unsigned char *key_bytes,
171 size_t key_length,
172 psa_algorithm_t alg,
173 psa_cipher_operation_t *operation,
174 psa_status_t *status )
175{
Ronald Cron5425a212020-08-04 14:58:35 +0200176 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200177 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100178
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200179 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
180 psa_set_key_algorithm( &attributes, alg );
181 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200182 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100183
Ronald Cron5425a212020-08-04 14:58:35 +0200184 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100185 /* Whether setup succeeded or failed, abort must succeed. */
186 PSA_ASSERT( psa_cipher_abort( operation ) );
187 /* If setup failed, reproduce the failure, so that the caller can
188 * test the resulting state of the operation object. */
189 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100190 {
Ronald Cron5425a212020-08-04 14:58:35 +0200191 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100192 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100193 }
194
Ronald Cron5425a212020-08-04 14:58:35 +0200195 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100196 return( 1 );
197
198exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200199 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100200 return( 0 );
201}
202
Ronald Cron5425a212020-08-04 14:58:35 +0200203static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200204{
205 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200206 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200207 uint8_t buffer[1];
208 size_t length;
209 int ok = 0;
210
Ronald Cronecfb2372020-07-23 17:13:42 +0200211 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200212 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
213 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
214 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200215 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000216 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200217 TEST_EQUAL(
218 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
219 TEST_EQUAL(
220 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200221 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200222 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
223 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
224 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
225 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
226
Ronald Cron5425a212020-08-04 14:58:35 +0200227 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000228 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200229 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200230 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000231 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200232
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200233 ok = 1;
234
235exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100236 /*
237 * Key attributes may have been returned by psa_get_key_attributes()
238 * thus reset them as required.
239 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200240 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100241
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200242 return( ok );
243}
244
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200245/* Assert that a key isn't reported as having a slot number. */
246#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
247#define ASSERT_NO_SLOT_NUMBER( attributes ) \
248 do \
249 { \
250 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
251 TEST_EQUAL( psa_get_key_slot_number( \
252 attributes, \
253 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
254 PSA_ERROR_INVALID_ARGUMENT ); \
255 } \
256 while( 0 )
257#else /* MBEDTLS_PSA_CRYPTO_SE_C */
258#define ASSERT_NO_SLOT_NUMBER( attributes ) \
259 ( (void) 0 )
260#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
261
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100262/* An overapproximation of the amount of storage needed for a key of the
263 * given type and with the given content. The API doesn't make it easy
264 * to find a good value for the size. The current implementation doesn't
265 * care about the value anyway. */
266#define KEY_BITS_FROM_DATA( type, data ) \
267 ( data )->len
268
Darryl Green0c6575a2018-11-07 16:05:30 +0000269typedef enum {
270 IMPORT_KEY = 0,
271 GENERATE_KEY = 1,
272 DERIVE_KEY = 2
273} generate_method;
274
Gilles Peskinee59236f2018-01-27 23:32:46 +0100275/* END_HEADER */
276
277/* BEGIN_DEPENDENCIES
278 * depends_on:MBEDTLS_PSA_CRYPTO_C
279 * END_DEPENDENCIES
280 */
281
282/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200283void static_checks( )
284{
285 size_t max_truncated_mac_size =
286 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
287
288 /* Check that the length for a truncated MAC always fits in the algorithm
289 * encoding. The shifted mask is the maximum truncated value. The
290 * untruncated algorithm may be one byte larger. */
291 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
292}
293/* END_CASE */
294
295/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200296void import_with_policy( int type_arg,
297 int usage_arg, int alg_arg,
298 int expected_status_arg )
299{
300 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
301 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200302 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200303 psa_key_type_t type = type_arg;
304 psa_key_usage_t usage = usage_arg;
305 psa_algorithm_t alg = alg_arg;
306 psa_status_t expected_status = expected_status_arg;
307 const uint8_t key_material[16] = {0};
308 psa_status_t status;
309
310 PSA_ASSERT( psa_crypto_init( ) );
311
312 psa_set_key_type( &attributes, type );
313 psa_set_key_usage_flags( &attributes, usage );
314 psa_set_key_algorithm( &attributes, alg );
315
316 status = psa_import_key( &attributes,
317 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200318 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200319 TEST_EQUAL( status, expected_status );
320 if( status != PSA_SUCCESS )
321 goto exit;
322
Ronald Cron5425a212020-08-04 14:58:35 +0200323 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200324 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200325 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200326 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200327 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200328 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200329
Ronald Cron5425a212020-08-04 14:58:35 +0200330 PSA_ASSERT( psa_destroy_key( key ) );
331 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200332
333exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100334 /*
335 * Key attributes may have been returned by psa_get_key_attributes()
336 * thus reset them as required.
337 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200338 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100339
340 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200341 PSA_DONE( );
342}
343/* END_CASE */
344
345/* BEGIN_CASE */
346void import_with_data( data_t *data, int type_arg,
347 int attr_bits_arg,
348 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200349{
350 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
351 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200352 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200353 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200354 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200355 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100356 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100357
Gilles Peskine8817f612018-12-18 00:18:46 +0100358 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100359
Gilles Peskine4747d192019-04-17 15:05:45 +0200360 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200361 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200362
Ronald Cron5425a212020-08-04 14:58:35 +0200363 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100364 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200365 if( status != PSA_SUCCESS )
366 goto exit;
367
Ronald Cron5425a212020-08-04 14:58:35 +0200368 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200369 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200370 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200371 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200372 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200373
Ronald Cron5425a212020-08-04 14:58:35 +0200374 PSA_ASSERT( psa_destroy_key( key ) );
375 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100376
377exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100378 /*
379 * Key attributes may have been returned by psa_get_key_attributes()
380 * thus reset them as required.
381 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200382 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100383
384 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200385 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100386}
387/* END_CASE */
388
389/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200390void import_large_key( int type_arg, int byte_size_arg,
391 int expected_status_arg )
392{
393 psa_key_type_t type = type_arg;
394 size_t byte_size = byte_size_arg;
395 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
396 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200397 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200398 psa_status_t status;
399 uint8_t *buffer = NULL;
400 size_t buffer_size = byte_size + 1;
401 size_t n;
402
Steven Cooreman69967ce2021-01-18 18:01:08 +0100403 /* Skip the test case if the target running the test cannot
404 * accomodate large keys due to heap size constraints */
405 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200406 memset( buffer, 'K', byte_size );
407
408 PSA_ASSERT( psa_crypto_init( ) );
409
410 /* Try importing the key */
411 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
412 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200413 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100414 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200415 TEST_EQUAL( status, expected_status );
416
417 if( status == PSA_SUCCESS )
418 {
Ronald Cron5425a212020-08-04 14:58:35 +0200419 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200420 TEST_EQUAL( psa_get_key_type( &attributes ), type );
421 TEST_EQUAL( psa_get_key_bits( &attributes ),
422 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200423 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200424 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200425 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200426 for( n = 0; n < byte_size; n++ )
427 TEST_EQUAL( buffer[n], 'K' );
428 for( n = byte_size; n < buffer_size; n++ )
429 TEST_EQUAL( buffer[n], 0 );
430 }
431
432exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100433 /*
434 * Key attributes may have been returned by psa_get_key_attributes()
435 * thus reset them as required.
436 */
437 psa_reset_key_attributes( &attributes );
438
Ronald Cron5425a212020-08-04 14:58:35 +0200439 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200440 PSA_DONE( );
441 mbedtls_free( buffer );
442}
443/* END_CASE */
444
445/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200446void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
447{
Ronald Cron5425a212020-08-04 14:58:35 +0200448 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200449 size_t bits = bits_arg;
450 psa_status_t expected_status = expected_status_arg;
451 psa_status_t status;
452 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200453 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200454 size_t buffer_size = /* Slight overapproximations */
455 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200456 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200457 unsigned char *p;
458 int ret;
459 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200460 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200461
Gilles Peskine8817f612018-12-18 00:18:46 +0100462 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200463 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200464
465 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
466 bits, keypair ) ) >= 0 );
467 length = ret;
468
469 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200470 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200471 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100472 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200473
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200474 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200475 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200476
477exit:
478 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200479 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200480}
481/* END_CASE */
482
483/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300484void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300485 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200486 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100487 int expected_bits,
488 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200489 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100490 int canonical_input )
491{
Ronald Cron5425a212020-08-04 14:58:35 +0200492 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100493 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200494 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200495 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100496 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100497 unsigned char *exported = NULL;
498 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100499 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100500 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100501 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200502 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200503 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100504
Moran Pekercb088e72018-07-17 17:36:59 +0300505 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200506 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100507 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200508 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100509 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100510
Gilles Peskine4747d192019-04-17 15:05:45 +0200511 psa_set_key_usage_flags( &attributes, usage_arg );
512 psa_set_key_algorithm( &attributes, alg );
513 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700514
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100515 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200516 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100517
518 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200519 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200520 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
521 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200522 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100523
524 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200525 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100526 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100527
528 /* The exported length must be set by psa_export_key() to a value between 0
529 * and export_size. On errors, the exported length must be 0. */
530 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
531 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
532 TEST_ASSERT( exported_length <= export_size );
533
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200534 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200535 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100536 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200537 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100538 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100539 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200540 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100541
Gilles Peskineea38a922021-02-13 00:05:16 +0100542 /* Run sanity checks on the exported key. For non-canonical inputs,
543 * this validates the canonical representations. For canonical inputs,
544 * this doesn't directly validate the implementation, but it still helps
545 * by cross-validating the test data with the sanity check code. */
546 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200547 goto exit;
548
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100549 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200550 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100551 else
552 {
Ronald Cron5425a212020-08-04 14:58:35 +0200553 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200554 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200555 &key2 ) );
556 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100557 reexported,
558 export_size,
559 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200560 ASSERT_COMPARE( exported, exported_length,
561 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200562 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100563 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100564 TEST_ASSERT( exported_length <=
565 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
566 psa_get_key_bits( &got_attributes ) ) );
567 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100568
569destroy:
570 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200571 PSA_ASSERT( psa_destroy_key( key ) );
572 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100573
574exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100575 /*
576 * Key attributes may have been returned by psa_get_key_attributes()
577 * thus reset them as required.
578 */
579 psa_reset_key_attributes( &got_attributes );
580
itayzafrir3e02b3b2018-06-12 17:06:52 +0300581 mbedtls_free( exported );
582 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200583 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100584}
585/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100586
Moran Pekerf709f4a2018-06-06 17:26:04 +0300587/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300588void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200589 int type_arg,
590 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100591 int export_size_delta,
592 int expected_export_status_arg,
593 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300594{
Ronald Cron5425a212020-08-04 14:58:35 +0200595 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300596 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200597 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200598 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300599 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300600 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100601 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100602 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200603 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300604
Gilles Peskine8817f612018-12-18 00:18:46 +0100605 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300606
Gilles Peskine4747d192019-04-17 15:05:45 +0200607 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
608 psa_set_key_algorithm( &attributes, alg );
609 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300610
611 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200612 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300613
Gilles Peskine49c25912018-10-29 15:15:31 +0100614 /* Export the public key */
615 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200616 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200617 exported, export_size,
618 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100619 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100620 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100621 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200622 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100623 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200624 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200625 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100626 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100627 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100628 TEST_ASSERT( expected_public_key->len <=
629 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
630 TEST_ASSERT( expected_public_key->len <=
631 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100632 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
633 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100634 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300635
636exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100637 /*
638 * Key attributes may have been returned by psa_get_key_attributes()
639 * thus reset them as required.
640 */
641 psa_reset_key_attributes( &attributes );
642
itayzafrir3e02b3b2018-06-12 17:06:52 +0300643 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200644 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200645 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300646}
647/* END_CASE */
648
Gilles Peskine20035e32018-02-03 22:44:14 +0100649/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200650void import_and_exercise_key( data_t *data,
651 int type_arg,
652 int bits_arg,
653 int alg_arg )
654{
Ronald Cron5425a212020-08-04 14:58:35 +0200655 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200656 psa_key_type_t type = type_arg;
657 size_t bits = bits_arg;
658 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100659 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200660 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200661 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200662
Gilles Peskine8817f612018-12-18 00:18:46 +0100663 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200664
Gilles Peskine4747d192019-04-17 15:05:45 +0200665 psa_set_key_usage_flags( &attributes, usage );
666 psa_set_key_algorithm( &attributes, alg );
667 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200668
669 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200670 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200671
672 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200673 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200674 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
675 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200676
677 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100678 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200679 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200680
Ronald Cron5425a212020-08-04 14:58:35 +0200681 PSA_ASSERT( psa_destroy_key( key ) );
682 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200683
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200684exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100685 /*
686 * Key attributes may have been returned by psa_get_key_attributes()
687 * thus reset them as required.
688 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200689 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100690
691 psa_reset_key_attributes( &attributes );
692 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200693 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200694}
695/* END_CASE */
696
697/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100698void effective_key_attributes( int type_arg, int expected_type_arg,
699 int bits_arg, int expected_bits_arg,
700 int usage_arg, int expected_usage_arg,
701 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200702{
Ronald Cron5425a212020-08-04 14:58:35 +0200703 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100704 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100705 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100706 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100707 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200708 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100709 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200710 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100711 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200712 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200713
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200714 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200715 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200716 TEST_EQUAL( expected_usage, mbedtls_test_update_key_usage_flags( usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200717
Gilles Peskine8817f612018-12-18 00:18:46 +0100718 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200719
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200720 psa_set_key_usage_flags( &attributes, usage );
721 psa_set_key_algorithm( &attributes, alg );
722 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +0100723 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +0200724
Ronald Cron5425a212020-08-04 14:58:35 +0200725 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +0100726 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +0200727
Ronald Cron5425a212020-08-04 14:58:35 +0200728 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +0100729 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
730 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
731 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
732 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200733
734exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100735 /*
736 * Key attributes may have been returned by psa_get_key_attributes()
737 * thus reset them as required.
738 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200739 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100740
741 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200742 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +0200743}
744/* END_CASE */
745
746/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100747void check_key_policy( int type_arg, int bits_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200748 int usage_arg, int expected_usage_arg, int alg_arg )
Gilles Peskine06c28892019-11-26 18:07:46 +0100749{
750 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200751 usage_arg, expected_usage_arg,
752 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +0100753 goto exit;
754}
755/* END_CASE */
756
757/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200758void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +0000759{
760 /* Test each valid way of initializing the object, except for `= {0}`, as
761 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
762 * though it's OK by the C standard. We could test for this, but we'd need
763 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200764 psa_key_attributes_t func = psa_key_attributes_init( );
765 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
766 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000767
768 memset( &zero, 0, sizeof( zero ) );
769
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200770 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
771 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
772 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000773
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200774 TEST_EQUAL( psa_get_key_type( &func ), 0 );
775 TEST_EQUAL( psa_get_key_type( &init ), 0 );
776 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
777
778 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
779 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
780 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
781
782 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
783 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
784 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
785
786 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
787 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
788 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +0000789}
790/* END_CASE */
791
792/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200793void mac_key_policy( int policy_usage_arg,
794 int policy_alg_arg,
795 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200796 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200797 int exercise_alg_arg,
798 int expected_usage_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100799 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200800{
Ronald Cron5425a212020-08-04 14:58:35 +0200801 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200802 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000803 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200804 psa_key_type_t key_type = key_type_arg;
805 psa_algorithm_t policy_alg = policy_alg_arg;
806 psa_algorithm_t exercise_alg = exercise_alg_arg;
807 psa_key_usage_t policy_usage = policy_usage_arg;
808 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200809 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100810 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200811 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200812
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200813 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200814 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200815 TEST_EQUAL( expected_usage,
816 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200817
Gilles Peskine8817f612018-12-18 00:18:46 +0100818 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200819
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200820 psa_set_key_usage_flags( &attributes, policy_usage );
821 psa_set_key_algorithm( &attributes, policy_alg );
822 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200823
Gilles Peskine049c7532019-05-15 20:22:09 +0200824 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200825 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200826
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200827 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
828
Ronald Cron5425a212020-08-04 14:58:35 +0200829 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100830 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100831 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100832 else
833 TEST_EQUAL( status, expected_status );
834
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200835 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200836
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200837 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200838 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100839 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100840 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100841 else
842 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200843
844exit:
845 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200846 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200847 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200848}
849/* END_CASE */
850
851/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200852void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200853 int policy_alg,
854 int key_type,
855 data_t *key_data,
856 int exercise_alg )
857{
Ronald Cron5425a212020-08-04 14:58:35 +0200858 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200859 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000860 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200861 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200862 psa_status_t status;
863
Gilles Peskine8817f612018-12-18 00:18:46 +0100864 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200865
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200866 psa_set_key_usage_flags( &attributes, policy_usage );
867 psa_set_key_algorithm( &attributes, policy_alg );
868 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200869
Gilles Peskine049c7532019-05-15 20:22:09 +0200870 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200871 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200872
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200873 /* Check if no key usage flag implication is done */
874 TEST_EQUAL( policy_usage,
875 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200876
Ronald Cron5425a212020-08-04 14:58:35 +0200877 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200878 if( policy_alg == exercise_alg &&
879 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100880 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200881 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100882 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200883 psa_cipher_abort( &operation );
884
Ronald Cron5425a212020-08-04 14:58:35 +0200885 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200886 if( policy_alg == exercise_alg &&
887 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100888 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200889 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100890 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200891
892exit:
893 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200894 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200895 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200896}
897/* END_CASE */
898
899/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200900void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200901 int policy_alg,
902 int key_type,
903 data_t *key_data,
904 int nonce_length_arg,
905 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100906 int exercise_alg,
907 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200908{
Ronald Cron5425a212020-08-04 14:58:35 +0200909 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200910 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200911 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200912 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100913 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200914 unsigned char nonce[16] = {0};
915 size_t nonce_length = nonce_length_arg;
916 unsigned char tag[16];
917 size_t tag_length = tag_length_arg;
918 size_t output_length;
919
920 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
921 TEST_ASSERT( tag_length <= sizeof( tag ) );
922
Gilles Peskine8817f612018-12-18 00:18:46 +0100923 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200924
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200925 psa_set_key_usage_flags( &attributes, policy_usage );
926 psa_set_key_algorithm( &attributes, policy_alg );
927 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200928
Gilles Peskine049c7532019-05-15 20:22:09 +0200929 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200930 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200931
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200932 /* Check if no key usage implication is done */
933 TEST_EQUAL( policy_usage,
934 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200935
Ronald Cron5425a212020-08-04 14:58:35 +0200936 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200937 nonce, nonce_length,
938 NULL, 0,
939 NULL, 0,
940 tag, tag_length,
941 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100942 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
943 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200944 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100945 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200946
947 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200948 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200949 nonce, nonce_length,
950 NULL, 0,
951 tag, tag_length,
952 NULL, 0,
953 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100954 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
955 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
956 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100957 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200958 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100959 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200960
961exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200962 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200963 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200964}
965/* END_CASE */
966
967/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200968void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200969 int policy_alg,
970 int key_type,
971 data_t *key_data,
972 int exercise_alg )
973{
Ronald Cron5425a212020-08-04 14:58:35 +0200974 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200975 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200976 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200977 psa_status_t status;
978 size_t key_bits;
979 size_t buffer_length;
980 unsigned char *buffer = NULL;
981 size_t output_length;
982
Gilles Peskine8817f612018-12-18 00:18:46 +0100983 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200984
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200985 psa_set_key_usage_flags( &attributes, policy_usage );
986 psa_set_key_algorithm( &attributes, policy_alg );
987 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200988
Gilles Peskine049c7532019-05-15 20:22:09 +0200989 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200990 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200991
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200992 /* Check if no key usage implication is done */
993 TEST_EQUAL( policy_usage,
994 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200995
Ronald Cron5425a212020-08-04 14:58:35 +0200996 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200997 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200998 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
999 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001000 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001001
Ronald Cron5425a212020-08-04 14:58:35 +02001002 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001003 NULL, 0,
1004 NULL, 0,
1005 buffer, buffer_length,
1006 &output_length );
1007 if( policy_alg == exercise_alg &&
1008 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001009 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001010 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001011 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001012
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001013 if( buffer_length != 0 )
1014 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001015 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001016 buffer, buffer_length,
1017 NULL, 0,
1018 buffer, buffer_length,
1019 &output_length );
1020 if( policy_alg == exercise_alg &&
1021 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001022 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001023 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001024 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001025
1026exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001027 /*
1028 * Key attributes may have been returned by psa_get_key_attributes()
1029 * thus reset them as required.
1030 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001031 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001032
1033 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001034 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001035 mbedtls_free( buffer );
1036}
1037/* END_CASE */
1038
1039/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001040void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001041 int policy_alg,
1042 int key_type,
1043 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001044 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001045 int payload_length_arg,
1046 int hashing_permitted,
1047 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001048{
Ronald Cron5425a212020-08-04 14:58:35 +02001049 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001050 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001051 psa_key_usage_t policy_usage = policy_usage_arg;
1052 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001053 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001054 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1055 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1056 * compatible with the policy and `payload_length_arg` is supposed to be
1057 * a valid input length to sign. If `payload_length_arg <= 0`,
1058 * `exercise_alg` is supposed to be forbidden by the policy. */
1059 int compatible_alg = payload_length_arg > 0;
1060 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001061 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001062 size_t signature_length;
1063
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001064 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001065 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001066 TEST_EQUAL( expected_usage,
1067 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001068
Gilles Peskine8817f612018-12-18 00:18:46 +01001069 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001070
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001071 psa_set_key_usage_flags( &attributes, policy_usage );
1072 psa_set_key_algorithm( &attributes, policy_alg );
1073 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001074
Gilles Peskine049c7532019-05-15 20:22:09 +02001075 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001076 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001077
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001078 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1079
Ronald Cron5425a212020-08-04 14:58:35 +02001080 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001081 payload, payload_length,
1082 signature, sizeof( signature ),
1083 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001084 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001085 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001086 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001087 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001088
1089 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001090 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001091 payload, payload_length,
1092 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001093 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001094 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001095 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001096 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001097
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001098 if( hashing_permitted )
1099 {
1100 status = psa_sign_message( key, exercise_alg,
1101 payload, payload_length,
1102 signature, sizeof( signature ),
1103 &signature_length );
1104 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1105 PSA_ASSERT( status );
1106 else
1107 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1108
1109 memset( signature, 0, sizeof( signature ) );
1110 status = psa_verify_message( key, exercise_alg,
1111 payload, payload_length,
1112 signature, sizeof( signature ) );
1113 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1114 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1115 else
1116 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1117 }
1118
Gilles Peskined5b33222018-06-18 22:20:03 +02001119exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001120 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001121 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001122}
1123/* END_CASE */
1124
Janos Follathba3fab92019-06-11 14:50:16 +01001125/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001126void derive_key_policy( int policy_usage,
1127 int policy_alg,
1128 int key_type,
1129 data_t *key_data,
1130 int exercise_alg )
1131{
Ronald Cron5425a212020-08-04 14:58:35 +02001132 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001133 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001134 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001135 psa_status_t status;
1136
Gilles Peskine8817f612018-12-18 00:18:46 +01001137 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001138
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001139 psa_set_key_usage_flags( &attributes, policy_usage );
1140 psa_set_key_algorithm( &attributes, policy_alg );
1141 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001142
Gilles Peskine049c7532019-05-15 20:22:09 +02001143 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001144 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001145
Janos Follathba3fab92019-06-11 14:50:16 +01001146 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1147
1148 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1149 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001150 {
Janos Follathba3fab92019-06-11 14:50:16 +01001151 PSA_ASSERT( psa_key_derivation_input_bytes(
1152 &operation,
1153 PSA_KEY_DERIVATION_INPUT_SEED,
1154 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001155 }
Janos Follathba3fab92019-06-11 14:50:16 +01001156
1157 status = psa_key_derivation_input_key( &operation,
1158 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001159 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001160
Gilles Peskineea0fb492018-07-12 17:17:20 +02001161 if( policy_alg == exercise_alg &&
1162 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001163 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001164 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001165 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001166
1167exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001168 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001169 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001170 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001171}
1172/* END_CASE */
1173
1174/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001175void agreement_key_policy( int policy_usage,
1176 int policy_alg,
1177 int key_type_arg,
1178 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001179 int exercise_alg,
1180 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001181{
Ronald Cron5425a212020-08-04 14:58:35 +02001182 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001183 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001184 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001185 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001186 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001187 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001188
Gilles Peskine8817f612018-12-18 00:18:46 +01001189 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001190
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001191 psa_set_key_usage_flags( &attributes, policy_usage );
1192 psa_set_key_algorithm( &attributes, policy_alg );
1193 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001194
Gilles Peskine049c7532019-05-15 20:22:09 +02001195 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001196 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001197
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001198 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001199 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001200
Steven Cooremance48e852020-10-05 16:02:45 +02001201 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001202
1203exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001204 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001205 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001206 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001207}
1208/* END_CASE */
1209
1210/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001211void key_policy_alg2( int key_type_arg, data_t *key_data,
1212 int usage_arg, int alg_arg, int alg2_arg )
1213{
Ronald Cron5425a212020-08-04 14:58:35 +02001214 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001215 psa_key_type_t key_type = key_type_arg;
1216 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1217 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1218 psa_key_usage_t usage = usage_arg;
1219 psa_algorithm_t alg = alg_arg;
1220 psa_algorithm_t alg2 = alg2_arg;
1221
1222 PSA_ASSERT( psa_crypto_init( ) );
1223
1224 psa_set_key_usage_flags( &attributes, usage );
1225 psa_set_key_algorithm( &attributes, alg );
1226 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1227 psa_set_key_type( &attributes, key_type );
1228 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001229 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001230
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001231 /* Update the usage flags to obtain implicit usage flags */
1232 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001233 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001234 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1235 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1236 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1237
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001238 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001239 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001240 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001241 goto exit;
1242
1243exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001244 /*
1245 * Key attributes may have been returned by psa_get_key_attributes()
1246 * thus reset them as required.
1247 */
1248 psa_reset_key_attributes( &got_attributes );
1249
Ronald Cron5425a212020-08-04 14:58:35 +02001250 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001251 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001252}
1253/* END_CASE */
1254
1255/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001256void raw_agreement_key_policy( int policy_usage,
1257 int policy_alg,
1258 int key_type_arg,
1259 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001260 int exercise_alg,
1261 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001262{
Ronald Cron5425a212020-08-04 14:58:35 +02001263 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001264 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001265 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001266 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001267 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001268 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001269
1270 PSA_ASSERT( psa_crypto_init( ) );
1271
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001272 psa_set_key_usage_flags( &attributes, policy_usage );
1273 psa_set_key_algorithm( &attributes, policy_alg );
1274 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001275
Gilles Peskine049c7532019-05-15 20:22:09 +02001276 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001277 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001278
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001279 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001280
Steven Cooremance48e852020-10-05 16:02:45 +02001281 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001282
1283exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001284 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001285 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001286 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001287}
1288/* END_CASE */
1289
1290/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001291void copy_success( int source_usage_arg,
1292 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001293 int type_arg, data_t *material,
1294 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001295 int target_usage_arg,
1296 int target_alg_arg, int target_alg2_arg,
1297 int expected_usage_arg,
1298 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001299{
Gilles Peskineca25db92019-04-19 11:43:08 +02001300 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1301 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001302 psa_key_usage_t expected_usage = expected_usage_arg;
1303 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001304 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001305 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1306 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001307 uint8_t *export_buffer = NULL;
1308
Gilles Peskine57ab7212019-01-28 13:03:09 +01001309 PSA_ASSERT( psa_crypto_init( ) );
1310
Gilles Peskineca25db92019-04-19 11:43:08 +02001311 /* Prepare the source key. */
1312 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1313 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001314 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001315 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001316 PSA_ASSERT( psa_import_key( &source_attributes,
1317 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001318 &source_key ) );
1319 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001320
Gilles Peskineca25db92019-04-19 11:43:08 +02001321 /* Prepare the target attributes. */
1322 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001323 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001324 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001325 /* Set volatile lifetime to reset the key identifier to 0. */
1326 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1327 }
1328
Gilles Peskineca25db92019-04-19 11:43:08 +02001329 if( target_usage_arg != -1 )
1330 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1331 if( target_alg_arg != -1 )
1332 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001333 if( target_alg2_arg != -1 )
1334 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001335
1336 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001337 PSA_ASSERT( psa_copy_key( source_key,
1338 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001339
1340 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001341 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001342
1343 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001344 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001345 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1346 psa_get_key_type( &target_attributes ) );
1347 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1348 psa_get_key_bits( &target_attributes ) );
1349 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1350 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001351 TEST_EQUAL( expected_alg2,
1352 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001353 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1354 {
1355 size_t length;
1356 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001357 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001358 material->len, &length ) );
1359 ASSERT_COMPARE( material->x, material->len,
1360 export_buffer, length );
1361 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001362
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001363 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001364 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001365 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001366 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001367
Ronald Cron5425a212020-08-04 14:58:35 +02001368 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001369
1370exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001371 /*
1372 * Source and target key attributes may have been returned by
1373 * psa_get_key_attributes() thus reset them as required.
1374 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001375 psa_reset_key_attributes( &source_attributes );
1376 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001377
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001378 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001379 mbedtls_free( export_buffer );
1380}
1381/* END_CASE */
1382
1383/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001384void copy_fail( int source_usage_arg,
1385 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001386 int type_arg, data_t *material,
1387 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001388 int target_usage_arg,
1389 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001390 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001391 int expected_status_arg )
1392{
1393 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1394 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001395 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1396 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001397 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001398
1399 PSA_ASSERT( psa_crypto_init( ) );
1400
1401 /* Prepare the source key. */
1402 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1403 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001404 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001405 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001406 PSA_ASSERT( psa_import_key( &source_attributes,
1407 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001408 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001409
1410 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001411 psa_set_key_id( &target_attributes, key_id );
1412 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001413 psa_set_key_type( &target_attributes, target_type_arg );
1414 psa_set_key_bits( &target_attributes, target_bits_arg );
1415 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1416 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001417 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001418
1419 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001420 TEST_EQUAL( psa_copy_key( source_key,
1421 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001422 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001423
Ronald Cron5425a212020-08-04 14:58:35 +02001424 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001425
Gilles Peskine4a644642019-05-03 17:14:08 +02001426exit:
1427 psa_reset_key_attributes( &source_attributes );
1428 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001429 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001430}
1431/* END_CASE */
1432
1433/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001434void hash_operation_init( )
1435{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001436 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001437 /* Test each valid way of initializing the object, except for `= {0}`, as
1438 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1439 * though it's OK by the C standard. We could test for this, but we'd need
1440 * to supress the Clang warning for the test. */
1441 psa_hash_operation_t func = psa_hash_operation_init( );
1442 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1443 psa_hash_operation_t zero;
1444
1445 memset( &zero, 0, sizeof( zero ) );
1446
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001447 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001448 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1449 PSA_ERROR_BAD_STATE );
1450 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1451 PSA_ERROR_BAD_STATE );
1452 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1453 PSA_ERROR_BAD_STATE );
1454
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001455 /* A default hash operation should be abortable without error. */
1456 PSA_ASSERT( psa_hash_abort( &func ) );
1457 PSA_ASSERT( psa_hash_abort( &init ) );
1458 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001459}
1460/* END_CASE */
1461
1462/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001463void hash_setup( int alg_arg,
1464 int expected_status_arg )
1465{
1466 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001467 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001468 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001469 psa_status_t status;
1470
Gilles Peskine8817f612018-12-18 00:18:46 +01001471 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001472
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001473 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001474 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001475
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001476 /* Whether setup succeeded or failed, abort must succeed. */
1477 PSA_ASSERT( psa_hash_abort( &operation ) );
1478
1479 /* If setup failed, reproduce the failure, so as to
1480 * test the resulting state of the operation object. */
1481 if( status != PSA_SUCCESS )
1482 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1483
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001484 /* Now the operation object should be reusable. */
1485#if defined(KNOWN_SUPPORTED_HASH_ALG)
1486 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1487 PSA_ASSERT( psa_hash_abort( &operation ) );
1488#endif
1489
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001490exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001491 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001492}
1493/* END_CASE */
1494
1495/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001496void hash_compute_fail( int alg_arg, data_t *input,
1497 int output_size_arg, int expected_status_arg )
1498{
1499 psa_algorithm_t alg = alg_arg;
1500 uint8_t *output = NULL;
1501 size_t output_size = output_size_arg;
1502 size_t output_length = INVALID_EXPORT_LENGTH;
1503 psa_status_t expected_status = expected_status_arg;
1504 psa_status_t status;
1505
1506 ASSERT_ALLOC( output, output_size );
1507
1508 PSA_ASSERT( psa_crypto_init( ) );
1509
1510 status = psa_hash_compute( alg, input->x, input->len,
1511 output, output_size, &output_length );
1512 TEST_EQUAL( status, expected_status );
1513 TEST_ASSERT( output_length <= output_size );
1514
1515exit:
1516 mbedtls_free( output );
1517 PSA_DONE( );
1518}
1519/* END_CASE */
1520
1521/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001522void hash_compare_fail( int alg_arg, data_t *input,
1523 data_t *reference_hash,
1524 int expected_status_arg )
1525{
1526 psa_algorithm_t alg = alg_arg;
1527 psa_status_t expected_status = expected_status_arg;
1528 psa_status_t status;
1529
1530 PSA_ASSERT( psa_crypto_init( ) );
1531
1532 status = psa_hash_compare( alg, input->x, input->len,
1533 reference_hash->x, reference_hash->len );
1534 TEST_EQUAL( status, expected_status );
1535
1536exit:
1537 PSA_DONE( );
1538}
1539/* END_CASE */
1540
1541/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001542void hash_compute_compare( int alg_arg, data_t *input,
1543 data_t *expected_output )
1544{
1545 psa_algorithm_t alg = alg_arg;
1546 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1547 size_t output_length = INVALID_EXPORT_LENGTH;
1548 size_t i;
1549
1550 PSA_ASSERT( psa_crypto_init( ) );
1551
1552 /* Compute with tight buffer */
1553 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001554 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001555 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001556 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001557 ASSERT_COMPARE( output, output_length,
1558 expected_output->x, expected_output->len );
1559
1560 /* Compute with larger buffer */
1561 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1562 output, sizeof( output ),
1563 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001564 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001565 ASSERT_COMPARE( output, output_length,
1566 expected_output->x, expected_output->len );
1567
1568 /* Compare with correct hash */
1569 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1570 output, output_length ) );
1571
1572 /* Compare with trailing garbage */
1573 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1574 output, output_length + 1 ),
1575 PSA_ERROR_INVALID_SIGNATURE );
1576
1577 /* Compare with truncated hash */
1578 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1579 output, output_length - 1 ),
1580 PSA_ERROR_INVALID_SIGNATURE );
1581
1582 /* Compare with corrupted value */
1583 for( i = 0; i < output_length; i++ )
1584 {
Chris Jones9634bb12021-01-20 15:56:42 +00001585 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001586 output[i] ^= 1;
1587 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1588 output, output_length ),
1589 PSA_ERROR_INVALID_SIGNATURE );
1590 output[i] ^= 1;
1591 }
1592
1593exit:
1594 PSA_DONE( );
1595}
1596/* END_CASE */
1597
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001598/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001599void hash_bad_order( )
1600{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001601 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001602 unsigned char input[] = "";
1603 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001604 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001605 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1606 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1607 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001608 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001609 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001610 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001611
Gilles Peskine8817f612018-12-18 00:18:46 +01001612 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001613
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001614 /* Call setup twice in a row. */
1615 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001616 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001617 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1618 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001619 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001620 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001621 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001622
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001623 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001624 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001625 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001626 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001627
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001628 /* Check that update calls abort on error. */
1629 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01001630 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001631 ASSERT_OPERATION_IS_ACTIVE( operation );
1632 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
1633 PSA_ERROR_BAD_STATE );
1634 ASSERT_OPERATION_IS_INACTIVE( operation );
1635 PSA_ASSERT( psa_hash_abort( &operation ) );
1636 ASSERT_OPERATION_IS_INACTIVE( operation );
1637
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001638 /* Call update after finish. */
1639 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1640 PSA_ASSERT( psa_hash_finish( &operation,
1641 hash, sizeof( hash ), &hash_len ) );
1642 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001643 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001644 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001645
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001646 /* Call verify without calling setup beforehand. */
1647 TEST_EQUAL( psa_hash_verify( &operation,
1648 valid_hash, sizeof( valid_hash ) ),
1649 PSA_ERROR_BAD_STATE );
1650 PSA_ASSERT( psa_hash_abort( &operation ) );
1651
1652 /* Call verify after finish. */
1653 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1654 PSA_ASSERT( psa_hash_finish( &operation,
1655 hash, sizeof( hash ), &hash_len ) );
1656 TEST_EQUAL( psa_hash_verify( &operation,
1657 valid_hash, sizeof( valid_hash ) ),
1658 PSA_ERROR_BAD_STATE );
1659 PSA_ASSERT( psa_hash_abort( &operation ) );
1660
1661 /* Call verify twice in a row. */
1662 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001663 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001664 PSA_ASSERT( psa_hash_verify( &operation,
1665 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001666 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001667 TEST_EQUAL( psa_hash_verify( &operation,
1668 valid_hash, sizeof( valid_hash ) ),
1669 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001670 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001671 PSA_ASSERT( psa_hash_abort( &operation ) );
1672
1673 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001674 TEST_EQUAL( psa_hash_finish( &operation,
1675 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001676 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001677 PSA_ASSERT( psa_hash_abort( &operation ) );
1678
1679 /* Call finish twice in a row. */
1680 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1681 PSA_ASSERT( psa_hash_finish( &operation,
1682 hash, sizeof( hash ), &hash_len ) );
1683 TEST_EQUAL( psa_hash_finish( &operation,
1684 hash, sizeof( hash ), &hash_len ),
1685 PSA_ERROR_BAD_STATE );
1686 PSA_ASSERT( psa_hash_abort( &operation ) );
1687
1688 /* Call finish after calling verify. */
1689 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1690 PSA_ASSERT( psa_hash_verify( &operation,
1691 valid_hash, sizeof( valid_hash ) ) );
1692 TEST_EQUAL( psa_hash_finish( &operation,
1693 hash, sizeof( hash ), &hash_len ),
1694 PSA_ERROR_BAD_STATE );
1695 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001696
1697exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001698 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001699}
1700/* END_CASE */
1701
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001702/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001703void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001704{
1705 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001706 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1707 * appended to it */
1708 unsigned char hash[] = {
1709 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1710 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1711 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001712 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001713 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001714
Gilles Peskine8817f612018-12-18 00:18:46 +01001715 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001716
itayzafrir27e69452018-11-01 14:26:34 +02001717 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001718 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001719 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001720 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001721 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001722 ASSERT_OPERATION_IS_INACTIVE( operation );
1723 PSA_ASSERT( psa_hash_abort( &operation ) );
1724 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03001725
itayzafrir27e69452018-11-01 14:26:34 +02001726 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001727 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001728 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001729 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001730
itayzafrir27e69452018-11-01 14:26:34 +02001731 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001732 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001733 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001734 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001735
itayzafrirec93d302018-10-18 18:01:10 +03001736exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001737 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001738}
1739/* END_CASE */
1740
Ronald Cronee414c72021-03-18 18:50:08 +01001741/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001742void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001743{
1744 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001745 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001746 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001747 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001748 size_t hash_len;
1749
Gilles Peskine8817f612018-12-18 00:18:46 +01001750 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001751
itayzafrir58028322018-10-25 10:22:01 +03001752 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001753 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001754 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001755 hash, expected_size - 1, &hash_len ),
1756 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001757
1758exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001759 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001760}
1761/* END_CASE */
1762
Ronald Cronee414c72021-03-18 18:50:08 +01001763/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001764void hash_clone_source_state( )
1765{
1766 psa_algorithm_t alg = PSA_ALG_SHA_256;
1767 unsigned char hash[PSA_HASH_MAX_SIZE];
1768 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1769 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1770 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1771 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1772 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1773 size_t hash_len;
1774
1775 PSA_ASSERT( psa_crypto_init( ) );
1776 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1777
1778 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1779 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1780 PSA_ASSERT( psa_hash_finish( &op_finished,
1781 hash, sizeof( hash ), &hash_len ) );
1782 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1783 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1784
1785 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1786 PSA_ERROR_BAD_STATE );
1787
1788 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1789 PSA_ASSERT( psa_hash_finish( &op_init,
1790 hash, sizeof( hash ), &hash_len ) );
1791 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1792 PSA_ASSERT( psa_hash_finish( &op_finished,
1793 hash, sizeof( hash ), &hash_len ) );
1794 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1795 PSA_ASSERT( psa_hash_finish( &op_aborted,
1796 hash, sizeof( hash ), &hash_len ) );
1797
1798exit:
1799 psa_hash_abort( &op_source );
1800 psa_hash_abort( &op_init );
1801 psa_hash_abort( &op_setup );
1802 psa_hash_abort( &op_finished );
1803 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001804 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001805}
1806/* END_CASE */
1807
Ronald Cronee414c72021-03-18 18:50:08 +01001808/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001809void hash_clone_target_state( )
1810{
1811 psa_algorithm_t alg = PSA_ALG_SHA_256;
1812 unsigned char hash[PSA_HASH_MAX_SIZE];
1813 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1814 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1815 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1816 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1817 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1818 size_t hash_len;
1819
1820 PSA_ASSERT( psa_crypto_init( ) );
1821
1822 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1823 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1824 PSA_ASSERT( psa_hash_finish( &op_finished,
1825 hash, sizeof( hash ), &hash_len ) );
1826 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1827 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1828
1829 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1830 PSA_ASSERT( psa_hash_finish( &op_target,
1831 hash, sizeof( hash ), &hash_len ) );
1832
1833 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1834 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1835 PSA_ERROR_BAD_STATE );
1836 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1837 PSA_ERROR_BAD_STATE );
1838
1839exit:
1840 psa_hash_abort( &op_target );
1841 psa_hash_abort( &op_init );
1842 psa_hash_abort( &op_setup );
1843 psa_hash_abort( &op_finished );
1844 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001845 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001846}
1847/* END_CASE */
1848
itayzafrir58028322018-10-25 10:22:01 +03001849/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001850void mac_operation_init( )
1851{
Jaeden Amero252ef282019-02-15 14:05:35 +00001852 const uint8_t input[1] = { 0 };
1853
Jaeden Amero769ce272019-01-04 11:48:03 +00001854 /* Test each valid way of initializing the object, except for `= {0}`, as
1855 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1856 * though it's OK by the C standard. We could test for this, but we'd need
1857 * to supress the Clang warning for the test. */
1858 psa_mac_operation_t func = psa_mac_operation_init( );
1859 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1860 psa_mac_operation_t zero;
1861
1862 memset( &zero, 0, sizeof( zero ) );
1863
Jaeden Amero252ef282019-02-15 14:05:35 +00001864 /* A freshly-initialized MAC operation should not be usable. */
1865 TEST_EQUAL( psa_mac_update( &func,
1866 input, sizeof( input ) ),
1867 PSA_ERROR_BAD_STATE );
1868 TEST_EQUAL( psa_mac_update( &init,
1869 input, sizeof( input ) ),
1870 PSA_ERROR_BAD_STATE );
1871 TEST_EQUAL( psa_mac_update( &zero,
1872 input, sizeof( input ) ),
1873 PSA_ERROR_BAD_STATE );
1874
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001875 /* A default MAC operation should be abortable without error. */
1876 PSA_ASSERT( psa_mac_abort( &func ) );
1877 PSA_ASSERT( psa_mac_abort( &init ) );
1878 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001879}
1880/* END_CASE */
1881
1882/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001883void mac_setup( int key_type_arg,
1884 data_t *key,
1885 int alg_arg,
1886 int expected_status_arg )
1887{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001888 psa_key_type_t key_type = key_type_arg;
1889 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001890 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001891 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001892 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1893#if defined(KNOWN_SUPPORTED_MAC_ALG)
1894 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1895#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001896
Gilles Peskine8817f612018-12-18 00:18:46 +01001897 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001898
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001899 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1900 &operation, &status ) )
1901 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001902 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001903
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001904 /* The operation object should be reusable. */
1905#if defined(KNOWN_SUPPORTED_MAC_ALG)
1906 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1907 smoke_test_key_data,
1908 sizeof( smoke_test_key_data ),
1909 KNOWN_SUPPORTED_MAC_ALG,
1910 &operation, &status ) )
1911 goto exit;
1912 TEST_EQUAL( status, PSA_SUCCESS );
1913#endif
1914
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001915exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001916 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001917}
1918/* END_CASE */
1919
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001920/* 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 +00001921void mac_bad_order( )
1922{
Ronald Cron5425a212020-08-04 14:58:35 +02001923 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001924 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1925 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001926 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001927 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1928 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1929 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001930 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001931 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1932 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1933 size_t sign_mac_length = 0;
1934 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1935 const uint8_t verify_mac[] = {
1936 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1937 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1938 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1939
1940 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001941 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001942 psa_set_key_algorithm( &attributes, alg );
1943 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001944
Ronald Cron5425a212020-08-04 14:58:35 +02001945 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1946 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001947
Jaeden Amero252ef282019-02-15 14:05:35 +00001948 /* Call update without calling setup beforehand. */
1949 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1950 PSA_ERROR_BAD_STATE );
1951 PSA_ASSERT( psa_mac_abort( &operation ) );
1952
1953 /* Call sign finish without calling setup beforehand. */
1954 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1955 &sign_mac_length),
1956 PSA_ERROR_BAD_STATE );
1957 PSA_ASSERT( psa_mac_abort( &operation ) );
1958
1959 /* Call verify finish without calling setup beforehand. */
1960 TEST_EQUAL( psa_mac_verify_finish( &operation,
1961 verify_mac, sizeof( verify_mac ) ),
1962 PSA_ERROR_BAD_STATE );
1963 PSA_ASSERT( psa_mac_abort( &operation ) );
1964
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001965 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001966 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001967 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001968 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001969 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001970 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001971 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001972 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001973
Jaeden Amero252ef282019-02-15 14:05:35 +00001974 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001975 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001976 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1977 PSA_ASSERT( psa_mac_sign_finish( &operation,
1978 sign_mac, sizeof( sign_mac ),
1979 &sign_mac_length ) );
1980 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1981 PSA_ERROR_BAD_STATE );
1982 PSA_ASSERT( psa_mac_abort( &operation ) );
1983
1984 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001985 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001986 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1987 PSA_ASSERT( psa_mac_verify_finish( &operation,
1988 verify_mac, sizeof( verify_mac ) ) );
1989 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1990 PSA_ERROR_BAD_STATE );
1991 PSA_ASSERT( psa_mac_abort( &operation ) );
1992
1993 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001994 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001995 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1996 PSA_ASSERT( psa_mac_sign_finish( &operation,
1997 sign_mac, sizeof( sign_mac ),
1998 &sign_mac_length ) );
1999 TEST_EQUAL( psa_mac_sign_finish( &operation,
2000 sign_mac, sizeof( sign_mac ),
2001 &sign_mac_length ),
2002 PSA_ERROR_BAD_STATE );
2003 PSA_ASSERT( psa_mac_abort( &operation ) );
2004
2005 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002006 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002007 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2008 PSA_ASSERT( psa_mac_verify_finish( &operation,
2009 verify_mac, sizeof( verify_mac ) ) );
2010 TEST_EQUAL( psa_mac_verify_finish( &operation,
2011 verify_mac, sizeof( verify_mac ) ),
2012 PSA_ERROR_BAD_STATE );
2013 PSA_ASSERT( psa_mac_abort( &operation ) );
2014
2015 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002016 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002017 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002018 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002019 TEST_EQUAL( psa_mac_verify_finish( &operation,
2020 verify_mac, sizeof( verify_mac ) ),
2021 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002022 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002023 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002024 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002025
2026 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002027 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002028 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002029 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002030 TEST_EQUAL( psa_mac_sign_finish( &operation,
2031 sign_mac, sizeof( sign_mac ),
2032 &sign_mac_length ),
2033 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002034 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002035 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002036 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002037
Ronald Cron5425a212020-08-04 14:58:35 +02002038 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002039
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002040exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002041 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002042}
2043/* END_CASE */
2044
2045/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002046void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002047 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002048 int alg_arg,
2049 data_t *input,
2050 data_t *expected_mac )
2051{
Ronald Cron5425a212020-08-04 14:58:35 +02002052 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002053 psa_key_type_t key_type = key_type_arg;
2054 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002055 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002056 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002057 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002058 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002059 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002060 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002061 const size_t output_sizes_to_test[] = {
2062 0,
2063 1,
2064 expected_mac->len - 1,
2065 expected_mac->len,
2066 expected_mac->len + 1,
2067 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002068
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002069 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002070 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002071 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002072
Gilles Peskine8817f612018-12-18 00:18:46 +01002073 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002074
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002075 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002076 psa_set_key_algorithm( &attributes, alg );
2077 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002078
Ronald Cron5425a212020-08-04 14:58:35 +02002079 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2080 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002081
Gilles Peskine8b356b52020-08-25 23:44:59 +02002082 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2083 {
2084 const size_t output_size = output_sizes_to_test[i];
2085 psa_status_t expected_status =
2086 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2087 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002088
Chris Jones9634bb12021-01-20 15:56:42 +00002089 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002090 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002091
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002092 /* Calculate the MAC, one-shot case. */
2093 TEST_EQUAL( psa_mac_compute( key, alg,
2094 input->x, input->len,
2095 actual_mac, output_size, &mac_length ),
2096 expected_status );
2097 if( expected_status == PSA_SUCCESS )
2098 {
2099 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2100 actual_mac, mac_length );
2101 }
2102
2103 if( output_size > 0 )
2104 memset( actual_mac, 0, output_size );
2105
2106 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002107 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002108 PSA_ASSERT( psa_mac_update( &operation,
2109 input->x, input->len ) );
2110 TEST_EQUAL( psa_mac_sign_finish( &operation,
2111 actual_mac, output_size,
2112 &mac_length ),
2113 expected_status );
2114 PSA_ASSERT( psa_mac_abort( &operation ) );
2115
2116 if( expected_status == PSA_SUCCESS )
2117 {
2118 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2119 actual_mac, mac_length );
2120 }
2121 mbedtls_free( actual_mac );
2122 actual_mac = NULL;
2123 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002124
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002125exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002126 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002127 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002128 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002129 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002130}
2131/* END_CASE */
2132
2133/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002134void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002135 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002136 int alg_arg,
2137 data_t *input,
2138 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002139{
Ronald Cron5425a212020-08-04 14:58:35 +02002140 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002141 psa_key_type_t key_type = key_type_arg;
2142 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002143 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002144 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002145 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002146
Gilles Peskine69c12672018-06-28 00:07:19 +02002147 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2148
Gilles Peskine8817f612018-12-18 00:18:46 +01002149 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002150
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002151 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002152 psa_set_key_algorithm( &attributes, alg );
2153 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002154
Ronald Cron5425a212020-08-04 14:58:35 +02002155 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2156 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002157
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002158 /* Verify correct MAC, one-shot case. */
2159 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2160 expected_mac->x, expected_mac->len ) );
2161
2162 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002163 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002164 PSA_ASSERT( psa_mac_update( &operation,
2165 input->x, input->len ) );
2166 PSA_ASSERT( psa_mac_verify_finish( &operation,
2167 expected_mac->x,
2168 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002169
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002170 /* Test a MAC that's too short, one-shot case. */
2171 TEST_EQUAL( psa_mac_verify( key, alg,
2172 input->x, input->len,
2173 expected_mac->x,
2174 expected_mac->len - 1 ),
2175 PSA_ERROR_INVALID_SIGNATURE );
2176
2177 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002178 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002179 PSA_ASSERT( psa_mac_update( &operation,
2180 input->x, input->len ) );
2181 TEST_EQUAL( psa_mac_verify_finish( &operation,
2182 expected_mac->x,
2183 expected_mac->len - 1 ),
2184 PSA_ERROR_INVALID_SIGNATURE );
2185
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002186 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002187 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2188 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002189 TEST_EQUAL( psa_mac_verify( key, alg,
2190 input->x, input->len,
2191 perturbed_mac, expected_mac->len + 1 ),
2192 PSA_ERROR_INVALID_SIGNATURE );
2193
2194 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002195 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002196 PSA_ASSERT( psa_mac_update( &operation,
2197 input->x, input->len ) );
2198 TEST_EQUAL( psa_mac_verify_finish( &operation,
2199 perturbed_mac,
2200 expected_mac->len + 1 ),
2201 PSA_ERROR_INVALID_SIGNATURE );
2202
2203 /* Test changing one byte. */
2204 for( size_t i = 0; i < expected_mac->len; i++ )
2205 {
Chris Jones9634bb12021-01-20 15:56:42 +00002206 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002207 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002208
2209 TEST_EQUAL( psa_mac_verify( key, alg,
2210 input->x, input->len,
2211 perturbed_mac, expected_mac->len ),
2212 PSA_ERROR_INVALID_SIGNATURE );
2213
Ronald Cron5425a212020-08-04 14:58:35 +02002214 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002215 PSA_ASSERT( psa_mac_update( &operation,
2216 input->x, input->len ) );
2217 TEST_EQUAL( psa_mac_verify_finish( &operation,
2218 perturbed_mac,
2219 expected_mac->len ),
2220 PSA_ERROR_INVALID_SIGNATURE );
2221 perturbed_mac[i] ^= 1;
2222 }
2223
Gilles Peskine8c9def32018-02-08 10:02:12 +01002224exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002225 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002226 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002227 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002228 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002229}
2230/* END_CASE */
2231
2232/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002233void cipher_operation_init( )
2234{
Jaeden Ameroab439972019-02-15 14:12:05 +00002235 const uint8_t input[1] = { 0 };
2236 unsigned char output[1] = { 0 };
2237 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002238 /* Test each valid way of initializing the object, except for `= {0}`, as
2239 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2240 * though it's OK by the C standard. We could test for this, but we'd need
2241 * to supress the Clang warning for the test. */
2242 psa_cipher_operation_t func = psa_cipher_operation_init( );
2243 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2244 psa_cipher_operation_t zero;
2245
2246 memset( &zero, 0, sizeof( zero ) );
2247
Jaeden Ameroab439972019-02-15 14:12:05 +00002248 /* A freshly-initialized cipher operation should not be usable. */
2249 TEST_EQUAL( psa_cipher_update( &func,
2250 input, sizeof( input ),
2251 output, sizeof( output ),
2252 &output_length ),
2253 PSA_ERROR_BAD_STATE );
2254 TEST_EQUAL( psa_cipher_update( &init,
2255 input, sizeof( input ),
2256 output, sizeof( output ),
2257 &output_length ),
2258 PSA_ERROR_BAD_STATE );
2259 TEST_EQUAL( psa_cipher_update( &zero,
2260 input, sizeof( input ),
2261 output, sizeof( output ),
2262 &output_length ),
2263 PSA_ERROR_BAD_STATE );
2264
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002265 /* A default cipher operation should be abortable without error. */
2266 PSA_ASSERT( psa_cipher_abort( &func ) );
2267 PSA_ASSERT( psa_cipher_abort( &init ) );
2268 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002269}
2270/* END_CASE */
2271
2272/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002273void cipher_setup( int key_type_arg,
2274 data_t *key,
2275 int alg_arg,
2276 int expected_status_arg )
2277{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002278 psa_key_type_t key_type = key_type_arg;
2279 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002280 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002281 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002282 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002283#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002284 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2285#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002286
Gilles Peskine8817f612018-12-18 00:18:46 +01002287 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002288
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002289 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2290 &operation, &status ) )
2291 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002292 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002293
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002294 /* The operation object should be reusable. */
2295#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2296 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2297 smoke_test_key_data,
2298 sizeof( smoke_test_key_data ),
2299 KNOWN_SUPPORTED_CIPHER_ALG,
2300 &operation, &status ) )
2301 goto exit;
2302 TEST_EQUAL( status, PSA_SUCCESS );
2303#endif
2304
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002305exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002306 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002307 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002308}
2309/* END_CASE */
2310
Ronald Cronee414c72021-03-18 18:50:08 +01002311/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002312void cipher_bad_order( )
2313{
Ronald Cron5425a212020-08-04 14:58:35 +02002314 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002315 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2316 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002317 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002318 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002319 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002320 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002321 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2322 0xaa, 0xaa, 0xaa, 0xaa };
2323 const uint8_t text[] = {
2324 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2325 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002326 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002327 size_t length = 0;
2328
2329 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002330 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2331 psa_set_key_algorithm( &attributes, alg );
2332 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002333 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2334 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002335
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002336 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002337 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002338 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002339 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002340 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002341 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002342 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002343 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002344
2345 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002346 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002347 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002348 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002349 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002350 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002351 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002352 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002353
Jaeden Ameroab439972019-02-15 14:12:05 +00002354 /* Generate an IV without calling setup beforehand. */
2355 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2356 buffer, sizeof( buffer ),
2357 &length ),
2358 PSA_ERROR_BAD_STATE );
2359 PSA_ASSERT( psa_cipher_abort( &operation ) );
2360
2361 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002362 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002363 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2364 buffer, sizeof( buffer ),
2365 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002366 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002367 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2368 buffer, sizeof( buffer ),
2369 &length ),
2370 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002371 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002372 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002373 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002374
2375 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002376 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002377 PSA_ASSERT( psa_cipher_set_iv( &operation,
2378 iv, sizeof( iv ) ) );
2379 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2380 buffer, sizeof( buffer ),
2381 &length ),
2382 PSA_ERROR_BAD_STATE );
2383 PSA_ASSERT( psa_cipher_abort( &operation ) );
2384
2385 /* Set an IV without calling setup beforehand. */
2386 TEST_EQUAL( psa_cipher_set_iv( &operation,
2387 iv, sizeof( iv ) ),
2388 PSA_ERROR_BAD_STATE );
2389 PSA_ASSERT( psa_cipher_abort( &operation ) );
2390
2391 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002392 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002393 PSA_ASSERT( psa_cipher_set_iv( &operation,
2394 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002395 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002396 TEST_EQUAL( psa_cipher_set_iv( &operation,
2397 iv, sizeof( iv ) ),
2398 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002399 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002400 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002401 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002402
2403 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002404 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002405 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2406 buffer, sizeof( buffer ),
2407 &length ) );
2408 TEST_EQUAL( psa_cipher_set_iv( &operation,
2409 iv, sizeof( iv ) ),
2410 PSA_ERROR_BAD_STATE );
2411 PSA_ASSERT( psa_cipher_abort( &operation ) );
2412
2413 /* Call update without calling setup beforehand. */
2414 TEST_EQUAL( psa_cipher_update( &operation,
2415 text, sizeof( text ),
2416 buffer, sizeof( buffer ),
2417 &length ),
2418 PSA_ERROR_BAD_STATE );
2419 PSA_ASSERT( psa_cipher_abort( &operation ) );
2420
2421 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01002422 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002423 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002424 TEST_EQUAL( psa_cipher_update( &operation,
2425 text, sizeof( text ),
2426 buffer, sizeof( buffer ),
2427 &length ),
2428 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002429 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002430 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002431 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002432
2433 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002434 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002435 PSA_ASSERT( psa_cipher_set_iv( &operation,
2436 iv, sizeof( iv ) ) );
2437 PSA_ASSERT( psa_cipher_finish( &operation,
2438 buffer, sizeof( buffer ), &length ) );
2439 TEST_EQUAL( psa_cipher_update( &operation,
2440 text, sizeof( text ),
2441 buffer, sizeof( buffer ),
2442 &length ),
2443 PSA_ERROR_BAD_STATE );
2444 PSA_ASSERT( psa_cipher_abort( &operation ) );
2445
2446 /* Call finish without calling setup beforehand. */
2447 TEST_EQUAL( psa_cipher_finish( &operation,
2448 buffer, sizeof( buffer ), &length ),
2449 PSA_ERROR_BAD_STATE );
2450 PSA_ASSERT( psa_cipher_abort( &operation ) );
2451
2452 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002453 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002454 /* Not calling update means we are encrypting an empty buffer, which is OK
2455 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01002456 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002457 TEST_EQUAL( psa_cipher_finish( &operation,
2458 buffer, sizeof( buffer ), &length ),
2459 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002460 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002461 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002462 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002463
2464 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002465 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002466 PSA_ASSERT( psa_cipher_set_iv( &operation,
2467 iv, sizeof( iv ) ) );
2468 PSA_ASSERT( psa_cipher_finish( &operation,
2469 buffer, sizeof( buffer ), &length ) );
2470 TEST_EQUAL( psa_cipher_finish( &operation,
2471 buffer, sizeof( buffer ), &length ),
2472 PSA_ERROR_BAD_STATE );
2473 PSA_ASSERT( psa_cipher_abort( &operation ) );
2474
Ronald Cron5425a212020-08-04 14:58:35 +02002475 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002476
Jaeden Ameroab439972019-02-15 14:12:05 +00002477exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002478 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002479 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002480}
2481/* END_CASE */
2482
2483/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002484void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002485 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002486 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002487 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002488{
Ronald Cron5425a212020-08-04 14:58:35 +02002489 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002490 psa_status_t status;
2491 psa_key_type_t key_type = key_type_arg;
2492 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002493 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002494 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002495 size_t output_buffer_size = 0;
2496 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002497 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002498 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002499 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002500
Gilles Peskine8817f612018-12-18 00:18:46 +01002501 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002502
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002503 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2504 psa_set_key_algorithm( &attributes, alg );
2505 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002506
Ronald Cron5425a212020-08-04 14:58:35 +02002507 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2508 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002509
Ronald Cron5425a212020-08-04 14:58:35 +02002510 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002511
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002512 if( iv->len > 0 )
2513 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002514 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002515 }
2516
gabor-mezei-armceface22021-01-21 12:26:17 +01002517 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2518 TEST_ASSERT( output_buffer_size <=
2519 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002520 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002521
Gilles Peskine8817f612018-12-18 00:18:46 +01002522 PSA_ASSERT( psa_cipher_update( &operation,
2523 input->x, input->len,
2524 output, output_buffer_size,
2525 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002526 TEST_ASSERT( function_output_length <=
2527 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2528 TEST_ASSERT( function_output_length <=
2529 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002530 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002531
Gilles Peskine50e586b2018-06-08 14:28:46 +02002532 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002533 ( output_buffer_size == 0 ? NULL :
2534 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002535 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002536 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002537 TEST_ASSERT( function_output_length <=
2538 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2539 TEST_ASSERT( function_output_length <=
2540 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002541 total_output_length += function_output_length;
2542
Gilles Peskinefe11b722018-12-18 00:24:04 +01002543 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002544 if( expected_status == PSA_SUCCESS )
2545 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002546 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002547 ASSERT_COMPARE( expected_output->x, expected_output->len,
2548 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002549 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002550
Gilles Peskine50e586b2018-06-08 14:28:46 +02002551exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002552 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002553 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002554 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002555 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002556}
2557/* END_CASE */
2558
2559/* BEGIN_CASE */
2560void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002561 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002562 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002563 int first_part_size_arg,
2564 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002565 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002566{
Ronald Cron5425a212020-08-04 14:58:35 +02002567 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002568 psa_key_type_t key_type = key_type_arg;
2569 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002570 size_t first_part_size = first_part_size_arg;
2571 size_t output1_length = output1_length_arg;
2572 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002573 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002574 size_t output_buffer_size = 0;
2575 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002576 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002577 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002578 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002579
Gilles Peskine8817f612018-12-18 00:18:46 +01002580 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002581
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002582 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2583 psa_set_key_algorithm( &attributes, alg );
2584 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002585
Ronald Cron5425a212020-08-04 14:58:35 +02002586 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2587 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002588
Ronald Cron5425a212020-08-04 14:58:35 +02002589 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002590
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002591 if( iv->len > 0 )
2592 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002593 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002594 }
2595
gabor-mezei-armceface22021-01-21 12:26:17 +01002596 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2597 TEST_ASSERT( output_buffer_size <=
2598 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002599 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002600
Gilles Peskinee0866522019-02-19 19:44:00 +01002601 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002602 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2603 output, output_buffer_size,
2604 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002605 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002606 TEST_ASSERT( function_output_length <=
2607 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2608 TEST_ASSERT( function_output_length <=
2609 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002610 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002611
Gilles Peskine8817f612018-12-18 00:18:46 +01002612 PSA_ASSERT( psa_cipher_update( &operation,
2613 input->x + first_part_size,
2614 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002615 ( output_buffer_size == 0 ? NULL :
2616 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002617 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002618 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002619 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002620 TEST_ASSERT( function_output_length <=
2621 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2622 alg,
2623 input->len - first_part_size ) );
2624 TEST_ASSERT( function_output_length <=
2625 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002626 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002627
Gilles Peskine8817f612018-12-18 00:18:46 +01002628 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002629 ( output_buffer_size == 0 ? NULL :
2630 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002631 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002632 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002633 TEST_ASSERT( function_output_length <=
2634 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2635 TEST_ASSERT( function_output_length <=
2636 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002637 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002638 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002639
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002640 ASSERT_COMPARE( expected_output->x, expected_output->len,
2641 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002642
2643exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002644 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002645 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002646 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002647 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002648}
2649/* END_CASE */
2650
2651/* BEGIN_CASE */
2652void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002653 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002654 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002655 int first_part_size_arg,
2656 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002657 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002658{
Ronald Cron5425a212020-08-04 14:58:35 +02002659 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002660 psa_key_type_t key_type = key_type_arg;
2661 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002662 size_t first_part_size = first_part_size_arg;
2663 size_t output1_length = output1_length_arg;
2664 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002665 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002666 size_t output_buffer_size = 0;
2667 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002668 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002669 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002670 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002671
Gilles Peskine8817f612018-12-18 00:18:46 +01002672 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002673
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002674 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2675 psa_set_key_algorithm( &attributes, alg );
2676 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002677
Ronald Cron5425a212020-08-04 14:58:35 +02002678 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2679 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002680
Ronald Cron5425a212020-08-04 14:58:35 +02002681 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002682
Steven Cooreman177deba2020-09-07 17:14:14 +02002683 if( iv->len > 0 )
2684 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002685 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002686 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002687
gabor-mezei-armceface22021-01-21 12:26:17 +01002688 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2689 TEST_ASSERT( output_buffer_size <=
2690 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002691 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002692
Gilles Peskinee0866522019-02-19 19:44:00 +01002693 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002694 PSA_ASSERT( psa_cipher_update( &operation,
2695 input->x, first_part_size,
2696 output, output_buffer_size,
2697 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002698 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002699 TEST_ASSERT( function_output_length <=
2700 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2701 TEST_ASSERT( function_output_length <=
2702 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002703 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002704
Gilles Peskine8817f612018-12-18 00:18:46 +01002705 PSA_ASSERT( psa_cipher_update( &operation,
2706 input->x + first_part_size,
2707 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002708 ( output_buffer_size == 0 ? NULL :
2709 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002710 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002711 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002712 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002713 TEST_ASSERT( function_output_length <=
2714 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2715 alg,
2716 input->len - first_part_size ) );
2717 TEST_ASSERT( function_output_length <=
2718 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002719 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002720
Gilles Peskine8817f612018-12-18 00:18:46 +01002721 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002722 ( output_buffer_size == 0 ? NULL :
2723 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002724 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002725 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002726 TEST_ASSERT( function_output_length <=
2727 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2728 TEST_ASSERT( function_output_length <=
2729 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002730 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002731 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002732
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002733 ASSERT_COMPARE( expected_output->x, expected_output->len,
2734 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002735
2736exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002737 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002738 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002739 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002740 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002741}
2742/* END_CASE */
2743
Gilles Peskine50e586b2018-06-08 14:28:46 +02002744/* BEGIN_CASE */
2745void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002746 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002747 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002748 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002749{
Ronald Cron5425a212020-08-04 14:58:35 +02002750 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002751 psa_status_t status;
2752 psa_key_type_t key_type = key_type_arg;
2753 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002754 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002755 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002756 size_t output_buffer_size = 0;
2757 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002758 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002759 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002760 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002761
Gilles Peskine8817f612018-12-18 00:18:46 +01002762 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002763
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002764 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2765 psa_set_key_algorithm( &attributes, alg );
2766 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002767
Ronald Cron5425a212020-08-04 14:58:35 +02002768 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2769 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002770
Ronald Cron5425a212020-08-04 14:58:35 +02002771 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002772
Steven Cooreman177deba2020-09-07 17:14:14 +02002773 if( iv->len > 0 )
2774 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002775 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002776 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002777
gabor-mezei-armceface22021-01-21 12:26:17 +01002778 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2779 TEST_ASSERT( output_buffer_size <=
2780 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002781 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002782
Gilles Peskine8817f612018-12-18 00:18:46 +01002783 PSA_ASSERT( psa_cipher_update( &operation,
2784 input->x, input->len,
2785 output, output_buffer_size,
2786 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002787 TEST_ASSERT( function_output_length <=
2788 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2789 TEST_ASSERT( function_output_length <=
2790 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002791 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002792
Gilles Peskine50e586b2018-06-08 14:28:46 +02002793 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002794 ( output_buffer_size == 0 ? NULL :
2795 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002796 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002797 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002798 TEST_ASSERT( function_output_length <=
2799 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2800 TEST_ASSERT( function_output_length <=
2801 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002802 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002803 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002804
2805 if( expected_status == PSA_SUCCESS )
2806 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002807 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002808 ASSERT_COMPARE( expected_output->x, expected_output->len,
2809 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002810 }
2811
Gilles Peskine50e586b2018-06-08 14:28:46 +02002812exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002813 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002814 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002815 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002816 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002817}
2818/* END_CASE */
2819
Gilles Peskine50e586b2018-06-08 14:28:46 +02002820/* BEGIN_CASE */
2821void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002822 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002823 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002824{
Ronald Cron5425a212020-08-04 14:58:35 +02002825 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002826 psa_key_type_t key_type = key_type_arg;
2827 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002828 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002829 size_t iv_size = 16;
2830 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002831 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002832 size_t output1_size = 0;
2833 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002834 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002835 size_t output2_size = 0;
2836 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002837 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002838 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2839 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002840 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002841
Gilles Peskine8817f612018-12-18 00:18:46 +01002842 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002843
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002844 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2845 psa_set_key_algorithm( &attributes, alg );
2846 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002847
Ronald Cron5425a212020-08-04 14:58:35 +02002848 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2849 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002850
Ronald Cron5425a212020-08-04 14:58:35 +02002851 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2852 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002853
Steven Cooreman177deba2020-09-07 17:14:14 +02002854 if( alg != PSA_ALG_ECB_NO_PADDING )
2855 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002856 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2857 iv, iv_size,
2858 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002859 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002860 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2861 TEST_ASSERT( output1_size <=
2862 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002863 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002864
Gilles Peskine8817f612018-12-18 00:18:46 +01002865 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2866 output1, output1_size,
2867 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002868 TEST_ASSERT( output1_length <=
2869 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2870 TEST_ASSERT( output1_length <=
2871 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2872
Gilles Peskine8817f612018-12-18 00:18:46 +01002873 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002874 output1 + output1_length,
2875 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002876 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002877 TEST_ASSERT( function_output_length <=
2878 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2879 TEST_ASSERT( function_output_length <=
2880 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002881
Gilles Peskine048b7f02018-06-08 14:20:49 +02002882 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002883
Gilles Peskine8817f612018-12-18 00:18:46 +01002884 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002885
2886 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002887 TEST_ASSERT( output2_size <=
2888 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2889 TEST_ASSERT( output2_size <=
2890 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002891 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002892
Steven Cooreman177deba2020-09-07 17:14:14 +02002893 if( iv_length > 0 )
2894 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002895 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2896 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002897 }
2898
Gilles Peskine8817f612018-12-18 00:18:46 +01002899 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2900 output2, output2_size,
2901 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002902 TEST_ASSERT( output2_length <=
2903 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
2904 TEST_ASSERT( output2_length <=
2905 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
2906
Gilles Peskine048b7f02018-06-08 14:20:49 +02002907 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002908 PSA_ASSERT( psa_cipher_finish( &operation2,
2909 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002910 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002911 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002912 TEST_ASSERT( function_output_length <=
2913 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2914 TEST_ASSERT( function_output_length <=
2915 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03002916
Gilles Peskine048b7f02018-06-08 14:20:49 +02002917 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002918
Gilles Peskine8817f612018-12-18 00:18:46 +01002919 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002920
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002921 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002922
2923exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002924 psa_cipher_abort( &operation1 );
2925 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002926 mbedtls_free( output1 );
2927 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002928 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002929 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03002930}
2931/* END_CASE */
2932
2933/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002934void cipher_verify_output_multipart( int alg_arg,
2935 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002936 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002937 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002938 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03002939{
Ronald Cron5425a212020-08-04 14:58:35 +02002940 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002941 psa_key_type_t key_type = key_type_arg;
2942 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002943 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002944 unsigned char iv[16] = {0};
2945 size_t iv_size = 16;
2946 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002947 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002948 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002949 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002950 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002951 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002952 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002953 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002954 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2955 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002956 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002957
Gilles Peskine8817f612018-12-18 00:18:46 +01002958 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002959
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002960 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2961 psa_set_key_algorithm( &attributes, alg );
2962 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002963
Ronald Cron5425a212020-08-04 14:58:35 +02002964 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2965 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03002966
Ronald Cron5425a212020-08-04 14:58:35 +02002967 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2968 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002969
Steven Cooreman177deba2020-09-07 17:14:14 +02002970 if( alg != PSA_ALG_ECB_NO_PADDING )
2971 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002972 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2973 iv, iv_size,
2974 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002975 }
2976
gabor-mezei-armceface22021-01-21 12:26:17 +01002977 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2978 TEST_ASSERT( output1_buffer_size <=
2979 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002980 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002981
Gilles Peskinee0866522019-02-19 19:44:00 +01002982 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002983
Gilles Peskine8817f612018-12-18 00:18:46 +01002984 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2985 output1, output1_buffer_size,
2986 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002987 TEST_ASSERT( function_output_length <=
2988 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2989 TEST_ASSERT( function_output_length <=
2990 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002991 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002992
Gilles Peskine8817f612018-12-18 00:18:46 +01002993 PSA_ASSERT( psa_cipher_update( &operation1,
2994 input->x + first_part_size,
2995 input->len - first_part_size,
2996 output1, output1_buffer_size,
2997 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002998 TEST_ASSERT( function_output_length <=
2999 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3000 alg,
3001 input->len - first_part_size ) );
3002 TEST_ASSERT( function_output_length <=
3003 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003004 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003005
Gilles Peskine8817f612018-12-18 00:18:46 +01003006 PSA_ASSERT( psa_cipher_finish( &operation1,
3007 output1 + output1_length,
3008 output1_buffer_size - output1_length,
3009 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003010 TEST_ASSERT( function_output_length <=
3011 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3012 TEST_ASSERT( function_output_length <=
3013 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003014 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003015
Gilles Peskine8817f612018-12-18 00:18:46 +01003016 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003017
Gilles Peskine048b7f02018-06-08 14:20:49 +02003018 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003019 TEST_ASSERT( output2_buffer_size <=
3020 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3021 TEST_ASSERT( output2_buffer_size <=
3022 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003023 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003024
Steven Cooreman177deba2020-09-07 17:14:14 +02003025 if( iv_length > 0 )
3026 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003027 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3028 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003029 }
Moran Pekerded84402018-06-06 16:36:50 +03003030
Gilles Peskine8817f612018-12-18 00:18:46 +01003031 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3032 output2, output2_buffer_size,
3033 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003034 TEST_ASSERT( function_output_length <=
3035 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3036 TEST_ASSERT( function_output_length <=
3037 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003038 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003039
Gilles Peskine8817f612018-12-18 00:18:46 +01003040 PSA_ASSERT( psa_cipher_update( &operation2,
3041 output1 + first_part_size,
3042 output1_length - first_part_size,
3043 output2, output2_buffer_size,
3044 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003045 TEST_ASSERT( function_output_length <=
3046 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3047 alg,
3048 output1_length - first_part_size ) );
3049 TEST_ASSERT( function_output_length <=
3050 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003051 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003052
Gilles Peskine8817f612018-12-18 00:18:46 +01003053 PSA_ASSERT( psa_cipher_finish( &operation2,
3054 output2 + output2_length,
3055 output2_buffer_size - output2_length,
3056 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003057 TEST_ASSERT( function_output_length <=
3058 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3059 TEST_ASSERT( function_output_length <=
3060 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003061 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003062
Gilles Peskine8817f612018-12-18 00:18:46 +01003063 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003064
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003065 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003066
3067exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003068 psa_cipher_abort( &operation1 );
3069 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003070 mbedtls_free( output1 );
3071 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003072 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003073 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003074}
3075/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003076
Gilles Peskine20035e32018-02-03 22:44:14 +01003077/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003078void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003079 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003080 data_t *nonce,
3081 data_t *additional_data,
3082 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003083 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003084{
Ronald Cron5425a212020-08-04 14:58:35 +02003085 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003086 psa_key_type_t key_type = key_type_arg;
3087 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003088 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003089 unsigned char *output_data = NULL;
3090 size_t output_size = 0;
3091 size_t output_length = 0;
3092 unsigned char *output_data2 = NULL;
3093 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003094 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003095 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003096 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003097
Gilles Peskine8817f612018-12-18 00:18:46 +01003098 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003099
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003100 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3101 psa_set_key_algorithm( &attributes, alg );
3102 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003103
Gilles Peskine049c7532019-05-15 20:22:09 +02003104 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003105 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003106 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3107 key_bits = psa_get_key_bits( &attributes );
3108
3109 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3110 alg );
3111 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3112 * should be exact. */
3113 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3114 expected_result != PSA_ERROR_NOT_SUPPORTED )
3115 {
3116 TEST_EQUAL( output_size,
3117 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3118 TEST_ASSERT( output_size <=
3119 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3120 }
3121 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003122
Steven Cooremanf49478b2021-02-15 15:19:25 +01003123 status = psa_aead_encrypt( key, alg,
3124 nonce->x, nonce->len,
3125 additional_data->x,
3126 additional_data->len,
3127 input_data->x, input_data->len,
3128 output_data, output_size,
3129 &output_length );
3130
3131 /* If the operation is not supported, just skip and not fail in case the
3132 * encryption involves a common limitation of cryptography hardwares and
3133 * an alternative implementation. */
3134 if( status == PSA_ERROR_NOT_SUPPORTED )
3135 {
3136 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3137 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3138 }
3139
3140 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003141
3142 if( PSA_SUCCESS == expected_result )
3143 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003144 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003145
Gilles Peskine003a4a92019-05-14 16:09:40 +02003146 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3147 * should be exact. */
3148 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003149 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003150
gabor-mezei-armceface22021-01-21 12:26:17 +01003151 TEST_ASSERT( input_data->len <=
3152 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3153
Ronald Cron5425a212020-08-04 14:58:35 +02003154 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003155 nonce->x, nonce->len,
3156 additional_data->x,
3157 additional_data->len,
3158 output_data, output_length,
3159 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003160 &output_length2 ),
3161 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003162
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003163 ASSERT_COMPARE( input_data->x, input_data->len,
3164 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003165 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003166
Gilles Peskinea1cac842018-06-11 19:33:02 +02003167exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003168 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003169 mbedtls_free( output_data );
3170 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003171 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003172}
3173/* END_CASE */
3174
3175/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003176void aead_encrypt( int key_type_arg, data_t *key_data,
3177 int alg_arg,
3178 data_t *nonce,
3179 data_t *additional_data,
3180 data_t *input_data,
3181 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003182{
Ronald Cron5425a212020-08-04 14:58:35 +02003183 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003184 psa_key_type_t key_type = key_type_arg;
3185 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003186 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003187 unsigned char *output_data = NULL;
3188 size_t output_size = 0;
3189 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003190 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003191 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003192
Gilles Peskine8817f612018-12-18 00:18:46 +01003193 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003194
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003195 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3196 psa_set_key_algorithm( &attributes, alg );
3197 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003198
Gilles Peskine049c7532019-05-15 20:22:09 +02003199 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003200 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003201 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3202 key_bits = psa_get_key_bits( &attributes );
3203
3204 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3205 alg );
3206 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3207 * should be exact. */
3208 TEST_EQUAL( output_size,
3209 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3210 TEST_ASSERT( output_size <=
3211 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3212 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003213
Steven Cooremand588ea12021-01-11 19:36:04 +01003214 status = psa_aead_encrypt( key, alg,
3215 nonce->x, nonce->len,
3216 additional_data->x, additional_data->len,
3217 input_data->x, input_data->len,
3218 output_data, output_size,
3219 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003220
Ronald Cron28a45ed2021-02-09 20:35:42 +01003221 /* If the operation is not supported, just skip and not fail in case the
3222 * encryption involves a common limitation of cryptography hardwares and
3223 * an alternative implementation. */
3224 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003225 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003226 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3227 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003228 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003229
3230 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003231 ASSERT_COMPARE( expected_result->x, expected_result->len,
3232 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003233
Gilles Peskinea1cac842018-06-11 19:33:02 +02003234exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003235 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003236 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003237 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003238}
3239/* END_CASE */
3240
3241/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003242void aead_decrypt( int key_type_arg, data_t *key_data,
3243 int alg_arg,
3244 data_t *nonce,
3245 data_t *additional_data,
3246 data_t *input_data,
3247 data_t *expected_data,
3248 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003249{
Ronald Cron5425a212020-08-04 14:58:35 +02003250 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003251 psa_key_type_t key_type = key_type_arg;
3252 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003253 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003254 unsigned char *output_data = NULL;
3255 size_t output_size = 0;
3256 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003257 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003258 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003259 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003260
Gilles Peskine8817f612018-12-18 00:18:46 +01003261 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003262
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003263 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3264 psa_set_key_algorithm( &attributes, alg );
3265 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003266
Gilles Peskine049c7532019-05-15 20:22:09 +02003267 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003268 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003269 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3270 key_bits = psa_get_key_bits( &attributes );
3271
3272 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3273 alg );
3274 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3275 expected_result != PSA_ERROR_NOT_SUPPORTED )
3276 {
3277 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3278 * should be exact. */
3279 TEST_EQUAL( output_size,
3280 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3281 TEST_ASSERT( output_size <=
3282 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3283 }
3284 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003285
Steven Cooremand588ea12021-01-11 19:36:04 +01003286 status = psa_aead_decrypt( key, alg,
3287 nonce->x, nonce->len,
3288 additional_data->x,
3289 additional_data->len,
3290 input_data->x, input_data->len,
3291 output_data, output_size,
3292 &output_length );
3293
Ronald Cron28a45ed2021-02-09 20:35:42 +01003294 /* If the operation is not supported, just skip and not fail in case the
3295 * decryption involves a common limitation of cryptography hardwares and
3296 * an alternative implementation. */
3297 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003298 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003299 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3300 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003301 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003302
3303 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003304
Gilles Peskine2d277862018-06-18 15:41:12 +02003305 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003306 ASSERT_COMPARE( expected_data->x, expected_data->len,
3307 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003308
Gilles Peskinea1cac842018-06-11 19:33:02 +02003309exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003310 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003311 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003312 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003313}
3314/* END_CASE */
3315
3316/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003317void signature_size( int type_arg,
3318 int bits,
3319 int alg_arg,
3320 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003321{
3322 psa_key_type_t type = type_arg;
3323 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003324 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003325
Gilles Peskinefe11b722018-12-18 00:24:04 +01003326 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003327
Gilles Peskinee59236f2018-01-27 23:32:46 +01003328exit:
3329 ;
3330}
3331/* END_CASE */
3332
3333/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003334void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3335 int alg_arg, data_t *input_data,
3336 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003337{
Ronald Cron5425a212020-08-04 14:58:35 +02003338 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003339 psa_key_type_t key_type = key_type_arg;
3340 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003341 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003342 unsigned char *signature = NULL;
3343 size_t signature_size;
3344 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003345 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003346
Gilles Peskine8817f612018-12-18 00:18:46 +01003347 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003348
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003349 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003350 psa_set_key_algorithm( &attributes, alg );
3351 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003352
Gilles Peskine049c7532019-05-15 20:22:09 +02003353 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003354 &key ) );
3355 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003356 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003357
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003358 /* Allocate a buffer which has the size advertized by the
3359 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003360 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003361 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003362 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003363 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003364 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003365
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003366 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003367 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003368 input_data->x, input_data->len,
3369 signature, signature_size,
3370 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003371 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003372 ASSERT_COMPARE( output_data->x, output_data->len,
3373 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003374
3375exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003376 /*
3377 * Key attributes may have been returned by psa_get_key_attributes()
3378 * thus reset them as required.
3379 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003380 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003381
Ronald Cron5425a212020-08-04 14:58:35 +02003382 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003383 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003384 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003385}
3386/* END_CASE */
3387
3388/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003389void sign_hash_fail( int key_type_arg, data_t *key_data,
3390 int alg_arg, data_t *input_data,
3391 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003392{
Ronald Cron5425a212020-08-04 14:58:35 +02003393 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003394 psa_key_type_t key_type = key_type_arg;
3395 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003396 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003397 psa_status_t actual_status;
3398 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003399 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003400 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003401 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003402
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003403 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003404
Gilles Peskine8817f612018-12-18 00:18:46 +01003405 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003406
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003407 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003408 psa_set_key_algorithm( &attributes, alg );
3409 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003410
Gilles Peskine049c7532019-05-15 20:22:09 +02003411 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003412 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003413
Ronald Cron5425a212020-08-04 14:58:35 +02003414 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003415 input_data->x, input_data->len,
3416 signature, signature_size,
3417 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003418 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003419 /* The value of *signature_length is unspecified on error, but
3420 * whatever it is, it should be less than signature_size, so that
3421 * if the caller tries to read *signature_length bytes without
3422 * checking the error code then they don't overflow a buffer. */
3423 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003424
3425exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003426 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003427 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003428 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003429 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003430}
3431/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003432
3433/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003434void sign_verify_hash( int key_type_arg, data_t *key_data,
3435 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003436{
Ronald Cron5425a212020-08-04 14:58:35 +02003437 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003438 psa_key_type_t key_type = key_type_arg;
3439 psa_algorithm_t alg = alg_arg;
3440 size_t key_bits;
3441 unsigned char *signature = NULL;
3442 size_t signature_size;
3443 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003444 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003445
Gilles Peskine8817f612018-12-18 00:18:46 +01003446 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003447
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003448 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003449 psa_set_key_algorithm( &attributes, alg );
3450 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003451
Gilles Peskine049c7532019-05-15 20:22:09 +02003452 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003453 &key ) );
3454 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003455 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003456
3457 /* Allocate a buffer which has the size advertized by the
3458 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003459 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003460 key_bits, alg );
3461 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003462 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003463 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003464
3465 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003466 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003467 input_data->x, input_data->len,
3468 signature, signature_size,
3469 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003470 /* Check that the signature length looks sensible. */
3471 TEST_ASSERT( signature_length <= signature_size );
3472 TEST_ASSERT( signature_length > 0 );
3473
3474 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003475 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003476 input_data->x, input_data->len,
3477 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003478
3479 if( input_data->len != 0 )
3480 {
3481 /* Flip a bit in the input and verify that the signature is now
3482 * detected as invalid. Flip a bit at the beginning, not at the end,
3483 * because ECDSA may ignore the last few bits of the input. */
3484 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003485 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003486 input_data->x, input_data->len,
3487 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003488 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003489 }
3490
3491exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003492 /*
3493 * Key attributes may have been returned by psa_get_key_attributes()
3494 * thus reset them as required.
3495 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003496 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003497
Ronald Cron5425a212020-08-04 14:58:35 +02003498 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003499 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003500 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003501}
3502/* END_CASE */
3503
3504/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003505void verify_hash( int key_type_arg, data_t *key_data,
3506 int alg_arg, data_t *hash_data,
3507 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003508{
Ronald Cron5425a212020-08-04 14:58:35 +02003509 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003510 psa_key_type_t key_type = key_type_arg;
3511 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003512 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003513
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003514 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003515
Gilles Peskine8817f612018-12-18 00:18:46 +01003516 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003517
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003518 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003519 psa_set_key_algorithm( &attributes, alg );
3520 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003521
Gilles Peskine049c7532019-05-15 20:22:09 +02003522 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003523 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003524
Ronald Cron5425a212020-08-04 14:58:35 +02003525 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003526 hash_data->x, hash_data->len,
3527 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003528
itayzafrir5c753392018-05-08 11:18:38 +03003529exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003530 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003531 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003532 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003533}
3534/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003535
3536/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003537void verify_hash_fail( int key_type_arg, data_t *key_data,
3538 int alg_arg, data_t *hash_data,
3539 data_t *signature_data,
3540 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003541{
Ronald Cron5425a212020-08-04 14:58:35 +02003542 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003543 psa_key_type_t key_type = key_type_arg;
3544 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003545 psa_status_t actual_status;
3546 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003547 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003548
Gilles Peskine8817f612018-12-18 00:18:46 +01003549 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003550
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003551 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003552 psa_set_key_algorithm( &attributes, alg );
3553 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003554
Gilles Peskine049c7532019-05-15 20:22:09 +02003555 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003556 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003557
Ronald Cron5425a212020-08-04 14:58:35 +02003558 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003559 hash_data->x, hash_data->len,
3560 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003561 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003562
3563exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003564 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003565 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003566 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003567}
3568/* END_CASE */
3569
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003570/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02003571void sign_message_deterministic( int key_type_arg,
3572 data_t *key_data,
3573 int alg_arg,
3574 data_t *input_data,
3575 data_t *output_data )
3576{
3577 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3578 psa_key_type_t key_type = key_type_arg;
3579 psa_algorithm_t alg = alg_arg;
3580 size_t key_bits;
3581 unsigned char *signature = NULL;
3582 size_t signature_size;
3583 size_t signature_length = 0xdeadbeef;
3584 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3585
3586 PSA_ASSERT( psa_crypto_init( ) );
3587
3588 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3589 psa_set_key_algorithm( &attributes, alg );
3590 psa_set_key_type( &attributes, key_type );
3591
3592 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3593 &key ) );
3594 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3595 key_bits = psa_get_key_bits( &attributes );
3596
3597 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3598 TEST_ASSERT( signature_size != 0 );
3599 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3600 ASSERT_ALLOC( signature, signature_size );
3601
3602 PSA_ASSERT( psa_sign_message( key, alg,
3603 input_data->x, input_data->len,
3604 signature, signature_size,
3605 &signature_length ) );
3606
3607 ASSERT_COMPARE( output_data->x, output_data->len,
3608 signature, signature_length );
3609
3610exit:
3611 psa_reset_key_attributes( &attributes );
3612
3613 psa_destroy_key( key );
3614 mbedtls_free( signature );
3615 PSA_DONE( );
3616
3617}
3618/* END_CASE */
3619
3620/* BEGIN_CASE */
3621void sign_message_fail( int key_type_arg,
3622 data_t *key_data,
3623 int alg_arg,
3624 data_t *input_data,
3625 int signature_size_arg,
3626 int expected_status_arg )
3627{
3628 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3629 psa_key_type_t key_type = key_type_arg;
3630 psa_algorithm_t alg = alg_arg;
3631 size_t signature_size = signature_size_arg;
3632 psa_status_t actual_status;
3633 psa_status_t expected_status = expected_status_arg;
3634 unsigned char *signature = NULL;
3635 size_t signature_length = 0xdeadbeef;
3636 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3637
3638 ASSERT_ALLOC( signature, signature_size );
3639
3640 PSA_ASSERT( psa_crypto_init( ) );
3641
3642 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3643 psa_set_key_algorithm( &attributes, alg );
3644 psa_set_key_type( &attributes, key_type );
3645
3646 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3647 &key ) );
3648
3649 actual_status = psa_sign_message( key, alg,
3650 input_data->x, input_data->len,
3651 signature, signature_size,
3652 &signature_length );
3653 TEST_EQUAL( actual_status, expected_status );
3654 /* The value of *signature_length is unspecified on error, but
3655 * whatever it is, it should be less than signature_size, so that
3656 * if the caller tries to read *signature_length bytes without
3657 * checking the error code then they don't overflow a buffer. */
3658 TEST_ASSERT( signature_length <= signature_size );
3659
3660exit:
3661 psa_reset_key_attributes( &attributes );
3662 psa_destroy_key( key );
3663 mbedtls_free( signature );
3664 PSA_DONE( );
3665}
3666/* END_CASE */
3667
3668/* BEGIN_CASE */
3669void sign_verify_message( int key_type_arg,
3670 data_t *key_data,
3671 int alg_arg,
3672 data_t *input_data )
3673{
3674 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3675 psa_key_type_t key_type = key_type_arg;
3676 psa_algorithm_t alg = alg_arg;
3677 size_t key_bits;
3678 unsigned char *signature = NULL;
3679 size_t signature_size;
3680 size_t signature_length = 0xdeadbeef;
3681 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3682
3683 PSA_ASSERT( psa_crypto_init( ) );
3684
3685 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3686 PSA_KEY_USAGE_VERIFY_MESSAGE );
3687 psa_set_key_algorithm( &attributes, alg );
3688 psa_set_key_type( &attributes, key_type );
3689
3690 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3691 &key ) );
3692 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3693 key_bits = psa_get_key_bits( &attributes );
3694
3695 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3696 TEST_ASSERT( signature_size != 0 );
3697 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3698 ASSERT_ALLOC( signature, signature_size );
3699
3700 PSA_ASSERT( psa_sign_message( key, alg,
3701 input_data->x, input_data->len,
3702 signature, signature_size,
3703 &signature_length ) );
3704 TEST_ASSERT( signature_length <= signature_size );
3705 TEST_ASSERT( signature_length > 0 );
3706
3707 PSA_ASSERT( psa_verify_message( key, alg,
3708 input_data->x, input_data->len,
3709 signature, signature_length ) );
3710
3711 if( input_data->len != 0 )
3712 {
3713 /* Flip a bit in the input and verify that the signature is now
3714 * detected as invalid. Flip a bit at the beginning, not at the end,
3715 * because ECDSA may ignore the last few bits of the input. */
3716 input_data->x[0] ^= 1;
3717 TEST_EQUAL( psa_verify_message( key, alg,
3718 input_data->x, input_data->len,
3719 signature, signature_length ),
3720 PSA_ERROR_INVALID_SIGNATURE );
3721 }
3722
3723exit:
3724 psa_reset_key_attributes( &attributes );
3725
3726 psa_destroy_key( key );
3727 mbedtls_free( signature );
3728 PSA_DONE( );
3729}
3730/* END_CASE */
3731
3732/* BEGIN_CASE */
3733void verify_message( int key_type_arg,
3734 data_t *key_data,
3735 int alg_arg,
3736 data_t *input_data,
3737 data_t *signature_data )
3738{
3739 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3740 psa_key_type_t key_type = key_type_arg;
3741 psa_algorithm_t alg = alg_arg;
3742 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3743
3744 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3745
3746 PSA_ASSERT( psa_crypto_init( ) );
3747
3748 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3749 psa_set_key_algorithm( &attributes, alg );
3750 psa_set_key_type( &attributes, key_type );
3751
3752 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3753 &key ) );
3754
3755 PSA_ASSERT( psa_verify_message( key, alg,
3756 input_data->x, input_data->len,
3757 signature_data->x, signature_data->len ) );
3758
3759exit:
3760 psa_reset_key_attributes( &attributes );
3761 psa_destroy_key( key );
3762 PSA_DONE( );
3763}
3764/* END_CASE */
3765
3766/* BEGIN_CASE */
3767void verify_message_fail( int key_type_arg,
3768 data_t *key_data,
3769 int alg_arg,
3770 data_t *hash_data,
3771 data_t *signature_data,
3772 int expected_status_arg )
3773{
3774 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3775 psa_key_type_t key_type = key_type_arg;
3776 psa_algorithm_t alg = alg_arg;
3777 psa_status_t actual_status;
3778 psa_status_t expected_status = expected_status_arg;
3779 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3780
3781 PSA_ASSERT( psa_crypto_init( ) );
3782
3783 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3784 psa_set_key_algorithm( &attributes, alg );
3785 psa_set_key_type( &attributes, key_type );
3786
3787 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3788 &key ) );
3789
3790 actual_status = psa_verify_message( key, alg,
3791 hash_data->x, hash_data->len,
3792 signature_data->x,
3793 signature_data->len );
3794 TEST_EQUAL( actual_status, expected_status );
3795
3796exit:
3797 psa_reset_key_attributes( &attributes );
3798 psa_destroy_key( key );
3799 PSA_DONE( );
3800}
3801/* END_CASE */
3802
3803/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003804void asymmetric_encrypt( int key_type_arg,
3805 data_t *key_data,
3806 int alg_arg,
3807 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003808 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003809 int expected_output_length_arg,
3810 int expected_status_arg )
3811{
Ronald Cron5425a212020-08-04 14:58:35 +02003812 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003813 psa_key_type_t key_type = key_type_arg;
3814 psa_algorithm_t alg = alg_arg;
3815 size_t expected_output_length = expected_output_length_arg;
3816 size_t key_bits;
3817 unsigned char *output = NULL;
3818 size_t output_size;
3819 size_t output_length = ~0;
3820 psa_status_t actual_status;
3821 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003822 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003823
Gilles Peskine8817f612018-12-18 00:18:46 +01003824 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003825
Gilles Peskine656896e2018-06-29 19:12:28 +02003826 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003827 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3828 psa_set_key_algorithm( &attributes, alg );
3829 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003830 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003831 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003832
3833 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003834 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003835 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003836
Gilles Peskine656896e2018-06-29 19:12:28 +02003837 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003838 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003839 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003840
3841 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003842 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003843 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003844 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003845 output, output_size,
3846 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003847 TEST_EQUAL( actual_status, expected_status );
3848 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003849
Gilles Peskine68428122018-06-30 18:42:41 +02003850 /* If the label is empty, the test framework puts a non-null pointer
3851 * in label->x. Test that a null pointer works as well. */
3852 if( label->len == 0 )
3853 {
3854 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003855 if( output_size != 0 )
3856 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003857 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003858 input_data->x, input_data->len,
3859 NULL, label->len,
3860 output, output_size,
3861 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003862 TEST_EQUAL( actual_status, expected_status );
3863 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003864 }
3865
Gilles Peskine656896e2018-06-29 19:12:28 +02003866exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003867 /*
3868 * Key attributes may have been returned by psa_get_key_attributes()
3869 * thus reset them as required.
3870 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003871 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003872
Ronald Cron5425a212020-08-04 14:58:35 +02003873 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02003874 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003875 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003876}
3877/* END_CASE */
3878
3879/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003880void asymmetric_encrypt_decrypt( int key_type_arg,
3881 data_t *key_data,
3882 int alg_arg,
3883 data_t *input_data,
3884 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003885{
Ronald Cron5425a212020-08-04 14:58:35 +02003886 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003887 psa_key_type_t key_type = key_type_arg;
3888 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003889 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003890 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003891 size_t output_size;
3892 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003893 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003894 size_t output2_size;
3895 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003896 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003897
Gilles Peskine8817f612018-12-18 00:18:46 +01003898 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003899
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003900 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3901 psa_set_key_algorithm( &attributes, alg );
3902 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003903
Gilles Peskine049c7532019-05-15 20:22:09 +02003904 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003905 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003906
3907 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02003908 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003909 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003910
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003911 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003912 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003913 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01003914
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003915 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01003916 TEST_ASSERT( output2_size <=
3917 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
3918 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003919 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003920
Gilles Peskineeebd7382018-06-08 18:11:54 +02003921 /* We test encryption by checking that encrypt-then-decrypt gives back
3922 * the original plaintext because of the non-optional random
3923 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02003924 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003925 input_data->x, input_data->len,
3926 label->x, label->len,
3927 output, output_size,
3928 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003929 /* We don't know what ciphertext length to expect, but check that
3930 * it looks sensible. */
3931 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003932
Ronald Cron5425a212020-08-04 14:58:35 +02003933 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003934 output, output_length,
3935 label->x, label->len,
3936 output2, output2_size,
3937 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003938 ASSERT_COMPARE( input_data->x, input_data->len,
3939 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003940
3941exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003942 /*
3943 * Key attributes may have been returned by psa_get_key_attributes()
3944 * thus reset them as required.
3945 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003946 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003947
Ronald Cron5425a212020-08-04 14:58:35 +02003948 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003949 mbedtls_free( output );
3950 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003951 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003952}
3953/* END_CASE */
3954
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003955/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003956void asymmetric_decrypt( int key_type_arg,
3957 data_t *key_data,
3958 int alg_arg,
3959 data_t *input_data,
3960 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003961 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003962{
Ronald Cron5425a212020-08-04 14:58:35 +02003963 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003964 psa_key_type_t key_type = key_type_arg;
3965 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01003966 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003967 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003968 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003969 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003970 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003971
Gilles Peskine8817f612018-12-18 00:18:46 +01003972 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003973
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003974 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3975 psa_set_key_algorithm( &attributes, alg );
3976 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003977
Gilles Peskine049c7532019-05-15 20:22:09 +02003978 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003979 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003980
gabor-mezei-armceface22021-01-21 12:26:17 +01003981 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3982 key_bits = psa_get_key_bits( &attributes );
3983
3984 /* Determine the maximum ciphertext length */
3985 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
3986 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
3987 ASSERT_ALLOC( output, output_size );
3988
Ronald Cron5425a212020-08-04 14:58:35 +02003989 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003990 input_data->x, input_data->len,
3991 label->x, label->len,
3992 output,
3993 output_size,
3994 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003995 ASSERT_COMPARE( expected_data->x, expected_data->len,
3996 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003997
Gilles Peskine68428122018-06-30 18:42:41 +02003998 /* If the label is empty, the test framework puts a non-null pointer
3999 * in label->x. Test that a null pointer works as well. */
4000 if( label->len == 0 )
4001 {
4002 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004003 if( output_size != 0 )
4004 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004005 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004006 input_data->x, input_data->len,
4007 NULL, label->len,
4008 output,
4009 output_size,
4010 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004011 ASSERT_COMPARE( expected_data->x, expected_data->len,
4012 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004013 }
4014
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004015exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004016 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004017 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004018 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004019 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004020}
4021/* END_CASE */
4022
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004023/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004024void asymmetric_decrypt_fail( int key_type_arg,
4025 data_t *key_data,
4026 int alg_arg,
4027 data_t *input_data,
4028 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004029 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004030 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004031{
Ronald Cron5425a212020-08-04 14:58:35 +02004032 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004033 psa_key_type_t key_type = key_type_arg;
4034 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004035 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004036 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004037 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004038 psa_status_t actual_status;
4039 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004040 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004041
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004042 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004043
Gilles Peskine8817f612018-12-18 00:18:46 +01004044 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004045
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004046 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4047 psa_set_key_algorithm( &attributes, alg );
4048 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004049
Gilles Peskine049c7532019-05-15 20:22:09 +02004050 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004051 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004052
Ronald Cron5425a212020-08-04 14:58:35 +02004053 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004054 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004055 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004056 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004057 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004058 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004059 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004060
Gilles Peskine68428122018-06-30 18:42:41 +02004061 /* If the label is empty, the test framework puts a non-null pointer
4062 * in label->x. Test that a null pointer works as well. */
4063 if( label->len == 0 )
4064 {
4065 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004066 if( output_size != 0 )
4067 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004068 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004069 input_data->x, input_data->len,
4070 NULL, label->len,
4071 output, output_size,
4072 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004073 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004074 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004075 }
4076
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004077exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004078 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004079 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004080 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004081 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004082}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004083/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004084
4085/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004086void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004087{
4088 /* Test each valid way of initializing the object, except for `= {0}`, as
4089 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4090 * though it's OK by the C standard. We could test for this, but we'd need
4091 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004092 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004093 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4094 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4095 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004096
4097 memset( &zero, 0, sizeof( zero ) );
4098
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004099 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004100 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004101 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004102 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004103 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004104 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004105 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004106
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004107 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004108 PSA_ASSERT( psa_key_derivation_abort(&func) );
4109 PSA_ASSERT( psa_key_derivation_abort(&init) );
4110 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004111}
4112/* END_CASE */
4113
Janos Follath16de4a42019-06-13 16:32:24 +01004114/* BEGIN_CASE */
4115void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004116{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004117 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004118 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004119 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004120
Gilles Peskine8817f612018-12-18 00:18:46 +01004121 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004122
Janos Follath16de4a42019-06-13 16:32:24 +01004123 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004124 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004125
4126exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004127 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004128 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004129}
4130/* END_CASE */
4131
Janos Follathaf3c2a02019-06-12 12:34:34 +01004132/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004133void derive_set_capacity( int alg_arg, int capacity_arg,
4134 int expected_status_arg )
4135{
4136 psa_algorithm_t alg = alg_arg;
4137 size_t capacity = capacity_arg;
4138 psa_status_t expected_status = expected_status_arg;
4139 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4140
4141 PSA_ASSERT( psa_crypto_init( ) );
4142
4143 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4144
4145 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4146 expected_status );
4147
4148exit:
4149 psa_key_derivation_abort( &operation );
4150 PSA_DONE( );
4151}
4152/* END_CASE */
4153
4154/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004155void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004156 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004157 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004158 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004159 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004160 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004161 int expected_status_arg3,
4162 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004163{
4164 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004165 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4166 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004167 psa_status_t expected_statuses[] = {expected_status_arg1,
4168 expected_status_arg2,
4169 expected_status_arg3};
4170 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004171 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4172 MBEDTLS_SVC_KEY_ID_INIT,
4173 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004174 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4175 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4176 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004177 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004178 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004179 psa_status_t expected_output_status = expected_output_status_arg;
4180 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004181
4182 PSA_ASSERT( psa_crypto_init( ) );
4183
4184 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4185 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004186
4187 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4188
4189 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4190 {
Gilles Peskine4023c012021-05-27 13:21:20 +02004191 mbedtls_test_set_step( i );
4192 if( steps[i] == 0 )
4193 {
4194 /* Skip this step */
4195 }
4196 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004197 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004198 psa_set_key_type( &attributes, key_types[i] );
4199 PSA_ASSERT( psa_import_key( &attributes,
4200 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004201 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004202 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4203 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4204 {
4205 // When taking a private key as secret input, use key agreement
4206 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004207 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4208 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004209 expected_statuses[i] );
4210 }
4211 else
4212 {
4213 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004214 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004215 expected_statuses[i] );
4216 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004217 }
4218 else
4219 {
4220 TEST_EQUAL( psa_key_derivation_input_bytes(
4221 &operation, steps[i],
4222 inputs[i]->x, inputs[i]->len ),
4223 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004224 }
4225 }
4226
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004227 if( output_key_type != PSA_KEY_TYPE_NONE )
4228 {
4229 psa_reset_key_attributes( &attributes );
4230 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4231 psa_set_key_bits( &attributes, 8 );
4232 actual_output_status =
4233 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004234 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004235 }
4236 else
4237 {
4238 uint8_t buffer[1];
4239 actual_output_status =
4240 psa_key_derivation_output_bytes( &operation,
4241 buffer, sizeof( buffer ) );
4242 }
4243 TEST_EQUAL( actual_output_status, expected_output_status );
4244
Janos Follathaf3c2a02019-06-12 12:34:34 +01004245exit:
4246 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004247 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4248 psa_destroy_key( keys[i] );
4249 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004250 PSA_DONE( );
4251}
4252/* END_CASE */
4253
Janos Follathd958bb72019-07-03 15:02:16 +01004254/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02004255void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004256{
Janos Follathd958bb72019-07-03 15:02:16 +01004257 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004258 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004259 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004260 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004261 unsigned char input1[] = "Input 1";
4262 size_t input1_length = sizeof( input1 );
4263 unsigned char input2[] = "Input 2";
4264 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004265 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004266 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004267 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4268 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4269 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004270 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004271
Gilles Peskine8817f612018-12-18 00:18:46 +01004272 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004273
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004274 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4275 psa_set_key_algorithm( &attributes, alg );
4276 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004277
Gilles Peskine73676cb2019-05-15 20:15:10 +02004278 PSA_ASSERT( psa_import_key( &attributes,
4279 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004280 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004281
4282 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004283 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4284 input1, input1_length,
4285 input2, input2_length,
4286 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004287 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004288
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004289 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004290 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004291 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004292
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004293 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004294
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004295 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004296 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004297
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004298exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004299 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004300 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004301 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004302}
4303/* END_CASE */
4304
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004305/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02004306void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004307{
4308 uint8_t output_buffer[16];
4309 size_t buffer_size = 16;
4310 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004311 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004312
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004313 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4314 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004315 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004316
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004317 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004318 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004319
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004320 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004321
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004322 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4323 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004324 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004325
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004326 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004327 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004328
4329exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004330 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004331}
4332/* END_CASE */
4333
4334/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004335void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004336 int step1_arg, data_t *input1,
4337 int step2_arg, data_t *input2,
4338 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004339 int requested_capacity_arg,
4340 data_t *expected_output1,
4341 data_t *expected_output2 )
4342{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004343 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004344 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4345 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004346 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4347 MBEDTLS_SVC_KEY_ID_INIT,
4348 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004349 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004350 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004351 uint8_t *expected_outputs[2] =
4352 {expected_output1->x, expected_output2->x};
4353 size_t output_sizes[2] =
4354 {expected_output1->len, expected_output2->len};
4355 size_t output_buffer_size = 0;
4356 uint8_t *output_buffer = NULL;
4357 size_t expected_capacity;
4358 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004359 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004360 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004361 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004362
4363 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4364 {
4365 if( output_sizes[i] > output_buffer_size )
4366 output_buffer_size = output_sizes[i];
4367 if( output_sizes[i] == 0 )
4368 expected_outputs[i] = NULL;
4369 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004370 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004371 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004372
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004373 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4374 psa_set_key_algorithm( &attributes, alg );
4375 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004376
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004377 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004378 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4379 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4380 requested_capacity ) );
4381 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004382 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004383 switch( steps[i] )
4384 {
4385 case 0:
4386 break;
4387 case PSA_KEY_DERIVATION_INPUT_SECRET:
4388 PSA_ASSERT( psa_import_key( &attributes,
4389 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004390 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004391
4392 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4393 {
4394 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4395 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4396 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4397 }
4398
Gilles Peskine1468da72019-05-29 17:35:49 +02004399 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004400 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004401 break;
4402 default:
4403 PSA_ASSERT( psa_key_derivation_input_bytes(
4404 &operation, steps[i],
4405 inputs[i]->x, inputs[i]->len ) );
4406 break;
4407 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004408 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004409
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004410 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004411 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004412 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004413 expected_capacity = requested_capacity;
4414
4415 /* Expansion phase. */
4416 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4417 {
4418 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004419 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004420 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004421 if( expected_capacity == 0 && output_sizes[i] == 0 )
4422 {
4423 /* Reading 0 bytes when 0 bytes are available can go either way. */
4424 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004425 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004426 continue;
4427 }
4428 else if( expected_capacity == 0 ||
4429 output_sizes[i] > expected_capacity )
4430 {
4431 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004432 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004433 expected_capacity = 0;
4434 continue;
4435 }
4436 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004437 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004438 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004439 ASSERT_COMPARE( output_buffer, output_sizes[i],
4440 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004441 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004442 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004443 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004444 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004445 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004446 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004447 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004448
4449exit:
4450 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004451 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004452 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4453 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004454 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004455}
4456/* END_CASE */
4457
4458/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004459void derive_full( int alg_arg,
4460 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004461 data_t *input1,
4462 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004463 int requested_capacity_arg )
4464{
Ronald Cron5425a212020-08-04 14:58:35 +02004465 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004466 psa_algorithm_t alg = alg_arg;
4467 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004468 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004469 unsigned char output_buffer[16];
4470 size_t expected_capacity = requested_capacity;
4471 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004472 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004473
Gilles Peskine8817f612018-12-18 00:18:46 +01004474 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004475
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004476 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4477 psa_set_key_algorithm( &attributes, alg );
4478 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004479
Gilles Peskine049c7532019-05-15 20:22:09 +02004480 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004481 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004482
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004483 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4484 input1->x, input1->len,
4485 input2->x, input2->len,
4486 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004487 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004488
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004489 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004490 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004491 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004492
4493 /* Expansion phase. */
4494 while( current_capacity > 0 )
4495 {
4496 size_t read_size = sizeof( output_buffer );
4497 if( read_size > current_capacity )
4498 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004499 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004500 output_buffer,
4501 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004502 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004503 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004504 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004505 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004506 }
4507
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004508 /* Check that the operation refuses to go over capacity. */
4509 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004510 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004511
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004512 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004513
4514exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004515 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004516 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004517 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004518}
4519/* END_CASE */
4520
Janos Follathe60c9052019-07-03 13:51:30 +01004521/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004522void derive_key_exercise( int alg_arg,
4523 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004524 data_t *input1,
4525 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004526 int derived_type_arg,
4527 int derived_bits_arg,
4528 int derived_usage_arg,
4529 int derived_alg_arg )
4530{
Ronald Cron5425a212020-08-04 14:58:35 +02004531 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4532 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004533 psa_algorithm_t alg = alg_arg;
4534 psa_key_type_t derived_type = derived_type_arg;
4535 size_t derived_bits = derived_bits_arg;
4536 psa_key_usage_t derived_usage = derived_usage_arg;
4537 psa_algorithm_t derived_alg = derived_alg_arg;
4538 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004539 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004540 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004541 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004542
Gilles Peskine8817f612018-12-18 00:18:46 +01004543 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004544
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004545 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4546 psa_set_key_algorithm( &attributes, alg );
4547 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004548 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004549 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004550
4551 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004552 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4553 input1->x, input1->len,
4554 input2->x, input2->len,
4555 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004556 goto exit;
4557
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004558 psa_set_key_usage_flags( &attributes, derived_usage );
4559 psa_set_key_algorithm( &attributes, derived_alg );
4560 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004561 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004562 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004563 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004564
4565 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004566 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004567 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4568 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004569
4570 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004571 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004572 goto exit;
4573
4574exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004575 /*
4576 * Key attributes may have been returned by psa_get_key_attributes()
4577 * thus reset them as required.
4578 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004579 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004580
4581 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004582 psa_destroy_key( base_key );
4583 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004584 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004585}
4586/* END_CASE */
4587
Janos Follath42fd8882019-07-03 14:17:09 +01004588/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004589void derive_key_export( int alg_arg,
4590 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004591 data_t *input1,
4592 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004593 int bytes1_arg,
4594 int bytes2_arg )
4595{
Ronald Cron5425a212020-08-04 14:58:35 +02004596 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4597 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004598 psa_algorithm_t alg = alg_arg;
4599 size_t bytes1 = bytes1_arg;
4600 size_t bytes2 = bytes2_arg;
4601 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004602 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004603 uint8_t *output_buffer = NULL;
4604 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004605 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4606 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004607 size_t length;
4608
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004609 ASSERT_ALLOC( output_buffer, capacity );
4610 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004611 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004612
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004613 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4614 psa_set_key_algorithm( &base_attributes, alg );
4615 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004616 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004617 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004618
4619 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004620 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4621 input1->x, input1->len,
4622 input2->x, input2->len,
4623 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004624 goto exit;
4625
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004626 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004627 output_buffer,
4628 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004629 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004630
4631 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004632 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4633 input1->x, input1->len,
4634 input2->x, input2->len,
4635 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004636 goto exit;
4637
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004638 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4639 psa_set_key_algorithm( &derived_attributes, 0 );
4640 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004641 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004642 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004643 &derived_key ) );
4644 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004645 export_buffer, bytes1,
4646 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004647 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004648 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004649 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004650 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004651 &derived_key ) );
4652 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004653 export_buffer + bytes1, bytes2,
4654 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004655 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004656
4657 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004658 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4659 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004660
4661exit:
4662 mbedtls_free( output_buffer );
4663 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004664 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004665 psa_destroy_key( base_key );
4666 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004667 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004668}
4669/* END_CASE */
4670
4671/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004672void derive_key( int alg_arg,
4673 data_t *key_data, data_t *input1, data_t *input2,
4674 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004675 int expected_status_arg,
4676 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004677{
Ronald Cron5425a212020-08-04 14:58:35 +02004678 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4679 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004680 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004681 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004682 size_t bits = bits_arg;
4683 psa_status_t expected_status = expected_status_arg;
4684 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4685 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4686 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4687
4688 PSA_ASSERT( psa_crypto_init( ) );
4689
4690 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4691 psa_set_key_algorithm( &base_attributes, alg );
4692 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4693 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004694 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004695
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004696 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4697 input1->x, input1->len,
4698 input2->x, input2->len,
4699 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004700 goto exit;
4701
4702 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4703 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004704 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004705 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004706
4707 psa_status_t status =
4708 psa_key_derivation_output_key( &derived_attributes,
4709 &operation,
4710 &derived_key );
4711 if( is_large_output > 0 )
4712 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4713 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004714
4715exit:
4716 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004717 psa_destroy_key( base_key );
4718 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004719 PSA_DONE( );
4720}
4721/* END_CASE */
4722
4723/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004724void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004725 int our_key_type_arg, int our_key_alg_arg,
4726 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004727 int expected_status_arg )
4728{
Ronald Cron5425a212020-08-04 14:58:35 +02004729 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004730 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004731 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004732 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004733 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004734 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004735 psa_status_t expected_status = expected_status_arg;
4736 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004737
Gilles Peskine8817f612018-12-18 00:18:46 +01004738 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004739
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004740 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004741 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004742 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004743 PSA_ASSERT( psa_import_key( &attributes,
4744 our_key_data->x, our_key_data->len,
4745 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004746
Gilles Peskine77f40d82019-04-11 21:27:06 +02004747 /* The tests currently include inputs that should fail at either step.
4748 * Test cases that fail at the setup step should be changed to call
4749 * key_derivation_setup instead, and this function should be renamed
4750 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004751 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004752 if( status == PSA_SUCCESS )
4753 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004754 TEST_EQUAL( psa_key_derivation_key_agreement(
4755 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4756 our_key,
4757 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004758 expected_status );
4759 }
4760 else
4761 {
4762 TEST_ASSERT( status == expected_status );
4763 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004764
4765exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004766 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004767 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004768 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004769}
4770/* END_CASE */
4771
4772/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004773void raw_key_agreement( int alg_arg,
4774 int our_key_type_arg, data_t *our_key_data,
4775 data_t *peer_key_data,
4776 data_t *expected_output )
4777{
Ronald Cron5425a212020-08-04 14:58:35 +02004778 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004779 psa_algorithm_t alg = alg_arg;
4780 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004781 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004782 unsigned char *output = NULL;
4783 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004784 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004785
4786 ASSERT_ALLOC( output, expected_output->len );
4787 PSA_ASSERT( psa_crypto_init( ) );
4788
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004789 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4790 psa_set_key_algorithm( &attributes, alg );
4791 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004792 PSA_ASSERT( psa_import_key( &attributes,
4793 our_key_data->x, our_key_data->len,
4794 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004795
gabor-mezei-armceface22021-01-21 12:26:17 +01004796 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4797 key_bits = psa_get_key_bits( &attributes );
4798
Gilles Peskinebe697d82019-05-16 18:00:41 +02004799 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4800 peer_key_data->x, peer_key_data->len,
4801 output, expected_output->len,
4802 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004803 ASSERT_COMPARE( output, output_length,
4804 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01004805 TEST_ASSERT( output_length <=
4806 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
4807 TEST_ASSERT( output_length <=
4808 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004809
4810exit:
4811 mbedtls_free( output );
4812 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004813 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004814}
4815/* END_CASE */
4816
4817/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004818void key_agreement_capacity( int alg_arg,
4819 int our_key_type_arg, data_t *our_key_data,
4820 data_t *peer_key_data,
4821 int expected_capacity_arg )
4822{
Ronald Cron5425a212020-08-04 14:58:35 +02004823 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004824 psa_algorithm_t alg = alg_arg;
4825 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004826 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004827 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004828 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004829 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004830
Gilles Peskine8817f612018-12-18 00:18:46 +01004831 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004832
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004833 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4834 psa_set_key_algorithm( &attributes, alg );
4835 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004836 PSA_ASSERT( psa_import_key( &attributes,
4837 our_key_data->x, our_key_data->len,
4838 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004839
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004840 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004841 PSA_ASSERT( psa_key_derivation_key_agreement(
4842 &operation,
4843 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4844 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004845 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4846 {
4847 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004848 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004849 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004850 NULL, 0 ) );
4851 }
Gilles Peskine59685592018-09-18 12:11:34 +02004852
Gilles Peskinebf491972018-10-25 22:36:12 +02004853 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004854 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004855 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004856 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004857
Gilles Peskinebf491972018-10-25 22:36:12 +02004858 /* Test the actual capacity by reading the output. */
4859 while( actual_capacity > sizeof( output ) )
4860 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004861 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004862 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004863 actual_capacity -= sizeof( output );
4864 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004865 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004866 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004867 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004868 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004869
Gilles Peskine59685592018-09-18 12:11:34 +02004870exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004871 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004872 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004873 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004874}
4875/* END_CASE */
4876
4877/* BEGIN_CASE */
4878void key_agreement_output( int alg_arg,
4879 int our_key_type_arg, data_t *our_key_data,
4880 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004881 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004882{
Ronald Cron5425a212020-08-04 14:58:35 +02004883 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004884 psa_algorithm_t alg = alg_arg;
4885 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004886 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004887 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004888 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004889
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004890 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4891 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004892
Gilles Peskine8817f612018-12-18 00:18:46 +01004893 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004894
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004895 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4896 psa_set_key_algorithm( &attributes, alg );
4897 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004898 PSA_ASSERT( psa_import_key( &attributes,
4899 our_key_data->x, our_key_data->len,
4900 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004901
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004902 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004903 PSA_ASSERT( psa_key_derivation_key_agreement(
4904 &operation,
4905 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4906 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004907 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4908 {
4909 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004910 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004911 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004912 NULL, 0 ) );
4913 }
Gilles Peskine59685592018-09-18 12:11:34 +02004914
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004915 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004916 actual_output,
4917 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004918 ASSERT_COMPARE( actual_output, expected_output1->len,
4919 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004920 if( expected_output2->len != 0 )
4921 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004922 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004923 actual_output,
4924 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004925 ASSERT_COMPARE( actual_output, expected_output2->len,
4926 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004927 }
Gilles Peskine59685592018-09-18 12:11:34 +02004928
4929exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004930 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004931 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004932 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004933 mbedtls_free( actual_output );
4934}
4935/* END_CASE */
4936
4937/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004938void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004939{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004940 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004941 unsigned char *output = NULL;
4942 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004943 size_t i;
4944 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004945
Simon Butcher49f8e312020-03-03 15:51:50 +00004946 TEST_ASSERT( bytes_arg >= 0 );
4947
Gilles Peskine91892022021-02-08 19:50:26 +01004948 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004949 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02004950
Gilles Peskine8817f612018-12-18 00:18:46 +01004951 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004952
Gilles Peskinea50d7392018-06-21 10:22:13 +02004953 /* Run several times, to ensure that every output byte will be
4954 * nonzero at least once with overwhelming probability
4955 * (2^(-8*number_of_runs)). */
4956 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004957 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004958 if( bytes != 0 )
4959 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004960 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004961
Gilles Peskinea50d7392018-06-21 10:22:13 +02004962 for( i = 0; i < bytes; i++ )
4963 {
4964 if( output[i] != 0 )
4965 ++changed[i];
4966 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004967 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004968
4969 /* Check that every byte was changed to nonzero at least once. This
4970 * validates that psa_generate_random is overwriting every byte of
4971 * the output buffer. */
4972 for( i = 0; i < bytes; i++ )
4973 {
4974 TEST_ASSERT( changed[i] != 0 );
4975 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004976
4977exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004978 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004979 mbedtls_free( output );
4980 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004981}
4982/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004983
4984/* BEGIN_CASE */
4985void generate_key( int type_arg,
4986 int bits_arg,
4987 int usage_arg,
4988 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004989 int expected_status_arg,
4990 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004991{
Ronald Cron5425a212020-08-04 14:58:35 +02004992 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004993 psa_key_type_t type = type_arg;
4994 psa_key_usage_t usage = usage_arg;
4995 size_t bits = bits_arg;
4996 psa_algorithm_t alg = alg_arg;
4997 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004998 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004999 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005000
Gilles Peskine8817f612018-12-18 00:18:46 +01005001 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005002
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005003 psa_set_key_usage_flags( &attributes, usage );
5004 psa_set_key_algorithm( &attributes, alg );
5005 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005006 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005007
5008 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005009 psa_status_t status = psa_generate_key( &attributes, &key );
5010
5011 if( is_large_key > 0 )
5012 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5013 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005014 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005015 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005016
5017 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005018 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005019 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5020 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005021
Gilles Peskine818ca122018-06-20 18:16:48 +02005022 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005023 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005024 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005025
5026exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005027 /*
5028 * Key attributes may have been returned by psa_get_key_attributes()
5029 * thus reset them as required.
5030 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005031 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005032
Ronald Cron5425a212020-08-04 14:58:35 +02005033 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005034 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005035}
5036/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005037
Ronald Cronee414c72021-03-18 18:50:08 +01005038/* 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 +02005039void generate_key_rsa( int bits_arg,
5040 data_t *e_arg,
5041 int expected_status_arg )
5042{
Ronald Cron5425a212020-08-04 14:58:35 +02005043 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005044 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005045 size_t bits = bits_arg;
5046 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5047 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5048 psa_status_t expected_status = expected_status_arg;
5049 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5050 uint8_t *exported = NULL;
5051 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005052 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005053 size_t exported_length = SIZE_MAX;
5054 uint8_t *e_read_buffer = NULL;
5055 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005056 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005057 size_t e_read_length = SIZE_MAX;
5058
5059 if( e_arg->len == 0 ||
5060 ( e_arg->len == 3 &&
5061 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5062 {
5063 is_default_public_exponent = 1;
5064 e_read_size = 0;
5065 }
5066 ASSERT_ALLOC( e_read_buffer, e_read_size );
5067 ASSERT_ALLOC( exported, exported_size );
5068
5069 PSA_ASSERT( psa_crypto_init( ) );
5070
5071 psa_set_key_usage_flags( &attributes, usage );
5072 psa_set_key_algorithm( &attributes, alg );
5073 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5074 e_arg->x, e_arg->len ) );
5075 psa_set_key_bits( &attributes, bits );
5076
5077 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005078 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005079 if( expected_status != PSA_SUCCESS )
5080 goto exit;
5081
5082 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005083 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005084 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5085 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5086 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5087 e_read_buffer, e_read_size,
5088 &e_read_length ) );
5089 if( is_default_public_exponent )
5090 TEST_EQUAL( e_read_length, 0 );
5091 else
5092 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5093
5094 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005095 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005096 goto exit;
5097
5098 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005099 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005100 exported, exported_size,
5101 &exported_length ) );
5102 {
5103 uint8_t *p = exported;
5104 uint8_t *end = exported + exported_length;
5105 size_t len;
5106 /* RSAPublicKey ::= SEQUENCE {
5107 * modulus INTEGER, -- n
5108 * publicExponent INTEGER } -- e
5109 */
5110 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005111 MBEDTLS_ASN1_SEQUENCE |
5112 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005113 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005114 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5115 MBEDTLS_ASN1_INTEGER ) );
5116 if( len >= 1 && p[0] == 0 )
5117 {
5118 ++p;
5119 --len;
5120 }
5121 if( e_arg->len == 0 )
5122 {
5123 TEST_EQUAL( len, 3 );
5124 TEST_EQUAL( p[0], 1 );
5125 TEST_EQUAL( p[1], 0 );
5126 TEST_EQUAL( p[2], 1 );
5127 }
5128 else
5129 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5130 }
5131
5132exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005133 /*
5134 * Key attributes may have been returned by psa_get_key_attributes() or
5135 * set by psa_set_key_domain_parameters() thus reset them as required.
5136 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005137 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005138
Ronald Cron5425a212020-08-04 14:58:35 +02005139 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005140 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005141 mbedtls_free( e_read_buffer );
5142 mbedtls_free( exported );
5143}
5144/* END_CASE */
5145
Darryl Greend49a4992018-06-18 17:27:26 +01005146/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005147void persistent_key_load_key_from_storage( data_t *data,
5148 int type_arg, int bits_arg,
5149 int usage_flags_arg, int alg_arg,
5150 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005151{
Ronald Cron71016a92020-08-28 19:01:50 +02005152 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005153 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005154 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5155 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005156 psa_key_type_t type = type_arg;
5157 size_t bits = bits_arg;
5158 psa_key_usage_t usage_flags = usage_flags_arg;
5159 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005160 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005161 unsigned char *first_export = NULL;
5162 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005163 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005164 size_t first_exported_length;
5165 size_t second_exported_length;
5166
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005167 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5168 {
5169 ASSERT_ALLOC( first_export, export_size );
5170 ASSERT_ALLOC( second_export, export_size );
5171 }
Darryl Greend49a4992018-06-18 17:27:26 +01005172
Gilles Peskine8817f612018-12-18 00:18:46 +01005173 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005174
Gilles Peskinec87af662019-05-15 16:12:22 +02005175 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005176 psa_set_key_usage_flags( &attributes, usage_flags );
5177 psa_set_key_algorithm( &attributes, alg );
5178 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005179 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005180
Darryl Green0c6575a2018-11-07 16:05:30 +00005181 switch( generation_method )
5182 {
5183 case IMPORT_KEY:
5184 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005185 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005186 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005187 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005188
Darryl Green0c6575a2018-11-07 16:05:30 +00005189 case GENERATE_KEY:
5190 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005191 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005192 break;
5193
5194 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005195#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005196 {
5197 /* Create base key */
5198 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5199 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5200 psa_set_key_usage_flags( &base_attributes,
5201 PSA_KEY_USAGE_DERIVE );
5202 psa_set_key_algorithm( &base_attributes, derive_alg );
5203 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005204 PSA_ASSERT( psa_import_key( &base_attributes,
5205 data->x, data->len,
5206 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005207 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005208 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005209 PSA_ASSERT( psa_key_derivation_input_key(
5210 &operation,
5211 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005212 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005213 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005214 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005215 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5216 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005217 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005218 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005219 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005220 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005221 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005222#else
5223 TEST_ASSUME( ! "KDF not supported in this configuration" );
5224#endif
5225 break;
5226
5227 default:
5228 TEST_ASSERT( ! "generation_method not implemented in test" );
5229 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005230 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005231 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005232
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005233 /* Export the key if permitted by the key policy. */
5234 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5235 {
Ronald Cron5425a212020-08-04 14:58:35 +02005236 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005237 first_export, export_size,
5238 &first_exported_length ) );
5239 if( generation_method == IMPORT_KEY )
5240 ASSERT_COMPARE( data->x, data->len,
5241 first_export, first_exported_length );
5242 }
Darryl Greend49a4992018-06-18 17:27:26 +01005243
5244 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005245 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005246 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005247 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005248
Darryl Greend49a4992018-06-18 17:27:26 +01005249 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005250 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005251 TEST_ASSERT( mbedtls_svc_key_id_equal(
5252 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005253 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5254 PSA_KEY_LIFETIME_PERSISTENT );
5255 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5256 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02005257 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02005258 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005259 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005260
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005261 /* Export the key again if permitted by the key policy. */
5262 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005263 {
Ronald Cron5425a212020-08-04 14:58:35 +02005264 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005265 second_export, export_size,
5266 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005267 ASSERT_COMPARE( first_export, first_exported_length,
5268 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005269 }
5270
5271 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005272 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005273 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005274
5275exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005276 /*
5277 * Key attributes may have been returned by psa_get_key_attributes()
5278 * thus reset them as required.
5279 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005280 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005281
Darryl Greend49a4992018-06-18 17:27:26 +01005282 mbedtls_free( first_export );
5283 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005284 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005285 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005286 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005287 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005288}
5289/* END_CASE */