blob: cde28a8e755c61d514cb5c5cc638e7eea3a69a1e [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,
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +0200795 int expected_status_sign_arg,
796 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200797{
Ronald Cron5425a212020-08-04 14:58:35 +0200798 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200799 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000800 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200801 psa_key_type_t key_type = key_type_arg;
802 psa_algorithm_t policy_alg = policy_alg_arg;
803 psa_algorithm_t exercise_alg = exercise_alg_arg;
804 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200805 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +0200806 psa_status_t expected_status_sign = expected_status_sign_arg;
807 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200808 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200809
Gilles Peskine8817f612018-12-18 00:18:46 +0100810 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200811
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200812 psa_set_key_usage_flags( &attributes, policy_usage );
813 psa_set_key_algorithm( &attributes, policy_alg );
814 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200815
Gilles Peskine049c7532019-05-15 20:22:09 +0200816 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200817 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200818
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +0200819 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
820 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200821
Ronald Cron5425a212020-08-04 14:58:35 +0200822 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +0200823 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100824
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200825 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200826
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200827 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200828 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +0200829 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200830
831exit:
832 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200833 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200834 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200835}
836/* END_CASE */
837
838/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200839void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200840 int policy_alg,
841 int key_type,
842 data_t *key_data,
843 int exercise_alg )
844{
Ronald Cron5425a212020-08-04 14:58:35 +0200845 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200846 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000847 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200848 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200849 psa_status_t status;
850
Gilles Peskine8817f612018-12-18 00:18:46 +0100851 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200852
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200853 psa_set_key_usage_flags( &attributes, policy_usage );
854 psa_set_key_algorithm( &attributes, policy_alg );
855 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200856
Gilles Peskine049c7532019-05-15 20:22:09 +0200857 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200858 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200859
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200860 /* Check if no key usage flag implication is done */
861 TEST_EQUAL( policy_usage,
862 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200863
Ronald Cron5425a212020-08-04 14:58:35 +0200864 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200865 if( policy_alg == exercise_alg &&
866 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100867 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200868 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100869 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200870 psa_cipher_abort( &operation );
871
Ronald Cron5425a212020-08-04 14:58:35 +0200872 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200873 if( policy_alg == exercise_alg &&
874 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100875 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200876 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100877 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200878
879exit:
880 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200881 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200882 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200883}
884/* END_CASE */
885
886/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200887void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200888 int policy_alg,
889 int key_type,
890 data_t *key_data,
891 int nonce_length_arg,
892 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100893 int exercise_alg,
894 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200895{
Ronald Cron5425a212020-08-04 14:58:35 +0200896 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200897 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200898 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200899 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100900 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200901 unsigned char nonce[16] = {0};
902 size_t nonce_length = nonce_length_arg;
903 unsigned char tag[16];
904 size_t tag_length = tag_length_arg;
905 size_t output_length;
906
907 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
908 TEST_ASSERT( tag_length <= sizeof( tag ) );
909
Gilles Peskine8817f612018-12-18 00:18:46 +0100910 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200911
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200912 psa_set_key_usage_flags( &attributes, policy_usage );
913 psa_set_key_algorithm( &attributes, policy_alg );
914 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200915
Gilles Peskine049c7532019-05-15 20:22:09 +0200916 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200917 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200918
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200919 /* Check if no key usage implication is done */
920 TEST_EQUAL( policy_usage,
921 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200922
Ronald Cron5425a212020-08-04 14:58:35 +0200923 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200924 nonce, nonce_length,
925 NULL, 0,
926 NULL, 0,
927 tag, tag_length,
928 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100929 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
930 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200931 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100932 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200933
934 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200935 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200936 nonce, nonce_length,
937 NULL, 0,
938 tag, tag_length,
939 NULL, 0,
940 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100941 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
942 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
943 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100944 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200945 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100946 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200947
948exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200949 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200950 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200951}
952/* END_CASE */
953
954/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200955void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200956 int policy_alg,
957 int key_type,
958 data_t *key_data,
959 int exercise_alg )
960{
Ronald Cron5425a212020-08-04 14:58:35 +0200961 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200962 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200963 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200964 psa_status_t status;
965 size_t key_bits;
966 size_t buffer_length;
967 unsigned char *buffer = NULL;
968 size_t output_length;
969
Gilles Peskine8817f612018-12-18 00:18:46 +0100970 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200971
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200972 psa_set_key_usage_flags( &attributes, policy_usage );
973 psa_set_key_algorithm( &attributes, policy_alg );
974 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200975
Gilles Peskine049c7532019-05-15 20:22:09 +0200976 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200977 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200978
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200979 /* Check if no key usage implication is done */
980 TEST_EQUAL( policy_usage,
981 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200982
Ronald Cron5425a212020-08-04 14:58:35 +0200983 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200984 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200985 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
986 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200987 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200988
Ronald Cron5425a212020-08-04 14:58:35 +0200989 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200990 NULL, 0,
991 NULL, 0,
992 buffer, buffer_length,
993 &output_length );
994 if( policy_alg == exercise_alg &&
995 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100996 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200997 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100998 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200999
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001000 if( buffer_length != 0 )
1001 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001002 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001003 buffer, buffer_length,
1004 NULL, 0,
1005 buffer, buffer_length,
1006 &output_length );
1007 if( policy_alg == exercise_alg &&
1008 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001009 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001010 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001011 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001012
1013exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001014 /*
1015 * Key attributes may have been returned by psa_get_key_attributes()
1016 * thus reset them as required.
1017 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001018 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001019
1020 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001021 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001022 mbedtls_free( buffer );
1023}
1024/* END_CASE */
1025
1026/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001027void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001028 int policy_alg,
1029 int key_type,
1030 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001031 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001032 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001033 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001034{
Ronald Cron5425a212020-08-04 14:58:35 +02001035 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001036 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001037 psa_key_usage_t policy_usage = policy_usage_arg;
1038 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001039 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001040 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1041 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1042 * compatible with the policy and `payload_length_arg` is supposed to be
1043 * a valid input length to sign. If `payload_length_arg <= 0`,
1044 * `exercise_alg` is supposed to be forbidden by the policy. */
1045 int compatible_alg = payload_length_arg > 0;
1046 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001047 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001048 size_t signature_length;
1049
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001050 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001051 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001052 TEST_EQUAL( expected_usage,
1053 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001054
Gilles Peskine8817f612018-12-18 00:18:46 +01001055 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001056
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001057 psa_set_key_usage_flags( &attributes, policy_usage );
1058 psa_set_key_algorithm( &attributes, policy_alg );
1059 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001060
Gilles Peskine049c7532019-05-15 20:22:09 +02001061 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001062 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001063
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001064 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1065
Ronald Cron5425a212020-08-04 14:58:35 +02001066 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001067 payload, payload_length,
1068 signature, sizeof( signature ),
1069 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001070 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001071 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001072 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001073 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001074
1075 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001076 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001077 payload, payload_length,
1078 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001079 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001080 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001081 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001082 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001083
gabor-mezei-armd851d682021-06-28 14:53:49 +02001084 if( PSA_ALG_IS_HASH_AND_SIGN( exercise_alg ) &&
1085 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001086 {
1087 status = psa_sign_message( key, exercise_alg,
1088 payload, payload_length,
1089 signature, sizeof( signature ),
1090 &signature_length );
1091 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1092 PSA_ASSERT( status );
1093 else
1094 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1095
1096 memset( signature, 0, sizeof( signature ) );
1097 status = psa_verify_message( key, exercise_alg,
1098 payload, payload_length,
1099 signature, sizeof( signature ) );
1100 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1101 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1102 else
1103 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1104 }
1105
Gilles Peskined5b33222018-06-18 22:20:03 +02001106exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001107 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001108 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001109}
1110/* END_CASE */
1111
Janos Follathba3fab92019-06-11 14:50:16 +01001112/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001113void derive_key_policy( int policy_usage,
1114 int policy_alg,
1115 int key_type,
1116 data_t *key_data,
1117 int exercise_alg )
1118{
Ronald Cron5425a212020-08-04 14:58:35 +02001119 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001120 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001121 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001122 psa_status_t status;
1123
Gilles Peskine8817f612018-12-18 00:18:46 +01001124 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001125
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001126 psa_set_key_usage_flags( &attributes, policy_usage );
1127 psa_set_key_algorithm( &attributes, policy_alg );
1128 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001129
Gilles Peskine049c7532019-05-15 20:22:09 +02001130 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001131 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001132
Janos Follathba3fab92019-06-11 14:50:16 +01001133 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1134
1135 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1136 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001137 {
Janos Follathba3fab92019-06-11 14:50:16 +01001138 PSA_ASSERT( psa_key_derivation_input_bytes(
1139 &operation,
1140 PSA_KEY_DERIVATION_INPUT_SEED,
1141 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001142 }
Janos Follathba3fab92019-06-11 14:50:16 +01001143
1144 status = psa_key_derivation_input_key( &operation,
1145 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001146 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001147
Gilles Peskineea0fb492018-07-12 17:17:20 +02001148 if( policy_alg == exercise_alg &&
1149 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001150 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001151 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001152 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001153
1154exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001155 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001156 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001157 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001158}
1159/* END_CASE */
1160
1161/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001162void agreement_key_policy( int policy_usage,
1163 int policy_alg,
1164 int key_type_arg,
1165 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001166 int exercise_alg,
1167 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001168{
Ronald Cron5425a212020-08-04 14:58:35 +02001169 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001170 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001171 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001172 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001173 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001174 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001175
Gilles Peskine8817f612018-12-18 00:18:46 +01001176 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001177
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001178 psa_set_key_usage_flags( &attributes, policy_usage );
1179 psa_set_key_algorithm( &attributes, policy_alg );
1180 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001181
Gilles Peskine049c7532019-05-15 20:22:09 +02001182 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001183 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001184
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001185 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001186 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001187
Steven Cooremance48e852020-10-05 16:02:45 +02001188 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001189
1190exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001191 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001192 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001193 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001194}
1195/* END_CASE */
1196
1197/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001198void key_policy_alg2( int key_type_arg, data_t *key_data,
1199 int usage_arg, int alg_arg, int alg2_arg )
1200{
Ronald Cron5425a212020-08-04 14:58:35 +02001201 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001202 psa_key_type_t key_type = key_type_arg;
1203 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1204 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1205 psa_key_usage_t usage = usage_arg;
1206 psa_algorithm_t alg = alg_arg;
1207 psa_algorithm_t alg2 = alg2_arg;
1208
1209 PSA_ASSERT( psa_crypto_init( ) );
1210
1211 psa_set_key_usage_flags( &attributes, usage );
1212 psa_set_key_algorithm( &attributes, alg );
1213 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1214 psa_set_key_type( &attributes, key_type );
1215 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001216 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001217
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001218 /* Update the usage flags to obtain implicit usage flags */
1219 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001220 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001221 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1222 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1223 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1224
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001225 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001226 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001227 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001228 goto exit;
1229
1230exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001231 /*
1232 * Key attributes may have been returned by psa_get_key_attributes()
1233 * thus reset them as required.
1234 */
1235 psa_reset_key_attributes( &got_attributes );
1236
Ronald Cron5425a212020-08-04 14:58:35 +02001237 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001238 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001239}
1240/* END_CASE */
1241
1242/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001243void raw_agreement_key_policy( int policy_usage,
1244 int policy_alg,
1245 int key_type_arg,
1246 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001247 int exercise_alg,
1248 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001249{
Ronald Cron5425a212020-08-04 14:58:35 +02001250 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001251 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001252 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001253 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001254 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001255 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001256
1257 PSA_ASSERT( psa_crypto_init( ) );
1258
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001259 psa_set_key_usage_flags( &attributes, policy_usage );
1260 psa_set_key_algorithm( &attributes, policy_alg );
1261 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001262
Gilles Peskine049c7532019-05-15 20:22:09 +02001263 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001264 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001265
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001266 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001267
Steven Cooremance48e852020-10-05 16:02:45 +02001268 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001269
1270exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001271 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001272 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001273 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001274}
1275/* END_CASE */
1276
1277/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001278void copy_success( int source_usage_arg,
1279 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001280 int type_arg, data_t *material,
1281 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001282 int target_usage_arg,
1283 int target_alg_arg, int target_alg2_arg,
1284 int expected_usage_arg,
1285 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001286{
Gilles Peskineca25db92019-04-19 11:43:08 +02001287 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1288 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001289 psa_key_usage_t expected_usage = expected_usage_arg;
1290 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001291 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001292 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1293 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001294 uint8_t *export_buffer = NULL;
1295
Gilles Peskine57ab7212019-01-28 13:03:09 +01001296 PSA_ASSERT( psa_crypto_init( ) );
1297
Gilles Peskineca25db92019-04-19 11:43:08 +02001298 /* Prepare the source key. */
1299 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1300 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001301 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001302 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001303 PSA_ASSERT( psa_import_key( &source_attributes,
1304 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001305 &source_key ) );
1306 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001307
Gilles Peskineca25db92019-04-19 11:43:08 +02001308 /* Prepare the target attributes. */
1309 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001310 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001311 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001312 /* Set volatile lifetime to reset the key identifier to 0. */
1313 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1314 }
1315
Gilles Peskineca25db92019-04-19 11:43:08 +02001316 if( target_usage_arg != -1 )
1317 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1318 if( target_alg_arg != -1 )
1319 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001320 if( target_alg2_arg != -1 )
1321 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001322
1323 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001324 PSA_ASSERT( psa_copy_key( source_key,
1325 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001326
1327 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001328 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001329
1330 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001331 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001332 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1333 psa_get_key_type( &target_attributes ) );
1334 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1335 psa_get_key_bits( &target_attributes ) );
1336 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1337 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001338 TEST_EQUAL( expected_alg2,
1339 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001340 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1341 {
1342 size_t length;
1343 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001344 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001345 material->len, &length ) );
1346 ASSERT_COMPARE( material->x, material->len,
1347 export_buffer, length );
1348 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001349
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001350 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001351 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001352 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001353 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001354
Ronald Cron5425a212020-08-04 14:58:35 +02001355 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001356
1357exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001358 /*
1359 * Source and target key attributes may have been returned by
1360 * psa_get_key_attributes() thus reset them as required.
1361 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001362 psa_reset_key_attributes( &source_attributes );
1363 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001364
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001365 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001366 mbedtls_free( export_buffer );
1367}
1368/* END_CASE */
1369
1370/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001371void copy_fail( int source_usage_arg,
1372 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001373 int type_arg, data_t *material,
1374 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001375 int target_usage_arg,
1376 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001377 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001378 int expected_status_arg )
1379{
1380 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1381 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001382 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1383 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001384 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001385
1386 PSA_ASSERT( psa_crypto_init( ) );
1387
1388 /* Prepare the source key. */
1389 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1390 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001391 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001392 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001393 PSA_ASSERT( psa_import_key( &source_attributes,
1394 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001395 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001396
1397 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001398 psa_set_key_id( &target_attributes, key_id );
1399 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001400 psa_set_key_type( &target_attributes, target_type_arg );
1401 psa_set_key_bits( &target_attributes, target_bits_arg );
1402 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1403 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001404 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001405
1406 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001407 TEST_EQUAL( psa_copy_key( source_key,
1408 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001409 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001410
Ronald Cron5425a212020-08-04 14:58:35 +02001411 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001412
Gilles Peskine4a644642019-05-03 17:14:08 +02001413exit:
1414 psa_reset_key_attributes( &source_attributes );
1415 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001416 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001417}
1418/* END_CASE */
1419
1420/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001421void hash_operation_init( )
1422{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001423 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001424 /* Test each valid way of initializing the object, except for `= {0}`, as
1425 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1426 * though it's OK by the C standard. We could test for this, but we'd need
1427 * to supress the Clang warning for the test. */
1428 psa_hash_operation_t func = psa_hash_operation_init( );
1429 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1430 psa_hash_operation_t zero;
1431
1432 memset( &zero, 0, sizeof( zero ) );
1433
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001434 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001435 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1436 PSA_ERROR_BAD_STATE );
1437 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1438 PSA_ERROR_BAD_STATE );
1439 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1440 PSA_ERROR_BAD_STATE );
1441
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001442 /* A default hash operation should be abortable without error. */
1443 PSA_ASSERT( psa_hash_abort( &func ) );
1444 PSA_ASSERT( psa_hash_abort( &init ) );
1445 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001446}
1447/* END_CASE */
1448
1449/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001450void hash_setup( int alg_arg,
1451 int expected_status_arg )
1452{
1453 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001454 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001455 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001456 psa_status_t status;
1457
Gilles Peskine8817f612018-12-18 00:18:46 +01001458 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001459
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001460 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001461 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001462
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001463 /* Whether setup succeeded or failed, abort must succeed. */
1464 PSA_ASSERT( psa_hash_abort( &operation ) );
1465
1466 /* If setup failed, reproduce the failure, so as to
1467 * test the resulting state of the operation object. */
1468 if( status != PSA_SUCCESS )
1469 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1470
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001471 /* Now the operation object should be reusable. */
1472#if defined(KNOWN_SUPPORTED_HASH_ALG)
1473 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1474 PSA_ASSERT( psa_hash_abort( &operation ) );
1475#endif
1476
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001477exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001478 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001479}
1480/* END_CASE */
1481
1482/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001483void hash_compute_fail( int alg_arg, data_t *input,
1484 int output_size_arg, int expected_status_arg )
1485{
1486 psa_algorithm_t alg = alg_arg;
1487 uint8_t *output = NULL;
1488 size_t output_size = output_size_arg;
1489 size_t output_length = INVALID_EXPORT_LENGTH;
1490 psa_status_t expected_status = expected_status_arg;
1491 psa_status_t status;
1492
1493 ASSERT_ALLOC( output, output_size );
1494
1495 PSA_ASSERT( psa_crypto_init( ) );
1496
1497 status = psa_hash_compute( alg, input->x, input->len,
1498 output, output_size, &output_length );
1499 TEST_EQUAL( status, expected_status );
1500 TEST_ASSERT( output_length <= output_size );
1501
1502exit:
1503 mbedtls_free( output );
1504 PSA_DONE( );
1505}
1506/* END_CASE */
1507
1508/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001509void hash_compare_fail( int alg_arg, data_t *input,
1510 data_t *reference_hash,
1511 int expected_status_arg )
1512{
1513 psa_algorithm_t alg = alg_arg;
1514 psa_status_t expected_status = expected_status_arg;
1515 psa_status_t status;
1516
1517 PSA_ASSERT( psa_crypto_init( ) );
1518
1519 status = psa_hash_compare( alg, input->x, input->len,
1520 reference_hash->x, reference_hash->len );
1521 TEST_EQUAL( status, expected_status );
1522
1523exit:
1524 PSA_DONE( );
1525}
1526/* END_CASE */
1527
1528/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001529void hash_compute_compare( int alg_arg, data_t *input,
1530 data_t *expected_output )
1531{
1532 psa_algorithm_t alg = alg_arg;
1533 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1534 size_t output_length = INVALID_EXPORT_LENGTH;
1535 size_t i;
1536
1537 PSA_ASSERT( psa_crypto_init( ) );
1538
1539 /* Compute with tight buffer */
1540 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001541 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001542 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001543 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001544 ASSERT_COMPARE( output, output_length,
1545 expected_output->x, expected_output->len );
1546
1547 /* Compute with larger buffer */
1548 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1549 output, sizeof( output ),
1550 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001551 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001552 ASSERT_COMPARE( output, output_length,
1553 expected_output->x, expected_output->len );
1554
1555 /* Compare with correct hash */
1556 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1557 output, output_length ) );
1558
1559 /* Compare with trailing garbage */
1560 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1561 output, output_length + 1 ),
1562 PSA_ERROR_INVALID_SIGNATURE );
1563
1564 /* Compare with truncated hash */
1565 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1566 output, output_length - 1 ),
1567 PSA_ERROR_INVALID_SIGNATURE );
1568
1569 /* Compare with corrupted value */
1570 for( i = 0; i < output_length; i++ )
1571 {
Chris Jones9634bb12021-01-20 15:56:42 +00001572 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001573 output[i] ^= 1;
1574 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1575 output, output_length ),
1576 PSA_ERROR_INVALID_SIGNATURE );
1577 output[i] ^= 1;
1578 }
1579
1580exit:
1581 PSA_DONE( );
1582}
1583/* END_CASE */
1584
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001585/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001586void hash_bad_order( )
1587{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001588 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001589 unsigned char input[] = "";
1590 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001591 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001592 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1593 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1594 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001595 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001596 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001597 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001598
Gilles Peskine8817f612018-12-18 00:18:46 +01001599 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001600
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001601 /* Call setup twice in a row. */
1602 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001603 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001604 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1605 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001606 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001607 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001608 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001609
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001610 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001611 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001612 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001613 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001614
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001615 /* Check that update calls abort on error. */
1616 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01001617 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001618 ASSERT_OPERATION_IS_ACTIVE( operation );
1619 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
1620 PSA_ERROR_BAD_STATE );
1621 ASSERT_OPERATION_IS_INACTIVE( operation );
1622 PSA_ASSERT( psa_hash_abort( &operation ) );
1623 ASSERT_OPERATION_IS_INACTIVE( operation );
1624
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001625 /* Call update after finish. */
1626 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1627 PSA_ASSERT( psa_hash_finish( &operation,
1628 hash, sizeof( hash ), &hash_len ) );
1629 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001630 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001631 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001632
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001633 /* Call verify without calling setup beforehand. */
1634 TEST_EQUAL( psa_hash_verify( &operation,
1635 valid_hash, sizeof( valid_hash ) ),
1636 PSA_ERROR_BAD_STATE );
1637 PSA_ASSERT( psa_hash_abort( &operation ) );
1638
1639 /* Call verify after finish. */
1640 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1641 PSA_ASSERT( psa_hash_finish( &operation,
1642 hash, sizeof( hash ), &hash_len ) );
1643 TEST_EQUAL( psa_hash_verify( &operation,
1644 valid_hash, sizeof( valid_hash ) ),
1645 PSA_ERROR_BAD_STATE );
1646 PSA_ASSERT( psa_hash_abort( &operation ) );
1647
1648 /* Call verify twice in a row. */
1649 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001650 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001651 PSA_ASSERT( psa_hash_verify( &operation,
1652 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001653 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001654 TEST_EQUAL( psa_hash_verify( &operation,
1655 valid_hash, sizeof( valid_hash ) ),
1656 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001657 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001658 PSA_ASSERT( psa_hash_abort( &operation ) );
1659
1660 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001661 TEST_EQUAL( psa_hash_finish( &operation,
1662 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001663 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001664 PSA_ASSERT( psa_hash_abort( &operation ) );
1665
1666 /* Call finish twice in a row. */
1667 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1668 PSA_ASSERT( psa_hash_finish( &operation,
1669 hash, sizeof( hash ), &hash_len ) );
1670 TEST_EQUAL( psa_hash_finish( &operation,
1671 hash, sizeof( hash ), &hash_len ),
1672 PSA_ERROR_BAD_STATE );
1673 PSA_ASSERT( psa_hash_abort( &operation ) );
1674
1675 /* Call finish after calling verify. */
1676 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1677 PSA_ASSERT( psa_hash_verify( &operation,
1678 valid_hash, sizeof( valid_hash ) ) );
1679 TEST_EQUAL( psa_hash_finish( &operation,
1680 hash, sizeof( hash ), &hash_len ),
1681 PSA_ERROR_BAD_STATE );
1682 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001683
1684exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001685 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001686}
1687/* END_CASE */
1688
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001689/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001690void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001691{
1692 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001693 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1694 * appended to it */
1695 unsigned char hash[] = {
1696 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1697 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1698 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001699 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001700 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001701
Gilles Peskine8817f612018-12-18 00:18:46 +01001702 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001703
itayzafrir27e69452018-11-01 14:26:34 +02001704 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001705 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001706 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001707 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001708 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001709 ASSERT_OPERATION_IS_INACTIVE( operation );
1710 PSA_ASSERT( psa_hash_abort( &operation ) );
1711 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03001712
itayzafrir27e69452018-11-01 14:26:34 +02001713 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001714 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001715 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001716 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001717
itayzafrir27e69452018-11-01 14:26:34 +02001718 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001719 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001720 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001721 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001722
itayzafrirec93d302018-10-18 18:01:10 +03001723exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001724 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001725}
1726/* END_CASE */
1727
Ronald Cronee414c72021-03-18 18:50:08 +01001728/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001729void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001730{
1731 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001732 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001733 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001734 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001735 size_t hash_len;
1736
Gilles Peskine8817f612018-12-18 00:18:46 +01001737 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001738
itayzafrir58028322018-10-25 10:22:01 +03001739 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001740 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001741 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001742 hash, expected_size - 1, &hash_len ),
1743 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001744
1745exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001746 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001747}
1748/* END_CASE */
1749
Ronald Cronee414c72021-03-18 18:50:08 +01001750/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001751void hash_clone_source_state( )
1752{
1753 psa_algorithm_t alg = PSA_ALG_SHA_256;
1754 unsigned char hash[PSA_HASH_MAX_SIZE];
1755 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1756 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1757 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1758 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1759 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1760 size_t hash_len;
1761
1762 PSA_ASSERT( psa_crypto_init( ) );
1763 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1764
1765 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1766 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1767 PSA_ASSERT( psa_hash_finish( &op_finished,
1768 hash, sizeof( hash ), &hash_len ) );
1769 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1770 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1771
1772 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1773 PSA_ERROR_BAD_STATE );
1774
1775 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1776 PSA_ASSERT( psa_hash_finish( &op_init,
1777 hash, sizeof( hash ), &hash_len ) );
1778 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1779 PSA_ASSERT( psa_hash_finish( &op_finished,
1780 hash, sizeof( hash ), &hash_len ) );
1781 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1782 PSA_ASSERT( psa_hash_finish( &op_aborted,
1783 hash, sizeof( hash ), &hash_len ) );
1784
1785exit:
1786 psa_hash_abort( &op_source );
1787 psa_hash_abort( &op_init );
1788 psa_hash_abort( &op_setup );
1789 psa_hash_abort( &op_finished );
1790 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001791 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001792}
1793/* END_CASE */
1794
Ronald Cronee414c72021-03-18 18:50:08 +01001795/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001796void hash_clone_target_state( )
1797{
1798 psa_algorithm_t alg = PSA_ALG_SHA_256;
1799 unsigned char hash[PSA_HASH_MAX_SIZE];
1800 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1801 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1802 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1803 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1804 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1805 size_t hash_len;
1806
1807 PSA_ASSERT( psa_crypto_init( ) );
1808
1809 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1810 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1811 PSA_ASSERT( psa_hash_finish( &op_finished,
1812 hash, sizeof( hash ), &hash_len ) );
1813 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1814 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1815
1816 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1817 PSA_ASSERT( psa_hash_finish( &op_target,
1818 hash, sizeof( hash ), &hash_len ) );
1819
1820 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1821 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1822 PSA_ERROR_BAD_STATE );
1823 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1824 PSA_ERROR_BAD_STATE );
1825
1826exit:
1827 psa_hash_abort( &op_target );
1828 psa_hash_abort( &op_init );
1829 psa_hash_abort( &op_setup );
1830 psa_hash_abort( &op_finished );
1831 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001832 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001833}
1834/* END_CASE */
1835
itayzafrir58028322018-10-25 10:22:01 +03001836/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001837void mac_operation_init( )
1838{
Jaeden Amero252ef282019-02-15 14:05:35 +00001839 const uint8_t input[1] = { 0 };
1840
Jaeden Amero769ce272019-01-04 11:48:03 +00001841 /* Test each valid way of initializing the object, except for `= {0}`, as
1842 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1843 * though it's OK by the C standard. We could test for this, but we'd need
1844 * to supress the Clang warning for the test. */
1845 psa_mac_operation_t func = psa_mac_operation_init( );
1846 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1847 psa_mac_operation_t zero;
1848
1849 memset( &zero, 0, sizeof( zero ) );
1850
Jaeden Amero252ef282019-02-15 14:05:35 +00001851 /* A freshly-initialized MAC operation should not be usable. */
1852 TEST_EQUAL( psa_mac_update( &func,
1853 input, sizeof( input ) ),
1854 PSA_ERROR_BAD_STATE );
1855 TEST_EQUAL( psa_mac_update( &init,
1856 input, sizeof( input ) ),
1857 PSA_ERROR_BAD_STATE );
1858 TEST_EQUAL( psa_mac_update( &zero,
1859 input, sizeof( input ) ),
1860 PSA_ERROR_BAD_STATE );
1861
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001862 /* A default MAC operation should be abortable without error. */
1863 PSA_ASSERT( psa_mac_abort( &func ) );
1864 PSA_ASSERT( psa_mac_abort( &init ) );
1865 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001866}
1867/* END_CASE */
1868
1869/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001870void mac_setup( int key_type_arg,
1871 data_t *key,
1872 int alg_arg,
1873 int expected_status_arg )
1874{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001875 psa_key_type_t key_type = key_type_arg;
1876 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001877 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001878 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001879 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1880#if defined(KNOWN_SUPPORTED_MAC_ALG)
1881 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1882#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001883
Gilles Peskine8817f612018-12-18 00:18:46 +01001884 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001885
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001886 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1887 &operation, &status ) )
1888 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001889 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001890
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001891 /* The operation object should be reusable. */
1892#if defined(KNOWN_SUPPORTED_MAC_ALG)
1893 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1894 smoke_test_key_data,
1895 sizeof( smoke_test_key_data ),
1896 KNOWN_SUPPORTED_MAC_ALG,
1897 &operation, &status ) )
1898 goto exit;
1899 TEST_EQUAL( status, PSA_SUCCESS );
1900#endif
1901
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001902exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001903 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001904}
1905/* END_CASE */
1906
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001907/* 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 +00001908void mac_bad_order( )
1909{
Ronald Cron5425a212020-08-04 14:58:35 +02001910 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001911 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1912 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001913 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001914 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1915 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1916 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001917 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001918 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1919 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1920 size_t sign_mac_length = 0;
1921 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1922 const uint8_t verify_mac[] = {
1923 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1924 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1925 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1926
1927 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001928 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001929 psa_set_key_algorithm( &attributes, alg );
1930 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001931
Ronald Cron5425a212020-08-04 14:58:35 +02001932 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1933 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001934
Jaeden Amero252ef282019-02-15 14:05:35 +00001935 /* Call update without calling setup beforehand. */
1936 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1937 PSA_ERROR_BAD_STATE );
1938 PSA_ASSERT( psa_mac_abort( &operation ) );
1939
1940 /* Call sign finish without calling setup beforehand. */
1941 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1942 &sign_mac_length),
1943 PSA_ERROR_BAD_STATE );
1944 PSA_ASSERT( psa_mac_abort( &operation ) );
1945
1946 /* Call verify finish without calling setup beforehand. */
1947 TEST_EQUAL( psa_mac_verify_finish( &operation,
1948 verify_mac, sizeof( verify_mac ) ),
1949 PSA_ERROR_BAD_STATE );
1950 PSA_ASSERT( psa_mac_abort( &operation ) );
1951
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001952 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001953 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001954 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001955 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001956 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001957 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001958 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001959 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001960
Jaeden Amero252ef282019-02-15 14:05:35 +00001961 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001962 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001963 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1964 PSA_ASSERT( psa_mac_sign_finish( &operation,
1965 sign_mac, sizeof( sign_mac ),
1966 &sign_mac_length ) );
1967 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1968 PSA_ERROR_BAD_STATE );
1969 PSA_ASSERT( psa_mac_abort( &operation ) );
1970
1971 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001972 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001973 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1974 PSA_ASSERT( psa_mac_verify_finish( &operation,
1975 verify_mac, sizeof( verify_mac ) ) );
1976 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1977 PSA_ERROR_BAD_STATE );
1978 PSA_ASSERT( psa_mac_abort( &operation ) );
1979
1980 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001981 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001982 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1983 PSA_ASSERT( psa_mac_sign_finish( &operation,
1984 sign_mac, sizeof( sign_mac ),
1985 &sign_mac_length ) );
1986 TEST_EQUAL( psa_mac_sign_finish( &operation,
1987 sign_mac, sizeof( sign_mac ),
1988 &sign_mac_length ),
1989 PSA_ERROR_BAD_STATE );
1990 PSA_ASSERT( psa_mac_abort( &operation ) );
1991
1992 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001993 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001994 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1995 PSA_ASSERT( psa_mac_verify_finish( &operation,
1996 verify_mac, sizeof( verify_mac ) ) );
1997 TEST_EQUAL( psa_mac_verify_finish( &operation,
1998 verify_mac, sizeof( verify_mac ) ),
1999 PSA_ERROR_BAD_STATE );
2000 PSA_ASSERT( psa_mac_abort( &operation ) );
2001
2002 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002003 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002004 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002005 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002006 TEST_EQUAL( psa_mac_verify_finish( &operation,
2007 verify_mac, sizeof( verify_mac ) ),
2008 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002009 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002010 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002011 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002012
2013 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002014 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002015 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002016 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002017 TEST_EQUAL( psa_mac_sign_finish( &operation,
2018 sign_mac, sizeof( sign_mac ),
2019 &sign_mac_length ),
2020 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002021 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002022 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002023 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002024
Ronald Cron5425a212020-08-04 14:58:35 +02002025 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002026
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002027exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002028 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002029}
2030/* END_CASE */
2031
2032/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002033void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002034 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002035 int alg_arg,
2036 data_t *input,
2037 data_t *expected_mac )
2038{
Ronald Cron5425a212020-08-04 14:58:35 +02002039 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002040 psa_key_type_t key_type = key_type_arg;
2041 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002042 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002043 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002044 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002045 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002046 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002047 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002048 const size_t output_sizes_to_test[] = {
2049 0,
2050 1,
2051 expected_mac->len - 1,
2052 expected_mac->len,
2053 expected_mac->len + 1,
2054 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002055
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002056 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002057 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002058 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002059
Gilles Peskine8817f612018-12-18 00:18:46 +01002060 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002061
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002062 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002063 psa_set_key_algorithm( &attributes, alg );
2064 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002065
Ronald Cron5425a212020-08-04 14:58:35 +02002066 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2067 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002068
Gilles Peskine8b356b52020-08-25 23:44:59 +02002069 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2070 {
2071 const size_t output_size = output_sizes_to_test[i];
2072 psa_status_t expected_status =
2073 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2074 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002075
Chris Jones9634bb12021-01-20 15:56:42 +00002076 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002077 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002078
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002079 /* Calculate the MAC, one-shot case. */
2080 TEST_EQUAL( psa_mac_compute( key, alg,
2081 input->x, input->len,
2082 actual_mac, output_size, &mac_length ),
2083 expected_status );
2084 if( expected_status == PSA_SUCCESS )
2085 {
2086 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2087 actual_mac, mac_length );
2088 }
2089
2090 if( output_size > 0 )
2091 memset( actual_mac, 0, output_size );
2092
2093 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002094 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002095 PSA_ASSERT( psa_mac_update( &operation,
2096 input->x, input->len ) );
2097 TEST_EQUAL( psa_mac_sign_finish( &operation,
2098 actual_mac, output_size,
2099 &mac_length ),
2100 expected_status );
2101 PSA_ASSERT( psa_mac_abort( &operation ) );
2102
2103 if( expected_status == PSA_SUCCESS )
2104 {
2105 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2106 actual_mac, mac_length );
2107 }
2108 mbedtls_free( actual_mac );
2109 actual_mac = NULL;
2110 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002111
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002112exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002113 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002114 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002115 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002116 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002117}
2118/* END_CASE */
2119
2120/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002121void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002122 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002123 int alg_arg,
2124 data_t *input,
2125 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002126{
Ronald Cron5425a212020-08-04 14:58:35 +02002127 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002128 psa_key_type_t key_type = key_type_arg;
2129 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002130 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002131 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002132 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002133
Gilles Peskine69c12672018-06-28 00:07:19 +02002134 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2135
Gilles Peskine8817f612018-12-18 00:18:46 +01002136 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002137
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002138 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002139 psa_set_key_algorithm( &attributes, alg );
2140 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002141
Ronald Cron5425a212020-08-04 14:58:35 +02002142 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2143 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002144
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002145 /* Verify correct MAC, one-shot case. */
2146 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2147 expected_mac->x, expected_mac->len ) );
2148
2149 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002150 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002151 PSA_ASSERT( psa_mac_update( &operation,
2152 input->x, input->len ) );
2153 PSA_ASSERT( psa_mac_verify_finish( &operation,
2154 expected_mac->x,
2155 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002156
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002157 /* Test a MAC that's too short, one-shot case. */
2158 TEST_EQUAL( psa_mac_verify( key, alg,
2159 input->x, input->len,
2160 expected_mac->x,
2161 expected_mac->len - 1 ),
2162 PSA_ERROR_INVALID_SIGNATURE );
2163
2164 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002165 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002166 PSA_ASSERT( psa_mac_update( &operation,
2167 input->x, input->len ) );
2168 TEST_EQUAL( psa_mac_verify_finish( &operation,
2169 expected_mac->x,
2170 expected_mac->len - 1 ),
2171 PSA_ERROR_INVALID_SIGNATURE );
2172
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002173 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002174 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2175 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002176 TEST_EQUAL( psa_mac_verify( key, alg,
2177 input->x, input->len,
2178 perturbed_mac, expected_mac->len + 1 ),
2179 PSA_ERROR_INVALID_SIGNATURE );
2180
2181 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002182 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002183 PSA_ASSERT( psa_mac_update( &operation,
2184 input->x, input->len ) );
2185 TEST_EQUAL( psa_mac_verify_finish( &operation,
2186 perturbed_mac,
2187 expected_mac->len + 1 ),
2188 PSA_ERROR_INVALID_SIGNATURE );
2189
2190 /* Test changing one byte. */
2191 for( size_t i = 0; i < expected_mac->len; i++ )
2192 {
Chris Jones9634bb12021-01-20 15:56:42 +00002193 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002194 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002195
2196 TEST_EQUAL( psa_mac_verify( key, alg,
2197 input->x, input->len,
2198 perturbed_mac, expected_mac->len ),
2199 PSA_ERROR_INVALID_SIGNATURE );
2200
Ronald Cron5425a212020-08-04 14:58:35 +02002201 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002202 PSA_ASSERT( psa_mac_update( &operation,
2203 input->x, input->len ) );
2204 TEST_EQUAL( psa_mac_verify_finish( &operation,
2205 perturbed_mac,
2206 expected_mac->len ),
2207 PSA_ERROR_INVALID_SIGNATURE );
2208 perturbed_mac[i] ^= 1;
2209 }
2210
Gilles Peskine8c9def32018-02-08 10:02:12 +01002211exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002212 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002213 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002214 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002215 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002216}
2217/* END_CASE */
2218
2219/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002220void cipher_operation_init( )
2221{
Jaeden Ameroab439972019-02-15 14:12:05 +00002222 const uint8_t input[1] = { 0 };
2223 unsigned char output[1] = { 0 };
2224 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002225 /* Test each valid way of initializing the object, except for `= {0}`, as
2226 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2227 * though it's OK by the C standard. We could test for this, but we'd need
2228 * to supress the Clang warning for the test. */
2229 psa_cipher_operation_t func = psa_cipher_operation_init( );
2230 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2231 psa_cipher_operation_t zero;
2232
2233 memset( &zero, 0, sizeof( zero ) );
2234
Jaeden Ameroab439972019-02-15 14:12:05 +00002235 /* A freshly-initialized cipher operation should not be usable. */
2236 TEST_EQUAL( psa_cipher_update( &func,
2237 input, sizeof( input ),
2238 output, sizeof( output ),
2239 &output_length ),
2240 PSA_ERROR_BAD_STATE );
2241 TEST_EQUAL( psa_cipher_update( &init,
2242 input, sizeof( input ),
2243 output, sizeof( output ),
2244 &output_length ),
2245 PSA_ERROR_BAD_STATE );
2246 TEST_EQUAL( psa_cipher_update( &zero,
2247 input, sizeof( input ),
2248 output, sizeof( output ),
2249 &output_length ),
2250 PSA_ERROR_BAD_STATE );
2251
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002252 /* A default cipher operation should be abortable without error. */
2253 PSA_ASSERT( psa_cipher_abort( &func ) );
2254 PSA_ASSERT( psa_cipher_abort( &init ) );
2255 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002256}
2257/* END_CASE */
2258
2259/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002260void cipher_setup( int key_type_arg,
2261 data_t *key,
2262 int alg_arg,
2263 int expected_status_arg )
2264{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002265 psa_key_type_t key_type = key_type_arg;
2266 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002267 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002268 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002269 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002270#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002271 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2272#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002273
Gilles Peskine8817f612018-12-18 00:18:46 +01002274 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002275
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002276 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2277 &operation, &status ) )
2278 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002279 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002280
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002281 /* The operation object should be reusable. */
2282#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2283 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2284 smoke_test_key_data,
2285 sizeof( smoke_test_key_data ),
2286 KNOWN_SUPPORTED_CIPHER_ALG,
2287 &operation, &status ) )
2288 goto exit;
2289 TEST_EQUAL( status, PSA_SUCCESS );
2290#endif
2291
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002292exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002293 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002294 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002295}
2296/* END_CASE */
2297
Ronald Cronee414c72021-03-18 18:50:08 +01002298/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002299void cipher_bad_order( )
2300{
Ronald Cron5425a212020-08-04 14:58:35 +02002301 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002302 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2303 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002304 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002305 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002306 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002307 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002308 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2309 0xaa, 0xaa, 0xaa, 0xaa };
2310 const uint8_t text[] = {
2311 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2312 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002313 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002314 size_t length = 0;
2315
2316 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002317 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2318 psa_set_key_algorithm( &attributes, alg );
2319 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002320 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2321 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002322
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002323 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002324 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002325 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002326 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002327 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002328 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002329 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002330 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002331
2332 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002333 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002334 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002335 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002336 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002337 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002338 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002339 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002340
Jaeden Ameroab439972019-02-15 14:12:05 +00002341 /* Generate an IV without calling setup beforehand. */
2342 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2343 buffer, sizeof( buffer ),
2344 &length ),
2345 PSA_ERROR_BAD_STATE );
2346 PSA_ASSERT( psa_cipher_abort( &operation ) );
2347
2348 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002349 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002350 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2351 buffer, sizeof( buffer ),
2352 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002353 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002354 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2355 buffer, sizeof( buffer ),
2356 &length ),
2357 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002358 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002359 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002360 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002361
2362 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002363 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002364 PSA_ASSERT( psa_cipher_set_iv( &operation,
2365 iv, sizeof( iv ) ) );
2366 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2367 buffer, sizeof( buffer ),
2368 &length ),
2369 PSA_ERROR_BAD_STATE );
2370 PSA_ASSERT( psa_cipher_abort( &operation ) );
2371
2372 /* Set an IV without calling setup beforehand. */
2373 TEST_EQUAL( psa_cipher_set_iv( &operation,
2374 iv, sizeof( iv ) ),
2375 PSA_ERROR_BAD_STATE );
2376 PSA_ASSERT( psa_cipher_abort( &operation ) );
2377
2378 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002379 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002380 PSA_ASSERT( psa_cipher_set_iv( &operation,
2381 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002382 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002383 TEST_EQUAL( psa_cipher_set_iv( &operation,
2384 iv, sizeof( iv ) ),
2385 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002386 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002387 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002388 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002389
2390 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002391 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002392 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2393 buffer, sizeof( buffer ),
2394 &length ) );
2395 TEST_EQUAL( psa_cipher_set_iv( &operation,
2396 iv, sizeof( iv ) ),
2397 PSA_ERROR_BAD_STATE );
2398 PSA_ASSERT( psa_cipher_abort( &operation ) );
2399
2400 /* Call update without calling setup beforehand. */
2401 TEST_EQUAL( psa_cipher_update( &operation,
2402 text, sizeof( text ),
2403 buffer, sizeof( buffer ),
2404 &length ),
2405 PSA_ERROR_BAD_STATE );
2406 PSA_ASSERT( psa_cipher_abort( &operation ) );
2407
2408 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01002409 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002410 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002411 TEST_EQUAL( psa_cipher_update( &operation,
2412 text, sizeof( text ),
2413 buffer, sizeof( buffer ),
2414 &length ),
2415 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002416 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002417 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002418 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002419
2420 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002421 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002422 PSA_ASSERT( psa_cipher_set_iv( &operation,
2423 iv, sizeof( iv ) ) );
2424 PSA_ASSERT( psa_cipher_finish( &operation,
2425 buffer, sizeof( buffer ), &length ) );
2426 TEST_EQUAL( psa_cipher_update( &operation,
2427 text, sizeof( text ),
2428 buffer, sizeof( buffer ),
2429 &length ),
2430 PSA_ERROR_BAD_STATE );
2431 PSA_ASSERT( psa_cipher_abort( &operation ) );
2432
2433 /* Call finish without calling setup beforehand. */
2434 TEST_EQUAL( psa_cipher_finish( &operation,
2435 buffer, sizeof( buffer ), &length ),
2436 PSA_ERROR_BAD_STATE );
2437 PSA_ASSERT( psa_cipher_abort( &operation ) );
2438
2439 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002440 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002441 /* Not calling update means we are encrypting an empty buffer, which is OK
2442 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01002443 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002444 TEST_EQUAL( psa_cipher_finish( &operation,
2445 buffer, sizeof( buffer ), &length ),
2446 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002447 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002448 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002449 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002450
2451 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002452 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002453 PSA_ASSERT( psa_cipher_set_iv( &operation,
2454 iv, sizeof( iv ) ) );
2455 PSA_ASSERT( psa_cipher_finish( &operation,
2456 buffer, sizeof( buffer ), &length ) );
2457 TEST_EQUAL( psa_cipher_finish( &operation,
2458 buffer, sizeof( buffer ), &length ),
2459 PSA_ERROR_BAD_STATE );
2460 PSA_ASSERT( psa_cipher_abort( &operation ) );
2461
Ronald Cron5425a212020-08-04 14:58:35 +02002462 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002463
Jaeden Ameroab439972019-02-15 14:12:05 +00002464exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002465 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002466 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002467}
2468/* END_CASE */
2469
2470/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02002471void cipher_encrypt_fail( int alg_arg,
2472 int key_type_arg,
2473 data_t *key_data,
2474 data_t *input,
2475 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002476{
Ronald Cron5425a212020-08-04 14:58:35 +02002477 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002478 psa_status_t status;
2479 psa_key_type_t key_type = key_type_arg;
2480 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002481 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002482 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002483 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002484 size_t output_length = 0;
2485 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2486
2487 if ( PSA_ERROR_BAD_STATE != expected_status )
2488 {
2489 PSA_ASSERT( psa_crypto_init( ) );
2490
2491 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2492 psa_set_key_algorithm( &attributes, alg );
2493 psa_set_key_type( &attributes, key_type );
2494
2495 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
2496 input->len );
2497 ASSERT_ALLOC( output, output_buffer_size );
2498
2499 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2500 &key ) );
2501 }
2502
2503 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
2504 output_buffer_size, &output_length );
2505
2506 TEST_EQUAL( status, expected_status );
2507
2508exit:
2509 mbedtls_free( output );
2510 psa_destroy_key( key );
2511 PSA_DONE( );
2512}
2513/* END_CASE */
2514
2515/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02002516void cipher_encrypt_alg_without_iv( int alg_arg,
2517 int key_type_arg,
2518 data_t *key_data,
2519 data_t *input,
2520 data_t *expected_output )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002521{
2522 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2523 psa_key_type_t key_type = key_type_arg;
2524 psa_algorithm_t alg = alg_arg;
2525 unsigned char *output = NULL;
2526 size_t output_buffer_size = 0;
2527 size_t output_length = 0;
2528 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2529
2530 PSA_ASSERT( psa_crypto_init( ) );
2531
2532 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2533 psa_set_key_algorithm( &attributes, alg );
2534 psa_set_key_type( &attributes, key_type );
2535
2536 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2537 ASSERT_ALLOC( output, output_buffer_size );
2538
2539 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2540 &key ) );
2541
2542 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
2543 output_buffer_size, &output_length ) );
2544 TEST_ASSERT( output_length <=
2545 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2546 TEST_ASSERT( output_length <=
2547 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
2548
2549 ASSERT_COMPARE( expected_output->x, expected_output->len,
2550 output, output_length );
2551exit:
2552 mbedtls_free( output );
2553 psa_destroy_key( key );
2554 PSA_DONE( );
2555}
2556/* END_CASE */
2557
2558/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01002559void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
2560{
2561 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2562 psa_algorithm_t alg = alg_arg;
2563 psa_key_type_t key_type = key_type_arg;
2564 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2565 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2566 psa_status_t status;
2567
2568 PSA_ASSERT( psa_crypto_init( ) );
2569
2570 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2571 psa_set_key_algorithm( &attributes, alg );
2572 psa_set_key_type( &attributes, key_type );
2573
2574 /* Usage of either of these two size macros would cause divide by zero
2575 * with incorrect key types previously. Input length should be irrelevant
2576 * here. */
2577 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
2578 0 );
2579 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
2580
2581
2582 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2583 &key ) );
2584
2585 /* Should fail due to invalid alg type (to support invalid key type).
2586 * Encrypt or decrypt will end up in the same place. */
2587 status = psa_cipher_encrypt_setup( &operation, key, alg );
2588
2589 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
2590
2591exit:
2592 psa_cipher_abort( &operation );
2593 psa_destroy_key( key );
2594 PSA_DONE( );
2595}
2596/* END_CASE */
2597
2598/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002599void cipher_encrypt_validation( int alg_arg,
2600 int key_type_arg,
2601 data_t *key_data,
2602 data_t *input )
2603{
2604 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2605 psa_key_type_t key_type = key_type_arg;
2606 psa_algorithm_t alg = alg_arg;
2607 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
2608 unsigned char *output1 = NULL;
2609 size_t output1_buffer_size = 0;
2610 size_t output1_length = 0;
2611 unsigned char *output2 = NULL;
2612 size_t output2_buffer_size = 0;
2613 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002614 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002615 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002616 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002617
Gilles Peskine8817f612018-12-18 00:18:46 +01002618 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002619
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002620 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2621 psa_set_key_algorithm( &attributes, alg );
2622 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002623
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002624 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2625 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2626 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
2627 ASSERT_ALLOC( output1, output1_buffer_size );
2628 ASSERT_ALLOC( output2, output2_buffer_size );
2629
Ronald Cron5425a212020-08-04 14:58:35 +02002630 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2631 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002632
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02002633 /* The one-shot cipher encryption uses generated iv so validating
2634 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002635 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
2636 output1_buffer_size, &output1_length ) );
2637 TEST_ASSERT( output1_length <=
2638 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2639 TEST_ASSERT( output1_length <=
gabor-mezei-armceface22021-01-21 12:26:17 +01002640 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002641
2642 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2643 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002644
Gilles Peskine8817f612018-12-18 00:18:46 +01002645 PSA_ASSERT( psa_cipher_update( &operation,
2646 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002647 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01002648 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002649 TEST_ASSERT( function_output_length <=
2650 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2651 TEST_ASSERT( function_output_length <=
2652 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002653 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002654
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002655 PSA_ASSERT( psa_cipher_finish( &operation,
2656 output2 + output2_length,
2657 output2_buffer_size - output2_length,
2658 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002659 TEST_ASSERT( function_output_length <=
2660 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2661 TEST_ASSERT( function_output_length <=
2662 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002663 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002664
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002665 PSA_ASSERT( psa_cipher_abort( &operation ) );
2666 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
2667 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002668
Gilles Peskine50e586b2018-06-08 14:28:46 +02002669exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002670 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002671 mbedtls_free( output1 );
2672 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002673 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002674 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002675}
2676/* END_CASE */
2677
2678/* BEGIN_CASE */
2679void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002680 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002681 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002682 int first_part_size_arg,
2683 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002684 data_t *expected_output,
2685 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002686{
Ronald Cron5425a212020-08-04 14:58:35 +02002687 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002688 psa_key_type_t key_type = key_type_arg;
2689 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002690 psa_status_t status;
2691 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002692 size_t first_part_size = first_part_size_arg;
2693 size_t output1_length = output1_length_arg;
2694 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002695 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002696 size_t output_buffer_size = 0;
2697 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002698 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002699 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002700 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002701
Gilles Peskine8817f612018-12-18 00:18:46 +01002702 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002703
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002704 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2705 psa_set_key_algorithm( &attributes, alg );
2706 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002707
Ronald Cron5425a212020-08-04 14:58:35 +02002708 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2709 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002710
Ronald Cron5425a212020-08-04 14:58:35 +02002711 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002712
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002713 if( iv->len > 0 )
2714 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002715 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002716 }
2717
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002718 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2719 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002720 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002721
Gilles Peskinee0866522019-02-19 19:44:00 +01002722 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002723 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2724 output, output_buffer_size,
2725 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002726 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002727 TEST_ASSERT( function_output_length <=
2728 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2729 TEST_ASSERT( function_output_length <=
2730 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002731 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002732
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002733 if( first_part_size < input->len )
2734 {
2735 PSA_ASSERT( psa_cipher_update( &operation,
2736 input->x + first_part_size,
2737 input->len - first_part_size,
2738 ( output_buffer_size == 0 ? NULL :
2739 output + total_output_length ),
2740 output_buffer_size - total_output_length,
2741 &function_output_length ) );
2742 TEST_ASSERT( function_output_length == output2_length );
2743 TEST_ASSERT( function_output_length <=
2744 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2745 alg,
2746 input->len - first_part_size ) );
2747 TEST_ASSERT( function_output_length <=
2748 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2749 total_output_length += function_output_length;
2750 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002751
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002752 status = psa_cipher_finish( &operation,
2753 ( output_buffer_size == 0 ? NULL :
2754 output + total_output_length ),
2755 output_buffer_size - total_output_length,
2756 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002757 TEST_ASSERT( function_output_length <=
2758 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2759 TEST_ASSERT( function_output_length <=
2760 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002761 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002762 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002763
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002764 if( expected_status == PSA_SUCCESS )
2765 {
2766 PSA_ASSERT( psa_cipher_abort( &operation ) );
2767
2768 ASSERT_COMPARE( expected_output->x, expected_output->len,
2769 output, total_output_length );
2770 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002771
2772exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002773 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002774 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002775 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002776 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002777}
2778/* END_CASE */
2779
2780/* BEGIN_CASE */
2781void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002782 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002783 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002784 int first_part_size_arg,
2785 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002786 data_t *expected_output,
2787 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002788{
Ronald Cron5425a212020-08-04 14:58:35 +02002789 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002790 psa_key_type_t key_type = key_type_arg;
2791 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002792 psa_status_t status;
2793 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002794 size_t first_part_size = first_part_size_arg;
2795 size_t output1_length = output1_length_arg;
2796 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002797 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002798 size_t output_buffer_size = 0;
2799 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002800 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002801 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002802 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002803
Gilles Peskine8817f612018-12-18 00:18:46 +01002804 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002805
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002806 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2807 psa_set_key_algorithm( &attributes, alg );
2808 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002809
Ronald Cron5425a212020-08-04 14:58:35 +02002810 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2811 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002812
Ronald Cron5425a212020-08-04 14:58:35 +02002813 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002814
Steven Cooreman177deba2020-09-07 17:14:14 +02002815 if( iv->len > 0 )
2816 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002817 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002818 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002819
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002820 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2821 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002822 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002823
Gilles Peskinee0866522019-02-19 19:44:00 +01002824 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002825 PSA_ASSERT( psa_cipher_update( &operation,
2826 input->x, first_part_size,
2827 output, output_buffer_size,
2828 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002829 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002830 TEST_ASSERT( function_output_length <=
2831 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2832 TEST_ASSERT( function_output_length <=
2833 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002834 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002835
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002836 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02002837 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002838 PSA_ASSERT( psa_cipher_update( &operation,
2839 input->x + first_part_size,
2840 input->len - first_part_size,
2841 ( output_buffer_size == 0 ? NULL :
2842 output + total_output_length ),
2843 output_buffer_size - total_output_length,
2844 &function_output_length ) );
2845 TEST_ASSERT( function_output_length == output2_length );
2846 TEST_ASSERT( function_output_length <=
2847 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2848 alg,
2849 input->len - first_part_size ) );
2850 TEST_ASSERT( function_output_length <=
2851 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2852 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002853 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002854
Gilles Peskine50e586b2018-06-08 14:28:46 +02002855 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002856 ( output_buffer_size == 0 ? NULL :
2857 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002858 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002859 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002860 TEST_ASSERT( function_output_length <=
2861 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2862 TEST_ASSERT( function_output_length <=
2863 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002864 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002865 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002866
2867 if( expected_status == PSA_SUCCESS )
2868 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002869 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002870
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002871 ASSERT_COMPARE( expected_output->x, expected_output->len,
2872 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002873 }
2874
Gilles Peskine50e586b2018-06-08 14:28:46 +02002875exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002876 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002877 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002878 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002879 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002880}
2881/* END_CASE */
2882
Gilles Peskine50e586b2018-06-08 14:28:46 +02002883/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02002884void cipher_decrypt_fail( int alg_arg,
2885 int key_type_arg,
2886 data_t *key_data,
2887 data_t *iv,
2888 data_t *input_arg,
2889 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002890{
2891 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2892 psa_status_t status;
2893 psa_key_type_t key_type = key_type_arg;
2894 psa_algorithm_t alg = alg_arg;
2895 psa_status_t expected_status = expected_status_arg;
2896 unsigned char *input = NULL;
2897 size_t input_buffer_size = 0;
2898 unsigned char *output = NULL;
2899 size_t output_buffer_size = 0;
2900 size_t output_length = 0;
2901 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2902
2903 if ( PSA_ERROR_BAD_STATE != expected_status )
2904 {
2905 PSA_ASSERT( psa_crypto_init( ) );
2906
2907 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2908 psa_set_key_algorithm( &attributes, alg );
2909 psa_set_key_type( &attributes, key_type );
2910
2911 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2912 &key ) );
2913 }
2914
2915 /* Allocate input buffer and copy the iv and the plaintext */
2916 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
2917 if ( input_buffer_size > 0 )
2918 {
2919 ASSERT_ALLOC( input, input_buffer_size );
2920 memcpy( input, iv->x, iv->len );
2921 memcpy( input + iv->len, input_arg->x, input_arg->len );
2922 }
2923
2924 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
2925 ASSERT_ALLOC( output, output_buffer_size );
2926
2927 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
2928 output_buffer_size, &output_length );
2929 TEST_EQUAL( status, expected_status );
2930
2931exit:
2932 mbedtls_free( input );
2933 mbedtls_free( output );
2934 psa_destroy_key( key );
2935 PSA_DONE( );
2936}
2937/* END_CASE */
2938
2939/* BEGIN_CASE */
2940void cipher_decrypt( int alg_arg,
2941 int key_type_arg,
2942 data_t *key_data,
2943 data_t *iv,
2944 data_t *input_arg,
2945 data_t *expected_output )
2946{
2947 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2948 psa_key_type_t key_type = key_type_arg;
2949 psa_algorithm_t alg = alg_arg;
2950 unsigned char *input = NULL;
2951 size_t input_buffer_size = 0;
2952 unsigned char *output = NULL;
2953 size_t output_buffer_size = 0;
2954 size_t output_length = 0;
2955 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2956
2957 PSA_ASSERT( psa_crypto_init( ) );
2958
2959 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2960 psa_set_key_algorithm( &attributes, alg );
2961 psa_set_key_type( &attributes, key_type );
2962
2963 /* Allocate input buffer and copy the iv and the plaintext */
2964 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
2965 if ( input_buffer_size > 0 )
2966 {
2967 ASSERT_ALLOC( input, input_buffer_size );
2968 memcpy( input, iv->x, iv->len );
2969 memcpy( input + iv->len, input_arg->x, input_arg->len );
2970 }
2971
2972 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
2973 ASSERT_ALLOC( output, output_buffer_size );
2974
2975 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2976 &key ) );
2977
2978 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
2979 output_buffer_size, &output_length ) );
2980 TEST_ASSERT( output_length <=
2981 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
2982 TEST_ASSERT( output_length <=
2983 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
2984
2985 ASSERT_COMPARE( expected_output->x, expected_output->len,
2986 output, output_length );
2987exit:
2988 mbedtls_free( input );
2989 mbedtls_free( output );
2990 psa_destroy_key( key );
2991 PSA_DONE( );
2992}
2993/* END_CASE */
2994
2995/* BEGIN_CASE */
2996void cipher_verify_output( int alg_arg,
2997 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002998 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002999 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003000{
Ronald Cron5425a212020-08-04 14:58:35 +02003001 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003002 psa_key_type_t key_type = key_type_arg;
3003 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003004 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003005 size_t output1_size = 0;
3006 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003007 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003008 size_t output2_size = 0;
3009 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003010 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003011
Gilles Peskine8817f612018-12-18 00:18:46 +01003012 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003013
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003014 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3015 psa_set_key_algorithm( &attributes, alg );
3016 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003017
Ronald Cron5425a212020-08-04 14:58:35 +02003018 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3019 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003020 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003021 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003022
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003023 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3024 output1, output1_size,
3025 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003026 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003027 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003028 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003029 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003030
3031 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003032 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003033
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003034 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3035 output2, output2_size,
3036 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003037 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003038 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003039 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003040 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003041
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003042 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003043
3044exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003045 mbedtls_free( output1 );
3046 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003047 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003048 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003049}
3050/* END_CASE */
3051
3052/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003053void cipher_verify_output_multipart( int alg_arg,
3054 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003055 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003056 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003057 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003058{
Ronald Cron5425a212020-08-04 14:58:35 +02003059 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003060 psa_key_type_t key_type = key_type_arg;
3061 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003062 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003063 unsigned char iv[16] = {0};
3064 size_t iv_size = 16;
3065 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003066 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003067 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003068 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003069 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003070 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003071 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003072 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003073 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3074 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003075 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003076
Gilles Peskine8817f612018-12-18 00:18:46 +01003077 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003078
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003079 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3080 psa_set_key_algorithm( &attributes, alg );
3081 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003082
Ronald Cron5425a212020-08-04 14:58:35 +02003083 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3084 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003085
Ronald Cron5425a212020-08-04 14:58:35 +02003086 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3087 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003088
Steven Cooreman177deba2020-09-07 17:14:14 +02003089 if( alg != PSA_ALG_ECB_NO_PADDING )
3090 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003091 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3092 iv, iv_size,
3093 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003094 }
3095
gabor-mezei-armceface22021-01-21 12:26:17 +01003096 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3097 TEST_ASSERT( output1_buffer_size <=
3098 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003099 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003100
Gilles Peskinee0866522019-02-19 19:44:00 +01003101 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003102
Gilles Peskine8817f612018-12-18 00:18:46 +01003103 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3104 output1, output1_buffer_size,
3105 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003106 TEST_ASSERT( function_output_length <=
3107 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3108 TEST_ASSERT( function_output_length <=
3109 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003110 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003111
Gilles Peskine8817f612018-12-18 00:18:46 +01003112 PSA_ASSERT( psa_cipher_update( &operation1,
3113 input->x + first_part_size,
3114 input->len - first_part_size,
3115 output1, output1_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,
3119 alg,
3120 input->len - first_part_size ) );
3121 TEST_ASSERT( function_output_length <=
3122 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003123 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003124
Gilles Peskine8817f612018-12-18 00:18:46 +01003125 PSA_ASSERT( psa_cipher_finish( &operation1,
3126 output1 + output1_length,
3127 output1_buffer_size - output1_length,
3128 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003129 TEST_ASSERT( function_output_length <=
3130 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3131 TEST_ASSERT( function_output_length <=
3132 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003133 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003134
Gilles Peskine8817f612018-12-18 00:18:46 +01003135 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003136
Gilles Peskine048b7f02018-06-08 14:20:49 +02003137 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003138 TEST_ASSERT( output2_buffer_size <=
3139 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3140 TEST_ASSERT( output2_buffer_size <=
3141 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003142 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003143
Steven Cooreman177deba2020-09-07 17:14:14 +02003144 if( iv_length > 0 )
3145 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003146 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3147 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003148 }
Moran Pekerded84402018-06-06 16:36:50 +03003149
Gilles Peskine8817f612018-12-18 00:18:46 +01003150 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3151 output2, output2_buffer_size,
3152 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003153 TEST_ASSERT( function_output_length <=
3154 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3155 TEST_ASSERT( function_output_length <=
3156 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003157 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003158
Gilles Peskine8817f612018-12-18 00:18:46 +01003159 PSA_ASSERT( psa_cipher_update( &operation2,
3160 output1 + first_part_size,
3161 output1_length - first_part_size,
3162 output2, output2_buffer_size,
3163 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003164 TEST_ASSERT( function_output_length <=
3165 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3166 alg,
3167 output1_length - first_part_size ) );
3168 TEST_ASSERT( function_output_length <=
3169 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003170 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003171
Gilles Peskine8817f612018-12-18 00:18:46 +01003172 PSA_ASSERT( psa_cipher_finish( &operation2,
3173 output2 + output2_length,
3174 output2_buffer_size - output2_length,
3175 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003176 TEST_ASSERT( function_output_length <=
3177 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3178 TEST_ASSERT( function_output_length <=
3179 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003180 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003181
Gilles Peskine8817f612018-12-18 00:18:46 +01003182 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003183
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003184 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003185
3186exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003187 psa_cipher_abort( &operation1 );
3188 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003189 mbedtls_free( output1 );
3190 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003191 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003192 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003193}
3194/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003195
Gilles Peskine20035e32018-02-03 22:44:14 +01003196/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003197void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003198 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003199 data_t *nonce,
3200 data_t *additional_data,
3201 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003202 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003203{
Ronald Cron5425a212020-08-04 14:58:35 +02003204 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003205 psa_key_type_t key_type = key_type_arg;
3206 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003207 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003208 unsigned char *output_data = NULL;
3209 size_t output_size = 0;
3210 size_t output_length = 0;
3211 unsigned char *output_data2 = NULL;
3212 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003213 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003214 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003215 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003216
Gilles Peskine8817f612018-12-18 00:18:46 +01003217 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003218
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003219 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3220 psa_set_key_algorithm( &attributes, alg );
3221 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003222
Gilles Peskine049c7532019-05-15 20:22:09 +02003223 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003224 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003225 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3226 key_bits = psa_get_key_bits( &attributes );
3227
3228 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3229 alg );
3230 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3231 * should be exact. */
3232 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3233 expected_result != PSA_ERROR_NOT_SUPPORTED )
3234 {
3235 TEST_EQUAL( output_size,
3236 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3237 TEST_ASSERT( output_size <=
3238 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3239 }
3240 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003241
Steven Cooremanf49478b2021-02-15 15:19:25 +01003242 status = psa_aead_encrypt( key, alg,
3243 nonce->x, nonce->len,
3244 additional_data->x,
3245 additional_data->len,
3246 input_data->x, input_data->len,
3247 output_data, output_size,
3248 &output_length );
3249
3250 /* If the operation is not supported, just skip and not fail in case the
3251 * encryption involves a common limitation of cryptography hardwares and
3252 * an alternative implementation. */
3253 if( status == PSA_ERROR_NOT_SUPPORTED )
3254 {
3255 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3256 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3257 }
3258
3259 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003260
3261 if( PSA_SUCCESS == expected_result )
3262 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003263 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003264
Gilles Peskine003a4a92019-05-14 16:09:40 +02003265 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3266 * should be exact. */
3267 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003268 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003269
gabor-mezei-armceface22021-01-21 12:26:17 +01003270 TEST_ASSERT( input_data->len <=
3271 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3272
Ronald Cron5425a212020-08-04 14:58:35 +02003273 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003274 nonce->x, nonce->len,
3275 additional_data->x,
3276 additional_data->len,
3277 output_data, output_length,
3278 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003279 &output_length2 ),
3280 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003281
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003282 ASSERT_COMPARE( input_data->x, input_data->len,
3283 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003284 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003285
Gilles Peskinea1cac842018-06-11 19:33:02 +02003286exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003287 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003288 mbedtls_free( output_data );
3289 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003290 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003291}
3292/* END_CASE */
3293
3294/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003295void aead_encrypt( int key_type_arg, data_t *key_data,
3296 int alg_arg,
3297 data_t *nonce,
3298 data_t *additional_data,
3299 data_t *input_data,
3300 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003301{
Ronald Cron5425a212020-08-04 14:58:35 +02003302 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003303 psa_key_type_t key_type = key_type_arg;
3304 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003305 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003306 unsigned char *output_data = NULL;
3307 size_t output_size = 0;
3308 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003309 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003310 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003311
Gilles Peskine8817f612018-12-18 00:18:46 +01003312 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003313
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003314 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3315 psa_set_key_algorithm( &attributes, alg );
3316 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003317
Gilles Peskine049c7532019-05-15 20:22:09 +02003318 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003319 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003320 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3321 key_bits = psa_get_key_bits( &attributes );
3322
3323 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3324 alg );
3325 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3326 * should be exact. */
3327 TEST_EQUAL( output_size,
3328 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3329 TEST_ASSERT( output_size <=
3330 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3331 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003332
Steven Cooremand588ea12021-01-11 19:36:04 +01003333 status = psa_aead_encrypt( key, alg,
3334 nonce->x, nonce->len,
3335 additional_data->x, additional_data->len,
3336 input_data->x, input_data->len,
3337 output_data, output_size,
3338 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003339
Ronald Cron28a45ed2021-02-09 20:35:42 +01003340 /* If the operation is not supported, just skip and not fail in case the
3341 * encryption involves a common limitation of cryptography hardwares and
3342 * an alternative implementation. */
3343 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003344 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003345 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3346 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003347 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003348
3349 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003350 ASSERT_COMPARE( expected_result->x, expected_result->len,
3351 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003352
Gilles Peskinea1cac842018-06-11 19:33:02 +02003353exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003354 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003355 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003356 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003357}
3358/* END_CASE */
3359
3360/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003361void aead_decrypt( int key_type_arg, data_t *key_data,
3362 int alg_arg,
3363 data_t *nonce,
3364 data_t *additional_data,
3365 data_t *input_data,
3366 data_t *expected_data,
3367 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003368{
Ronald Cron5425a212020-08-04 14:58:35 +02003369 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003370 psa_key_type_t key_type = key_type_arg;
3371 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003372 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003373 unsigned char *output_data = NULL;
3374 size_t output_size = 0;
3375 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003376 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003377 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003378 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003379
Gilles Peskine8817f612018-12-18 00:18:46 +01003380 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003381
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003382 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3383 psa_set_key_algorithm( &attributes, alg );
3384 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003385
Gilles Peskine049c7532019-05-15 20:22:09 +02003386 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003387 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003388 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3389 key_bits = psa_get_key_bits( &attributes );
3390
3391 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3392 alg );
3393 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3394 expected_result != PSA_ERROR_NOT_SUPPORTED )
3395 {
3396 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3397 * should be exact. */
3398 TEST_EQUAL( output_size,
3399 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3400 TEST_ASSERT( output_size <=
3401 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3402 }
3403 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003404
Steven Cooremand588ea12021-01-11 19:36:04 +01003405 status = psa_aead_decrypt( key, alg,
3406 nonce->x, nonce->len,
3407 additional_data->x,
3408 additional_data->len,
3409 input_data->x, input_data->len,
3410 output_data, output_size,
3411 &output_length );
3412
Ronald Cron28a45ed2021-02-09 20:35:42 +01003413 /* If the operation is not supported, just skip and not fail in case the
3414 * decryption involves a common limitation of cryptography hardwares and
3415 * an alternative implementation. */
3416 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003417 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003418 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3419 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003420 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003421
3422 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003423
Gilles Peskine2d277862018-06-18 15:41:12 +02003424 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003425 ASSERT_COMPARE( expected_data->x, expected_data->len,
3426 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003427
Gilles Peskinea1cac842018-06-11 19:33:02 +02003428exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003429 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003430 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003431 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003432}
3433/* END_CASE */
3434
3435/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003436void signature_size( int type_arg,
3437 int bits,
3438 int alg_arg,
3439 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003440{
3441 psa_key_type_t type = type_arg;
3442 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003443 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003444
Gilles Peskinefe11b722018-12-18 00:24:04 +01003445 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003446
Gilles Peskinee59236f2018-01-27 23:32:46 +01003447exit:
3448 ;
3449}
3450/* END_CASE */
3451
3452/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003453void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3454 int alg_arg, data_t *input_data,
3455 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003456{
Ronald Cron5425a212020-08-04 14:58:35 +02003457 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003458 psa_key_type_t key_type = key_type_arg;
3459 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003460 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003461 unsigned char *signature = NULL;
3462 size_t signature_size;
3463 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003464 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003465
Gilles Peskine8817f612018-12-18 00:18:46 +01003466 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003467
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003468 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003469 psa_set_key_algorithm( &attributes, alg );
3470 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003471
Gilles Peskine049c7532019-05-15 20:22:09 +02003472 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003473 &key ) );
3474 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003475 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003476
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003477 /* Allocate a buffer which has the size advertized by the
3478 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003479 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003480 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003481 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003482 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003483 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003484
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003485 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003486 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003487 input_data->x, input_data->len,
3488 signature, signature_size,
3489 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003490 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003491 ASSERT_COMPARE( output_data->x, output_data->len,
3492 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003493
3494exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003495 /*
3496 * Key attributes may have been returned by psa_get_key_attributes()
3497 * thus reset them as required.
3498 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003499 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003500
Ronald Cron5425a212020-08-04 14:58:35 +02003501 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003502 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003503 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003504}
3505/* END_CASE */
3506
3507/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003508void sign_hash_fail( int key_type_arg, data_t *key_data,
3509 int alg_arg, data_t *input_data,
3510 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003511{
Ronald Cron5425a212020-08-04 14:58:35 +02003512 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003513 psa_key_type_t key_type = key_type_arg;
3514 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003515 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003516 psa_status_t actual_status;
3517 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003518 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003519 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003520 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003521
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003522 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003523
Gilles Peskine8817f612018-12-18 00:18:46 +01003524 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003525
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003526 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003527 psa_set_key_algorithm( &attributes, alg );
3528 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003529
Gilles Peskine049c7532019-05-15 20:22:09 +02003530 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003531 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003532
Ronald Cron5425a212020-08-04 14:58:35 +02003533 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003534 input_data->x, input_data->len,
3535 signature, signature_size,
3536 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003537 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003538 /* The value of *signature_length is unspecified on error, but
3539 * whatever it is, it should be less than signature_size, so that
3540 * if the caller tries to read *signature_length bytes without
3541 * checking the error code then they don't overflow a buffer. */
3542 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003543
3544exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003545 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003546 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003547 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003548 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003549}
3550/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003551
3552/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003553void sign_verify_hash( int key_type_arg, data_t *key_data,
3554 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003555{
Ronald Cron5425a212020-08-04 14:58:35 +02003556 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003557 psa_key_type_t key_type = key_type_arg;
3558 psa_algorithm_t alg = alg_arg;
3559 size_t key_bits;
3560 unsigned char *signature = NULL;
3561 size_t signature_size;
3562 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003563 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003564
Gilles Peskine8817f612018-12-18 00:18:46 +01003565 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003566
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003567 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003568 psa_set_key_algorithm( &attributes, alg );
3569 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003570
Gilles Peskine049c7532019-05-15 20:22:09 +02003571 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003572 &key ) );
3573 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003574 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003575
3576 /* Allocate a buffer which has the size advertized by the
3577 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003578 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003579 key_bits, alg );
3580 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003581 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003582 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003583
3584 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003585 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003586 input_data->x, input_data->len,
3587 signature, signature_size,
3588 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003589 /* Check that the signature length looks sensible. */
3590 TEST_ASSERT( signature_length <= signature_size );
3591 TEST_ASSERT( signature_length > 0 );
3592
3593 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003594 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003595 input_data->x, input_data->len,
3596 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003597
3598 if( input_data->len != 0 )
3599 {
3600 /* Flip a bit in the input and verify that the signature is now
3601 * detected as invalid. Flip a bit at the beginning, not at the end,
3602 * because ECDSA may ignore the last few bits of the input. */
3603 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003604 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003605 input_data->x, input_data->len,
3606 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003607 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003608 }
3609
3610exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003611 /*
3612 * Key attributes may have been returned by psa_get_key_attributes()
3613 * thus reset them as required.
3614 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003615 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003616
Ronald Cron5425a212020-08-04 14:58:35 +02003617 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003618 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003619 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003620}
3621/* END_CASE */
3622
3623/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003624void verify_hash( int key_type_arg, data_t *key_data,
3625 int alg_arg, data_t *hash_data,
3626 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003627{
Ronald Cron5425a212020-08-04 14:58:35 +02003628 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003629 psa_key_type_t key_type = key_type_arg;
3630 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003631 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003632
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003633 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003634
Gilles Peskine8817f612018-12-18 00:18:46 +01003635 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003636
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003637 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003638 psa_set_key_algorithm( &attributes, alg );
3639 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003640
Gilles Peskine049c7532019-05-15 20:22:09 +02003641 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003642 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003643
Ronald Cron5425a212020-08-04 14:58:35 +02003644 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003645 hash_data->x, hash_data->len,
3646 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003647
itayzafrir5c753392018-05-08 11:18:38 +03003648exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003649 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003650 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003651 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003652}
3653/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003654
3655/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003656void verify_hash_fail( int key_type_arg, data_t *key_data,
3657 int alg_arg, data_t *hash_data,
3658 data_t *signature_data,
3659 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003660{
Ronald Cron5425a212020-08-04 14:58:35 +02003661 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003662 psa_key_type_t key_type = key_type_arg;
3663 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003664 psa_status_t actual_status;
3665 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003666 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003667
Gilles Peskine8817f612018-12-18 00:18:46 +01003668 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003669
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003670 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003671 psa_set_key_algorithm( &attributes, alg );
3672 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003673
Gilles Peskine049c7532019-05-15 20:22:09 +02003674 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003675 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003676
Ronald Cron5425a212020-08-04 14:58:35 +02003677 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003678 hash_data->x, hash_data->len,
3679 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003680 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003681
3682exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003683 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003684 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003685 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003686}
3687/* END_CASE */
3688
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003689/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02003690void sign_message_deterministic( int key_type_arg,
3691 data_t *key_data,
3692 int alg_arg,
3693 data_t *input_data,
3694 data_t *output_data )
3695{
3696 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3697 psa_key_type_t key_type = key_type_arg;
3698 psa_algorithm_t alg = alg_arg;
3699 size_t key_bits;
3700 unsigned char *signature = NULL;
3701 size_t signature_size;
3702 size_t signature_length = 0xdeadbeef;
3703 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3704
3705 PSA_ASSERT( psa_crypto_init( ) );
3706
3707 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3708 psa_set_key_algorithm( &attributes, alg );
3709 psa_set_key_type( &attributes, key_type );
3710
3711 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3712 &key ) );
3713 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3714 key_bits = psa_get_key_bits( &attributes );
3715
3716 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3717 TEST_ASSERT( signature_size != 0 );
3718 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3719 ASSERT_ALLOC( signature, signature_size );
3720
3721 PSA_ASSERT( psa_sign_message( key, alg,
3722 input_data->x, input_data->len,
3723 signature, signature_size,
3724 &signature_length ) );
3725
3726 ASSERT_COMPARE( output_data->x, output_data->len,
3727 signature, signature_length );
3728
3729exit:
3730 psa_reset_key_attributes( &attributes );
3731
3732 psa_destroy_key( key );
3733 mbedtls_free( signature );
3734 PSA_DONE( );
3735
3736}
3737/* END_CASE */
3738
3739/* BEGIN_CASE */
3740void sign_message_fail( int key_type_arg,
3741 data_t *key_data,
3742 int alg_arg,
3743 data_t *input_data,
3744 int signature_size_arg,
3745 int expected_status_arg )
3746{
3747 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3748 psa_key_type_t key_type = key_type_arg;
3749 psa_algorithm_t alg = alg_arg;
3750 size_t signature_size = signature_size_arg;
3751 psa_status_t actual_status;
3752 psa_status_t expected_status = expected_status_arg;
3753 unsigned char *signature = NULL;
3754 size_t signature_length = 0xdeadbeef;
3755 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3756
3757 ASSERT_ALLOC( signature, signature_size );
3758
3759 PSA_ASSERT( psa_crypto_init( ) );
3760
3761 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3762 psa_set_key_algorithm( &attributes, alg );
3763 psa_set_key_type( &attributes, key_type );
3764
3765 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3766 &key ) );
3767
3768 actual_status = psa_sign_message( key, alg,
3769 input_data->x, input_data->len,
3770 signature, signature_size,
3771 &signature_length );
3772 TEST_EQUAL( actual_status, expected_status );
3773 /* The value of *signature_length is unspecified on error, but
3774 * whatever it is, it should be less than signature_size, so that
3775 * if the caller tries to read *signature_length bytes without
3776 * checking the error code then they don't overflow a buffer. */
3777 TEST_ASSERT( signature_length <= signature_size );
3778
3779exit:
3780 psa_reset_key_attributes( &attributes );
3781 psa_destroy_key( key );
3782 mbedtls_free( signature );
3783 PSA_DONE( );
3784}
3785/* END_CASE */
3786
3787/* BEGIN_CASE */
3788void sign_verify_message( int key_type_arg,
3789 data_t *key_data,
3790 int alg_arg,
3791 data_t *input_data )
3792{
3793 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3794 psa_key_type_t key_type = key_type_arg;
3795 psa_algorithm_t alg = alg_arg;
3796 size_t key_bits;
3797 unsigned char *signature = NULL;
3798 size_t signature_size;
3799 size_t signature_length = 0xdeadbeef;
3800 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3801
3802 PSA_ASSERT( psa_crypto_init( ) );
3803
3804 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3805 PSA_KEY_USAGE_VERIFY_MESSAGE );
3806 psa_set_key_algorithm( &attributes, alg );
3807 psa_set_key_type( &attributes, key_type );
3808
3809 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3810 &key ) );
3811 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3812 key_bits = psa_get_key_bits( &attributes );
3813
3814 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3815 TEST_ASSERT( signature_size != 0 );
3816 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3817 ASSERT_ALLOC( signature, signature_size );
3818
3819 PSA_ASSERT( psa_sign_message( key, alg,
3820 input_data->x, input_data->len,
3821 signature, signature_size,
3822 &signature_length ) );
3823 TEST_ASSERT( signature_length <= signature_size );
3824 TEST_ASSERT( signature_length > 0 );
3825
3826 PSA_ASSERT( psa_verify_message( key, alg,
3827 input_data->x, input_data->len,
3828 signature, signature_length ) );
3829
3830 if( input_data->len != 0 )
3831 {
3832 /* Flip a bit in the input and verify that the signature is now
3833 * detected as invalid. Flip a bit at the beginning, not at the end,
3834 * because ECDSA may ignore the last few bits of the input. */
3835 input_data->x[0] ^= 1;
3836 TEST_EQUAL( psa_verify_message( key, alg,
3837 input_data->x, input_data->len,
3838 signature, signature_length ),
3839 PSA_ERROR_INVALID_SIGNATURE );
3840 }
3841
3842exit:
3843 psa_reset_key_attributes( &attributes );
3844
3845 psa_destroy_key( key );
3846 mbedtls_free( signature );
3847 PSA_DONE( );
3848}
3849/* END_CASE */
3850
3851/* BEGIN_CASE */
3852void verify_message( int key_type_arg,
3853 data_t *key_data,
3854 int alg_arg,
3855 data_t *input_data,
3856 data_t *signature_data )
3857{
3858 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3859 psa_key_type_t key_type = key_type_arg;
3860 psa_algorithm_t alg = alg_arg;
3861 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3862
3863 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3864
3865 PSA_ASSERT( psa_crypto_init( ) );
3866
3867 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3868 psa_set_key_algorithm( &attributes, alg );
3869 psa_set_key_type( &attributes, key_type );
3870
3871 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3872 &key ) );
3873
3874 PSA_ASSERT( psa_verify_message( key, alg,
3875 input_data->x, input_data->len,
3876 signature_data->x, signature_data->len ) );
3877
3878exit:
3879 psa_reset_key_attributes( &attributes );
3880 psa_destroy_key( key );
3881 PSA_DONE( );
3882}
3883/* END_CASE */
3884
3885/* BEGIN_CASE */
3886void verify_message_fail( int key_type_arg,
3887 data_t *key_data,
3888 int alg_arg,
3889 data_t *hash_data,
3890 data_t *signature_data,
3891 int expected_status_arg )
3892{
3893 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3894 psa_key_type_t key_type = key_type_arg;
3895 psa_algorithm_t alg = alg_arg;
3896 psa_status_t actual_status;
3897 psa_status_t expected_status = expected_status_arg;
3898 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3899
3900 PSA_ASSERT( psa_crypto_init( ) );
3901
3902 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3903 psa_set_key_algorithm( &attributes, alg );
3904 psa_set_key_type( &attributes, key_type );
3905
3906 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3907 &key ) );
3908
3909 actual_status = psa_verify_message( key, alg,
3910 hash_data->x, hash_data->len,
3911 signature_data->x,
3912 signature_data->len );
3913 TEST_EQUAL( actual_status, expected_status );
3914
3915exit:
3916 psa_reset_key_attributes( &attributes );
3917 psa_destroy_key( key );
3918 PSA_DONE( );
3919}
3920/* END_CASE */
3921
3922/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003923void asymmetric_encrypt( int key_type_arg,
3924 data_t *key_data,
3925 int alg_arg,
3926 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003927 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003928 int expected_output_length_arg,
3929 int expected_status_arg )
3930{
Ronald Cron5425a212020-08-04 14:58:35 +02003931 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003932 psa_key_type_t key_type = key_type_arg;
3933 psa_algorithm_t alg = alg_arg;
3934 size_t expected_output_length = expected_output_length_arg;
3935 size_t key_bits;
3936 unsigned char *output = NULL;
3937 size_t output_size;
3938 size_t output_length = ~0;
3939 psa_status_t actual_status;
3940 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003941 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003942
Gilles Peskine8817f612018-12-18 00:18:46 +01003943 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003944
Gilles Peskine656896e2018-06-29 19:12:28 +02003945 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003946 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3947 psa_set_key_algorithm( &attributes, alg );
3948 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003949 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003950 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003951
3952 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003953 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003954 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003955
Gilles Peskine656896e2018-06-29 19:12:28 +02003956 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003957 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003958 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003959
3960 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003961 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003962 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003963 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003964 output, output_size,
3965 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003966 TEST_EQUAL( actual_status, expected_status );
3967 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003968
Gilles Peskine68428122018-06-30 18:42:41 +02003969 /* If the label is empty, the test framework puts a non-null pointer
3970 * in label->x. Test that a null pointer works as well. */
3971 if( label->len == 0 )
3972 {
3973 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003974 if( output_size != 0 )
3975 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003976 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003977 input_data->x, input_data->len,
3978 NULL, label->len,
3979 output, output_size,
3980 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003981 TEST_EQUAL( actual_status, expected_status );
3982 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003983 }
3984
Gilles Peskine656896e2018-06-29 19:12:28 +02003985exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003986 /*
3987 * Key attributes may have been returned by psa_get_key_attributes()
3988 * thus reset them as required.
3989 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003990 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003991
Ronald Cron5425a212020-08-04 14:58:35 +02003992 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02003993 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003994 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003995}
3996/* END_CASE */
3997
3998/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003999void asymmetric_encrypt_decrypt( int key_type_arg,
4000 data_t *key_data,
4001 int alg_arg,
4002 data_t *input_data,
4003 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004004{
Ronald Cron5425a212020-08-04 14:58:35 +02004005 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004006 psa_key_type_t key_type = key_type_arg;
4007 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004008 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004009 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004010 size_t output_size;
4011 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004012 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004013 size_t output2_size;
4014 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004015 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004016
Gilles Peskine8817f612018-12-18 00:18:46 +01004017 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004018
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004019 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4020 psa_set_key_algorithm( &attributes, alg );
4021 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004022
Gilles Peskine049c7532019-05-15 20:22:09 +02004023 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004024 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004025
4026 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004027 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004028 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004029
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004030 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004031 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004032 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01004033
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004034 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01004035 TEST_ASSERT( output2_size <=
4036 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
4037 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004038 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004039
Gilles Peskineeebd7382018-06-08 18:11:54 +02004040 /* We test encryption by checking that encrypt-then-decrypt gives back
4041 * the original plaintext because of the non-optional random
4042 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004043 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004044 input_data->x, input_data->len,
4045 label->x, label->len,
4046 output, output_size,
4047 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004048 /* We don't know what ciphertext length to expect, but check that
4049 * it looks sensible. */
4050 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004051
Ronald Cron5425a212020-08-04 14:58:35 +02004052 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004053 output, output_length,
4054 label->x, label->len,
4055 output2, output2_size,
4056 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004057 ASSERT_COMPARE( input_data->x, input_data->len,
4058 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004059
4060exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004061 /*
4062 * Key attributes may have been returned by psa_get_key_attributes()
4063 * thus reset them as required.
4064 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004065 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004066
Ronald Cron5425a212020-08-04 14:58:35 +02004067 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004068 mbedtls_free( output );
4069 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004070 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004071}
4072/* END_CASE */
4073
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004074/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004075void asymmetric_decrypt( int key_type_arg,
4076 data_t *key_data,
4077 int alg_arg,
4078 data_t *input_data,
4079 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004080 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004081{
Ronald Cron5425a212020-08-04 14:58:35 +02004082 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004083 psa_key_type_t key_type = key_type_arg;
4084 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004085 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004086 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004087 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004088 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004089 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004090
Gilles Peskine8817f612018-12-18 00:18:46 +01004091 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004092
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004093 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4094 psa_set_key_algorithm( &attributes, alg );
4095 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004096
Gilles Peskine049c7532019-05-15 20:22:09 +02004097 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004098 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004099
gabor-mezei-armceface22021-01-21 12:26:17 +01004100 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4101 key_bits = psa_get_key_bits( &attributes );
4102
4103 /* Determine the maximum ciphertext length */
4104 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
4105 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
4106 ASSERT_ALLOC( output, output_size );
4107
Ronald Cron5425a212020-08-04 14:58:35 +02004108 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004109 input_data->x, input_data->len,
4110 label->x, label->len,
4111 output,
4112 output_size,
4113 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004114 ASSERT_COMPARE( expected_data->x, expected_data->len,
4115 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004116
Gilles Peskine68428122018-06-30 18:42:41 +02004117 /* If the label is empty, the test framework puts a non-null pointer
4118 * in label->x. Test that a null pointer works as well. */
4119 if( label->len == 0 )
4120 {
4121 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004122 if( output_size != 0 )
4123 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004124 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004125 input_data->x, input_data->len,
4126 NULL, label->len,
4127 output,
4128 output_size,
4129 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004130 ASSERT_COMPARE( expected_data->x, expected_data->len,
4131 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004132 }
4133
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004134exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004135 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004136 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004137 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004138 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004139}
4140/* END_CASE */
4141
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004142/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004143void asymmetric_decrypt_fail( int key_type_arg,
4144 data_t *key_data,
4145 int alg_arg,
4146 data_t *input_data,
4147 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004148 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004149 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004150{
Ronald Cron5425a212020-08-04 14:58:35 +02004151 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004152 psa_key_type_t key_type = key_type_arg;
4153 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004154 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004155 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004156 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004157 psa_status_t actual_status;
4158 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004159 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004160
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004161 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004162
Gilles Peskine8817f612018-12-18 00:18:46 +01004163 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004164
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004165 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4166 psa_set_key_algorithm( &attributes, alg );
4167 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004168
Gilles Peskine049c7532019-05-15 20:22:09 +02004169 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004170 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004171
Ronald Cron5425a212020-08-04 14:58:35 +02004172 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004173 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004174 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004175 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004176 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004177 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004178 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004179
Gilles Peskine68428122018-06-30 18:42:41 +02004180 /* If the label is empty, the test framework puts a non-null pointer
4181 * in label->x. Test that a null pointer works as well. */
4182 if( label->len == 0 )
4183 {
4184 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004185 if( output_size != 0 )
4186 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004187 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004188 input_data->x, input_data->len,
4189 NULL, label->len,
4190 output, output_size,
4191 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004192 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004193 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004194 }
4195
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004196exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004197 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004198 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004199 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004200 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004201}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004202/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004203
4204/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004205void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004206{
4207 /* Test each valid way of initializing the object, except for `= {0}`, as
4208 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4209 * though it's OK by the C standard. We could test for this, but we'd need
4210 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004211 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004212 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4213 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4214 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004215
4216 memset( &zero, 0, sizeof( zero ) );
4217
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004218 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004219 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004220 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004221 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004222 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004223 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004224 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004225
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004226 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004227 PSA_ASSERT( psa_key_derivation_abort(&func) );
4228 PSA_ASSERT( psa_key_derivation_abort(&init) );
4229 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004230}
4231/* END_CASE */
4232
Janos Follath16de4a42019-06-13 16:32:24 +01004233/* BEGIN_CASE */
4234void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004235{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004236 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004237 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004238 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004239
Gilles Peskine8817f612018-12-18 00:18:46 +01004240 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004241
Janos Follath16de4a42019-06-13 16:32:24 +01004242 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004243 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004244
4245exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004246 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004247 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004248}
4249/* END_CASE */
4250
Janos Follathaf3c2a02019-06-12 12:34:34 +01004251/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004252void derive_set_capacity( int alg_arg, int capacity_arg,
4253 int expected_status_arg )
4254{
4255 psa_algorithm_t alg = alg_arg;
4256 size_t capacity = capacity_arg;
4257 psa_status_t expected_status = expected_status_arg;
4258 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4259
4260 PSA_ASSERT( psa_crypto_init( ) );
4261
4262 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4263
4264 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4265 expected_status );
4266
4267exit:
4268 psa_key_derivation_abort( &operation );
4269 PSA_DONE( );
4270}
4271/* END_CASE */
4272
4273/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004274void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004275 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004276 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004277 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004278 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004279 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004280 int expected_status_arg3,
4281 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004282{
4283 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004284 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4285 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004286 psa_status_t expected_statuses[] = {expected_status_arg1,
4287 expected_status_arg2,
4288 expected_status_arg3};
4289 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004290 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4291 MBEDTLS_SVC_KEY_ID_INIT,
4292 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004293 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4294 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4295 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004296 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004297 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004298 psa_status_t expected_output_status = expected_output_status_arg;
4299 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004300
4301 PSA_ASSERT( psa_crypto_init( ) );
4302
4303 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4304 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004305
4306 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4307
4308 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4309 {
Gilles Peskine4023c012021-05-27 13:21:20 +02004310 mbedtls_test_set_step( i );
4311 if( steps[i] == 0 )
4312 {
4313 /* Skip this step */
4314 }
4315 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004316 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004317 psa_set_key_type( &attributes, key_types[i] );
4318 PSA_ASSERT( psa_import_key( &attributes,
4319 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004320 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004321 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4322 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4323 {
4324 // When taking a private key as secret input, use key agreement
4325 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004326 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4327 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004328 expected_statuses[i] );
4329 }
4330 else
4331 {
4332 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004333 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004334 expected_statuses[i] );
4335 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004336 }
4337 else
4338 {
4339 TEST_EQUAL( psa_key_derivation_input_bytes(
4340 &operation, steps[i],
4341 inputs[i]->x, inputs[i]->len ),
4342 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004343 }
4344 }
4345
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004346 if( output_key_type != PSA_KEY_TYPE_NONE )
4347 {
4348 psa_reset_key_attributes( &attributes );
4349 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4350 psa_set_key_bits( &attributes, 8 );
4351 actual_output_status =
4352 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004353 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004354 }
4355 else
4356 {
4357 uint8_t buffer[1];
4358 actual_output_status =
4359 psa_key_derivation_output_bytes( &operation,
4360 buffer, sizeof( buffer ) );
4361 }
4362 TEST_EQUAL( actual_output_status, expected_output_status );
4363
Janos Follathaf3c2a02019-06-12 12:34:34 +01004364exit:
4365 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004366 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4367 psa_destroy_key( keys[i] );
4368 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004369 PSA_DONE( );
4370}
4371/* END_CASE */
4372
Janos Follathd958bb72019-07-03 15:02:16 +01004373/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02004374void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004375{
Janos Follathd958bb72019-07-03 15:02:16 +01004376 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004377 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004378 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004379 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004380 unsigned char input1[] = "Input 1";
4381 size_t input1_length = sizeof( input1 );
4382 unsigned char input2[] = "Input 2";
4383 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004384 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004385 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004386 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4387 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4388 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004389 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004390
Gilles Peskine8817f612018-12-18 00:18:46 +01004391 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004392
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004393 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4394 psa_set_key_algorithm( &attributes, alg );
4395 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004396
Gilles Peskine73676cb2019-05-15 20:15:10 +02004397 PSA_ASSERT( psa_import_key( &attributes,
4398 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004399 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004400
4401 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004402 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4403 input1, input1_length,
4404 input2, input2_length,
4405 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004406 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004407
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004408 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004409 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004410 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004411
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004412 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004413
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004414 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004415 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004416
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004417exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004418 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004419 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004420 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004421}
4422/* END_CASE */
4423
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004424/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02004425void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004426{
4427 uint8_t output_buffer[16];
4428 size_t buffer_size = 16;
4429 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004430 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004431
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004432 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4433 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004434 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004435
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004436 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004437 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004438
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004439 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004440
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004441 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4442 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004443 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004444
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004445 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004446 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004447
4448exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004449 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004450}
4451/* END_CASE */
4452
4453/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004454void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004455 int step1_arg, data_t *input1,
4456 int step2_arg, data_t *input2,
4457 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004458 int requested_capacity_arg,
4459 data_t *expected_output1,
4460 data_t *expected_output2 )
4461{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004462 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004463 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4464 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004465 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4466 MBEDTLS_SVC_KEY_ID_INIT,
4467 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004468 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004469 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004470 uint8_t *expected_outputs[2] =
4471 {expected_output1->x, expected_output2->x};
4472 size_t output_sizes[2] =
4473 {expected_output1->len, expected_output2->len};
4474 size_t output_buffer_size = 0;
4475 uint8_t *output_buffer = NULL;
4476 size_t expected_capacity;
4477 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004478 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004479 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004480 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004481
4482 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4483 {
4484 if( output_sizes[i] > output_buffer_size )
4485 output_buffer_size = output_sizes[i];
4486 if( output_sizes[i] == 0 )
4487 expected_outputs[i] = NULL;
4488 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004489 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004490 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004491
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004492 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4493 psa_set_key_algorithm( &attributes, alg );
4494 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004495
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004496 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004497 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4498 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4499 requested_capacity ) );
4500 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004501 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004502 switch( steps[i] )
4503 {
4504 case 0:
4505 break;
4506 case PSA_KEY_DERIVATION_INPUT_SECRET:
4507 PSA_ASSERT( psa_import_key( &attributes,
4508 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004509 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004510
4511 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4512 {
4513 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4514 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4515 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4516 }
4517
Gilles Peskine1468da72019-05-29 17:35:49 +02004518 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004519 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004520 break;
4521 default:
4522 PSA_ASSERT( psa_key_derivation_input_bytes(
4523 &operation, steps[i],
4524 inputs[i]->x, inputs[i]->len ) );
4525 break;
4526 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004527 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004528
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004529 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004530 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004531 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004532 expected_capacity = requested_capacity;
4533
4534 /* Expansion phase. */
4535 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4536 {
4537 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004538 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004539 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004540 if( expected_capacity == 0 && output_sizes[i] == 0 )
4541 {
4542 /* Reading 0 bytes when 0 bytes are available can go either way. */
4543 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004544 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004545 continue;
4546 }
4547 else if( expected_capacity == 0 ||
4548 output_sizes[i] > expected_capacity )
4549 {
4550 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004551 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004552 expected_capacity = 0;
4553 continue;
4554 }
4555 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004556 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004557 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004558 ASSERT_COMPARE( output_buffer, output_sizes[i],
4559 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004560 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004561 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004562 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004563 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004564 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004565 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004566 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004567
4568exit:
4569 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004570 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004571 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4572 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004573 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004574}
4575/* END_CASE */
4576
4577/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004578void derive_full( int alg_arg,
4579 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004580 data_t *input1,
4581 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004582 int requested_capacity_arg )
4583{
Ronald Cron5425a212020-08-04 14:58:35 +02004584 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004585 psa_algorithm_t alg = alg_arg;
4586 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004587 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004588 unsigned char output_buffer[16];
4589 size_t expected_capacity = requested_capacity;
4590 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004591 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004592
Gilles Peskine8817f612018-12-18 00:18:46 +01004593 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004594
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004595 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4596 psa_set_key_algorithm( &attributes, alg );
4597 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004598
Gilles Peskine049c7532019-05-15 20:22:09 +02004599 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004600 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004601
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004602 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4603 input1->x, input1->len,
4604 input2->x, input2->len,
4605 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004606 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004607
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004608 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004609 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004610 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004611
4612 /* Expansion phase. */
4613 while( current_capacity > 0 )
4614 {
4615 size_t read_size = sizeof( output_buffer );
4616 if( read_size > current_capacity )
4617 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004618 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004619 output_buffer,
4620 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004621 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004622 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004623 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004624 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004625 }
4626
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004627 /* Check that the operation refuses to go over capacity. */
4628 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004629 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004630
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004631 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004632
4633exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004634 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004635 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004636 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004637}
4638/* END_CASE */
4639
Janos Follathe60c9052019-07-03 13:51:30 +01004640/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004641void derive_key_exercise( int alg_arg,
4642 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004643 data_t *input1,
4644 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004645 int derived_type_arg,
4646 int derived_bits_arg,
4647 int derived_usage_arg,
4648 int derived_alg_arg )
4649{
Ronald Cron5425a212020-08-04 14:58:35 +02004650 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4651 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004652 psa_algorithm_t alg = alg_arg;
4653 psa_key_type_t derived_type = derived_type_arg;
4654 size_t derived_bits = derived_bits_arg;
4655 psa_key_usage_t derived_usage = derived_usage_arg;
4656 psa_algorithm_t derived_alg = derived_alg_arg;
4657 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004658 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004659 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004660 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004661
Gilles Peskine8817f612018-12-18 00:18:46 +01004662 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004663
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004664 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4665 psa_set_key_algorithm( &attributes, alg );
4666 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004667 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004668 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004669
4670 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004671 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4672 input1->x, input1->len,
4673 input2->x, input2->len,
4674 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004675 goto exit;
4676
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004677 psa_set_key_usage_flags( &attributes, derived_usage );
4678 psa_set_key_algorithm( &attributes, derived_alg );
4679 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004680 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004681 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004682 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004683
4684 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004685 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004686 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4687 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004688
4689 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004690 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004691 goto exit;
4692
4693exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004694 /*
4695 * Key attributes may have been returned by psa_get_key_attributes()
4696 * thus reset them as required.
4697 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004698 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004699
4700 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004701 psa_destroy_key( base_key );
4702 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004703 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004704}
4705/* END_CASE */
4706
Janos Follath42fd8882019-07-03 14:17:09 +01004707/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004708void derive_key_export( int alg_arg,
4709 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004710 data_t *input1,
4711 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004712 int bytes1_arg,
4713 int bytes2_arg )
4714{
Ronald Cron5425a212020-08-04 14:58:35 +02004715 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4716 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004717 psa_algorithm_t alg = alg_arg;
4718 size_t bytes1 = bytes1_arg;
4719 size_t bytes2 = bytes2_arg;
4720 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004721 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004722 uint8_t *output_buffer = NULL;
4723 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004724 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4725 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004726 size_t length;
4727
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004728 ASSERT_ALLOC( output_buffer, capacity );
4729 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004730 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004731
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004732 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4733 psa_set_key_algorithm( &base_attributes, alg );
4734 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004735 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004736 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004737
4738 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004739 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4740 input1->x, input1->len,
4741 input2->x, input2->len,
4742 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004743 goto exit;
4744
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004745 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004746 output_buffer,
4747 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004748 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004749
4750 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004751 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4752 input1->x, input1->len,
4753 input2->x, input2->len,
4754 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004755 goto exit;
4756
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004757 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4758 psa_set_key_algorithm( &derived_attributes, 0 );
4759 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004760 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004761 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004762 &derived_key ) );
4763 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004764 export_buffer, bytes1,
4765 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004766 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004767 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004768 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004769 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004770 &derived_key ) );
4771 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004772 export_buffer + bytes1, bytes2,
4773 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004774 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004775
4776 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004777 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4778 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004779
4780exit:
4781 mbedtls_free( output_buffer );
4782 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004783 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004784 psa_destroy_key( base_key );
4785 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004786 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004787}
4788/* END_CASE */
4789
4790/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004791void derive_key( int alg_arg,
4792 data_t *key_data, data_t *input1, data_t *input2,
4793 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004794 int expected_status_arg,
4795 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004796{
Ronald Cron5425a212020-08-04 14:58:35 +02004797 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4798 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004799 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004800 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004801 size_t bits = bits_arg;
4802 psa_status_t expected_status = expected_status_arg;
4803 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4804 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4805 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4806
4807 PSA_ASSERT( psa_crypto_init( ) );
4808
4809 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4810 psa_set_key_algorithm( &base_attributes, alg );
4811 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4812 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004813 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004814
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004815 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4816 input1->x, input1->len,
4817 input2->x, input2->len,
4818 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004819 goto exit;
4820
4821 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4822 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004823 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004824 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004825
4826 psa_status_t status =
4827 psa_key_derivation_output_key( &derived_attributes,
4828 &operation,
4829 &derived_key );
4830 if( is_large_output > 0 )
4831 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4832 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004833
4834exit:
4835 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004836 psa_destroy_key( base_key );
4837 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004838 PSA_DONE( );
4839}
4840/* END_CASE */
4841
4842/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004843void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004844 int our_key_type_arg, int our_key_alg_arg,
4845 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004846 int expected_status_arg )
4847{
Ronald Cron5425a212020-08-04 14:58:35 +02004848 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004849 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004850 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004851 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004852 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004853 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004854 psa_status_t expected_status = expected_status_arg;
4855 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004856
Gilles Peskine8817f612018-12-18 00:18:46 +01004857 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004858
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004859 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004860 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004861 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004862 PSA_ASSERT( psa_import_key( &attributes,
4863 our_key_data->x, our_key_data->len,
4864 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004865
Gilles Peskine77f40d82019-04-11 21:27:06 +02004866 /* The tests currently include inputs that should fail at either step.
4867 * Test cases that fail at the setup step should be changed to call
4868 * key_derivation_setup instead, and this function should be renamed
4869 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004870 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004871 if( status == PSA_SUCCESS )
4872 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004873 TEST_EQUAL( psa_key_derivation_key_agreement(
4874 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4875 our_key,
4876 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004877 expected_status );
4878 }
4879 else
4880 {
4881 TEST_ASSERT( status == expected_status );
4882 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004883
4884exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004885 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004886 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004887 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004888}
4889/* END_CASE */
4890
4891/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004892void raw_key_agreement( int alg_arg,
4893 int our_key_type_arg, data_t *our_key_data,
4894 data_t *peer_key_data,
4895 data_t *expected_output )
4896{
Ronald Cron5425a212020-08-04 14:58:35 +02004897 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004898 psa_algorithm_t alg = alg_arg;
4899 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004900 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004901 unsigned char *output = NULL;
4902 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004903 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004904
4905 ASSERT_ALLOC( output, expected_output->len );
4906 PSA_ASSERT( psa_crypto_init( ) );
4907
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004908 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4909 psa_set_key_algorithm( &attributes, alg );
4910 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004911 PSA_ASSERT( psa_import_key( &attributes,
4912 our_key_data->x, our_key_data->len,
4913 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004914
gabor-mezei-armceface22021-01-21 12:26:17 +01004915 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4916 key_bits = psa_get_key_bits( &attributes );
4917
Gilles Peskinebe697d82019-05-16 18:00:41 +02004918 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4919 peer_key_data->x, peer_key_data->len,
4920 output, expected_output->len,
4921 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004922 ASSERT_COMPARE( output, output_length,
4923 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01004924 TEST_ASSERT( output_length <=
4925 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
4926 TEST_ASSERT( output_length <=
4927 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004928
4929exit:
4930 mbedtls_free( output );
4931 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004932 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004933}
4934/* END_CASE */
4935
4936/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004937void key_agreement_capacity( int alg_arg,
4938 int our_key_type_arg, data_t *our_key_data,
4939 data_t *peer_key_data,
4940 int expected_capacity_arg )
4941{
Ronald Cron5425a212020-08-04 14:58:35 +02004942 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004943 psa_algorithm_t alg = alg_arg;
4944 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004945 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004946 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004947 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004948 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004949
Gilles Peskine8817f612018-12-18 00:18:46 +01004950 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004951
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004952 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4953 psa_set_key_algorithm( &attributes, alg );
4954 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004955 PSA_ASSERT( psa_import_key( &attributes,
4956 our_key_data->x, our_key_data->len,
4957 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004958
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004959 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004960 PSA_ASSERT( psa_key_derivation_key_agreement(
4961 &operation,
4962 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4963 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004964 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4965 {
4966 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004967 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004968 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004969 NULL, 0 ) );
4970 }
Gilles Peskine59685592018-09-18 12:11:34 +02004971
Gilles Peskinebf491972018-10-25 22:36:12 +02004972 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004973 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004974 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004975 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004976
Gilles Peskinebf491972018-10-25 22:36:12 +02004977 /* Test the actual capacity by reading the output. */
4978 while( actual_capacity > sizeof( output ) )
4979 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004980 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004981 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004982 actual_capacity -= sizeof( output );
4983 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004984 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004985 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004986 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004987 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004988
Gilles Peskine59685592018-09-18 12:11:34 +02004989exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004990 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004991 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004992 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004993}
4994/* END_CASE */
4995
4996/* BEGIN_CASE */
4997void key_agreement_output( int alg_arg,
4998 int our_key_type_arg, data_t *our_key_data,
4999 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005000 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005001{
Ronald Cron5425a212020-08-04 14:58:35 +02005002 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005003 psa_algorithm_t alg = alg_arg;
5004 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005005 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005006 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005007 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005008
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005009 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5010 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005011
Gilles Peskine8817f612018-12-18 00:18:46 +01005012 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005013
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005014 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5015 psa_set_key_algorithm( &attributes, alg );
5016 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005017 PSA_ASSERT( psa_import_key( &attributes,
5018 our_key_data->x, our_key_data->len,
5019 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005020
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005021 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005022 PSA_ASSERT( psa_key_derivation_key_agreement(
5023 &operation,
5024 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5025 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005026 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5027 {
5028 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005029 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005030 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005031 NULL, 0 ) );
5032 }
Gilles Peskine59685592018-09-18 12:11:34 +02005033
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005034 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005035 actual_output,
5036 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005037 ASSERT_COMPARE( actual_output, expected_output1->len,
5038 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005039 if( expected_output2->len != 0 )
5040 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005041 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005042 actual_output,
5043 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005044 ASSERT_COMPARE( actual_output, expected_output2->len,
5045 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005046 }
Gilles Peskine59685592018-09-18 12:11:34 +02005047
5048exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005049 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005050 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005051 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005052 mbedtls_free( actual_output );
5053}
5054/* END_CASE */
5055
5056/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005057void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005058{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005059 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005060 unsigned char *output = NULL;
5061 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005062 size_t i;
5063 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005064
Simon Butcher49f8e312020-03-03 15:51:50 +00005065 TEST_ASSERT( bytes_arg >= 0 );
5066
Gilles Peskine91892022021-02-08 19:50:26 +01005067 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005068 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005069
Gilles Peskine8817f612018-12-18 00:18:46 +01005070 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005071
Gilles Peskinea50d7392018-06-21 10:22:13 +02005072 /* Run several times, to ensure that every output byte will be
5073 * nonzero at least once with overwhelming probability
5074 * (2^(-8*number_of_runs)). */
5075 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005076 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005077 if( bytes != 0 )
5078 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005079 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005080
Gilles Peskinea50d7392018-06-21 10:22:13 +02005081 for( i = 0; i < bytes; i++ )
5082 {
5083 if( output[i] != 0 )
5084 ++changed[i];
5085 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005086 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005087
5088 /* Check that every byte was changed to nonzero at least once. This
5089 * validates that psa_generate_random is overwriting every byte of
5090 * the output buffer. */
5091 for( i = 0; i < bytes; i++ )
5092 {
5093 TEST_ASSERT( changed[i] != 0 );
5094 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005095
5096exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005097 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005098 mbedtls_free( output );
5099 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005100}
5101/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005102
5103/* BEGIN_CASE */
5104void generate_key( int type_arg,
5105 int bits_arg,
5106 int usage_arg,
5107 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005108 int expected_status_arg,
5109 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005110{
Ronald Cron5425a212020-08-04 14:58:35 +02005111 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005112 psa_key_type_t type = type_arg;
5113 psa_key_usage_t usage = usage_arg;
5114 size_t bits = bits_arg;
5115 psa_algorithm_t alg = alg_arg;
5116 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005117 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005118 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005119
Gilles Peskine8817f612018-12-18 00:18:46 +01005120 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005121
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005122 psa_set_key_usage_flags( &attributes, usage );
5123 psa_set_key_algorithm( &attributes, alg );
5124 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005125 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005126
5127 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005128 psa_status_t status = psa_generate_key( &attributes, &key );
5129
5130 if( is_large_key > 0 )
5131 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5132 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005133 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005134 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005135
5136 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005137 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005138 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5139 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005140
Gilles Peskine818ca122018-06-20 18:16:48 +02005141 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005142 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005143 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005144
5145exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005146 /*
5147 * Key attributes may have been returned by psa_get_key_attributes()
5148 * thus reset them as required.
5149 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005150 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005151
Ronald Cron5425a212020-08-04 14:58:35 +02005152 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005153 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005154}
5155/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005156
Ronald Cronee414c72021-03-18 18:50:08 +01005157/* 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 +02005158void generate_key_rsa( int bits_arg,
5159 data_t *e_arg,
5160 int expected_status_arg )
5161{
Ronald Cron5425a212020-08-04 14:58:35 +02005162 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005163 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005164 size_t bits = bits_arg;
5165 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5166 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5167 psa_status_t expected_status = expected_status_arg;
5168 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5169 uint8_t *exported = NULL;
5170 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005171 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005172 size_t exported_length = SIZE_MAX;
5173 uint8_t *e_read_buffer = NULL;
5174 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005175 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005176 size_t e_read_length = SIZE_MAX;
5177
5178 if( e_arg->len == 0 ||
5179 ( e_arg->len == 3 &&
5180 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5181 {
5182 is_default_public_exponent = 1;
5183 e_read_size = 0;
5184 }
5185 ASSERT_ALLOC( e_read_buffer, e_read_size );
5186 ASSERT_ALLOC( exported, exported_size );
5187
5188 PSA_ASSERT( psa_crypto_init( ) );
5189
5190 psa_set_key_usage_flags( &attributes, usage );
5191 psa_set_key_algorithm( &attributes, alg );
5192 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5193 e_arg->x, e_arg->len ) );
5194 psa_set_key_bits( &attributes, bits );
5195
5196 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005197 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005198 if( expected_status != PSA_SUCCESS )
5199 goto exit;
5200
5201 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005202 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005203 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5204 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5205 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5206 e_read_buffer, e_read_size,
5207 &e_read_length ) );
5208 if( is_default_public_exponent )
5209 TEST_EQUAL( e_read_length, 0 );
5210 else
5211 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5212
5213 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005214 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005215 goto exit;
5216
5217 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005218 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005219 exported, exported_size,
5220 &exported_length ) );
5221 {
5222 uint8_t *p = exported;
5223 uint8_t *end = exported + exported_length;
5224 size_t len;
5225 /* RSAPublicKey ::= SEQUENCE {
5226 * modulus INTEGER, -- n
5227 * publicExponent INTEGER } -- e
5228 */
5229 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005230 MBEDTLS_ASN1_SEQUENCE |
5231 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005232 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005233 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5234 MBEDTLS_ASN1_INTEGER ) );
5235 if( len >= 1 && p[0] == 0 )
5236 {
5237 ++p;
5238 --len;
5239 }
5240 if( e_arg->len == 0 )
5241 {
5242 TEST_EQUAL( len, 3 );
5243 TEST_EQUAL( p[0], 1 );
5244 TEST_EQUAL( p[1], 0 );
5245 TEST_EQUAL( p[2], 1 );
5246 }
5247 else
5248 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5249 }
5250
5251exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005252 /*
5253 * Key attributes may have been returned by psa_get_key_attributes() or
5254 * set by psa_set_key_domain_parameters() thus reset them as required.
5255 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005256 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005257
Ronald Cron5425a212020-08-04 14:58:35 +02005258 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005259 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005260 mbedtls_free( e_read_buffer );
5261 mbedtls_free( exported );
5262}
5263/* END_CASE */
5264
Darryl Greend49a4992018-06-18 17:27:26 +01005265/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005266void persistent_key_load_key_from_storage( data_t *data,
5267 int type_arg, int bits_arg,
5268 int usage_flags_arg, int alg_arg,
5269 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005270{
Ronald Cron71016a92020-08-28 19:01:50 +02005271 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005272 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005273 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5274 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005275 psa_key_type_t type = type_arg;
5276 size_t bits = bits_arg;
5277 psa_key_usage_t usage_flags = usage_flags_arg;
5278 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005279 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005280 unsigned char *first_export = NULL;
5281 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005282 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005283 size_t first_exported_length;
5284 size_t second_exported_length;
5285
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005286 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5287 {
5288 ASSERT_ALLOC( first_export, export_size );
5289 ASSERT_ALLOC( second_export, export_size );
5290 }
Darryl Greend49a4992018-06-18 17:27:26 +01005291
Gilles Peskine8817f612018-12-18 00:18:46 +01005292 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005293
Gilles Peskinec87af662019-05-15 16:12:22 +02005294 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005295 psa_set_key_usage_flags( &attributes, usage_flags );
5296 psa_set_key_algorithm( &attributes, alg );
5297 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005298 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005299
Darryl Green0c6575a2018-11-07 16:05:30 +00005300 switch( generation_method )
5301 {
5302 case IMPORT_KEY:
5303 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005304 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005305 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005306 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005307
Darryl Green0c6575a2018-11-07 16:05:30 +00005308 case GENERATE_KEY:
5309 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005310 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005311 break;
5312
5313 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005314#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005315 {
5316 /* Create base key */
5317 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5318 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5319 psa_set_key_usage_flags( &base_attributes,
5320 PSA_KEY_USAGE_DERIVE );
5321 psa_set_key_algorithm( &base_attributes, derive_alg );
5322 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005323 PSA_ASSERT( psa_import_key( &base_attributes,
5324 data->x, data->len,
5325 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005326 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005327 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005328 PSA_ASSERT( psa_key_derivation_input_key(
5329 &operation,
5330 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005331 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005332 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005333 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005334 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5335 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005336 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005337 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005338 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005339 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005340 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005341#else
5342 TEST_ASSUME( ! "KDF not supported in this configuration" );
5343#endif
5344 break;
5345
5346 default:
5347 TEST_ASSERT( ! "generation_method not implemented in test" );
5348 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005349 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005350 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005351
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005352 /* Export the key if permitted by the key policy. */
5353 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5354 {
Ronald Cron5425a212020-08-04 14:58:35 +02005355 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005356 first_export, export_size,
5357 &first_exported_length ) );
5358 if( generation_method == IMPORT_KEY )
5359 ASSERT_COMPARE( data->x, data->len,
5360 first_export, first_exported_length );
5361 }
Darryl Greend49a4992018-06-18 17:27:26 +01005362
5363 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005364 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005365 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005366 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005367
Darryl Greend49a4992018-06-18 17:27:26 +01005368 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005369 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005370 TEST_ASSERT( mbedtls_svc_key_id_equal(
5371 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005372 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5373 PSA_KEY_LIFETIME_PERSISTENT );
5374 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5375 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02005376 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02005377 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005378 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005379
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005380 /* Export the key again if permitted by the key policy. */
5381 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005382 {
Ronald Cron5425a212020-08-04 14:58:35 +02005383 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005384 second_export, export_size,
5385 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005386 ASSERT_COMPARE( first_export, first_exported_length,
5387 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005388 }
5389
5390 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005391 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005392 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005393
5394exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005395 /*
5396 * Key attributes may have been returned by psa_get_key_attributes()
5397 * thus reset them as required.
5398 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005399 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005400
Darryl Greend49a4992018-06-18 17:27:26 +01005401 mbedtls_free( first_export );
5402 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005403 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005404 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005405 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005406 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005407}
5408/* END_CASE */