blob: 9136cb91da2eedd1b77c7e60190193f3c938ead9 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010018
Gilles Peskine4023c012021-05-27 13:21:20 +020019/* If this comes up, it's a bug in the test code or in the test data. */
20#define UNUSED 0xdeadbeef
21
Dave Rodgman647791d2021-06-23 12:49:59 +010022/* Assert that an operation is (not) active.
23 * This serves as a proxy for checking if the operation is aborted. */
24#define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 )
25#define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 )
26
Jaeden Amerof24c7f82018-06-27 17:20:43 +010027/** An invalid export length that will never be set by psa_export_key(). */
28static const size_t INVALID_EXPORT_LENGTH = ~0U;
29
Gilles Peskinea7aa4422018-08-14 15:17:54 +020030/** Test if a buffer contains a constant byte value.
31 *
32 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020033 *
34 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020035 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020036 * \param size Size of the buffer in bytes.
37 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020038 * \return 1 if the buffer is all-bits-zero.
39 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020040 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042{
43 size_t i;
44 for( i = 0; i < size; i++ )
45 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020046 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020047 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020048 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020049 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020050}
Gilles Peskine818ca122018-06-20 18:16:48 +020051
Gilles Peskine0b352bc2018-06-28 00:16:11 +020052/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
53static int asn1_write_10x( unsigned char **p,
54 unsigned char *start,
55 size_t bits,
56 unsigned char x )
57{
58 int ret;
59 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020060 if( bits == 0 )
61 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
62 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020063 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030064 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020065 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
66 *p -= len;
67 ( *p )[len-1] = x;
68 if( bits % 8 == 0 )
69 ( *p )[1] |= 1;
70 else
71 ( *p )[0] |= 1 << ( bits % 8 );
72 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
73 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
74 MBEDTLS_ASN1_INTEGER ) );
75 return( len );
76}
77
78static int construct_fake_rsa_key( unsigned char *buffer,
79 size_t buffer_size,
80 unsigned char **p,
81 size_t bits,
82 int keypair )
83{
84 size_t half_bits = ( bits + 1 ) / 2;
85 int ret;
86 int len = 0;
87 /* Construct something that looks like a DER encoding of
88 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
89 * RSAPrivateKey ::= SEQUENCE {
90 * version Version,
91 * modulus INTEGER, -- n
92 * publicExponent INTEGER, -- e
93 * privateExponent INTEGER, -- d
94 * prime1 INTEGER, -- p
95 * prime2 INTEGER, -- q
96 * exponent1 INTEGER, -- d mod (p-1)
97 * exponent2 INTEGER, -- d mod (q-1)
98 * coefficient INTEGER, -- (inverse of q) mod p
99 * otherPrimeInfos OtherPrimeInfos OPTIONAL
100 * }
101 * Or, for a public key, the same structure with only
102 * version, modulus and publicExponent.
103 */
104 *p = buffer + buffer_size;
105 if( keypair )
106 {
107 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
108 asn1_write_10x( p, buffer, half_bits, 1 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
110 asn1_write_10x( p, buffer, half_bits, 1 ) );
111 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
112 asn1_write_10x( p, buffer, half_bits, 1 ) );
113 MBEDTLS_ASN1_CHK_ADD( len, /* q */
114 asn1_write_10x( p, buffer, half_bits, 1 ) );
115 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
116 asn1_write_10x( p, buffer, half_bits, 3 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* d */
118 asn1_write_10x( p, buffer, bits, 1 ) );
119 }
120 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
121 asn1_write_10x( p, buffer, 17, 1 ) );
122 MBEDTLS_ASN1_CHK_ADD( len, /* n */
123 asn1_write_10x( p, buffer, bits, 1 ) );
124 if( keypair )
125 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
126 mbedtls_asn1_write_int( p, buffer, 0 ) );
127 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
128 {
129 const unsigned char tag =
130 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
131 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
132 }
133 return( len );
134}
135
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100136int exercise_mac_setup( psa_key_type_t key_type,
137 const unsigned char *key_bytes,
138 size_t key_length,
139 psa_algorithm_t alg,
140 psa_mac_operation_t *operation,
141 psa_status_t *status )
142{
Ronald Cron5425a212020-08-04 14:58:35 +0200143 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200144 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100145
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100146 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200147 psa_set_key_algorithm( &attributes, alg );
148 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200149 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100150
Ronald Cron5425a212020-08-04 14:58:35 +0200151 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100152 /* Whether setup succeeded or failed, abort must succeed. */
153 PSA_ASSERT( psa_mac_abort( operation ) );
154 /* If setup failed, reproduce the failure, so that the caller can
155 * test the resulting state of the operation object. */
156 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100157 {
Ronald Cron5425a212020-08-04 14:58:35 +0200158 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100159 }
160
Ronald Cron5425a212020-08-04 14:58:35 +0200161 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100162 return( 1 );
163
164exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200165 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100166 return( 0 );
167}
168
169int exercise_cipher_setup( psa_key_type_t key_type,
170 const unsigned char *key_bytes,
171 size_t key_length,
172 psa_algorithm_t alg,
173 psa_cipher_operation_t *operation,
174 psa_status_t *status )
175{
Ronald Cron5425a212020-08-04 14:58:35 +0200176 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200177 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100178
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200179 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
180 psa_set_key_algorithm( &attributes, alg );
181 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200182 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100183
Ronald Cron5425a212020-08-04 14:58:35 +0200184 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100185 /* Whether setup succeeded or failed, abort must succeed. */
186 PSA_ASSERT( psa_cipher_abort( operation ) );
187 /* If setup failed, reproduce the failure, so that the caller can
188 * test the resulting state of the operation object. */
189 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100190 {
Ronald Cron5425a212020-08-04 14:58:35 +0200191 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100192 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100193 }
194
Ronald Cron5425a212020-08-04 14:58:35 +0200195 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100196 return( 1 );
197
198exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200199 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100200 return( 0 );
201}
202
Ronald Cron5425a212020-08-04 14:58:35 +0200203static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200204{
205 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200206 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200207 uint8_t buffer[1];
208 size_t length;
209 int ok = 0;
210
Ronald Cronecfb2372020-07-23 17:13:42 +0200211 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200212 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
213 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
214 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200215 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000216 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200217 TEST_EQUAL(
218 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
219 TEST_EQUAL(
220 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200221 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200222 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
223 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
224 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
225 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
226
Ronald Cron5425a212020-08-04 14:58:35 +0200227 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000228 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200229 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200230 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000231 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200232
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200233 ok = 1;
234
235exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100236 /*
237 * Key attributes may have been returned by psa_get_key_attributes()
238 * thus reset them as required.
239 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200240 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100241
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200242 return( ok );
243}
244
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200245/* Assert that a key isn't reported as having a slot number. */
246#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
247#define ASSERT_NO_SLOT_NUMBER( attributes ) \
248 do \
249 { \
250 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
251 TEST_EQUAL( psa_get_key_slot_number( \
252 attributes, \
253 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
254 PSA_ERROR_INVALID_ARGUMENT ); \
255 } \
256 while( 0 )
257#else /* MBEDTLS_PSA_CRYPTO_SE_C */
258#define ASSERT_NO_SLOT_NUMBER( attributes ) \
259 ( (void) 0 )
260#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
261
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100262/* An overapproximation of the amount of storage needed for a key of the
263 * given type and with the given content. The API doesn't make it easy
264 * to find a good value for the size. The current implementation doesn't
265 * care about the value anyway. */
266#define KEY_BITS_FROM_DATA( type, data ) \
267 ( data )->len
268
Darryl Green0c6575a2018-11-07 16:05:30 +0000269typedef enum {
270 IMPORT_KEY = 0,
271 GENERATE_KEY = 1,
272 DERIVE_KEY = 2
273} generate_method;
274
Gilles Peskinee59236f2018-01-27 23:32:46 +0100275/* END_HEADER */
276
277/* BEGIN_DEPENDENCIES
278 * depends_on:MBEDTLS_PSA_CRYPTO_C
279 * END_DEPENDENCIES
280 */
281
282/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200283void static_checks( )
284{
285 size_t max_truncated_mac_size =
286 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
287
288 /* Check that the length for a truncated MAC always fits in the algorithm
289 * encoding. The shifted mask is the maximum truncated value. The
290 * untruncated algorithm may be one byte larger. */
291 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
292}
293/* END_CASE */
294
295/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200296void import_with_policy( int type_arg,
297 int usage_arg, int alg_arg,
298 int expected_status_arg )
299{
300 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
301 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200302 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200303 psa_key_type_t type = type_arg;
304 psa_key_usage_t usage = usage_arg;
305 psa_algorithm_t alg = alg_arg;
306 psa_status_t expected_status = expected_status_arg;
307 const uint8_t key_material[16] = {0};
308 psa_status_t status;
309
310 PSA_ASSERT( psa_crypto_init( ) );
311
312 psa_set_key_type( &attributes, type );
313 psa_set_key_usage_flags( &attributes, usage );
314 psa_set_key_algorithm( &attributes, alg );
315
316 status = psa_import_key( &attributes,
317 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200318 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200319 TEST_EQUAL( status, expected_status );
320 if( status != PSA_SUCCESS )
321 goto exit;
322
Ronald Cron5425a212020-08-04 14:58:35 +0200323 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200324 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200325 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200326 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200327 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200328 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200329
Ronald Cron5425a212020-08-04 14:58:35 +0200330 PSA_ASSERT( psa_destroy_key( key ) );
331 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200332
333exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100334 /*
335 * Key attributes may have been returned by psa_get_key_attributes()
336 * thus reset them as required.
337 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200338 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100339
340 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200341 PSA_DONE( );
342}
343/* END_CASE */
344
345/* BEGIN_CASE */
346void import_with_data( data_t *data, int type_arg,
347 int attr_bits_arg,
348 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200349{
350 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
351 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200352 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200353 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200354 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200355 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100356 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100357
Gilles Peskine8817f612018-12-18 00:18:46 +0100358 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100359
Gilles Peskine4747d192019-04-17 15:05:45 +0200360 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200361 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200362
Ronald Cron5425a212020-08-04 14:58:35 +0200363 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100364 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200365 if( status != PSA_SUCCESS )
366 goto exit;
367
Ronald Cron5425a212020-08-04 14:58:35 +0200368 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200369 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200370 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200371 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200372 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200373
Ronald Cron5425a212020-08-04 14:58:35 +0200374 PSA_ASSERT( psa_destroy_key( key ) );
375 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100376
377exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100378 /*
379 * Key attributes may have been returned by psa_get_key_attributes()
380 * thus reset them as required.
381 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200382 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100383
384 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200385 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100386}
387/* END_CASE */
388
389/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200390void import_large_key( int type_arg, int byte_size_arg,
391 int expected_status_arg )
392{
393 psa_key_type_t type = type_arg;
394 size_t byte_size = byte_size_arg;
395 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
396 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200397 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200398 psa_status_t status;
399 uint8_t *buffer = NULL;
400 size_t buffer_size = byte_size + 1;
401 size_t n;
402
Steven Cooreman69967ce2021-01-18 18:01:08 +0100403 /* Skip the test case if the target running the test cannot
404 * accomodate large keys due to heap size constraints */
405 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200406 memset( buffer, 'K', byte_size );
407
408 PSA_ASSERT( psa_crypto_init( ) );
409
410 /* Try importing the key */
411 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
412 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200413 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100414 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200415 TEST_EQUAL( status, expected_status );
416
417 if( status == PSA_SUCCESS )
418 {
Ronald Cron5425a212020-08-04 14:58:35 +0200419 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200420 TEST_EQUAL( psa_get_key_type( &attributes ), type );
421 TEST_EQUAL( psa_get_key_bits( &attributes ),
422 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200423 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200424 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200425 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200426 for( n = 0; n < byte_size; n++ )
427 TEST_EQUAL( buffer[n], 'K' );
428 for( n = byte_size; n < buffer_size; n++ )
429 TEST_EQUAL( buffer[n], 0 );
430 }
431
432exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100433 /*
434 * Key attributes may have been returned by psa_get_key_attributes()
435 * thus reset them as required.
436 */
437 psa_reset_key_attributes( &attributes );
438
Ronald Cron5425a212020-08-04 14:58:35 +0200439 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200440 PSA_DONE( );
441 mbedtls_free( buffer );
442}
443/* END_CASE */
444
445/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200446void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
447{
Ronald Cron5425a212020-08-04 14:58:35 +0200448 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200449 size_t bits = bits_arg;
450 psa_status_t expected_status = expected_status_arg;
451 psa_status_t status;
452 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200453 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200454 size_t buffer_size = /* Slight overapproximations */
455 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200456 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200457 unsigned char *p;
458 int ret;
459 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200460 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200461
Gilles Peskine8817f612018-12-18 00:18:46 +0100462 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200463 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200464
465 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
466 bits, keypair ) ) >= 0 );
467 length = ret;
468
469 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200470 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200471 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100472 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200473
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200474 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200475 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200476
477exit:
478 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200479 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200480}
481/* END_CASE */
482
483/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300484void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300485 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200486 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100487 int expected_bits,
488 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200489 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100490 int canonical_input )
491{
Ronald Cron5425a212020-08-04 14:58:35 +0200492 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100493 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200494 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200495 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100496 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100497 unsigned char *exported = NULL;
498 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100499 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100500 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100501 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200502 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200503 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100504
Moran Pekercb088e72018-07-17 17:36:59 +0300505 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200506 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100507 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200508 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100509 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100510
Gilles Peskine4747d192019-04-17 15:05:45 +0200511 psa_set_key_usage_flags( &attributes, usage_arg );
512 psa_set_key_algorithm( &attributes, alg );
513 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700514
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100515 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200516 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100517
518 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200519 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200520 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
521 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200522 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100523
524 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200525 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100526 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100527
528 /* The exported length must be set by psa_export_key() to a value between 0
529 * and export_size. On errors, the exported length must be 0. */
530 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
531 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
532 TEST_ASSERT( exported_length <= export_size );
533
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200534 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200535 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100536 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200537 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100538 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100539 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200540 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100541
Gilles Peskineea38a922021-02-13 00:05:16 +0100542 /* Run sanity checks on the exported key. For non-canonical inputs,
543 * this validates the canonical representations. For canonical inputs,
544 * this doesn't directly validate the implementation, but it still helps
545 * by cross-validating the test data with the sanity check code. */
546 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200547 goto exit;
548
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100549 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200550 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100551 else
552 {
Ronald Cron5425a212020-08-04 14:58:35 +0200553 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200554 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200555 &key2 ) );
556 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100557 reexported,
558 export_size,
559 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200560 ASSERT_COMPARE( exported, exported_length,
561 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200562 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100563 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100564 TEST_ASSERT( exported_length <=
565 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
566 psa_get_key_bits( &got_attributes ) ) );
567 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100568
569destroy:
570 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200571 PSA_ASSERT( psa_destroy_key( key ) );
572 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100573
574exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100575 /*
576 * Key attributes may have been returned by psa_get_key_attributes()
577 * thus reset them as required.
578 */
579 psa_reset_key_attributes( &got_attributes );
580
itayzafrir3e02b3b2018-06-12 17:06:52 +0300581 mbedtls_free( exported );
582 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200583 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100584}
585/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100586
Moran Pekerf709f4a2018-06-06 17:26:04 +0300587/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300588void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200589 int type_arg,
590 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100591 int export_size_delta,
592 int expected_export_status_arg,
593 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300594{
Ronald Cron5425a212020-08-04 14:58:35 +0200595 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300596 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200597 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200598 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300599 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300600 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100601 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100602 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200603 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300604
Gilles Peskine8817f612018-12-18 00:18:46 +0100605 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300606
Gilles Peskine4747d192019-04-17 15:05:45 +0200607 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
608 psa_set_key_algorithm( &attributes, alg );
609 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300610
611 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200612 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300613
Gilles Peskine49c25912018-10-29 15:15:31 +0100614 /* Export the public key */
615 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200616 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200617 exported, export_size,
618 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100619 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100620 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100621 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200622 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100623 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200624 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200625 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100626 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100627 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100628 TEST_ASSERT( expected_public_key->len <=
629 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
630 TEST_ASSERT( expected_public_key->len <=
631 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100632 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
633 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100634 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300635
636exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100637 /*
638 * Key attributes may have been returned by psa_get_key_attributes()
639 * thus reset them as required.
640 */
641 psa_reset_key_attributes( &attributes );
642
itayzafrir3e02b3b2018-06-12 17:06:52 +0300643 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200644 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200645 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300646}
647/* END_CASE */
648
Gilles Peskine20035e32018-02-03 22:44:14 +0100649/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200650void import_and_exercise_key( data_t *data,
651 int type_arg,
652 int bits_arg,
653 int alg_arg )
654{
Ronald Cron5425a212020-08-04 14:58:35 +0200655 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200656 psa_key_type_t type = type_arg;
657 size_t bits = bits_arg;
658 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100659 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200660 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200661 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200662
Gilles Peskine8817f612018-12-18 00:18:46 +0100663 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200664
Gilles Peskine4747d192019-04-17 15:05:45 +0200665 psa_set_key_usage_flags( &attributes, usage );
666 psa_set_key_algorithm( &attributes, alg );
667 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200668
669 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200670 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200671
672 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200673 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200674 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
675 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200676
677 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100678 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200679 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200680
Ronald Cron5425a212020-08-04 14:58:35 +0200681 PSA_ASSERT( psa_destroy_key( key ) );
682 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200683
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200684exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100685 /*
686 * Key attributes may have been returned by psa_get_key_attributes()
687 * thus reset them as required.
688 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200689 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100690
691 psa_reset_key_attributes( &attributes );
692 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200693 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200694}
695/* END_CASE */
696
697/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100698void effective_key_attributes( int type_arg, int expected_type_arg,
699 int bits_arg, int expected_bits_arg,
700 int usage_arg, int expected_usage_arg,
701 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200702{
Ronald Cron5425a212020-08-04 14:58:35 +0200703 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100704 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100705 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100706 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100707 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200708 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100709 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200710 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100711 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200712 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200713
Gilles Peskine8817f612018-12-18 00:18:46 +0100714 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200715
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200716 psa_set_key_usage_flags( &attributes, usage );
717 psa_set_key_algorithm( &attributes, alg );
718 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +0100719 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +0200720
Ronald Cron5425a212020-08-04 14:58:35 +0200721 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +0100722 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +0200723
Ronald Cron5425a212020-08-04 14:58:35 +0200724 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +0100725 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
726 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
727 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
728 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200729
730exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100731 /*
732 * Key attributes may have been returned by psa_get_key_attributes()
733 * thus reset them as required.
734 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200735 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100736
737 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200738 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +0200739}
740/* END_CASE */
741
742/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100743void check_key_policy( int type_arg, int bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +0200744 int usage_arg, int alg_arg )
Gilles Peskine06c28892019-11-26 18:07:46 +0100745{
746 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +0200747 usage_arg,
748 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200749 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +0100750 goto exit;
751}
752/* END_CASE */
753
754/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200755void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +0000756{
757 /* Test each valid way of initializing the object, except for `= {0}`, as
758 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
759 * though it's OK by the C standard. We could test for this, but we'd need
760 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200761 psa_key_attributes_t func = psa_key_attributes_init( );
762 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
763 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000764
765 memset( &zero, 0, sizeof( zero ) );
766
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200767 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
768 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
769 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000770
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200771 TEST_EQUAL( psa_get_key_type( &func ), 0 );
772 TEST_EQUAL( psa_get_key_type( &init ), 0 );
773 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
774
775 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
776 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
777 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
778
779 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
780 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
781 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
782
783 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
784 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
785 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +0000786}
787/* END_CASE */
788
789/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200790void mac_key_policy( int policy_usage_arg,
791 int policy_alg_arg,
792 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200793 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200794 int exercise_alg_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100795 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200796{
Ronald Cron5425a212020-08-04 14:58:35 +0200797 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200798 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000799 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200800 psa_key_type_t key_type = key_type_arg;
801 psa_algorithm_t policy_alg = policy_alg_arg;
802 psa_algorithm_t exercise_alg = exercise_alg_arg;
803 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200804 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100805 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200806 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200807
Gilles Peskine8817f612018-12-18 00:18:46 +0100808 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200809
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200810 psa_set_key_usage_flags( &attributes, policy_usage );
811 psa_set_key_algorithm( &attributes, policy_alg );
812 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200813
Gilles Peskine049c7532019-05-15 20:22:09 +0200814 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200815 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200816
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +0200817 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
818 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200819
Ronald Cron5425a212020-08-04 14:58:35 +0200820 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100821 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100822 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100823 else
824 TEST_EQUAL( status, expected_status );
825
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200826 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200827
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200828 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200829 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100830 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100831 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100832 else
833 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200834
835exit:
836 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200837 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200838 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200839}
840/* END_CASE */
841
842/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200843void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200844 int policy_alg,
845 int key_type,
846 data_t *key_data,
847 int exercise_alg )
848{
Ronald Cron5425a212020-08-04 14:58:35 +0200849 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200850 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000851 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200852 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200853 psa_status_t status;
854
Gilles Peskine8817f612018-12-18 00:18:46 +0100855 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200856
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200857 psa_set_key_usage_flags( &attributes, policy_usage );
858 psa_set_key_algorithm( &attributes, policy_alg );
859 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200860
Gilles Peskine049c7532019-05-15 20:22:09 +0200861 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200862 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200863
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200864 /* Check if no key usage flag implication is done */
865 TEST_EQUAL( policy_usage,
866 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200867
Ronald Cron5425a212020-08-04 14:58:35 +0200868 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200869 if( policy_alg == exercise_alg &&
870 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100871 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200872 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100873 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200874 psa_cipher_abort( &operation );
875
Ronald Cron5425a212020-08-04 14:58:35 +0200876 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200877 if( policy_alg == exercise_alg &&
878 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100879 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200880 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100881 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200882
883exit:
884 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200885 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200886 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200887}
888/* END_CASE */
889
890/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200891void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200892 int policy_alg,
893 int key_type,
894 data_t *key_data,
895 int nonce_length_arg,
896 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100897 int exercise_alg,
898 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200899{
Ronald Cron5425a212020-08-04 14:58:35 +0200900 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200901 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200902 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200903 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100904 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200905 unsigned char nonce[16] = {0};
906 size_t nonce_length = nonce_length_arg;
907 unsigned char tag[16];
908 size_t tag_length = tag_length_arg;
909 size_t output_length;
910
911 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
912 TEST_ASSERT( tag_length <= sizeof( tag ) );
913
Gilles Peskine8817f612018-12-18 00:18:46 +0100914 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200915
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200916 psa_set_key_usage_flags( &attributes, policy_usage );
917 psa_set_key_algorithm( &attributes, policy_alg );
918 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200919
Gilles Peskine049c7532019-05-15 20:22:09 +0200920 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200921 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200922
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200923 /* Check if no key usage implication is done */
924 TEST_EQUAL( policy_usage,
925 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200926
Ronald Cron5425a212020-08-04 14:58:35 +0200927 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200928 nonce, nonce_length,
929 NULL, 0,
930 NULL, 0,
931 tag, tag_length,
932 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100933 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
934 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200935 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100936 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200937
938 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200939 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200940 nonce, nonce_length,
941 NULL, 0,
942 tag, tag_length,
943 NULL, 0,
944 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100945 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
946 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
947 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100948 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200949 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100950 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200951
952exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200953 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200954 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200955}
956/* END_CASE */
957
958/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200959void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200960 int policy_alg,
961 int key_type,
962 data_t *key_data,
963 int exercise_alg )
964{
Ronald Cron5425a212020-08-04 14:58:35 +0200965 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200966 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200967 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200968 psa_status_t status;
969 size_t key_bits;
970 size_t buffer_length;
971 unsigned char *buffer = NULL;
972 size_t output_length;
973
Gilles Peskine8817f612018-12-18 00:18:46 +0100974 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200975
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200976 psa_set_key_usage_flags( &attributes, policy_usage );
977 psa_set_key_algorithm( &attributes, policy_alg );
978 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200979
Gilles Peskine049c7532019-05-15 20:22:09 +0200980 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200981 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200982
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200983 /* Check if no key usage implication is done */
984 TEST_EQUAL( policy_usage,
985 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200986
Ronald Cron5425a212020-08-04 14:58:35 +0200987 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200988 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200989 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
990 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200991 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200992
Ronald Cron5425a212020-08-04 14:58:35 +0200993 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200994 NULL, 0,
995 NULL, 0,
996 buffer, buffer_length,
997 &output_length );
998 if( policy_alg == exercise_alg &&
999 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001000 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001001 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001002 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001003
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001004 if( buffer_length != 0 )
1005 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001006 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001007 buffer, buffer_length,
1008 NULL, 0,
1009 buffer, buffer_length,
1010 &output_length );
1011 if( policy_alg == exercise_alg &&
1012 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001013 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001014 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001015 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001016
1017exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001018 /*
1019 * Key attributes may have been returned by psa_get_key_attributes()
1020 * thus reset them as required.
1021 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001022 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001023
1024 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001025 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001026 mbedtls_free( buffer );
1027}
1028/* END_CASE */
1029
1030/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001031void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001032 int policy_alg,
1033 int key_type,
1034 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001035 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001036 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001037 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001038{
Ronald Cron5425a212020-08-04 14:58:35 +02001039 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001040 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001041 psa_key_usage_t policy_usage = policy_usage_arg;
1042 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001043 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001044 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1045 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1046 * compatible with the policy and `payload_length_arg` is supposed to be
1047 * a valid input length to sign. If `payload_length_arg <= 0`,
1048 * `exercise_alg` is supposed to be forbidden by the policy. */
1049 int compatible_alg = payload_length_arg > 0;
1050 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001051 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001052 size_t signature_length;
1053
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001054 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001055 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001056 TEST_EQUAL( expected_usage,
1057 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001058
Gilles Peskine8817f612018-12-18 00:18:46 +01001059 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001060
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001061 psa_set_key_usage_flags( &attributes, policy_usage );
1062 psa_set_key_algorithm( &attributes, policy_alg );
1063 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001064
Gilles Peskine049c7532019-05-15 20:22:09 +02001065 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001066 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001067
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001068 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1069
Ronald Cron5425a212020-08-04 14:58:35 +02001070 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001071 payload, payload_length,
1072 signature, sizeof( signature ),
1073 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001074 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001075 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001076 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001077 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001078
1079 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001080 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001081 payload, payload_length,
1082 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001083 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001084 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001085 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001086 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001087
gabor-mezei-armd851d682021-06-28 14:53:49 +02001088 if( PSA_ALG_IS_HASH_AND_SIGN( exercise_alg ) &&
1089 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001090 {
1091 status = psa_sign_message( key, exercise_alg,
1092 payload, payload_length,
1093 signature, sizeof( signature ),
1094 &signature_length );
1095 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1096 PSA_ASSERT( status );
1097 else
1098 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1099
1100 memset( signature, 0, sizeof( signature ) );
1101 status = psa_verify_message( key, exercise_alg,
1102 payload, payload_length,
1103 signature, sizeof( signature ) );
1104 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1105 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1106 else
1107 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1108 }
1109
Gilles Peskined5b33222018-06-18 22:20:03 +02001110exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001111 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001112 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001113}
1114/* END_CASE */
1115
Janos Follathba3fab92019-06-11 14:50:16 +01001116/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001117void derive_key_policy( int policy_usage,
1118 int policy_alg,
1119 int key_type,
1120 data_t *key_data,
1121 int exercise_alg )
1122{
Ronald Cron5425a212020-08-04 14:58:35 +02001123 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001124 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001125 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001126 psa_status_t status;
1127
Gilles Peskine8817f612018-12-18 00:18:46 +01001128 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001129
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001130 psa_set_key_usage_flags( &attributes, policy_usage );
1131 psa_set_key_algorithm( &attributes, policy_alg );
1132 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001133
Gilles Peskine049c7532019-05-15 20:22:09 +02001134 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001135 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001136
Janos Follathba3fab92019-06-11 14:50:16 +01001137 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1138
1139 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1140 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001141 {
Janos Follathba3fab92019-06-11 14:50:16 +01001142 PSA_ASSERT( psa_key_derivation_input_bytes(
1143 &operation,
1144 PSA_KEY_DERIVATION_INPUT_SEED,
1145 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001146 }
Janos Follathba3fab92019-06-11 14:50:16 +01001147
1148 status = psa_key_derivation_input_key( &operation,
1149 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001150 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001151
Gilles Peskineea0fb492018-07-12 17:17:20 +02001152 if( policy_alg == exercise_alg &&
1153 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001154 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001155 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001156 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001157
1158exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001159 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001160 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001161 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001162}
1163/* END_CASE */
1164
1165/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001166void agreement_key_policy( int policy_usage,
1167 int policy_alg,
1168 int key_type_arg,
1169 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001170 int exercise_alg,
1171 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001172{
Ronald Cron5425a212020-08-04 14:58:35 +02001173 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001174 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001175 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001176 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001177 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001178 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001179
Gilles Peskine8817f612018-12-18 00:18:46 +01001180 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001181
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001182 psa_set_key_usage_flags( &attributes, policy_usage );
1183 psa_set_key_algorithm( &attributes, policy_alg );
1184 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001185
Gilles Peskine049c7532019-05-15 20:22:09 +02001186 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001187 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001188
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001189 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001190 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001191
Steven Cooremance48e852020-10-05 16:02:45 +02001192 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001193
1194exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001195 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001196 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001197 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001198}
1199/* END_CASE */
1200
1201/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001202void key_policy_alg2( int key_type_arg, data_t *key_data,
1203 int usage_arg, int alg_arg, int alg2_arg )
1204{
Ronald Cron5425a212020-08-04 14:58:35 +02001205 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001206 psa_key_type_t key_type = key_type_arg;
1207 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1208 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1209 psa_key_usage_t usage = usage_arg;
1210 psa_algorithm_t alg = alg_arg;
1211 psa_algorithm_t alg2 = alg2_arg;
1212
1213 PSA_ASSERT( psa_crypto_init( ) );
1214
1215 psa_set_key_usage_flags( &attributes, usage );
1216 psa_set_key_algorithm( &attributes, alg );
1217 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1218 psa_set_key_type( &attributes, key_type );
1219 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001220 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001221
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001222 /* Update the usage flags to obtain implicit usage flags */
1223 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001224 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001225 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1226 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1227 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1228
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001229 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001230 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001231 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001232 goto exit;
1233
1234exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001235 /*
1236 * Key attributes may have been returned by psa_get_key_attributes()
1237 * thus reset them as required.
1238 */
1239 psa_reset_key_attributes( &got_attributes );
1240
Ronald Cron5425a212020-08-04 14:58:35 +02001241 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001242 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001243}
1244/* END_CASE */
1245
1246/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001247void raw_agreement_key_policy( int policy_usage,
1248 int policy_alg,
1249 int key_type_arg,
1250 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001251 int exercise_alg,
1252 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001253{
Ronald Cron5425a212020-08-04 14:58:35 +02001254 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001255 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001256 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001257 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001258 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001259 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001260
1261 PSA_ASSERT( psa_crypto_init( ) );
1262
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001263 psa_set_key_usage_flags( &attributes, policy_usage );
1264 psa_set_key_algorithm( &attributes, policy_alg );
1265 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001266
Gilles Peskine049c7532019-05-15 20:22:09 +02001267 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001268 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001269
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001270 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001271
Steven Cooremance48e852020-10-05 16:02:45 +02001272 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001273
1274exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001275 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001276 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001277 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001278}
1279/* END_CASE */
1280
1281/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001282void copy_success( int source_usage_arg,
1283 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001284 int type_arg, data_t *material,
1285 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001286 int target_usage_arg,
1287 int target_alg_arg, int target_alg2_arg,
1288 int expected_usage_arg,
1289 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001290{
Gilles Peskineca25db92019-04-19 11:43:08 +02001291 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1292 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001293 psa_key_usage_t expected_usage = expected_usage_arg;
1294 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001295 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001296 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1297 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001298 uint8_t *export_buffer = NULL;
1299
Gilles Peskine57ab7212019-01-28 13:03:09 +01001300 PSA_ASSERT( psa_crypto_init( ) );
1301
Gilles Peskineca25db92019-04-19 11:43:08 +02001302 /* Prepare the source key. */
1303 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1304 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001305 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001306 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001307 PSA_ASSERT( psa_import_key( &source_attributes,
1308 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001309 &source_key ) );
1310 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001311
Gilles Peskineca25db92019-04-19 11:43:08 +02001312 /* Prepare the target attributes. */
1313 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001314 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001315 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001316 /* Set volatile lifetime to reset the key identifier to 0. */
1317 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1318 }
1319
Gilles Peskineca25db92019-04-19 11:43:08 +02001320 if( target_usage_arg != -1 )
1321 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1322 if( target_alg_arg != -1 )
1323 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001324 if( target_alg2_arg != -1 )
1325 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001326
1327 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001328 PSA_ASSERT( psa_copy_key( source_key,
1329 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001330
1331 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001332 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001333
1334 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001335 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001336 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1337 psa_get_key_type( &target_attributes ) );
1338 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1339 psa_get_key_bits( &target_attributes ) );
1340 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1341 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001342 TEST_EQUAL( expected_alg2,
1343 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001344 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1345 {
1346 size_t length;
1347 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001348 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001349 material->len, &length ) );
1350 ASSERT_COMPARE( material->x, material->len,
1351 export_buffer, length );
1352 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001353
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001354 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001355 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001356 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001357 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001358
Ronald Cron5425a212020-08-04 14:58:35 +02001359 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001360
1361exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001362 /*
1363 * Source and target key attributes may have been returned by
1364 * psa_get_key_attributes() thus reset them as required.
1365 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001366 psa_reset_key_attributes( &source_attributes );
1367 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001368
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001369 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001370 mbedtls_free( export_buffer );
1371}
1372/* END_CASE */
1373
1374/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001375void copy_fail( int source_usage_arg,
1376 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001377 int type_arg, data_t *material,
1378 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001379 int target_usage_arg,
1380 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001381 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001382 int expected_status_arg )
1383{
1384 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1385 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001386 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1387 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001388 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001389
1390 PSA_ASSERT( psa_crypto_init( ) );
1391
1392 /* Prepare the source key. */
1393 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1394 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001395 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001396 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001397 PSA_ASSERT( psa_import_key( &source_attributes,
1398 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001399 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001400
1401 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001402 psa_set_key_id( &target_attributes, key_id );
1403 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001404 psa_set_key_type( &target_attributes, target_type_arg );
1405 psa_set_key_bits( &target_attributes, target_bits_arg );
1406 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1407 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001408 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001409
1410 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001411 TEST_EQUAL( psa_copy_key( source_key,
1412 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001413 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001414
Ronald Cron5425a212020-08-04 14:58:35 +02001415 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001416
Gilles Peskine4a644642019-05-03 17:14:08 +02001417exit:
1418 psa_reset_key_attributes( &source_attributes );
1419 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001420 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001421}
1422/* END_CASE */
1423
1424/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001425void hash_operation_init( )
1426{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001427 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001428 /* Test each valid way of initializing the object, except for `= {0}`, as
1429 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1430 * though it's OK by the C standard. We could test for this, but we'd need
1431 * to supress the Clang warning for the test. */
1432 psa_hash_operation_t func = psa_hash_operation_init( );
1433 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1434 psa_hash_operation_t zero;
1435
1436 memset( &zero, 0, sizeof( zero ) );
1437
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001438 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001439 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1440 PSA_ERROR_BAD_STATE );
1441 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1442 PSA_ERROR_BAD_STATE );
1443 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1444 PSA_ERROR_BAD_STATE );
1445
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001446 /* A default hash operation should be abortable without error. */
1447 PSA_ASSERT( psa_hash_abort( &func ) );
1448 PSA_ASSERT( psa_hash_abort( &init ) );
1449 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001450}
1451/* END_CASE */
1452
1453/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001454void hash_setup( int alg_arg,
1455 int expected_status_arg )
1456{
1457 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001458 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001459 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001460 psa_status_t status;
1461
Gilles Peskine8817f612018-12-18 00:18:46 +01001462 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001463
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001464 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001465 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001466
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001467 /* Whether setup succeeded or failed, abort must succeed. */
1468 PSA_ASSERT( psa_hash_abort( &operation ) );
1469
1470 /* If setup failed, reproduce the failure, so as to
1471 * test the resulting state of the operation object. */
1472 if( status != PSA_SUCCESS )
1473 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1474
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001475 /* Now the operation object should be reusable. */
1476#if defined(KNOWN_SUPPORTED_HASH_ALG)
1477 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1478 PSA_ASSERT( psa_hash_abort( &operation ) );
1479#endif
1480
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001481exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001482 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001483}
1484/* END_CASE */
1485
1486/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001487void hash_compute_fail( int alg_arg, data_t *input,
1488 int output_size_arg, int expected_status_arg )
1489{
1490 psa_algorithm_t alg = alg_arg;
1491 uint8_t *output = NULL;
1492 size_t output_size = output_size_arg;
1493 size_t output_length = INVALID_EXPORT_LENGTH;
1494 psa_status_t expected_status = expected_status_arg;
1495 psa_status_t status;
1496
1497 ASSERT_ALLOC( output, output_size );
1498
1499 PSA_ASSERT( psa_crypto_init( ) );
1500
1501 status = psa_hash_compute( alg, input->x, input->len,
1502 output, output_size, &output_length );
1503 TEST_EQUAL( status, expected_status );
1504 TEST_ASSERT( output_length <= output_size );
1505
1506exit:
1507 mbedtls_free( output );
1508 PSA_DONE( );
1509}
1510/* END_CASE */
1511
1512/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001513void hash_compare_fail( int alg_arg, data_t *input,
1514 data_t *reference_hash,
1515 int expected_status_arg )
1516{
1517 psa_algorithm_t alg = alg_arg;
1518 psa_status_t expected_status = expected_status_arg;
1519 psa_status_t status;
1520
1521 PSA_ASSERT( psa_crypto_init( ) );
1522
1523 status = psa_hash_compare( alg, input->x, input->len,
1524 reference_hash->x, reference_hash->len );
1525 TEST_EQUAL( status, expected_status );
1526
1527exit:
1528 PSA_DONE( );
1529}
1530/* END_CASE */
1531
1532/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001533void hash_compute_compare( int alg_arg, data_t *input,
1534 data_t *expected_output )
1535{
1536 psa_algorithm_t alg = alg_arg;
1537 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1538 size_t output_length = INVALID_EXPORT_LENGTH;
1539 size_t i;
1540
1541 PSA_ASSERT( psa_crypto_init( ) );
1542
1543 /* Compute with tight buffer */
1544 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001545 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001546 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001547 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001548 ASSERT_COMPARE( output, output_length,
1549 expected_output->x, expected_output->len );
1550
1551 /* Compute with larger buffer */
1552 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1553 output, sizeof( output ),
1554 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001555 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001556 ASSERT_COMPARE( output, output_length,
1557 expected_output->x, expected_output->len );
1558
1559 /* Compare with correct hash */
1560 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1561 output, output_length ) );
1562
1563 /* Compare with trailing garbage */
1564 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1565 output, output_length + 1 ),
1566 PSA_ERROR_INVALID_SIGNATURE );
1567
1568 /* Compare with truncated hash */
1569 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1570 output, output_length - 1 ),
1571 PSA_ERROR_INVALID_SIGNATURE );
1572
1573 /* Compare with corrupted value */
1574 for( i = 0; i < output_length; i++ )
1575 {
Chris Jones9634bb12021-01-20 15:56:42 +00001576 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001577 output[i] ^= 1;
1578 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1579 output, output_length ),
1580 PSA_ERROR_INVALID_SIGNATURE );
1581 output[i] ^= 1;
1582 }
1583
1584exit:
1585 PSA_DONE( );
1586}
1587/* END_CASE */
1588
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001589/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001590void hash_bad_order( )
1591{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001592 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001593 unsigned char input[] = "";
1594 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001595 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001596 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1597 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1598 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001599 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001600 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001601 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001602
Gilles Peskine8817f612018-12-18 00:18:46 +01001603 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001604
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001605 /* Call setup twice in a row. */
1606 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001607 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001608 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1609 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001610 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001611 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001612 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001613
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001614 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001615 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001616 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001617 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001618
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001619 /* Check that update calls abort on error. */
1620 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01001621 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001622 ASSERT_OPERATION_IS_ACTIVE( operation );
1623 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
1624 PSA_ERROR_BAD_STATE );
1625 ASSERT_OPERATION_IS_INACTIVE( operation );
1626 PSA_ASSERT( psa_hash_abort( &operation ) );
1627 ASSERT_OPERATION_IS_INACTIVE( operation );
1628
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001629 /* Call update after finish. */
1630 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1631 PSA_ASSERT( psa_hash_finish( &operation,
1632 hash, sizeof( hash ), &hash_len ) );
1633 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001634 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001635 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001636
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001637 /* Call verify without calling setup beforehand. */
1638 TEST_EQUAL( psa_hash_verify( &operation,
1639 valid_hash, sizeof( valid_hash ) ),
1640 PSA_ERROR_BAD_STATE );
1641 PSA_ASSERT( psa_hash_abort( &operation ) );
1642
1643 /* Call verify after finish. */
1644 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1645 PSA_ASSERT( psa_hash_finish( &operation,
1646 hash, sizeof( hash ), &hash_len ) );
1647 TEST_EQUAL( psa_hash_verify( &operation,
1648 valid_hash, sizeof( valid_hash ) ),
1649 PSA_ERROR_BAD_STATE );
1650 PSA_ASSERT( psa_hash_abort( &operation ) );
1651
1652 /* Call verify twice in a row. */
1653 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001654 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001655 PSA_ASSERT( psa_hash_verify( &operation,
1656 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001657 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001658 TEST_EQUAL( psa_hash_verify( &operation,
1659 valid_hash, sizeof( valid_hash ) ),
1660 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001661 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001662 PSA_ASSERT( psa_hash_abort( &operation ) );
1663
1664 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001665 TEST_EQUAL( psa_hash_finish( &operation,
1666 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001667 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001668 PSA_ASSERT( psa_hash_abort( &operation ) );
1669
1670 /* Call finish twice in a row. */
1671 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1672 PSA_ASSERT( psa_hash_finish( &operation,
1673 hash, sizeof( hash ), &hash_len ) );
1674 TEST_EQUAL( psa_hash_finish( &operation,
1675 hash, sizeof( hash ), &hash_len ),
1676 PSA_ERROR_BAD_STATE );
1677 PSA_ASSERT( psa_hash_abort( &operation ) );
1678
1679 /* Call finish after calling verify. */
1680 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1681 PSA_ASSERT( psa_hash_verify( &operation,
1682 valid_hash, sizeof( valid_hash ) ) );
1683 TEST_EQUAL( psa_hash_finish( &operation,
1684 hash, sizeof( hash ), &hash_len ),
1685 PSA_ERROR_BAD_STATE );
1686 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001687
1688exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001689 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001690}
1691/* END_CASE */
1692
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001693/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001694void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001695{
1696 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001697 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1698 * appended to it */
1699 unsigned char hash[] = {
1700 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1701 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1702 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001703 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001704 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001705
Gilles Peskine8817f612018-12-18 00:18:46 +01001706 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001707
itayzafrir27e69452018-11-01 14:26:34 +02001708 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001709 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001710 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001711 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001712 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001713 ASSERT_OPERATION_IS_INACTIVE( operation );
1714 PSA_ASSERT( psa_hash_abort( &operation ) );
1715 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03001716
itayzafrir27e69452018-11-01 14:26:34 +02001717 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001718 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001719 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001720 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001721
itayzafrir27e69452018-11-01 14:26:34 +02001722 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001723 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001724 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001725 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001726
itayzafrirec93d302018-10-18 18:01:10 +03001727exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001728 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001729}
1730/* END_CASE */
1731
Ronald Cronee414c72021-03-18 18:50:08 +01001732/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001733void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001734{
1735 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001736 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001737 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001738 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001739 size_t hash_len;
1740
Gilles Peskine8817f612018-12-18 00:18:46 +01001741 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001742
itayzafrir58028322018-10-25 10:22:01 +03001743 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001744 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001745 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001746 hash, expected_size - 1, &hash_len ),
1747 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001748
1749exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001750 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001751}
1752/* END_CASE */
1753
Ronald Cronee414c72021-03-18 18:50:08 +01001754/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001755void hash_clone_source_state( )
1756{
1757 psa_algorithm_t alg = PSA_ALG_SHA_256;
1758 unsigned char hash[PSA_HASH_MAX_SIZE];
1759 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1760 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1761 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1762 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1763 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1764 size_t hash_len;
1765
1766 PSA_ASSERT( psa_crypto_init( ) );
1767 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1768
1769 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1770 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1771 PSA_ASSERT( psa_hash_finish( &op_finished,
1772 hash, sizeof( hash ), &hash_len ) );
1773 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1774 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1775
1776 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1777 PSA_ERROR_BAD_STATE );
1778
1779 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1780 PSA_ASSERT( psa_hash_finish( &op_init,
1781 hash, sizeof( hash ), &hash_len ) );
1782 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1783 PSA_ASSERT( psa_hash_finish( &op_finished,
1784 hash, sizeof( hash ), &hash_len ) );
1785 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1786 PSA_ASSERT( psa_hash_finish( &op_aborted,
1787 hash, sizeof( hash ), &hash_len ) );
1788
1789exit:
1790 psa_hash_abort( &op_source );
1791 psa_hash_abort( &op_init );
1792 psa_hash_abort( &op_setup );
1793 psa_hash_abort( &op_finished );
1794 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001795 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001796}
1797/* END_CASE */
1798
Ronald Cronee414c72021-03-18 18:50:08 +01001799/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001800void hash_clone_target_state( )
1801{
1802 psa_algorithm_t alg = PSA_ALG_SHA_256;
1803 unsigned char hash[PSA_HASH_MAX_SIZE];
1804 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1805 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1806 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1807 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1808 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1809 size_t hash_len;
1810
1811 PSA_ASSERT( psa_crypto_init( ) );
1812
1813 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1814 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1815 PSA_ASSERT( psa_hash_finish( &op_finished,
1816 hash, sizeof( hash ), &hash_len ) );
1817 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1818 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1819
1820 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1821 PSA_ASSERT( psa_hash_finish( &op_target,
1822 hash, sizeof( hash ), &hash_len ) );
1823
1824 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1825 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1826 PSA_ERROR_BAD_STATE );
1827 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1828 PSA_ERROR_BAD_STATE );
1829
1830exit:
1831 psa_hash_abort( &op_target );
1832 psa_hash_abort( &op_init );
1833 psa_hash_abort( &op_setup );
1834 psa_hash_abort( &op_finished );
1835 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001836 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001837}
1838/* END_CASE */
1839
itayzafrir58028322018-10-25 10:22:01 +03001840/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001841void mac_operation_init( )
1842{
Jaeden Amero252ef282019-02-15 14:05:35 +00001843 const uint8_t input[1] = { 0 };
1844
Jaeden Amero769ce272019-01-04 11:48:03 +00001845 /* Test each valid way of initializing the object, except for `= {0}`, as
1846 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1847 * though it's OK by the C standard. We could test for this, but we'd need
1848 * to supress the Clang warning for the test. */
1849 psa_mac_operation_t func = psa_mac_operation_init( );
1850 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1851 psa_mac_operation_t zero;
1852
1853 memset( &zero, 0, sizeof( zero ) );
1854
Jaeden Amero252ef282019-02-15 14:05:35 +00001855 /* A freshly-initialized MAC operation should not be usable. */
1856 TEST_EQUAL( psa_mac_update( &func,
1857 input, sizeof( input ) ),
1858 PSA_ERROR_BAD_STATE );
1859 TEST_EQUAL( psa_mac_update( &init,
1860 input, sizeof( input ) ),
1861 PSA_ERROR_BAD_STATE );
1862 TEST_EQUAL( psa_mac_update( &zero,
1863 input, sizeof( input ) ),
1864 PSA_ERROR_BAD_STATE );
1865
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001866 /* A default MAC operation should be abortable without error. */
1867 PSA_ASSERT( psa_mac_abort( &func ) );
1868 PSA_ASSERT( psa_mac_abort( &init ) );
1869 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001870}
1871/* END_CASE */
1872
1873/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001874void mac_setup( int key_type_arg,
1875 data_t *key,
1876 int alg_arg,
1877 int expected_status_arg )
1878{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001879 psa_key_type_t key_type = key_type_arg;
1880 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001881 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001882 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001883 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1884#if defined(KNOWN_SUPPORTED_MAC_ALG)
1885 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1886#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001887
Gilles Peskine8817f612018-12-18 00:18:46 +01001888 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001889
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001890 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1891 &operation, &status ) )
1892 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001893 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001894
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001895 /* The operation object should be reusable. */
1896#if defined(KNOWN_SUPPORTED_MAC_ALG)
1897 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1898 smoke_test_key_data,
1899 sizeof( smoke_test_key_data ),
1900 KNOWN_SUPPORTED_MAC_ALG,
1901 &operation, &status ) )
1902 goto exit;
1903 TEST_EQUAL( status, PSA_SUCCESS );
1904#endif
1905
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001906exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001907 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001908}
1909/* END_CASE */
1910
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001911/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_HMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256 */
Jaeden Amero252ef282019-02-15 14:05:35 +00001912void mac_bad_order( )
1913{
Ronald Cron5425a212020-08-04 14:58:35 +02001914 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001915 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1916 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001917 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001918 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1919 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1920 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001921 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001922 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1923 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1924 size_t sign_mac_length = 0;
1925 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1926 const uint8_t verify_mac[] = {
1927 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1928 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1929 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1930
1931 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001932 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001933 psa_set_key_algorithm( &attributes, alg );
1934 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001935
Ronald Cron5425a212020-08-04 14:58:35 +02001936 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1937 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001938
Jaeden Amero252ef282019-02-15 14:05:35 +00001939 /* Call update without calling setup beforehand. */
1940 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1941 PSA_ERROR_BAD_STATE );
1942 PSA_ASSERT( psa_mac_abort( &operation ) );
1943
1944 /* Call sign finish without calling setup beforehand. */
1945 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1946 &sign_mac_length),
1947 PSA_ERROR_BAD_STATE );
1948 PSA_ASSERT( psa_mac_abort( &operation ) );
1949
1950 /* Call verify finish without calling setup beforehand. */
1951 TEST_EQUAL( psa_mac_verify_finish( &operation,
1952 verify_mac, sizeof( verify_mac ) ),
1953 PSA_ERROR_BAD_STATE );
1954 PSA_ASSERT( psa_mac_abort( &operation ) );
1955
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001956 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001957 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001958 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001959 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001960 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001961 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001962 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001963 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001964
Jaeden Amero252ef282019-02-15 14:05:35 +00001965 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001966 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001967 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1968 PSA_ASSERT( psa_mac_sign_finish( &operation,
1969 sign_mac, sizeof( sign_mac ),
1970 &sign_mac_length ) );
1971 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1972 PSA_ERROR_BAD_STATE );
1973 PSA_ASSERT( psa_mac_abort( &operation ) );
1974
1975 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001976 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001977 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1978 PSA_ASSERT( psa_mac_verify_finish( &operation,
1979 verify_mac, sizeof( verify_mac ) ) );
1980 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1981 PSA_ERROR_BAD_STATE );
1982 PSA_ASSERT( psa_mac_abort( &operation ) );
1983
1984 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001985 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001986 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1987 PSA_ASSERT( psa_mac_sign_finish( &operation,
1988 sign_mac, sizeof( sign_mac ),
1989 &sign_mac_length ) );
1990 TEST_EQUAL( psa_mac_sign_finish( &operation,
1991 sign_mac, sizeof( sign_mac ),
1992 &sign_mac_length ),
1993 PSA_ERROR_BAD_STATE );
1994 PSA_ASSERT( psa_mac_abort( &operation ) );
1995
1996 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001997 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001998 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1999 PSA_ASSERT( psa_mac_verify_finish( &operation,
2000 verify_mac, sizeof( verify_mac ) ) );
2001 TEST_EQUAL( psa_mac_verify_finish( &operation,
2002 verify_mac, sizeof( verify_mac ) ),
2003 PSA_ERROR_BAD_STATE );
2004 PSA_ASSERT( psa_mac_abort( &operation ) );
2005
2006 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002007 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002008 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002009 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002010 TEST_EQUAL( psa_mac_verify_finish( &operation,
2011 verify_mac, sizeof( verify_mac ) ),
2012 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002013 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002014 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002015 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002016
2017 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002018 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002019 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002020 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002021 TEST_EQUAL( psa_mac_sign_finish( &operation,
2022 sign_mac, sizeof( sign_mac ),
2023 &sign_mac_length ),
2024 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002025 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002026 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002027 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002028
Ronald Cron5425a212020-08-04 14:58:35 +02002029 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002030
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002031exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002032 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002033}
2034/* END_CASE */
2035
2036/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002037void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002038 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002039 int alg_arg,
2040 data_t *input,
2041 data_t *expected_mac )
2042{
Ronald Cron5425a212020-08-04 14:58:35 +02002043 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002044 psa_key_type_t key_type = key_type_arg;
2045 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002046 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002047 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002048 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002049 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002050 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002051 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002052 const size_t output_sizes_to_test[] = {
2053 0,
2054 1,
2055 expected_mac->len - 1,
2056 expected_mac->len,
2057 expected_mac->len + 1,
2058 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002059
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002060 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002061 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002062 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002063
Gilles Peskine8817f612018-12-18 00:18:46 +01002064 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002065
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002066 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002067 psa_set_key_algorithm( &attributes, alg );
2068 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002069
Ronald Cron5425a212020-08-04 14:58:35 +02002070 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2071 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002072
Gilles Peskine8b356b52020-08-25 23:44:59 +02002073 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2074 {
2075 const size_t output_size = output_sizes_to_test[i];
2076 psa_status_t expected_status =
2077 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2078 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002079
Chris Jones9634bb12021-01-20 15:56:42 +00002080 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002081 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002082
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002083 /* Calculate the MAC, one-shot case. */
2084 TEST_EQUAL( psa_mac_compute( key, alg,
2085 input->x, input->len,
2086 actual_mac, output_size, &mac_length ),
2087 expected_status );
2088 if( expected_status == PSA_SUCCESS )
2089 {
2090 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2091 actual_mac, mac_length );
2092 }
2093
2094 if( output_size > 0 )
2095 memset( actual_mac, 0, output_size );
2096
2097 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002098 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002099 PSA_ASSERT( psa_mac_update( &operation,
2100 input->x, input->len ) );
2101 TEST_EQUAL( psa_mac_sign_finish( &operation,
2102 actual_mac, output_size,
2103 &mac_length ),
2104 expected_status );
2105 PSA_ASSERT( psa_mac_abort( &operation ) );
2106
2107 if( expected_status == PSA_SUCCESS )
2108 {
2109 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2110 actual_mac, mac_length );
2111 }
2112 mbedtls_free( actual_mac );
2113 actual_mac = NULL;
2114 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002115
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002116exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002117 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002118 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002119 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002120 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002121}
2122/* END_CASE */
2123
2124/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002125void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002126 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002127 int alg_arg,
2128 data_t *input,
2129 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002130{
Ronald Cron5425a212020-08-04 14:58:35 +02002131 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002132 psa_key_type_t key_type = key_type_arg;
2133 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002134 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002135 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002136 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002137
Gilles Peskine69c12672018-06-28 00:07:19 +02002138 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2139
Gilles Peskine8817f612018-12-18 00:18:46 +01002140 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002141
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002142 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002143 psa_set_key_algorithm( &attributes, alg );
2144 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002145
Ronald Cron5425a212020-08-04 14:58:35 +02002146 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2147 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002148
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002149 /* Verify correct MAC, one-shot case. */
2150 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2151 expected_mac->x, expected_mac->len ) );
2152
2153 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002154 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002155 PSA_ASSERT( psa_mac_update( &operation,
2156 input->x, input->len ) );
2157 PSA_ASSERT( psa_mac_verify_finish( &operation,
2158 expected_mac->x,
2159 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002160
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002161 /* Test a MAC that's too short, one-shot case. */
2162 TEST_EQUAL( psa_mac_verify( key, alg,
2163 input->x, input->len,
2164 expected_mac->x,
2165 expected_mac->len - 1 ),
2166 PSA_ERROR_INVALID_SIGNATURE );
2167
2168 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002169 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002170 PSA_ASSERT( psa_mac_update( &operation,
2171 input->x, input->len ) );
2172 TEST_EQUAL( psa_mac_verify_finish( &operation,
2173 expected_mac->x,
2174 expected_mac->len - 1 ),
2175 PSA_ERROR_INVALID_SIGNATURE );
2176
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002177 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002178 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2179 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002180 TEST_EQUAL( psa_mac_verify( key, alg,
2181 input->x, input->len,
2182 perturbed_mac, expected_mac->len + 1 ),
2183 PSA_ERROR_INVALID_SIGNATURE );
2184
2185 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002186 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002187 PSA_ASSERT( psa_mac_update( &operation,
2188 input->x, input->len ) );
2189 TEST_EQUAL( psa_mac_verify_finish( &operation,
2190 perturbed_mac,
2191 expected_mac->len + 1 ),
2192 PSA_ERROR_INVALID_SIGNATURE );
2193
2194 /* Test changing one byte. */
2195 for( size_t i = 0; i < expected_mac->len; i++ )
2196 {
Chris Jones9634bb12021-01-20 15:56:42 +00002197 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002198 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002199
2200 TEST_EQUAL( psa_mac_verify( key, alg,
2201 input->x, input->len,
2202 perturbed_mac, expected_mac->len ),
2203 PSA_ERROR_INVALID_SIGNATURE );
2204
Ronald Cron5425a212020-08-04 14:58:35 +02002205 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002206 PSA_ASSERT( psa_mac_update( &operation,
2207 input->x, input->len ) );
2208 TEST_EQUAL( psa_mac_verify_finish( &operation,
2209 perturbed_mac,
2210 expected_mac->len ),
2211 PSA_ERROR_INVALID_SIGNATURE );
2212 perturbed_mac[i] ^= 1;
2213 }
2214
Gilles Peskine8c9def32018-02-08 10:02:12 +01002215exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002216 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002217 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002218 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002219 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002220}
2221/* END_CASE */
2222
2223/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002224void cipher_operation_init( )
2225{
Jaeden Ameroab439972019-02-15 14:12:05 +00002226 const uint8_t input[1] = { 0 };
2227 unsigned char output[1] = { 0 };
2228 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002229 /* Test each valid way of initializing the object, except for `= {0}`, as
2230 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2231 * though it's OK by the C standard. We could test for this, but we'd need
2232 * to supress the Clang warning for the test. */
2233 psa_cipher_operation_t func = psa_cipher_operation_init( );
2234 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2235 psa_cipher_operation_t zero;
2236
2237 memset( &zero, 0, sizeof( zero ) );
2238
Jaeden Ameroab439972019-02-15 14:12:05 +00002239 /* A freshly-initialized cipher operation should not be usable. */
2240 TEST_EQUAL( psa_cipher_update( &func,
2241 input, sizeof( input ),
2242 output, sizeof( output ),
2243 &output_length ),
2244 PSA_ERROR_BAD_STATE );
2245 TEST_EQUAL( psa_cipher_update( &init,
2246 input, sizeof( input ),
2247 output, sizeof( output ),
2248 &output_length ),
2249 PSA_ERROR_BAD_STATE );
2250 TEST_EQUAL( psa_cipher_update( &zero,
2251 input, sizeof( input ),
2252 output, sizeof( output ),
2253 &output_length ),
2254 PSA_ERROR_BAD_STATE );
2255
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002256 /* A default cipher operation should be abortable without error. */
2257 PSA_ASSERT( psa_cipher_abort( &func ) );
2258 PSA_ASSERT( psa_cipher_abort( &init ) );
2259 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002260}
2261/* END_CASE */
2262
2263/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002264void cipher_setup( int key_type_arg,
2265 data_t *key,
2266 int alg_arg,
2267 int expected_status_arg )
2268{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002269 psa_key_type_t key_type = key_type_arg;
2270 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002271 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002272 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002273 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002274#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002275 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2276#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002277
Gilles Peskine8817f612018-12-18 00:18:46 +01002278 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002279
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002280 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2281 &operation, &status ) )
2282 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002283 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002284
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002285 /* The operation object should be reusable. */
2286#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2287 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2288 smoke_test_key_data,
2289 sizeof( smoke_test_key_data ),
2290 KNOWN_SUPPORTED_CIPHER_ALG,
2291 &operation, &status ) )
2292 goto exit;
2293 TEST_EQUAL( status, PSA_SUCCESS );
2294#endif
2295
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002296exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002297 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002298 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002299}
2300/* END_CASE */
2301
Ronald Cronee414c72021-03-18 18:50:08 +01002302/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002303void cipher_bad_order( )
2304{
Ronald Cron5425a212020-08-04 14:58:35 +02002305 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002306 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2307 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002308 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002309 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002310 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002311 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002312 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2313 0xaa, 0xaa, 0xaa, 0xaa };
2314 const uint8_t text[] = {
2315 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2316 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002317 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002318 size_t length = 0;
2319
2320 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002321 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2322 psa_set_key_algorithm( &attributes, alg );
2323 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002324 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2325 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002326
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002327 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002328 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002329 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002330 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002331 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002332 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002333 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002334 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002335
2336 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002337 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002338 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002339 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002340 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002341 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002342 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002343 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002344
Jaeden Ameroab439972019-02-15 14:12:05 +00002345 /* Generate an IV without calling setup beforehand. */
2346 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2347 buffer, sizeof( buffer ),
2348 &length ),
2349 PSA_ERROR_BAD_STATE );
2350 PSA_ASSERT( psa_cipher_abort( &operation ) );
2351
2352 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002353 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002354 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2355 buffer, sizeof( buffer ),
2356 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002357 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002358 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2359 buffer, sizeof( buffer ),
2360 &length ),
2361 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002362 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002363 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002364 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002365
2366 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002367 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002368 PSA_ASSERT( psa_cipher_set_iv( &operation,
2369 iv, sizeof( iv ) ) );
2370 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2371 buffer, sizeof( buffer ),
2372 &length ),
2373 PSA_ERROR_BAD_STATE );
2374 PSA_ASSERT( psa_cipher_abort( &operation ) );
2375
2376 /* Set an IV without calling setup beforehand. */
2377 TEST_EQUAL( psa_cipher_set_iv( &operation,
2378 iv, sizeof( iv ) ),
2379 PSA_ERROR_BAD_STATE );
2380 PSA_ASSERT( psa_cipher_abort( &operation ) );
2381
2382 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002383 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002384 PSA_ASSERT( psa_cipher_set_iv( &operation,
2385 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002386 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002387 TEST_EQUAL( psa_cipher_set_iv( &operation,
2388 iv, sizeof( iv ) ),
2389 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002390 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002391 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002392 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002393
2394 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002395 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002396 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2397 buffer, sizeof( buffer ),
2398 &length ) );
2399 TEST_EQUAL( psa_cipher_set_iv( &operation,
2400 iv, sizeof( iv ) ),
2401 PSA_ERROR_BAD_STATE );
2402 PSA_ASSERT( psa_cipher_abort( &operation ) );
2403
2404 /* Call update without calling setup beforehand. */
2405 TEST_EQUAL( psa_cipher_update( &operation,
2406 text, sizeof( text ),
2407 buffer, sizeof( buffer ),
2408 &length ),
2409 PSA_ERROR_BAD_STATE );
2410 PSA_ASSERT( psa_cipher_abort( &operation ) );
2411
2412 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01002413 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002414 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002415 TEST_EQUAL( psa_cipher_update( &operation,
2416 text, sizeof( text ),
2417 buffer, sizeof( buffer ),
2418 &length ),
2419 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002420 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002421 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002422 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002423
2424 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002425 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002426 PSA_ASSERT( psa_cipher_set_iv( &operation,
2427 iv, sizeof( iv ) ) );
2428 PSA_ASSERT( psa_cipher_finish( &operation,
2429 buffer, sizeof( buffer ), &length ) );
2430 TEST_EQUAL( psa_cipher_update( &operation,
2431 text, sizeof( text ),
2432 buffer, sizeof( buffer ),
2433 &length ),
2434 PSA_ERROR_BAD_STATE );
2435 PSA_ASSERT( psa_cipher_abort( &operation ) );
2436
2437 /* Call finish without calling setup beforehand. */
2438 TEST_EQUAL( psa_cipher_finish( &operation,
2439 buffer, sizeof( buffer ), &length ),
2440 PSA_ERROR_BAD_STATE );
2441 PSA_ASSERT( psa_cipher_abort( &operation ) );
2442
2443 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002444 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002445 /* Not calling update means we are encrypting an empty buffer, which is OK
2446 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01002447 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002448 TEST_EQUAL( psa_cipher_finish( &operation,
2449 buffer, sizeof( buffer ), &length ),
2450 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002451 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002452 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002453 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002454
2455 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002456 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002457 PSA_ASSERT( psa_cipher_set_iv( &operation,
2458 iv, sizeof( iv ) ) );
2459 PSA_ASSERT( psa_cipher_finish( &operation,
2460 buffer, sizeof( buffer ), &length ) );
2461 TEST_EQUAL( psa_cipher_finish( &operation,
2462 buffer, sizeof( buffer ), &length ),
2463 PSA_ERROR_BAD_STATE );
2464 PSA_ASSERT( psa_cipher_abort( &operation ) );
2465
Ronald Cron5425a212020-08-04 14:58:35 +02002466 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002467
Jaeden Ameroab439972019-02-15 14:12:05 +00002468exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002469 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002470 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002471}
2472/* END_CASE */
2473
2474/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02002475void cipher_encrypt_fail( int alg_arg,
2476 int key_type_arg,
2477 data_t *key_data,
2478 data_t *input,
2479 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002480{
Ronald Cron5425a212020-08-04 14:58:35 +02002481 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002482 psa_status_t status;
2483 psa_key_type_t key_type = key_type_arg;
2484 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002485 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002486 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002487 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002488 size_t output_length = 0;
2489 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2490
2491 if ( PSA_ERROR_BAD_STATE != expected_status )
2492 {
2493 PSA_ASSERT( psa_crypto_init( ) );
2494
2495 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2496 psa_set_key_algorithm( &attributes, alg );
2497 psa_set_key_type( &attributes, key_type );
2498
2499 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
2500 input->len );
2501 ASSERT_ALLOC( output, output_buffer_size );
2502
2503 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2504 &key ) );
2505 }
2506
2507 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
2508 output_buffer_size, &output_length );
2509
2510 TEST_EQUAL( status, expected_status );
2511
2512exit:
2513 mbedtls_free( output );
2514 psa_destroy_key( key );
2515 PSA_DONE( );
2516}
2517/* END_CASE */
2518
2519/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02002520void cipher_encrypt_alg_without_iv( int alg_arg,
2521 int key_type_arg,
2522 data_t *key_data,
2523 data_t *input,
2524 data_t *expected_output )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002525{
2526 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2527 psa_key_type_t key_type = key_type_arg;
2528 psa_algorithm_t alg = alg_arg;
2529 unsigned char *output = NULL;
2530 size_t output_buffer_size = 0;
2531 size_t output_length = 0;
2532 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2533
2534 PSA_ASSERT( psa_crypto_init( ) );
2535
2536 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2537 psa_set_key_algorithm( &attributes, alg );
2538 psa_set_key_type( &attributes, key_type );
2539
2540 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2541 ASSERT_ALLOC( output, output_buffer_size );
2542
2543 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2544 &key ) );
2545
2546 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
2547 output_buffer_size, &output_length ) );
2548 TEST_ASSERT( output_length <=
2549 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2550 TEST_ASSERT( output_length <=
2551 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
2552
2553 ASSERT_COMPARE( expected_output->x, expected_output->len,
2554 output, output_length );
2555exit:
2556 mbedtls_free( output );
2557 psa_destroy_key( key );
2558 PSA_DONE( );
2559}
2560/* END_CASE */
2561
2562/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002563void cipher_encrypt_validation( int alg_arg,
2564 int key_type_arg,
2565 data_t *key_data,
2566 data_t *input )
2567{
2568 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2569 psa_key_type_t key_type = key_type_arg;
2570 psa_algorithm_t alg = alg_arg;
2571 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
2572 unsigned char *output1 = NULL;
2573 size_t output1_buffer_size = 0;
2574 size_t output1_length = 0;
2575 unsigned char *output2 = NULL;
2576 size_t output2_buffer_size = 0;
2577 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002578 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002579 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002580 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002581
Gilles Peskine8817f612018-12-18 00:18:46 +01002582 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002583
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002584 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2585 psa_set_key_algorithm( &attributes, alg );
2586 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002587
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002588 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2589 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2590 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
2591 ASSERT_ALLOC( output1, output1_buffer_size );
2592 ASSERT_ALLOC( output2, output2_buffer_size );
2593
Ronald Cron5425a212020-08-04 14:58:35 +02002594 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2595 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002596
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02002597 /* The one-shot cipher encryption uses generated iv so validating
2598 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002599 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
2600 output1_buffer_size, &output1_length ) );
2601 TEST_ASSERT( output1_length <=
2602 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2603 TEST_ASSERT( output1_length <=
gabor-mezei-armceface22021-01-21 12:26:17 +01002604 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002605
2606 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2607 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002608
Gilles Peskine8817f612018-12-18 00:18:46 +01002609 PSA_ASSERT( psa_cipher_update( &operation,
2610 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002611 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01002612 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002613 TEST_ASSERT( function_output_length <=
2614 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2615 TEST_ASSERT( function_output_length <=
2616 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002617 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002618
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002619 PSA_ASSERT( psa_cipher_finish( &operation,
2620 output2 + output2_length,
2621 output2_buffer_size - output2_length,
2622 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002623 TEST_ASSERT( function_output_length <=
2624 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2625 TEST_ASSERT( function_output_length <=
2626 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002627 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002628
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002629 PSA_ASSERT( psa_cipher_abort( &operation ) );
2630 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
2631 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002632
Gilles Peskine50e586b2018-06-08 14:28:46 +02002633exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002634 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002635 mbedtls_free( output1 );
2636 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002637 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002638 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002639}
2640/* END_CASE */
2641
2642/* BEGIN_CASE */
2643void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002644 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002645 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002646 int first_part_size_arg,
2647 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002648 data_t *expected_output,
2649 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002650{
Ronald Cron5425a212020-08-04 14:58:35 +02002651 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002652 psa_key_type_t key_type = key_type_arg;
2653 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002654 psa_status_t status;
2655 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002656 size_t first_part_size = first_part_size_arg;
2657 size_t output1_length = output1_length_arg;
2658 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002659 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002660 size_t output_buffer_size = 0;
2661 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002662 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002663 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002664 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002665
Gilles Peskine8817f612018-12-18 00:18:46 +01002666 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002667
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002668 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2669 psa_set_key_algorithm( &attributes, alg );
2670 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002671
Ronald Cron5425a212020-08-04 14:58:35 +02002672 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2673 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002674
Ronald Cron5425a212020-08-04 14:58:35 +02002675 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002676
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002677 if( iv->len > 0 )
2678 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002679 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002680 }
2681
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002682 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2683 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002684 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002685
Gilles Peskinee0866522019-02-19 19:44:00 +01002686 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002687 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2688 output, output_buffer_size,
2689 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002690 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002691 TEST_ASSERT( function_output_length <=
2692 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2693 TEST_ASSERT( function_output_length <=
2694 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002695 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002696
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002697 if( first_part_size < input->len )
2698 {
2699 PSA_ASSERT( psa_cipher_update( &operation,
2700 input->x + first_part_size,
2701 input->len - first_part_size,
2702 ( output_buffer_size == 0 ? NULL :
2703 output + total_output_length ),
2704 output_buffer_size - total_output_length,
2705 &function_output_length ) );
2706 TEST_ASSERT( function_output_length == output2_length );
2707 TEST_ASSERT( function_output_length <=
2708 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2709 alg,
2710 input->len - first_part_size ) );
2711 TEST_ASSERT( function_output_length <=
2712 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2713 total_output_length += function_output_length;
2714 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002715
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002716 status = psa_cipher_finish( &operation,
2717 ( output_buffer_size == 0 ? NULL :
2718 output + total_output_length ),
2719 output_buffer_size - total_output_length,
2720 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002721 TEST_ASSERT( function_output_length <=
2722 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2723 TEST_ASSERT( function_output_length <=
2724 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002725 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002726 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002727
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002728 if( expected_status == PSA_SUCCESS )
2729 {
2730 PSA_ASSERT( psa_cipher_abort( &operation ) );
2731
2732 ASSERT_COMPARE( expected_output->x, expected_output->len,
2733 output, total_output_length );
2734 }
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
2744/* BEGIN_CASE */
2745void cipher_decrypt_multipart( 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,
Gilles Peskinee0866522019-02-19 19:44:00 +01002748 int first_part_size_arg,
2749 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002750 data_t *expected_output,
2751 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002752{
Ronald Cron5425a212020-08-04 14:58:35 +02002753 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002754 psa_key_type_t key_type = key_type_arg;
2755 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002756 psa_status_t status;
2757 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002758 size_t first_part_size = first_part_size_arg;
2759 size_t output1_length = output1_length_arg;
2760 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002761 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002762 size_t output_buffer_size = 0;
2763 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002764 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002765 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002766 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002767
Gilles Peskine8817f612018-12-18 00:18:46 +01002768 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002769
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002770 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2771 psa_set_key_algorithm( &attributes, alg );
2772 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002773
Ronald Cron5425a212020-08-04 14:58:35 +02002774 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2775 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002776
Ronald Cron5425a212020-08-04 14:58:35 +02002777 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002778
Steven Cooreman177deba2020-09-07 17:14:14 +02002779 if( iv->len > 0 )
2780 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002781 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002782 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002783
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002784 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2785 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002786 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002787
Gilles Peskinee0866522019-02-19 19:44:00 +01002788 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002789 PSA_ASSERT( psa_cipher_update( &operation,
2790 input->x, first_part_size,
2791 output, output_buffer_size,
2792 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002793 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002794 TEST_ASSERT( function_output_length <=
2795 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2796 TEST_ASSERT( function_output_length <=
2797 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002798 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002799
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002800 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02002801 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002802 PSA_ASSERT( psa_cipher_update( &operation,
2803 input->x + first_part_size,
2804 input->len - first_part_size,
2805 ( output_buffer_size == 0 ? NULL :
2806 output + total_output_length ),
2807 output_buffer_size - total_output_length,
2808 &function_output_length ) );
2809 TEST_ASSERT( function_output_length == output2_length );
2810 TEST_ASSERT( function_output_length <=
2811 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2812 alg,
2813 input->len - first_part_size ) );
2814 TEST_ASSERT( function_output_length <=
2815 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2816 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002817 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002818
Gilles Peskine50e586b2018-06-08 14:28:46 +02002819 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002820 ( output_buffer_size == 0 ? NULL :
2821 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002822 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002823 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002824 TEST_ASSERT( function_output_length <=
2825 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2826 TEST_ASSERT( function_output_length <=
2827 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002828 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002829 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002830
2831 if( expected_status == PSA_SUCCESS )
2832 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002833 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002834
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002835 ASSERT_COMPARE( expected_output->x, expected_output->len,
2836 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002837 }
2838
Gilles Peskine50e586b2018-06-08 14:28:46 +02002839exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002840 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002841 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002842 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002843 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002844}
2845/* END_CASE */
2846
Gilles Peskine50e586b2018-06-08 14:28:46 +02002847/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02002848void cipher_decrypt_fail( int alg_arg,
2849 int key_type_arg,
2850 data_t *key_data,
2851 data_t *iv,
2852 data_t *input_arg,
2853 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002854{
2855 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2856 psa_status_t status;
2857 psa_key_type_t key_type = key_type_arg;
2858 psa_algorithm_t alg = alg_arg;
2859 psa_status_t expected_status = expected_status_arg;
2860 unsigned char *input = NULL;
2861 size_t input_buffer_size = 0;
2862 unsigned char *output = NULL;
2863 size_t output_buffer_size = 0;
2864 size_t output_length = 0;
2865 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2866
2867 if ( PSA_ERROR_BAD_STATE != expected_status )
2868 {
2869 PSA_ASSERT( psa_crypto_init( ) );
2870
2871 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2872 psa_set_key_algorithm( &attributes, alg );
2873 psa_set_key_type( &attributes, key_type );
2874
2875 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2876 &key ) );
2877 }
2878
2879 /* Allocate input buffer and copy the iv and the plaintext */
2880 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
2881 if ( input_buffer_size > 0 )
2882 {
2883 ASSERT_ALLOC( input, input_buffer_size );
2884 memcpy( input, iv->x, iv->len );
2885 memcpy( input + iv->len, input_arg->x, input_arg->len );
2886 }
2887
2888 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
2889 ASSERT_ALLOC( output, output_buffer_size );
2890
2891 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
2892 output_buffer_size, &output_length );
2893 TEST_EQUAL( status, expected_status );
2894
2895exit:
2896 mbedtls_free( input );
2897 mbedtls_free( output );
2898 psa_destroy_key( key );
2899 PSA_DONE( );
2900}
2901/* END_CASE */
2902
2903/* BEGIN_CASE */
2904void cipher_decrypt( int alg_arg,
2905 int key_type_arg,
2906 data_t *key_data,
2907 data_t *iv,
2908 data_t *input_arg,
2909 data_t *expected_output )
2910{
2911 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2912 psa_key_type_t key_type = key_type_arg;
2913 psa_algorithm_t alg = alg_arg;
2914 unsigned char *input = NULL;
2915 size_t input_buffer_size = 0;
2916 unsigned char *output = NULL;
2917 size_t output_buffer_size = 0;
2918 size_t output_length = 0;
2919 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2920
2921 PSA_ASSERT( psa_crypto_init( ) );
2922
2923 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2924 psa_set_key_algorithm( &attributes, alg );
2925 psa_set_key_type( &attributes, key_type );
2926
2927 /* Allocate input buffer and copy the iv and the plaintext */
2928 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
2929 if ( input_buffer_size > 0 )
2930 {
2931 ASSERT_ALLOC( input, input_buffer_size );
2932 memcpy( input, iv->x, iv->len );
2933 memcpy( input + iv->len, input_arg->x, input_arg->len );
2934 }
2935
2936 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
2937 ASSERT_ALLOC( output, output_buffer_size );
2938
2939 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2940 &key ) );
2941
2942 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
2943 output_buffer_size, &output_length ) );
2944 TEST_ASSERT( output_length <=
2945 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
2946 TEST_ASSERT( output_length <=
2947 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
2948
2949 ASSERT_COMPARE( expected_output->x, expected_output->len,
2950 output, output_length );
2951exit:
2952 mbedtls_free( input );
2953 mbedtls_free( output );
2954 psa_destroy_key( key );
2955 PSA_DONE( );
2956}
2957/* END_CASE */
2958
2959/* BEGIN_CASE */
2960void cipher_verify_output( int alg_arg,
2961 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002962 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002963 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002964{
Ronald Cron5425a212020-08-04 14:58:35 +02002965 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002966 psa_key_type_t key_type = key_type_arg;
2967 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002968 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002969 size_t output1_size = 0;
2970 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002971 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002972 size_t output2_size = 0;
2973 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002974 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002975
Gilles Peskine8817f612018-12-18 00:18:46 +01002976 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002977
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002978 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2979 psa_set_key_algorithm( &attributes, alg );
2980 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002981
Ronald Cron5425a212020-08-04 14:58:35 +02002982 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2983 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002984 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002985 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002986
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002987 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
2988 output1, output1_size,
2989 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002990 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002991 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002992 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002993 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03002994
2995 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002996 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002997
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002998 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
2999 output2, output2_size,
3000 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003001 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003002 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003003 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003004 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003005
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003006 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003007
3008exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003009 mbedtls_free( output1 );
3010 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003011 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003012 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003013}
3014/* END_CASE */
3015
3016/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003017void cipher_verify_output_multipart( int alg_arg,
3018 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003019 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003020 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003021 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003022{
Ronald Cron5425a212020-08-04 14:58:35 +02003023 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003024 psa_key_type_t key_type = key_type_arg;
3025 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003026 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003027 unsigned char iv[16] = {0};
3028 size_t iv_size = 16;
3029 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003030 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003031 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003032 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003033 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003034 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003035 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003036 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003037 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3038 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003039 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003040
Gilles Peskine8817f612018-12-18 00:18:46 +01003041 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003042
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003043 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3044 psa_set_key_algorithm( &attributes, alg );
3045 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003046
Ronald Cron5425a212020-08-04 14:58:35 +02003047 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3048 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003049
Ronald Cron5425a212020-08-04 14:58:35 +02003050 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3051 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003052
Steven Cooreman177deba2020-09-07 17:14:14 +02003053 if( alg != PSA_ALG_ECB_NO_PADDING )
3054 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003055 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3056 iv, iv_size,
3057 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003058 }
3059
gabor-mezei-armceface22021-01-21 12:26:17 +01003060 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3061 TEST_ASSERT( output1_buffer_size <=
3062 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003063 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003064
Gilles Peskinee0866522019-02-19 19:44:00 +01003065 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003066
Gilles Peskine8817f612018-12-18 00:18:46 +01003067 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3068 output1, output1_buffer_size,
3069 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003070 TEST_ASSERT( function_output_length <=
3071 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3072 TEST_ASSERT( function_output_length <=
3073 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003074 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003075
Gilles Peskine8817f612018-12-18 00:18:46 +01003076 PSA_ASSERT( psa_cipher_update( &operation1,
3077 input->x + first_part_size,
3078 input->len - first_part_size,
3079 output1, output1_buffer_size,
3080 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003081 TEST_ASSERT( function_output_length <=
3082 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3083 alg,
3084 input->len - first_part_size ) );
3085 TEST_ASSERT( function_output_length <=
3086 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003087 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003088
Gilles Peskine8817f612018-12-18 00:18:46 +01003089 PSA_ASSERT( psa_cipher_finish( &operation1,
3090 output1 + output1_length,
3091 output1_buffer_size - output1_length,
3092 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003093 TEST_ASSERT( function_output_length <=
3094 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3095 TEST_ASSERT( function_output_length <=
3096 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003097 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003098
Gilles Peskine8817f612018-12-18 00:18:46 +01003099 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003100
Gilles Peskine048b7f02018-06-08 14:20:49 +02003101 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003102 TEST_ASSERT( output2_buffer_size <=
3103 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3104 TEST_ASSERT( output2_buffer_size <=
3105 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003106 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003107
Steven Cooreman177deba2020-09-07 17:14:14 +02003108 if( iv_length > 0 )
3109 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003110 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3111 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003112 }
Moran Pekerded84402018-06-06 16:36:50 +03003113
Gilles Peskine8817f612018-12-18 00:18:46 +01003114 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3115 output2, output2_buffer_size,
3116 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003117 TEST_ASSERT( function_output_length <=
3118 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3119 TEST_ASSERT( function_output_length <=
3120 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003121 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003122
Gilles Peskine8817f612018-12-18 00:18:46 +01003123 PSA_ASSERT( psa_cipher_update( &operation2,
3124 output1 + first_part_size,
3125 output1_length - first_part_size,
3126 output2, output2_buffer_size,
3127 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003128 TEST_ASSERT( function_output_length <=
3129 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3130 alg,
3131 output1_length - first_part_size ) );
3132 TEST_ASSERT( function_output_length <=
3133 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003134 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003135
Gilles Peskine8817f612018-12-18 00:18:46 +01003136 PSA_ASSERT( psa_cipher_finish( &operation2,
3137 output2 + output2_length,
3138 output2_buffer_size - output2_length,
3139 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003140 TEST_ASSERT( function_output_length <=
3141 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3142 TEST_ASSERT( function_output_length <=
3143 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003144 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003145
Gilles Peskine8817f612018-12-18 00:18:46 +01003146 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003147
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003148 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003149
3150exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003151 psa_cipher_abort( &operation1 );
3152 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003153 mbedtls_free( output1 );
3154 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003155 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003156 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003157}
3158/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003159
Gilles Peskine20035e32018-02-03 22:44:14 +01003160/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003161void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003162 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003163 data_t *nonce,
3164 data_t *additional_data,
3165 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003166 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003167{
Ronald Cron5425a212020-08-04 14:58:35 +02003168 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003169 psa_key_type_t key_type = key_type_arg;
3170 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003171 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003172 unsigned char *output_data = NULL;
3173 size_t output_size = 0;
3174 size_t output_length = 0;
3175 unsigned char *output_data2 = NULL;
3176 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003177 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003178 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003179 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003180
Gilles Peskine8817f612018-12-18 00:18:46 +01003181 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003182
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003183 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3184 psa_set_key_algorithm( &attributes, alg );
3185 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003186
Gilles Peskine049c7532019-05-15 20:22:09 +02003187 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003188 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003189 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3190 key_bits = psa_get_key_bits( &attributes );
3191
3192 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3193 alg );
3194 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3195 * should be exact. */
3196 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3197 expected_result != PSA_ERROR_NOT_SUPPORTED )
3198 {
3199 TEST_EQUAL( output_size,
3200 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3201 TEST_ASSERT( output_size <=
3202 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3203 }
3204 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003205
Steven Cooremanf49478b2021-02-15 15:19:25 +01003206 status = psa_aead_encrypt( key, alg,
3207 nonce->x, nonce->len,
3208 additional_data->x,
3209 additional_data->len,
3210 input_data->x, input_data->len,
3211 output_data, output_size,
3212 &output_length );
3213
3214 /* If the operation is not supported, just skip and not fail in case the
3215 * encryption involves a common limitation of cryptography hardwares and
3216 * an alternative implementation. */
3217 if( status == PSA_ERROR_NOT_SUPPORTED )
3218 {
3219 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3220 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3221 }
3222
3223 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003224
3225 if( PSA_SUCCESS == expected_result )
3226 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003227 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003228
Gilles Peskine003a4a92019-05-14 16:09:40 +02003229 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3230 * should be exact. */
3231 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003232 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003233
gabor-mezei-armceface22021-01-21 12:26:17 +01003234 TEST_ASSERT( input_data->len <=
3235 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3236
Ronald Cron5425a212020-08-04 14:58:35 +02003237 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003238 nonce->x, nonce->len,
3239 additional_data->x,
3240 additional_data->len,
3241 output_data, output_length,
3242 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003243 &output_length2 ),
3244 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003245
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003246 ASSERT_COMPARE( input_data->x, input_data->len,
3247 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003248 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003249
Gilles Peskinea1cac842018-06-11 19:33:02 +02003250exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003251 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003252 mbedtls_free( output_data );
3253 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003254 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003255}
3256/* END_CASE */
3257
3258/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003259void aead_encrypt( int key_type_arg, data_t *key_data,
3260 int alg_arg,
3261 data_t *nonce,
3262 data_t *additional_data,
3263 data_t *input_data,
3264 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003265{
Ronald Cron5425a212020-08-04 14:58:35 +02003266 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003267 psa_key_type_t key_type = key_type_arg;
3268 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003269 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003270 unsigned char *output_data = NULL;
3271 size_t output_size = 0;
3272 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003273 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003274 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003275
Gilles Peskine8817f612018-12-18 00:18:46 +01003276 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003277
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003278 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3279 psa_set_key_algorithm( &attributes, alg );
3280 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003281
Gilles Peskine049c7532019-05-15 20:22:09 +02003282 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003283 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003284 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3285 key_bits = psa_get_key_bits( &attributes );
3286
3287 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3288 alg );
3289 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3290 * should be exact. */
3291 TEST_EQUAL( output_size,
3292 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3293 TEST_ASSERT( output_size <=
3294 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3295 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003296
Steven Cooremand588ea12021-01-11 19:36:04 +01003297 status = psa_aead_encrypt( key, alg,
3298 nonce->x, nonce->len,
3299 additional_data->x, additional_data->len,
3300 input_data->x, input_data->len,
3301 output_data, output_size,
3302 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003303
Ronald Cron28a45ed2021-02-09 20:35:42 +01003304 /* If the operation is not supported, just skip and not fail in case the
3305 * encryption involves a common limitation of cryptography hardwares and
3306 * an alternative implementation. */
3307 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003308 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003309 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3310 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003311 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003312
3313 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003314 ASSERT_COMPARE( expected_result->x, expected_result->len,
3315 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003316
Gilles Peskinea1cac842018-06-11 19:33:02 +02003317exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003318 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003319 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003320 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003321}
3322/* END_CASE */
3323
3324/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003325void aead_decrypt( int key_type_arg, data_t *key_data,
3326 int alg_arg,
3327 data_t *nonce,
3328 data_t *additional_data,
3329 data_t *input_data,
3330 data_t *expected_data,
3331 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003332{
Ronald Cron5425a212020-08-04 14:58:35 +02003333 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003334 psa_key_type_t key_type = key_type_arg;
3335 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003336 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003337 unsigned char *output_data = NULL;
3338 size_t output_size = 0;
3339 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003340 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003341 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003342 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003343
Gilles Peskine8817f612018-12-18 00:18:46 +01003344 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003345
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003346 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3347 psa_set_key_algorithm( &attributes, alg );
3348 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003349
Gilles Peskine049c7532019-05-15 20:22:09 +02003350 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003351 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003352 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3353 key_bits = psa_get_key_bits( &attributes );
3354
3355 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3356 alg );
3357 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3358 expected_result != PSA_ERROR_NOT_SUPPORTED )
3359 {
3360 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3361 * should be exact. */
3362 TEST_EQUAL( output_size,
3363 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3364 TEST_ASSERT( output_size <=
3365 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3366 }
3367 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003368
Steven Cooremand588ea12021-01-11 19:36:04 +01003369 status = psa_aead_decrypt( key, alg,
3370 nonce->x, nonce->len,
3371 additional_data->x,
3372 additional_data->len,
3373 input_data->x, input_data->len,
3374 output_data, output_size,
3375 &output_length );
3376
Ronald Cron28a45ed2021-02-09 20:35:42 +01003377 /* If the operation is not supported, just skip and not fail in case the
3378 * decryption involves a common limitation of cryptography hardwares and
3379 * an alternative implementation. */
3380 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003381 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003382 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3383 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003384 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003385
3386 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003387
Gilles Peskine2d277862018-06-18 15:41:12 +02003388 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003389 ASSERT_COMPARE( expected_data->x, expected_data->len,
3390 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003391
Gilles Peskinea1cac842018-06-11 19:33:02 +02003392exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003393 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003394 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003395 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003396}
3397/* END_CASE */
3398
3399/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003400void signature_size( int type_arg,
3401 int bits,
3402 int alg_arg,
3403 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003404{
3405 psa_key_type_t type = type_arg;
3406 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003407 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003408
Gilles Peskinefe11b722018-12-18 00:24:04 +01003409 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003410
Gilles Peskinee59236f2018-01-27 23:32:46 +01003411exit:
3412 ;
3413}
3414/* END_CASE */
3415
3416/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003417void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3418 int alg_arg, data_t *input_data,
3419 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003420{
Ronald Cron5425a212020-08-04 14:58:35 +02003421 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003422 psa_key_type_t key_type = key_type_arg;
3423 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003424 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003425 unsigned char *signature = NULL;
3426 size_t signature_size;
3427 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003428 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003429
Gilles Peskine8817f612018-12-18 00:18:46 +01003430 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003431
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003432 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003433 psa_set_key_algorithm( &attributes, alg );
3434 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003435
Gilles Peskine049c7532019-05-15 20:22:09 +02003436 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003437 &key ) );
3438 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003439 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003440
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003441 /* Allocate a buffer which has the size advertized by the
3442 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003443 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003444 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003445 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003446 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003447 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003448
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003449 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003450 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003451 input_data->x, input_data->len,
3452 signature, signature_size,
3453 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003454 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003455 ASSERT_COMPARE( output_data->x, output_data->len,
3456 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003457
3458exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003459 /*
3460 * Key attributes may have been returned by psa_get_key_attributes()
3461 * thus reset them as required.
3462 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003463 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003464
Ronald Cron5425a212020-08-04 14:58:35 +02003465 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003466 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003467 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003468}
3469/* END_CASE */
3470
3471/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003472void sign_hash_fail( int key_type_arg, data_t *key_data,
3473 int alg_arg, data_t *input_data,
3474 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003475{
Ronald Cron5425a212020-08-04 14:58:35 +02003476 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003477 psa_key_type_t key_type = key_type_arg;
3478 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003479 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003480 psa_status_t actual_status;
3481 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003482 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003483 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003484 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003485
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003486 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003487
Gilles Peskine8817f612018-12-18 00:18:46 +01003488 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003489
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003490 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003491 psa_set_key_algorithm( &attributes, alg );
3492 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003493
Gilles Peskine049c7532019-05-15 20:22:09 +02003494 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003495 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003496
Ronald Cron5425a212020-08-04 14:58:35 +02003497 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003498 input_data->x, input_data->len,
3499 signature, signature_size,
3500 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003501 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003502 /* The value of *signature_length is unspecified on error, but
3503 * whatever it is, it should be less than signature_size, so that
3504 * if the caller tries to read *signature_length bytes without
3505 * checking the error code then they don't overflow a buffer. */
3506 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003507
3508exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003509 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003510 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003511 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003512 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003513}
3514/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003515
3516/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003517void sign_verify_hash( int key_type_arg, data_t *key_data,
3518 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003519{
Ronald Cron5425a212020-08-04 14:58:35 +02003520 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003521 psa_key_type_t key_type = key_type_arg;
3522 psa_algorithm_t alg = alg_arg;
3523 size_t key_bits;
3524 unsigned char *signature = NULL;
3525 size_t signature_size;
3526 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003527 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003528
Gilles Peskine8817f612018-12-18 00:18:46 +01003529 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003530
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003531 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003532 psa_set_key_algorithm( &attributes, alg );
3533 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003534
Gilles Peskine049c7532019-05-15 20:22:09 +02003535 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003536 &key ) );
3537 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003538 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003539
3540 /* Allocate a buffer which has the size advertized by the
3541 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003542 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003543 key_bits, alg );
3544 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003545 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003546 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003547
3548 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003549 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003550 input_data->x, input_data->len,
3551 signature, signature_size,
3552 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003553 /* Check that the signature length looks sensible. */
3554 TEST_ASSERT( signature_length <= signature_size );
3555 TEST_ASSERT( signature_length > 0 );
3556
3557 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003558 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003559 input_data->x, input_data->len,
3560 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003561
3562 if( input_data->len != 0 )
3563 {
3564 /* Flip a bit in the input and verify that the signature is now
3565 * detected as invalid. Flip a bit at the beginning, not at the end,
3566 * because ECDSA may ignore the last few bits of the input. */
3567 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003568 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003569 input_data->x, input_data->len,
3570 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003571 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003572 }
3573
3574exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003575 /*
3576 * Key attributes may have been returned by psa_get_key_attributes()
3577 * thus reset them as required.
3578 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003579 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003580
Ronald Cron5425a212020-08-04 14:58:35 +02003581 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003582 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003583 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003584}
3585/* END_CASE */
3586
3587/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003588void verify_hash( int key_type_arg, data_t *key_data,
3589 int alg_arg, data_t *hash_data,
3590 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003591{
Ronald Cron5425a212020-08-04 14:58:35 +02003592 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003593 psa_key_type_t key_type = key_type_arg;
3594 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003595 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003596
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003597 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003598
Gilles Peskine8817f612018-12-18 00:18:46 +01003599 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003600
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003601 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003602 psa_set_key_algorithm( &attributes, alg );
3603 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003604
Gilles Peskine049c7532019-05-15 20:22:09 +02003605 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003606 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003607
Ronald Cron5425a212020-08-04 14:58:35 +02003608 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003609 hash_data->x, hash_data->len,
3610 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003611
itayzafrir5c753392018-05-08 11:18:38 +03003612exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003613 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003614 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003615 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003616}
3617/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003618
3619/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003620void verify_hash_fail( int key_type_arg, data_t *key_data,
3621 int alg_arg, data_t *hash_data,
3622 data_t *signature_data,
3623 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003624{
Ronald Cron5425a212020-08-04 14:58:35 +02003625 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003626 psa_key_type_t key_type = key_type_arg;
3627 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003628 psa_status_t actual_status;
3629 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003630 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003631
Gilles Peskine8817f612018-12-18 00:18:46 +01003632 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003633
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003634 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003635 psa_set_key_algorithm( &attributes, alg );
3636 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003637
Gilles Peskine049c7532019-05-15 20:22:09 +02003638 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003639 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003640
Ronald Cron5425a212020-08-04 14:58:35 +02003641 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003642 hash_data->x, hash_data->len,
3643 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003644 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003645
3646exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003647 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003648 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003649 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003650}
3651/* END_CASE */
3652
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003653/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02003654void sign_message_deterministic( int key_type_arg,
3655 data_t *key_data,
3656 int alg_arg,
3657 data_t *input_data,
3658 data_t *output_data )
3659{
3660 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3661 psa_key_type_t key_type = key_type_arg;
3662 psa_algorithm_t alg = alg_arg;
3663 size_t key_bits;
3664 unsigned char *signature = NULL;
3665 size_t signature_size;
3666 size_t signature_length = 0xdeadbeef;
3667 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3668
3669 PSA_ASSERT( psa_crypto_init( ) );
3670
3671 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3672 psa_set_key_algorithm( &attributes, alg );
3673 psa_set_key_type( &attributes, key_type );
3674
3675 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3676 &key ) );
3677 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3678 key_bits = psa_get_key_bits( &attributes );
3679
3680 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3681 TEST_ASSERT( signature_size != 0 );
3682 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3683 ASSERT_ALLOC( signature, signature_size );
3684
3685 PSA_ASSERT( psa_sign_message( key, alg,
3686 input_data->x, input_data->len,
3687 signature, signature_size,
3688 &signature_length ) );
3689
3690 ASSERT_COMPARE( output_data->x, output_data->len,
3691 signature, signature_length );
3692
3693exit:
3694 psa_reset_key_attributes( &attributes );
3695
3696 psa_destroy_key( key );
3697 mbedtls_free( signature );
3698 PSA_DONE( );
3699
3700}
3701/* END_CASE */
3702
3703/* BEGIN_CASE */
3704void sign_message_fail( int key_type_arg,
3705 data_t *key_data,
3706 int alg_arg,
3707 data_t *input_data,
3708 int signature_size_arg,
3709 int expected_status_arg )
3710{
3711 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3712 psa_key_type_t key_type = key_type_arg;
3713 psa_algorithm_t alg = alg_arg;
3714 size_t signature_size = signature_size_arg;
3715 psa_status_t actual_status;
3716 psa_status_t expected_status = expected_status_arg;
3717 unsigned char *signature = NULL;
3718 size_t signature_length = 0xdeadbeef;
3719 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3720
3721 ASSERT_ALLOC( signature, signature_size );
3722
3723 PSA_ASSERT( psa_crypto_init( ) );
3724
3725 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3726 psa_set_key_algorithm( &attributes, alg );
3727 psa_set_key_type( &attributes, key_type );
3728
3729 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3730 &key ) );
3731
3732 actual_status = psa_sign_message( key, alg,
3733 input_data->x, input_data->len,
3734 signature, signature_size,
3735 &signature_length );
3736 TEST_EQUAL( actual_status, expected_status );
3737 /* The value of *signature_length is unspecified on error, but
3738 * whatever it is, it should be less than signature_size, so that
3739 * if the caller tries to read *signature_length bytes without
3740 * checking the error code then they don't overflow a buffer. */
3741 TEST_ASSERT( signature_length <= signature_size );
3742
3743exit:
3744 psa_reset_key_attributes( &attributes );
3745 psa_destroy_key( key );
3746 mbedtls_free( signature );
3747 PSA_DONE( );
3748}
3749/* END_CASE */
3750
3751/* BEGIN_CASE */
3752void sign_verify_message( int key_type_arg,
3753 data_t *key_data,
3754 int alg_arg,
3755 data_t *input_data )
3756{
3757 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3758 psa_key_type_t key_type = key_type_arg;
3759 psa_algorithm_t alg = alg_arg;
3760 size_t key_bits;
3761 unsigned char *signature = NULL;
3762 size_t signature_size;
3763 size_t signature_length = 0xdeadbeef;
3764 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3765
3766 PSA_ASSERT( psa_crypto_init( ) );
3767
3768 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3769 PSA_KEY_USAGE_VERIFY_MESSAGE );
3770 psa_set_key_algorithm( &attributes, alg );
3771 psa_set_key_type( &attributes, key_type );
3772
3773 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3774 &key ) );
3775 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3776 key_bits = psa_get_key_bits( &attributes );
3777
3778 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3779 TEST_ASSERT( signature_size != 0 );
3780 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3781 ASSERT_ALLOC( signature, signature_size );
3782
3783 PSA_ASSERT( psa_sign_message( key, alg,
3784 input_data->x, input_data->len,
3785 signature, signature_size,
3786 &signature_length ) );
3787 TEST_ASSERT( signature_length <= signature_size );
3788 TEST_ASSERT( signature_length > 0 );
3789
3790 PSA_ASSERT( psa_verify_message( key, alg,
3791 input_data->x, input_data->len,
3792 signature, signature_length ) );
3793
3794 if( input_data->len != 0 )
3795 {
3796 /* Flip a bit in the input and verify that the signature is now
3797 * detected as invalid. Flip a bit at the beginning, not at the end,
3798 * because ECDSA may ignore the last few bits of the input. */
3799 input_data->x[0] ^= 1;
3800 TEST_EQUAL( psa_verify_message( key, alg,
3801 input_data->x, input_data->len,
3802 signature, signature_length ),
3803 PSA_ERROR_INVALID_SIGNATURE );
3804 }
3805
3806exit:
3807 psa_reset_key_attributes( &attributes );
3808
3809 psa_destroy_key( key );
3810 mbedtls_free( signature );
3811 PSA_DONE( );
3812}
3813/* END_CASE */
3814
3815/* BEGIN_CASE */
3816void verify_message( int key_type_arg,
3817 data_t *key_data,
3818 int alg_arg,
3819 data_t *input_data,
3820 data_t *signature_data )
3821{
3822 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3823 psa_key_type_t key_type = key_type_arg;
3824 psa_algorithm_t alg = alg_arg;
3825 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3826
3827 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3828
3829 PSA_ASSERT( psa_crypto_init( ) );
3830
3831 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3832 psa_set_key_algorithm( &attributes, alg );
3833 psa_set_key_type( &attributes, key_type );
3834
3835 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3836 &key ) );
3837
3838 PSA_ASSERT( psa_verify_message( key, alg,
3839 input_data->x, input_data->len,
3840 signature_data->x, signature_data->len ) );
3841
3842exit:
3843 psa_reset_key_attributes( &attributes );
3844 psa_destroy_key( key );
3845 PSA_DONE( );
3846}
3847/* END_CASE */
3848
3849/* BEGIN_CASE */
3850void verify_message_fail( int key_type_arg,
3851 data_t *key_data,
3852 int alg_arg,
3853 data_t *hash_data,
3854 data_t *signature_data,
3855 int expected_status_arg )
3856{
3857 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3858 psa_key_type_t key_type = key_type_arg;
3859 psa_algorithm_t alg = alg_arg;
3860 psa_status_t actual_status;
3861 psa_status_t expected_status = expected_status_arg;
3862 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3863
3864 PSA_ASSERT( psa_crypto_init( ) );
3865
3866 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3867 psa_set_key_algorithm( &attributes, alg );
3868 psa_set_key_type( &attributes, key_type );
3869
3870 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3871 &key ) );
3872
3873 actual_status = psa_verify_message( key, alg,
3874 hash_data->x, hash_data->len,
3875 signature_data->x,
3876 signature_data->len );
3877 TEST_EQUAL( actual_status, expected_status );
3878
3879exit:
3880 psa_reset_key_attributes( &attributes );
3881 psa_destroy_key( key );
3882 PSA_DONE( );
3883}
3884/* END_CASE */
3885
3886/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003887void asymmetric_encrypt( int key_type_arg,
3888 data_t *key_data,
3889 int alg_arg,
3890 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003891 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003892 int expected_output_length_arg,
3893 int expected_status_arg )
3894{
Ronald Cron5425a212020-08-04 14:58:35 +02003895 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003896 psa_key_type_t key_type = key_type_arg;
3897 psa_algorithm_t alg = alg_arg;
3898 size_t expected_output_length = expected_output_length_arg;
3899 size_t key_bits;
3900 unsigned char *output = NULL;
3901 size_t output_size;
3902 size_t output_length = ~0;
3903 psa_status_t actual_status;
3904 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003905 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003906
Gilles Peskine8817f612018-12-18 00:18:46 +01003907 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003908
Gilles Peskine656896e2018-06-29 19:12:28 +02003909 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003910 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3911 psa_set_key_algorithm( &attributes, alg );
3912 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003913 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003914 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003915
3916 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003917 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003918 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003919
Gilles Peskine656896e2018-06-29 19:12:28 +02003920 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003921 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003922 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003923
3924 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003925 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003926 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003927 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003928 output, output_size,
3929 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003930 TEST_EQUAL( actual_status, expected_status );
3931 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003932
Gilles Peskine68428122018-06-30 18:42:41 +02003933 /* If the label is empty, the test framework puts a non-null pointer
3934 * in label->x. Test that a null pointer works as well. */
3935 if( label->len == 0 )
3936 {
3937 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003938 if( output_size != 0 )
3939 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003940 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003941 input_data->x, input_data->len,
3942 NULL, label->len,
3943 output, output_size,
3944 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003945 TEST_EQUAL( actual_status, expected_status );
3946 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003947 }
3948
Gilles Peskine656896e2018-06-29 19:12:28 +02003949exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003950 /*
3951 * Key attributes may have been returned by psa_get_key_attributes()
3952 * thus reset them as required.
3953 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003954 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003955
Ronald Cron5425a212020-08-04 14:58:35 +02003956 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02003957 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003958 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003959}
3960/* END_CASE */
3961
3962/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003963void asymmetric_encrypt_decrypt( int key_type_arg,
3964 data_t *key_data,
3965 int alg_arg,
3966 data_t *input_data,
3967 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003968{
Ronald Cron5425a212020-08-04 14:58:35 +02003969 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003970 psa_key_type_t key_type = key_type_arg;
3971 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003972 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003973 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003974 size_t output_size;
3975 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003976 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003977 size_t output2_size;
3978 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003979 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003980
Gilles Peskine8817f612018-12-18 00:18:46 +01003981 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003982
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003983 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3984 psa_set_key_algorithm( &attributes, alg );
3985 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003986
Gilles Peskine049c7532019-05-15 20:22:09 +02003987 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003988 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003989
3990 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02003991 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003992 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003993
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003994 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003995 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003996 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01003997
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003998 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01003999 TEST_ASSERT( output2_size <=
4000 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
4001 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004002 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004003
Gilles Peskineeebd7382018-06-08 18:11:54 +02004004 /* We test encryption by checking that encrypt-then-decrypt gives back
4005 * the original plaintext because of the non-optional random
4006 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004007 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004008 input_data->x, input_data->len,
4009 label->x, label->len,
4010 output, output_size,
4011 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004012 /* We don't know what ciphertext length to expect, but check that
4013 * it looks sensible. */
4014 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004015
Ronald Cron5425a212020-08-04 14:58:35 +02004016 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004017 output, output_length,
4018 label->x, label->len,
4019 output2, output2_size,
4020 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004021 ASSERT_COMPARE( input_data->x, input_data->len,
4022 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004023
4024exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004025 /*
4026 * Key attributes may have been returned by psa_get_key_attributes()
4027 * thus reset them as required.
4028 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004029 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004030
Ronald Cron5425a212020-08-04 14:58:35 +02004031 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004032 mbedtls_free( output );
4033 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004034 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004035}
4036/* END_CASE */
4037
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004038/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004039void asymmetric_decrypt( int key_type_arg,
4040 data_t *key_data,
4041 int alg_arg,
4042 data_t *input_data,
4043 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004044 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004045{
Ronald Cron5425a212020-08-04 14:58:35 +02004046 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004047 psa_key_type_t key_type = key_type_arg;
4048 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004049 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004050 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004051 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004052 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004053 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004054
Gilles Peskine8817f612018-12-18 00:18:46 +01004055 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004056
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004057 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4058 psa_set_key_algorithm( &attributes, alg );
4059 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004060
Gilles Peskine049c7532019-05-15 20:22:09 +02004061 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004062 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004063
gabor-mezei-armceface22021-01-21 12:26:17 +01004064 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4065 key_bits = psa_get_key_bits( &attributes );
4066
4067 /* Determine the maximum ciphertext length */
4068 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
4069 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
4070 ASSERT_ALLOC( output, output_size );
4071
Ronald Cron5425a212020-08-04 14:58:35 +02004072 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004073 input_data->x, input_data->len,
4074 label->x, label->len,
4075 output,
4076 output_size,
4077 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004078 ASSERT_COMPARE( expected_data->x, expected_data->len,
4079 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004080
Gilles Peskine68428122018-06-30 18:42:41 +02004081 /* If the label is empty, the test framework puts a non-null pointer
4082 * in label->x. Test that a null pointer works as well. */
4083 if( label->len == 0 )
4084 {
4085 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004086 if( output_size != 0 )
4087 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004088 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004089 input_data->x, input_data->len,
4090 NULL, label->len,
4091 output,
4092 output_size,
4093 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004094 ASSERT_COMPARE( expected_data->x, expected_data->len,
4095 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004096 }
4097
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004098exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004099 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004100 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004101 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004102 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004103}
4104/* END_CASE */
4105
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004106/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004107void asymmetric_decrypt_fail( int key_type_arg,
4108 data_t *key_data,
4109 int alg_arg,
4110 data_t *input_data,
4111 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004112 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004113 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004114{
Ronald Cron5425a212020-08-04 14:58:35 +02004115 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004116 psa_key_type_t key_type = key_type_arg;
4117 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004118 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004119 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004120 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004121 psa_status_t actual_status;
4122 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004123 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004124
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004125 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004126
Gilles Peskine8817f612018-12-18 00:18:46 +01004127 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004128
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004129 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4130 psa_set_key_algorithm( &attributes, alg );
4131 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004132
Gilles Peskine049c7532019-05-15 20:22:09 +02004133 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004134 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004135
Ronald Cron5425a212020-08-04 14:58:35 +02004136 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004137 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004138 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004139 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004140 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004141 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004142 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004143
Gilles Peskine68428122018-06-30 18:42:41 +02004144 /* If the label is empty, the test framework puts a non-null pointer
4145 * in label->x. Test that a null pointer works as well. */
4146 if( label->len == 0 )
4147 {
4148 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004149 if( output_size != 0 )
4150 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004151 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004152 input_data->x, input_data->len,
4153 NULL, label->len,
4154 output, output_size,
4155 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004156 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004157 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004158 }
4159
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004160exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004161 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004162 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004163 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004164 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004165}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004166/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004167
4168/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004169void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004170{
4171 /* Test each valid way of initializing the object, except for `= {0}`, as
4172 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4173 * though it's OK by the C standard. We could test for this, but we'd need
4174 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004175 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004176 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4177 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4178 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004179
4180 memset( &zero, 0, sizeof( zero ) );
4181
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004182 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004183 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004184 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004185 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004186 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004187 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004188 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004189
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004190 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004191 PSA_ASSERT( psa_key_derivation_abort(&func) );
4192 PSA_ASSERT( psa_key_derivation_abort(&init) );
4193 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004194}
4195/* END_CASE */
4196
Janos Follath16de4a42019-06-13 16:32:24 +01004197/* BEGIN_CASE */
4198void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004199{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004200 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004201 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004202 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004203
Gilles Peskine8817f612018-12-18 00:18:46 +01004204 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004205
Janos Follath16de4a42019-06-13 16:32:24 +01004206 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004207 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004208
4209exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004210 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004211 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004212}
4213/* END_CASE */
4214
Janos Follathaf3c2a02019-06-12 12:34:34 +01004215/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004216void derive_set_capacity( int alg_arg, int capacity_arg,
4217 int expected_status_arg )
4218{
4219 psa_algorithm_t alg = alg_arg;
4220 size_t capacity = capacity_arg;
4221 psa_status_t expected_status = expected_status_arg;
4222 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4223
4224 PSA_ASSERT( psa_crypto_init( ) );
4225
4226 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4227
4228 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4229 expected_status );
4230
4231exit:
4232 psa_key_derivation_abort( &operation );
4233 PSA_DONE( );
4234}
4235/* END_CASE */
4236
4237/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004238void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004239 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004240 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004241 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004242 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004243 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004244 int expected_status_arg3,
4245 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004246{
4247 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004248 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4249 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004250 psa_status_t expected_statuses[] = {expected_status_arg1,
4251 expected_status_arg2,
4252 expected_status_arg3};
4253 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004254 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4255 MBEDTLS_SVC_KEY_ID_INIT,
4256 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004257 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4258 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4259 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004260 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004261 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004262 psa_status_t expected_output_status = expected_output_status_arg;
4263 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004264
4265 PSA_ASSERT( psa_crypto_init( ) );
4266
4267 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4268 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004269
4270 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4271
4272 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4273 {
Gilles Peskine4023c012021-05-27 13:21:20 +02004274 mbedtls_test_set_step( i );
4275 if( steps[i] == 0 )
4276 {
4277 /* Skip this step */
4278 }
4279 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004280 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004281 psa_set_key_type( &attributes, key_types[i] );
4282 PSA_ASSERT( psa_import_key( &attributes,
4283 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004284 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004285 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4286 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4287 {
4288 // When taking a private key as secret input, use key agreement
4289 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004290 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4291 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004292 expected_statuses[i] );
4293 }
4294 else
4295 {
4296 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004297 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004298 expected_statuses[i] );
4299 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004300 }
4301 else
4302 {
4303 TEST_EQUAL( psa_key_derivation_input_bytes(
4304 &operation, steps[i],
4305 inputs[i]->x, inputs[i]->len ),
4306 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004307 }
4308 }
4309
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004310 if( output_key_type != PSA_KEY_TYPE_NONE )
4311 {
4312 psa_reset_key_attributes( &attributes );
4313 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4314 psa_set_key_bits( &attributes, 8 );
4315 actual_output_status =
4316 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004317 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004318 }
4319 else
4320 {
4321 uint8_t buffer[1];
4322 actual_output_status =
4323 psa_key_derivation_output_bytes( &operation,
4324 buffer, sizeof( buffer ) );
4325 }
4326 TEST_EQUAL( actual_output_status, expected_output_status );
4327
Janos Follathaf3c2a02019-06-12 12:34:34 +01004328exit:
4329 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004330 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4331 psa_destroy_key( keys[i] );
4332 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004333 PSA_DONE( );
4334}
4335/* END_CASE */
4336
Janos Follathd958bb72019-07-03 15:02:16 +01004337/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02004338void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004339{
Janos Follathd958bb72019-07-03 15:02:16 +01004340 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004341 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004342 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004343 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004344 unsigned char input1[] = "Input 1";
4345 size_t input1_length = sizeof( input1 );
4346 unsigned char input2[] = "Input 2";
4347 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004348 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004349 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004350 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4351 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4352 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004353 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004354
Gilles Peskine8817f612018-12-18 00:18:46 +01004355 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004356
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004357 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4358 psa_set_key_algorithm( &attributes, alg );
4359 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004360
Gilles Peskine73676cb2019-05-15 20:15:10 +02004361 PSA_ASSERT( psa_import_key( &attributes,
4362 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004363 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004364
4365 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004366 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4367 input1, input1_length,
4368 input2, input2_length,
4369 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004370 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004371
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004372 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004373 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004374 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004375
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004376 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004377
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004378 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004379 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004380
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004381exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004382 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004383 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004384 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004385}
4386/* END_CASE */
4387
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004388/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02004389void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004390{
4391 uint8_t output_buffer[16];
4392 size_t buffer_size = 16;
4393 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004394 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004395
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004396 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4397 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004398 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004399
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004400 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004401 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004402
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004403 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004404
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004405 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4406 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004407 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004408
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004409 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004410 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004411
4412exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004413 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004414}
4415/* END_CASE */
4416
4417/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004418void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004419 int step1_arg, data_t *input1,
4420 int step2_arg, data_t *input2,
4421 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004422 int requested_capacity_arg,
4423 data_t *expected_output1,
4424 data_t *expected_output2 )
4425{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004426 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004427 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4428 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004429 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4430 MBEDTLS_SVC_KEY_ID_INIT,
4431 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004432 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004433 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004434 uint8_t *expected_outputs[2] =
4435 {expected_output1->x, expected_output2->x};
4436 size_t output_sizes[2] =
4437 {expected_output1->len, expected_output2->len};
4438 size_t output_buffer_size = 0;
4439 uint8_t *output_buffer = NULL;
4440 size_t expected_capacity;
4441 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004442 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004443 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004444 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004445
4446 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4447 {
4448 if( output_sizes[i] > output_buffer_size )
4449 output_buffer_size = output_sizes[i];
4450 if( output_sizes[i] == 0 )
4451 expected_outputs[i] = NULL;
4452 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004453 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004454 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004455
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004456 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4457 psa_set_key_algorithm( &attributes, alg );
4458 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004459
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004460 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004461 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4462 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4463 requested_capacity ) );
4464 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004465 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004466 switch( steps[i] )
4467 {
4468 case 0:
4469 break;
4470 case PSA_KEY_DERIVATION_INPUT_SECRET:
4471 PSA_ASSERT( psa_import_key( &attributes,
4472 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004473 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004474
4475 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4476 {
4477 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4478 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4479 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4480 }
4481
Gilles Peskine1468da72019-05-29 17:35:49 +02004482 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004483 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004484 break;
4485 default:
4486 PSA_ASSERT( psa_key_derivation_input_bytes(
4487 &operation, steps[i],
4488 inputs[i]->x, inputs[i]->len ) );
4489 break;
4490 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004491 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004492
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004493 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004494 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004495 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004496 expected_capacity = requested_capacity;
4497
4498 /* Expansion phase. */
4499 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4500 {
4501 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004502 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004503 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004504 if( expected_capacity == 0 && output_sizes[i] == 0 )
4505 {
4506 /* Reading 0 bytes when 0 bytes are available can go either way. */
4507 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004508 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004509 continue;
4510 }
4511 else if( expected_capacity == 0 ||
4512 output_sizes[i] > expected_capacity )
4513 {
4514 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004515 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004516 expected_capacity = 0;
4517 continue;
4518 }
4519 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004520 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004521 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004522 ASSERT_COMPARE( output_buffer, output_sizes[i],
4523 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004524 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004525 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004526 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004527 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004528 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004529 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004530 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004531
4532exit:
4533 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004534 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004535 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4536 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004537 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004538}
4539/* END_CASE */
4540
4541/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004542void derive_full( int alg_arg,
4543 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004544 data_t *input1,
4545 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004546 int requested_capacity_arg )
4547{
Ronald Cron5425a212020-08-04 14:58:35 +02004548 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004549 psa_algorithm_t alg = alg_arg;
4550 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004551 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004552 unsigned char output_buffer[16];
4553 size_t expected_capacity = requested_capacity;
4554 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004555 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004556
Gilles Peskine8817f612018-12-18 00:18:46 +01004557 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004558
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004559 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4560 psa_set_key_algorithm( &attributes, alg );
4561 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004562
Gilles Peskine049c7532019-05-15 20:22:09 +02004563 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004564 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004565
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004566 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4567 input1->x, input1->len,
4568 input2->x, input2->len,
4569 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004570 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004571
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004572 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004573 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004574 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004575
4576 /* Expansion phase. */
4577 while( current_capacity > 0 )
4578 {
4579 size_t read_size = sizeof( output_buffer );
4580 if( read_size > current_capacity )
4581 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004582 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004583 output_buffer,
4584 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004585 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004586 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004587 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004588 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004589 }
4590
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004591 /* Check that the operation refuses to go over capacity. */
4592 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004593 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004594
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004595 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004596
4597exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004598 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004599 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004600 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004601}
4602/* END_CASE */
4603
Janos Follathe60c9052019-07-03 13:51:30 +01004604/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004605void derive_key_exercise( int alg_arg,
4606 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004607 data_t *input1,
4608 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004609 int derived_type_arg,
4610 int derived_bits_arg,
4611 int derived_usage_arg,
4612 int derived_alg_arg )
4613{
Ronald Cron5425a212020-08-04 14:58:35 +02004614 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4615 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004616 psa_algorithm_t alg = alg_arg;
4617 psa_key_type_t derived_type = derived_type_arg;
4618 size_t derived_bits = derived_bits_arg;
4619 psa_key_usage_t derived_usage = derived_usage_arg;
4620 psa_algorithm_t derived_alg = derived_alg_arg;
4621 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004622 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004623 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004624 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004625
Gilles Peskine8817f612018-12-18 00:18:46 +01004626 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004627
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004628 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4629 psa_set_key_algorithm( &attributes, alg );
4630 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004631 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004632 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004633
4634 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004635 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4636 input1->x, input1->len,
4637 input2->x, input2->len,
4638 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004639 goto exit;
4640
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004641 psa_set_key_usage_flags( &attributes, derived_usage );
4642 psa_set_key_algorithm( &attributes, derived_alg );
4643 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004644 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004645 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004646 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004647
4648 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004649 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004650 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4651 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004652
4653 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004654 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004655 goto exit;
4656
4657exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004658 /*
4659 * Key attributes may have been returned by psa_get_key_attributes()
4660 * thus reset them as required.
4661 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004662 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004663
4664 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
Janos Follath42fd8882019-07-03 14:17:09 +01004671/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004672void derive_key_export( int alg_arg,
4673 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004674 data_t *input1,
4675 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004676 int bytes1_arg,
4677 int bytes2_arg )
4678{
Ronald Cron5425a212020-08-04 14:58:35 +02004679 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4680 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004681 psa_algorithm_t alg = alg_arg;
4682 size_t bytes1 = bytes1_arg;
4683 size_t bytes2 = bytes2_arg;
4684 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004685 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004686 uint8_t *output_buffer = NULL;
4687 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004688 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4689 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004690 size_t length;
4691
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004692 ASSERT_ALLOC( output_buffer, capacity );
4693 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004694 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004695
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004696 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4697 psa_set_key_algorithm( &base_attributes, alg );
4698 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004699 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004700 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004701
4702 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004703 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4704 input1->x, input1->len,
4705 input2->x, input2->len,
4706 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004707 goto exit;
4708
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004709 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004710 output_buffer,
4711 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004712 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004713
4714 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004715 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4716 input1->x, input1->len,
4717 input2->x, input2->len,
4718 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004719 goto exit;
4720
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004721 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4722 psa_set_key_algorithm( &derived_attributes, 0 );
4723 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004724 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004725 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004726 &derived_key ) );
4727 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004728 export_buffer, bytes1,
4729 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004730 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004731 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004732 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004733 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004734 &derived_key ) );
4735 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004736 export_buffer + bytes1, bytes2,
4737 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004738 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004739
4740 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004741 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4742 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004743
4744exit:
4745 mbedtls_free( output_buffer );
4746 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004747 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004748 psa_destroy_key( base_key );
4749 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004750 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004751}
4752/* END_CASE */
4753
4754/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004755void derive_key( int alg_arg,
4756 data_t *key_data, data_t *input1, data_t *input2,
4757 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004758 int expected_status_arg,
4759 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004760{
Ronald Cron5425a212020-08-04 14:58:35 +02004761 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4762 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004763 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004764 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004765 size_t bits = bits_arg;
4766 psa_status_t expected_status = expected_status_arg;
4767 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4768 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4769 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4770
4771 PSA_ASSERT( psa_crypto_init( ) );
4772
4773 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4774 psa_set_key_algorithm( &base_attributes, alg );
4775 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4776 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004777 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004778
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004779 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4780 input1->x, input1->len,
4781 input2->x, input2->len,
4782 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004783 goto exit;
4784
4785 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4786 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004787 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004788 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004789
4790 psa_status_t status =
4791 psa_key_derivation_output_key( &derived_attributes,
4792 &operation,
4793 &derived_key );
4794 if( is_large_output > 0 )
4795 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4796 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004797
4798exit:
4799 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004800 psa_destroy_key( base_key );
4801 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004802 PSA_DONE( );
4803}
4804/* END_CASE */
4805
4806/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004807void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004808 int our_key_type_arg, int our_key_alg_arg,
4809 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004810 int expected_status_arg )
4811{
Ronald Cron5425a212020-08-04 14:58:35 +02004812 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004813 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004814 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004815 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004816 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004817 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004818 psa_status_t expected_status = expected_status_arg;
4819 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004820
Gilles Peskine8817f612018-12-18 00:18:46 +01004821 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004822
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004823 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004824 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004825 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004826 PSA_ASSERT( psa_import_key( &attributes,
4827 our_key_data->x, our_key_data->len,
4828 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004829
Gilles Peskine77f40d82019-04-11 21:27:06 +02004830 /* The tests currently include inputs that should fail at either step.
4831 * Test cases that fail at the setup step should be changed to call
4832 * key_derivation_setup instead, and this function should be renamed
4833 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004834 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004835 if( status == PSA_SUCCESS )
4836 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004837 TEST_EQUAL( psa_key_derivation_key_agreement(
4838 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4839 our_key,
4840 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004841 expected_status );
4842 }
4843 else
4844 {
4845 TEST_ASSERT( status == expected_status );
4846 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004847
4848exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004849 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004850 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004851 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004852}
4853/* END_CASE */
4854
4855/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004856void raw_key_agreement( int alg_arg,
4857 int our_key_type_arg, data_t *our_key_data,
4858 data_t *peer_key_data,
4859 data_t *expected_output )
4860{
Ronald Cron5425a212020-08-04 14:58:35 +02004861 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004862 psa_algorithm_t alg = alg_arg;
4863 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004864 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004865 unsigned char *output = NULL;
4866 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004867 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004868
4869 ASSERT_ALLOC( output, expected_output->len );
4870 PSA_ASSERT( psa_crypto_init( ) );
4871
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004872 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4873 psa_set_key_algorithm( &attributes, alg );
4874 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004875 PSA_ASSERT( psa_import_key( &attributes,
4876 our_key_data->x, our_key_data->len,
4877 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004878
gabor-mezei-armceface22021-01-21 12:26:17 +01004879 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4880 key_bits = psa_get_key_bits( &attributes );
4881
Gilles Peskinebe697d82019-05-16 18:00:41 +02004882 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4883 peer_key_data->x, peer_key_data->len,
4884 output, expected_output->len,
4885 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004886 ASSERT_COMPARE( output, output_length,
4887 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01004888 TEST_ASSERT( output_length <=
4889 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
4890 TEST_ASSERT( output_length <=
4891 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004892
4893exit:
4894 mbedtls_free( output );
4895 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004896 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004897}
4898/* END_CASE */
4899
4900/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004901void key_agreement_capacity( int alg_arg,
4902 int our_key_type_arg, data_t *our_key_data,
4903 data_t *peer_key_data,
4904 int expected_capacity_arg )
4905{
Ronald Cron5425a212020-08-04 14:58:35 +02004906 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004907 psa_algorithm_t alg = alg_arg;
4908 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004909 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004910 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004911 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004912 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004913
Gilles Peskine8817f612018-12-18 00:18:46 +01004914 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004915
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004916 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4917 psa_set_key_algorithm( &attributes, alg );
4918 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004919 PSA_ASSERT( psa_import_key( &attributes,
4920 our_key_data->x, our_key_data->len,
4921 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004922
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004923 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004924 PSA_ASSERT( psa_key_derivation_key_agreement(
4925 &operation,
4926 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4927 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004928 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4929 {
4930 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004931 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004932 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004933 NULL, 0 ) );
4934 }
Gilles Peskine59685592018-09-18 12:11:34 +02004935
Gilles Peskinebf491972018-10-25 22:36:12 +02004936 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004937 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004938 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004939 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004940
Gilles Peskinebf491972018-10-25 22:36:12 +02004941 /* Test the actual capacity by reading the output. */
4942 while( actual_capacity > sizeof( output ) )
4943 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004944 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004945 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004946 actual_capacity -= sizeof( output );
4947 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004948 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004949 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004950 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004951 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004952
Gilles Peskine59685592018-09-18 12:11:34 +02004953exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004954 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004955 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004956 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004957}
4958/* END_CASE */
4959
4960/* BEGIN_CASE */
4961void key_agreement_output( int alg_arg,
4962 int our_key_type_arg, data_t *our_key_data,
4963 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004964 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004965{
Ronald Cron5425a212020-08-04 14:58:35 +02004966 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004967 psa_algorithm_t alg = alg_arg;
4968 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004969 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004970 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004971 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004972
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004973 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4974 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004975
Gilles Peskine8817f612018-12-18 00:18:46 +01004976 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004977
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004978 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4979 psa_set_key_algorithm( &attributes, alg );
4980 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004981 PSA_ASSERT( psa_import_key( &attributes,
4982 our_key_data->x, our_key_data->len,
4983 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004984
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004985 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004986 PSA_ASSERT( psa_key_derivation_key_agreement(
4987 &operation,
4988 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4989 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004990 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4991 {
4992 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004993 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004994 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004995 NULL, 0 ) );
4996 }
Gilles Peskine59685592018-09-18 12:11:34 +02004997
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004998 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004999 actual_output,
5000 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005001 ASSERT_COMPARE( actual_output, expected_output1->len,
5002 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005003 if( expected_output2->len != 0 )
5004 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005005 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005006 actual_output,
5007 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005008 ASSERT_COMPARE( actual_output, expected_output2->len,
5009 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005010 }
Gilles Peskine59685592018-09-18 12:11:34 +02005011
5012exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005013 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005014 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005015 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005016 mbedtls_free( actual_output );
5017}
5018/* END_CASE */
5019
5020/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005021void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005022{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005023 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005024 unsigned char *output = NULL;
5025 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005026 size_t i;
5027 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005028
Simon Butcher49f8e312020-03-03 15:51:50 +00005029 TEST_ASSERT( bytes_arg >= 0 );
5030
Gilles Peskine91892022021-02-08 19:50:26 +01005031 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005032 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005033
Gilles Peskine8817f612018-12-18 00:18:46 +01005034 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005035
Gilles Peskinea50d7392018-06-21 10:22:13 +02005036 /* Run several times, to ensure that every output byte will be
5037 * nonzero at least once with overwhelming probability
5038 * (2^(-8*number_of_runs)). */
5039 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005040 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005041 if( bytes != 0 )
5042 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005043 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005044
Gilles Peskinea50d7392018-06-21 10:22:13 +02005045 for( i = 0; i < bytes; i++ )
5046 {
5047 if( output[i] != 0 )
5048 ++changed[i];
5049 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005050 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005051
5052 /* Check that every byte was changed to nonzero at least once. This
5053 * validates that psa_generate_random is overwriting every byte of
5054 * the output buffer. */
5055 for( i = 0; i < bytes; i++ )
5056 {
5057 TEST_ASSERT( changed[i] != 0 );
5058 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005059
5060exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005061 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005062 mbedtls_free( output );
5063 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005064}
5065/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005066
5067/* BEGIN_CASE */
5068void generate_key( int type_arg,
5069 int bits_arg,
5070 int usage_arg,
5071 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005072 int expected_status_arg,
5073 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005074{
Ronald Cron5425a212020-08-04 14:58:35 +02005075 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005076 psa_key_type_t type = type_arg;
5077 psa_key_usage_t usage = usage_arg;
5078 size_t bits = bits_arg;
5079 psa_algorithm_t alg = alg_arg;
5080 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005081 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005082 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005083
Gilles Peskine8817f612018-12-18 00:18:46 +01005084 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005085
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005086 psa_set_key_usage_flags( &attributes, usage );
5087 psa_set_key_algorithm( &attributes, alg );
5088 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005089 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005090
5091 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005092 psa_status_t status = psa_generate_key( &attributes, &key );
5093
5094 if( is_large_key > 0 )
5095 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5096 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005097 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005098 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005099
5100 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005101 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005102 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5103 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005104
Gilles Peskine818ca122018-06-20 18:16:48 +02005105 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005106 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005107 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005108
5109exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005110 /*
5111 * Key attributes may have been returned by psa_get_key_attributes()
5112 * thus reset them as required.
5113 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005114 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005115
Ronald Cron5425a212020-08-04 14:58:35 +02005116 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005117 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005118}
5119/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005120
Ronald Cronee414c72021-03-18 18:50:08 +01005121/* 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 +02005122void generate_key_rsa( int bits_arg,
5123 data_t *e_arg,
5124 int expected_status_arg )
5125{
Ronald Cron5425a212020-08-04 14:58:35 +02005126 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005127 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005128 size_t bits = bits_arg;
5129 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5130 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5131 psa_status_t expected_status = expected_status_arg;
5132 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5133 uint8_t *exported = NULL;
5134 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005135 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005136 size_t exported_length = SIZE_MAX;
5137 uint8_t *e_read_buffer = NULL;
5138 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005139 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005140 size_t e_read_length = SIZE_MAX;
5141
5142 if( e_arg->len == 0 ||
5143 ( e_arg->len == 3 &&
5144 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5145 {
5146 is_default_public_exponent = 1;
5147 e_read_size = 0;
5148 }
5149 ASSERT_ALLOC( e_read_buffer, e_read_size );
5150 ASSERT_ALLOC( exported, exported_size );
5151
5152 PSA_ASSERT( psa_crypto_init( ) );
5153
5154 psa_set_key_usage_flags( &attributes, usage );
5155 psa_set_key_algorithm( &attributes, alg );
5156 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5157 e_arg->x, e_arg->len ) );
5158 psa_set_key_bits( &attributes, bits );
5159
5160 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005161 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005162 if( expected_status != PSA_SUCCESS )
5163 goto exit;
5164
5165 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005166 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005167 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5168 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5169 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5170 e_read_buffer, e_read_size,
5171 &e_read_length ) );
5172 if( is_default_public_exponent )
5173 TEST_EQUAL( e_read_length, 0 );
5174 else
5175 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5176
5177 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005178 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005179 goto exit;
5180
5181 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005182 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005183 exported, exported_size,
5184 &exported_length ) );
5185 {
5186 uint8_t *p = exported;
5187 uint8_t *end = exported + exported_length;
5188 size_t len;
5189 /* RSAPublicKey ::= SEQUENCE {
5190 * modulus INTEGER, -- n
5191 * publicExponent INTEGER } -- e
5192 */
5193 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005194 MBEDTLS_ASN1_SEQUENCE |
5195 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005196 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005197 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5198 MBEDTLS_ASN1_INTEGER ) );
5199 if( len >= 1 && p[0] == 0 )
5200 {
5201 ++p;
5202 --len;
5203 }
5204 if( e_arg->len == 0 )
5205 {
5206 TEST_EQUAL( len, 3 );
5207 TEST_EQUAL( p[0], 1 );
5208 TEST_EQUAL( p[1], 0 );
5209 TEST_EQUAL( p[2], 1 );
5210 }
5211 else
5212 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5213 }
5214
5215exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005216 /*
5217 * Key attributes may have been returned by psa_get_key_attributes() or
5218 * set by psa_set_key_domain_parameters() thus reset them as required.
5219 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005220 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005221
Ronald Cron5425a212020-08-04 14:58:35 +02005222 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005223 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005224 mbedtls_free( e_read_buffer );
5225 mbedtls_free( exported );
5226}
5227/* END_CASE */
5228
Darryl Greend49a4992018-06-18 17:27:26 +01005229/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005230void persistent_key_load_key_from_storage( data_t *data,
5231 int type_arg, int bits_arg,
5232 int usage_flags_arg, int alg_arg,
5233 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005234{
Ronald Cron71016a92020-08-28 19:01:50 +02005235 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005236 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005237 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5238 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005239 psa_key_type_t type = type_arg;
5240 size_t bits = bits_arg;
5241 psa_key_usage_t usage_flags = usage_flags_arg;
5242 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005243 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005244 unsigned char *first_export = NULL;
5245 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005246 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005247 size_t first_exported_length;
5248 size_t second_exported_length;
5249
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005250 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5251 {
5252 ASSERT_ALLOC( first_export, export_size );
5253 ASSERT_ALLOC( second_export, export_size );
5254 }
Darryl Greend49a4992018-06-18 17:27:26 +01005255
Gilles Peskine8817f612018-12-18 00:18:46 +01005256 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005257
Gilles Peskinec87af662019-05-15 16:12:22 +02005258 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005259 psa_set_key_usage_flags( &attributes, usage_flags );
5260 psa_set_key_algorithm( &attributes, alg );
5261 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005262 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005263
Darryl Green0c6575a2018-11-07 16:05:30 +00005264 switch( generation_method )
5265 {
5266 case IMPORT_KEY:
5267 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005268 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005269 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005270 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005271
Darryl Green0c6575a2018-11-07 16:05:30 +00005272 case GENERATE_KEY:
5273 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005274 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005275 break;
5276
5277 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005278#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005279 {
5280 /* Create base key */
5281 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5282 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5283 psa_set_key_usage_flags( &base_attributes,
5284 PSA_KEY_USAGE_DERIVE );
5285 psa_set_key_algorithm( &base_attributes, derive_alg );
5286 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005287 PSA_ASSERT( psa_import_key( &base_attributes,
5288 data->x, data->len,
5289 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005290 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005291 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005292 PSA_ASSERT( psa_key_derivation_input_key(
5293 &operation,
5294 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005295 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005296 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005297 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005298 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5299 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005300 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005301 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005302 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005303 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005304 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005305#else
5306 TEST_ASSUME( ! "KDF not supported in this configuration" );
5307#endif
5308 break;
5309
5310 default:
5311 TEST_ASSERT( ! "generation_method not implemented in test" );
5312 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005313 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005314 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005315
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005316 /* Export the key if permitted by the key policy. */
5317 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5318 {
Ronald Cron5425a212020-08-04 14:58:35 +02005319 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005320 first_export, export_size,
5321 &first_exported_length ) );
5322 if( generation_method == IMPORT_KEY )
5323 ASSERT_COMPARE( data->x, data->len,
5324 first_export, first_exported_length );
5325 }
Darryl Greend49a4992018-06-18 17:27:26 +01005326
5327 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005328 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005329 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005330 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005331
Darryl Greend49a4992018-06-18 17:27:26 +01005332 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005333 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005334 TEST_ASSERT( mbedtls_svc_key_id_equal(
5335 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005336 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5337 PSA_KEY_LIFETIME_PERSISTENT );
5338 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5339 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02005340 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02005341 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005342 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005343
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005344 /* Export the key again if permitted by the key policy. */
5345 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005346 {
Ronald Cron5425a212020-08-04 14:58:35 +02005347 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005348 second_export, export_size,
5349 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005350 ASSERT_COMPARE( first_export, first_exported_length,
5351 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005352 }
5353
5354 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005355 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005356 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005357
5358exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005359 /*
5360 * Key attributes may have been returned by psa_get_key_attributes()
5361 * thus reset them as required.
5362 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005363 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005364
Darryl Greend49a4992018-06-18 17:27:26 +01005365 mbedtls_free( first_export );
5366 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005367 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005368 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005369 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005370 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005371}
5372/* END_CASE */