blob: 8df2ceafef3af704f0c39165b077fe2bcb520ced [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
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +0200825 /* Calculate the MAC, one-shot case. */
826 uint8_t input[128] = {0};
827 size_t mac_len;
828 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
829 input, 128,
830 mac, PSA_MAC_MAX_SIZE, &mac_len ),
831 expected_status_sign );
832
833 /* Verify correct MAC, one-shot case. */
834 status = psa_mac_verify( key, exercise_alg, input, 128,
835 mac, mac_len );
836
837 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
838 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
839 else
840 TEST_EQUAL( status, expected_status_verify );
841
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200842 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200843
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200844 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200845 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +0200846 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200847
848exit:
849 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200850 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200851 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200852}
853/* END_CASE */
854
855/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200856void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200857 int policy_alg,
858 int key_type,
859 data_t *key_data,
860 int exercise_alg )
861{
Ronald Cron5425a212020-08-04 14:58:35 +0200862 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200863 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000864 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200865 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200866 psa_status_t status;
867
Gilles Peskine8817f612018-12-18 00:18:46 +0100868 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200869
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200870 psa_set_key_usage_flags( &attributes, policy_usage );
871 psa_set_key_algorithm( &attributes, policy_alg );
872 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200873
Gilles Peskine049c7532019-05-15 20:22:09 +0200874 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200875 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200876
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200877 /* Check if no key usage flag implication is done */
878 TEST_EQUAL( policy_usage,
879 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200880
Ronald Cron5425a212020-08-04 14:58:35 +0200881 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200882 if( policy_alg == exercise_alg &&
883 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100884 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200885 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100886 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200887 psa_cipher_abort( &operation );
888
Ronald Cron5425a212020-08-04 14:58:35 +0200889 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200890 if( policy_alg == exercise_alg &&
891 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100892 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200893 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100894 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200895
896exit:
897 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200898 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200899 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200900}
901/* END_CASE */
902
903/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200904void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200905 int policy_alg,
906 int key_type,
907 data_t *key_data,
908 int nonce_length_arg,
909 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100910 int exercise_alg,
911 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200912{
Ronald Cron5425a212020-08-04 14:58:35 +0200913 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200914 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200915 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200916 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100917 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200918 unsigned char nonce[16] = {0};
919 size_t nonce_length = nonce_length_arg;
920 unsigned char tag[16];
921 size_t tag_length = tag_length_arg;
922 size_t output_length;
923
924 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
925 TEST_ASSERT( tag_length <= sizeof( tag ) );
926
Gilles Peskine8817f612018-12-18 00:18:46 +0100927 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200928
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200929 psa_set_key_usage_flags( &attributes, policy_usage );
930 psa_set_key_algorithm( &attributes, policy_alg );
931 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200932
Gilles Peskine049c7532019-05-15 20:22:09 +0200933 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200934 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200935
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200936 /* Check if no key usage implication is done */
937 TEST_EQUAL( policy_usage,
938 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200939
Ronald Cron5425a212020-08-04 14:58:35 +0200940 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200941 nonce, nonce_length,
942 NULL, 0,
943 NULL, 0,
944 tag, tag_length,
945 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100946 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
947 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200948 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100949 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200950
951 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200952 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200953 nonce, nonce_length,
954 NULL, 0,
955 tag, tag_length,
956 NULL, 0,
957 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100958 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
959 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
960 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100961 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200962 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100963 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200964
965exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200966 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200967 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200968}
969/* END_CASE */
970
971/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200972void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200973 int policy_alg,
974 int key_type,
975 data_t *key_data,
976 int exercise_alg )
977{
Ronald Cron5425a212020-08-04 14:58:35 +0200978 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200979 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200980 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200981 psa_status_t status;
982 size_t key_bits;
983 size_t buffer_length;
984 unsigned char *buffer = NULL;
985 size_t output_length;
986
Gilles Peskine8817f612018-12-18 00:18:46 +0100987 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200988
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200989 psa_set_key_usage_flags( &attributes, policy_usage );
990 psa_set_key_algorithm( &attributes, policy_alg );
991 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200992
Gilles Peskine049c7532019-05-15 20:22:09 +0200993 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200994 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200995
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200996 /* Check if no key usage implication is done */
997 TEST_EQUAL( policy_usage,
998 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200999
Ronald Cron5425a212020-08-04 14:58:35 +02001000 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001001 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001002 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1003 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001004 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001005
Ronald Cron5425a212020-08-04 14:58:35 +02001006 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001007 NULL, 0,
1008 NULL, 0,
1009 buffer, buffer_length,
1010 &output_length );
1011 if( policy_alg == exercise_alg &&
1012 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001013 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001014 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001015 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001016
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001017 if( buffer_length != 0 )
1018 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001019 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001020 buffer, buffer_length,
1021 NULL, 0,
1022 buffer, buffer_length,
1023 &output_length );
1024 if( policy_alg == exercise_alg &&
1025 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001026 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001027 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001028 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001029
1030exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001031 /*
1032 * Key attributes may have been returned by psa_get_key_attributes()
1033 * thus reset them as required.
1034 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001035 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001036
1037 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001038 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001039 mbedtls_free( buffer );
1040}
1041/* END_CASE */
1042
1043/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001044void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001045 int policy_alg,
1046 int key_type,
1047 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001048 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001049 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001050 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001051{
Ronald Cron5425a212020-08-04 14:58:35 +02001052 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001053 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001054 psa_key_usage_t policy_usage = policy_usage_arg;
1055 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001056 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001057 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1058 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1059 * compatible with the policy and `payload_length_arg` is supposed to be
1060 * a valid input length to sign. If `payload_length_arg <= 0`,
1061 * `exercise_alg` is supposed to be forbidden by the policy. */
1062 int compatible_alg = payload_length_arg > 0;
1063 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001064 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001065 size_t signature_length;
1066
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001067 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001068 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001069 TEST_EQUAL( expected_usage,
1070 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001071
Gilles Peskine8817f612018-12-18 00:18:46 +01001072 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001073
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001074 psa_set_key_usage_flags( &attributes, policy_usage );
1075 psa_set_key_algorithm( &attributes, policy_alg );
1076 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001077
Gilles Peskine049c7532019-05-15 20:22:09 +02001078 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001079 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001080
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001081 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1082
Ronald Cron5425a212020-08-04 14:58:35 +02001083 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001084 payload, payload_length,
1085 signature, sizeof( signature ),
1086 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001087 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001088 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001089 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001090 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001091
1092 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001093 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001094 payload, payload_length,
1095 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001096 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001097 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001098 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001099 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001100
gabor-mezei-armd851d682021-06-28 14:53:49 +02001101 if( PSA_ALG_IS_HASH_AND_SIGN( exercise_alg ) &&
1102 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001103 {
1104 status = psa_sign_message( key, exercise_alg,
1105 payload, payload_length,
1106 signature, sizeof( signature ),
1107 &signature_length );
1108 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1109 PSA_ASSERT( status );
1110 else
1111 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1112
1113 memset( signature, 0, sizeof( signature ) );
1114 status = psa_verify_message( key, exercise_alg,
1115 payload, payload_length,
1116 signature, sizeof( signature ) );
1117 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1118 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1119 else
1120 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1121 }
1122
Gilles Peskined5b33222018-06-18 22:20:03 +02001123exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001124 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001125 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001126}
1127/* END_CASE */
1128
Janos Follathba3fab92019-06-11 14:50:16 +01001129/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001130void derive_key_policy( int policy_usage,
1131 int policy_alg,
1132 int key_type,
1133 data_t *key_data,
1134 int exercise_alg )
1135{
Ronald Cron5425a212020-08-04 14:58:35 +02001136 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001137 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001138 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001139 psa_status_t status;
1140
Gilles Peskine8817f612018-12-18 00:18:46 +01001141 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001142
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001143 psa_set_key_usage_flags( &attributes, policy_usage );
1144 psa_set_key_algorithm( &attributes, policy_alg );
1145 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001146
Gilles Peskine049c7532019-05-15 20:22:09 +02001147 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001148 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001149
Janos Follathba3fab92019-06-11 14:50:16 +01001150 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1151
1152 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1153 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001154 {
Janos Follathba3fab92019-06-11 14:50:16 +01001155 PSA_ASSERT( psa_key_derivation_input_bytes(
1156 &operation,
1157 PSA_KEY_DERIVATION_INPUT_SEED,
1158 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001159 }
Janos Follathba3fab92019-06-11 14:50:16 +01001160
1161 status = psa_key_derivation_input_key( &operation,
1162 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001163 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001164
Gilles Peskineea0fb492018-07-12 17:17:20 +02001165 if( policy_alg == exercise_alg &&
1166 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001167 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001168 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001169 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001170
1171exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001172 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001173 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001174 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001175}
1176/* END_CASE */
1177
1178/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001179void agreement_key_policy( int policy_usage,
1180 int policy_alg,
1181 int key_type_arg,
1182 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001183 int exercise_alg,
1184 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001185{
Ronald Cron5425a212020-08-04 14:58:35 +02001186 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001187 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001188 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001189 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001190 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001191 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001192
Gilles Peskine8817f612018-12-18 00:18:46 +01001193 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001194
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001195 psa_set_key_usage_flags( &attributes, policy_usage );
1196 psa_set_key_algorithm( &attributes, policy_alg );
1197 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001198
Gilles Peskine049c7532019-05-15 20:22:09 +02001199 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001200 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001201
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001202 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001203 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001204
Steven Cooremance48e852020-10-05 16:02:45 +02001205 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001206
1207exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001208 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001209 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001210 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001211}
1212/* END_CASE */
1213
1214/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001215void key_policy_alg2( int key_type_arg, data_t *key_data,
1216 int usage_arg, int alg_arg, int alg2_arg )
1217{
Ronald Cron5425a212020-08-04 14:58:35 +02001218 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001219 psa_key_type_t key_type = key_type_arg;
1220 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1221 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1222 psa_key_usage_t usage = usage_arg;
1223 psa_algorithm_t alg = alg_arg;
1224 psa_algorithm_t alg2 = alg2_arg;
1225
1226 PSA_ASSERT( psa_crypto_init( ) );
1227
1228 psa_set_key_usage_flags( &attributes, usage );
1229 psa_set_key_algorithm( &attributes, alg );
1230 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1231 psa_set_key_type( &attributes, key_type );
1232 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001233 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001234
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001235 /* Update the usage flags to obtain implicit usage flags */
1236 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001237 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001238 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1239 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1240 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1241
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001242 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001243 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001244 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001245 goto exit;
1246
1247exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001248 /*
1249 * Key attributes may have been returned by psa_get_key_attributes()
1250 * thus reset them as required.
1251 */
1252 psa_reset_key_attributes( &got_attributes );
1253
Ronald Cron5425a212020-08-04 14:58:35 +02001254 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001255 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001256}
1257/* END_CASE */
1258
1259/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001260void raw_agreement_key_policy( int policy_usage,
1261 int policy_alg,
1262 int key_type_arg,
1263 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001264 int exercise_alg,
1265 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001266{
Ronald Cron5425a212020-08-04 14:58:35 +02001267 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001268 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001269 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001270 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001271 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001272 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001273
1274 PSA_ASSERT( psa_crypto_init( ) );
1275
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001276 psa_set_key_usage_flags( &attributes, policy_usage );
1277 psa_set_key_algorithm( &attributes, policy_alg );
1278 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001279
Gilles Peskine049c7532019-05-15 20:22:09 +02001280 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001281 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001282
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001283 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001284
Steven Cooremance48e852020-10-05 16:02:45 +02001285 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001286
1287exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001288 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001289 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001290 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001291}
1292/* END_CASE */
1293
1294/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001295void copy_success( int source_usage_arg,
1296 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001297 int type_arg, data_t *material,
1298 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001299 int target_usage_arg,
1300 int target_alg_arg, int target_alg2_arg,
1301 int expected_usage_arg,
1302 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001303{
Gilles Peskineca25db92019-04-19 11:43:08 +02001304 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1305 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001306 psa_key_usage_t expected_usage = expected_usage_arg;
1307 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001308 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001309 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1310 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001311 uint8_t *export_buffer = NULL;
1312
Gilles Peskine57ab7212019-01-28 13:03:09 +01001313 PSA_ASSERT( psa_crypto_init( ) );
1314
Gilles Peskineca25db92019-04-19 11:43:08 +02001315 /* Prepare the source key. */
1316 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1317 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001318 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001319 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001320 PSA_ASSERT( psa_import_key( &source_attributes,
1321 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001322 &source_key ) );
1323 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001324
Gilles Peskineca25db92019-04-19 11:43:08 +02001325 /* Prepare the target attributes. */
1326 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001327 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001328 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001329 /* Set volatile lifetime to reset the key identifier to 0. */
1330 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1331 }
1332
Gilles Peskineca25db92019-04-19 11:43:08 +02001333 if( target_usage_arg != -1 )
1334 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1335 if( target_alg_arg != -1 )
1336 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001337 if( target_alg2_arg != -1 )
1338 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001339
1340 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001341 PSA_ASSERT( psa_copy_key( source_key,
1342 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001343
1344 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001345 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001346
1347 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001348 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001349 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1350 psa_get_key_type( &target_attributes ) );
1351 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1352 psa_get_key_bits( &target_attributes ) );
1353 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1354 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001355 TEST_EQUAL( expected_alg2,
1356 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001357 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1358 {
1359 size_t length;
1360 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001361 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001362 material->len, &length ) );
1363 ASSERT_COMPARE( material->x, material->len,
1364 export_buffer, length );
1365 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001366
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001367 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001368 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001369 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001370 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001371
Ronald Cron5425a212020-08-04 14:58:35 +02001372 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001373
1374exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001375 /*
1376 * Source and target key attributes may have been returned by
1377 * psa_get_key_attributes() thus reset them as required.
1378 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001379 psa_reset_key_attributes( &source_attributes );
1380 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001381
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001382 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001383 mbedtls_free( export_buffer );
1384}
1385/* END_CASE */
1386
1387/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001388void copy_fail( int source_usage_arg,
1389 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001390 int type_arg, data_t *material,
1391 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001392 int target_usage_arg,
1393 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001394 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001395 int expected_status_arg )
1396{
1397 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1398 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001399 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1400 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001401 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001402
1403 PSA_ASSERT( psa_crypto_init( ) );
1404
1405 /* Prepare the source key. */
1406 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1407 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001408 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001409 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001410 PSA_ASSERT( psa_import_key( &source_attributes,
1411 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001412 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001413
1414 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001415 psa_set_key_id( &target_attributes, key_id );
1416 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001417 psa_set_key_type( &target_attributes, target_type_arg );
1418 psa_set_key_bits( &target_attributes, target_bits_arg );
1419 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1420 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001421 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001422
1423 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001424 TEST_EQUAL( psa_copy_key( source_key,
1425 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001426 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001427
Ronald Cron5425a212020-08-04 14:58:35 +02001428 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001429
Gilles Peskine4a644642019-05-03 17:14:08 +02001430exit:
1431 psa_reset_key_attributes( &source_attributes );
1432 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001433 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001434}
1435/* END_CASE */
1436
1437/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001438void hash_operation_init( )
1439{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001440 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001441 /* Test each valid way of initializing the object, except for `= {0}`, as
1442 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1443 * though it's OK by the C standard. We could test for this, but we'd need
1444 * to supress the Clang warning for the test. */
1445 psa_hash_operation_t func = psa_hash_operation_init( );
1446 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1447 psa_hash_operation_t zero;
1448
1449 memset( &zero, 0, sizeof( zero ) );
1450
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001451 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001452 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1453 PSA_ERROR_BAD_STATE );
1454 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1455 PSA_ERROR_BAD_STATE );
1456 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1457 PSA_ERROR_BAD_STATE );
1458
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001459 /* A default hash operation should be abortable without error. */
1460 PSA_ASSERT( psa_hash_abort( &func ) );
1461 PSA_ASSERT( psa_hash_abort( &init ) );
1462 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001463}
1464/* END_CASE */
1465
1466/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001467void hash_setup( int alg_arg,
1468 int expected_status_arg )
1469{
1470 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001471 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001472 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001473 psa_status_t status;
1474
Gilles Peskine8817f612018-12-18 00:18:46 +01001475 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001476
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001477 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001478 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001479
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001480 /* Whether setup succeeded or failed, abort must succeed. */
1481 PSA_ASSERT( psa_hash_abort( &operation ) );
1482
1483 /* If setup failed, reproduce the failure, so as to
1484 * test the resulting state of the operation object. */
1485 if( status != PSA_SUCCESS )
1486 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1487
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001488 /* Now the operation object should be reusable. */
1489#if defined(KNOWN_SUPPORTED_HASH_ALG)
1490 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1491 PSA_ASSERT( psa_hash_abort( &operation ) );
1492#endif
1493
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001494exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001495 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001496}
1497/* END_CASE */
1498
1499/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001500void hash_compute_fail( int alg_arg, data_t *input,
1501 int output_size_arg, int expected_status_arg )
1502{
1503 psa_algorithm_t alg = alg_arg;
1504 uint8_t *output = NULL;
1505 size_t output_size = output_size_arg;
1506 size_t output_length = INVALID_EXPORT_LENGTH;
1507 psa_status_t expected_status = expected_status_arg;
1508 psa_status_t status;
1509
1510 ASSERT_ALLOC( output, output_size );
1511
1512 PSA_ASSERT( psa_crypto_init( ) );
1513
1514 status = psa_hash_compute( alg, input->x, input->len,
1515 output, output_size, &output_length );
1516 TEST_EQUAL( status, expected_status );
1517 TEST_ASSERT( output_length <= output_size );
1518
1519exit:
1520 mbedtls_free( output );
1521 PSA_DONE( );
1522}
1523/* END_CASE */
1524
1525/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001526void hash_compare_fail( int alg_arg, data_t *input,
1527 data_t *reference_hash,
1528 int expected_status_arg )
1529{
1530 psa_algorithm_t alg = alg_arg;
1531 psa_status_t expected_status = expected_status_arg;
1532 psa_status_t status;
1533
1534 PSA_ASSERT( psa_crypto_init( ) );
1535
1536 status = psa_hash_compare( alg, input->x, input->len,
1537 reference_hash->x, reference_hash->len );
1538 TEST_EQUAL( status, expected_status );
1539
1540exit:
1541 PSA_DONE( );
1542}
1543/* END_CASE */
1544
1545/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001546void hash_compute_compare( int alg_arg, data_t *input,
1547 data_t *expected_output )
1548{
1549 psa_algorithm_t alg = alg_arg;
1550 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1551 size_t output_length = INVALID_EXPORT_LENGTH;
1552 size_t i;
1553
1554 PSA_ASSERT( psa_crypto_init( ) );
1555
1556 /* Compute with tight buffer */
1557 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001558 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001559 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001560 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001561 ASSERT_COMPARE( output, output_length,
1562 expected_output->x, expected_output->len );
1563
1564 /* Compute with larger buffer */
1565 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1566 output, sizeof( output ),
1567 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001568 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001569 ASSERT_COMPARE( output, output_length,
1570 expected_output->x, expected_output->len );
1571
1572 /* Compare with correct hash */
1573 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1574 output, output_length ) );
1575
1576 /* Compare with trailing garbage */
1577 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1578 output, output_length + 1 ),
1579 PSA_ERROR_INVALID_SIGNATURE );
1580
1581 /* Compare with truncated hash */
1582 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1583 output, output_length - 1 ),
1584 PSA_ERROR_INVALID_SIGNATURE );
1585
1586 /* Compare with corrupted value */
1587 for( i = 0; i < output_length; i++ )
1588 {
Chris Jones9634bb12021-01-20 15:56:42 +00001589 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001590 output[i] ^= 1;
1591 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1592 output, output_length ),
1593 PSA_ERROR_INVALID_SIGNATURE );
1594 output[i] ^= 1;
1595 }
1596
1597exit:
1598 PSA_DONE( );
1599}
1600/* END_CASE */
1601
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001602/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001603void hash_bad_order( )
1604{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001605 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001606 unsigned char input[] = "";
1607 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001608 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001609 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1610 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1611 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001612 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001613 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001614 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001615
Gilles Peskine8817f612018-12-18 00:18:46 +01001616 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001617
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001618 /* Call setup twice in a row. */
1619 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001620 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001621 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1622 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001623 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001624 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001625 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001626
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001627 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001628 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001629 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001630 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001631
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001632 /* Check that update calls abort on error. */
1633 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01001634 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001635 ASSERT_OPERATION_IS_ACTIVE( operation );
1636 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
1637 PSA_ERROR_BAD_STATE );
1638 ASSERT_OPERATION_IS_INACTIVE( operation );
1639 PSA_ASSERT( psa_hash_abort( &operation ) );
1640 ASSERT_OPERATION_IS_INACTIVE( operation );
1641
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001642 /* Call update after finish. */
1643 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1644 PSA_ASSERT( psa_hash_finish( &operation,
1645 hash, sizeof( hash ), &hash_len ) );
1646 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001647 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001648 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001649
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001650 /* Call verify without calling setup beforehand. */
1651 TEST_EQUAL( psa_hash_verify( &operation,
1652 valid_hash, sizeof( valid_hash ) ),
1653 PSA_ERROR_BAD_STATE );
1654 PSA_ASSERT( psa_hash_abort( &operation ) );
1655
1656 /* Call verify after finish. */
1657 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1658 PSA_ASSERT( psa_hash_finish( &operation,
1659 hash, sizeof( hash ), &hash_len ) );
1660 TEST_EQUAL( psa_hash_verify( &operation,
1661 valid_hash, sizeof( valid_hash ) ),
1662 PSA_ERROR_BAD_STATE );
1663 PSA_ASSERT( psa_hash_abort( &operation ) );
1664
1665 /* Call verify twice in a row. */
1666 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001667 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001668 PSA_ASSERT( psa_hash_verify( &operation,
1669 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001670 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001671 TEST_EQUAL( psa_hash_verify( &operation,
1672 valid_hash, sizeof( valid_hash ) ),
1673 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001674 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001675 PSA_ASSERT( psa_hash_abort( &operation ) );
1676
1677 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001678 TEST_EQUAL( psa_hash_finish( &operation,
1679 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001680 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001681 PSA_ASSERT( psa_hash_abort( &operation ) );
1682
1683 /* Call finish twice in a row. */
1684 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1685 PSA_ASSERT( psa_hash_finish( &operation,
1686 hash, sizeof( hash ), &hash_len ) );
1687 TEST_EQUAL( psa_hash_finish( &operation,
1688 hash, sizeof( hash ), &hash_len ),
1689 PSA_ERROR_BAD_STATE );
1690 PSA_ASSERT( psa_hash_abort( &operation ) );
1691
1692 /* Call finish after calling verify. */
1693 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1694 PSA_ASSERT( psa_hash_verify( &operation,
1695 valid_hash, sizeof( valid_hash ) ) );
1696 TEST_EQUAL( psa_hash_finish( &operation,
1697 hash, sizeof( hash ), &hash_len ),
1698 PSA_ERROR_BAD_STATE );
1699 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001700
1701exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001702 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001703}
1704/* END_CASE */
1705
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001706/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001707void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001708{
1709 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001710 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1711 * appended to it */
1712 unsigned char hash[] = {
1713 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1714 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1715 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001716 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001717 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001718
Gilles Peskine8817f612018-12-18 00:18:46 +01001719 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001720
itayzafrir27e69452018-11-01 14:26:34 +02001721 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001722 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001723 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001724 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001725 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001726 ASSERT_OPERATION_IS_INACTIVE( operation );
1727 PSA_ASSERT( psa_hash_abort( &operation ) );
1728 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03001729
itayzafrir27e69452018-11-01 14:26:34 +02001730 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001731 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001732 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001733 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001734
itayzafrir27e69452018-11-01 14:26:34 +02001735 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001736 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001737 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001738 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001739
itayzafrirec93d302018-10-18 18:01:10 +03001740exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001741 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001742}
1743/* END_CASE */
1744
Ronald Cronee414c72021-03-18 18:50:08 +01001745/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001746void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001747{
1748 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001749 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001750 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001751 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001752 size_t hash_len;
1753
Gilles Peskine8817f612018-12-18 00:18:46 +01001754 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001755
itayzafrir58028322018-10-25 10:22:01 +03001756 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001757 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001758 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001759 hash, expected_size - 1, &hash_len ),
1760 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001761
1762exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001763 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001764}
1765/* END_CASE */
1766
Ronald Cronee414c72021-03-18 18:50:08 +01001767/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001768void hash_clone_source_state( )
1769{
1770 psa_algorithm_t alg = PSA_ALG_SHA_256;
1771 unsigned char hash[PSA_HASH_MAX_SIZE];
1772 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1773 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1774 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1775 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1776 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1777 size_t hash_len;
1778
1779 PSA_ASSERT( psa_crypto_init( ) );
1780 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1781
1782 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1783 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1784 PSA_ASSERT( psa_hash_finish( &op_finished,
1785 hash, sizeof( hash ), &hash_len ) );
1786 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1787 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1788
1789 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1790 PSA_ERROR_BAD_STATE );
1791
1792 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1793 PSA_ASSERT( psa_hash_finish( &op_init,
1794 hash, sizeof( hash ), &hash_len ) );
1795 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1796 PSA_ASSERT( psa_hash_finish( &op_finished,
1797 hash, sizeof( hash ), &hash_len ) );
1798 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1799 PSA_ASSERT( psa_hash_finish( &op_aborted,
1800 hash, sizeof( hash ), &hash_len ) );
1801
1802exit:
1803 psa_hash_abort( &op_source );
1804 psa_hash_abort( &op_init );
1805 psa_hash_abort( &op_setup );
1806 psa_hash_abort( &op_finished );
1807 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001808 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001809}
1810/* END_CASE */
1811
Ronald Cronee414c72021-03-18 18:50:08 +01001812/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001813void hash_clone_target_state( )
1814{
1815 psa_algorithm_t alg = PSA_ALG_SHA_256;
1816 unsigned char hash[PSA_HASH_MAX_SIZE];
1817 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1818 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1819 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1820 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1821 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1822 size_t hash_len;
1823
1824 PSA_ASSERT( psa_crypto_init( ) );
1825
1826 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1827 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1828 PSA_ASSERT( psa_hash_finish( &op_finished,
1829 hash, sizeof( hash ), &hash_len ) );
1830 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1831 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1832
1833 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1834 PSA_ASSERT( psa_hash_finish( &op_target,
1835 hash, sizeof( hash ), &hash_len ) );
1836
1837 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1838 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1839 PSA_ERROR_BAD_STATE );
1840 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1841 PSA_ERROR_BAD_STATE );
1842
1843exit:
1844 psa_hash_abort( &op_target );
1845 psa_hash_abort( &op_init );
1846 psa_hash_abort( &op_setup );
1847 psa_hash_abort( &op_finished );
1848 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001849 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001850}
1851/* END_CASE */
1852
itayzafrir58028322018-10-25 10:22:01 +03001853/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001854void mac_operation_init( )
1855{
Jaeden Amero252ef282019-02-15 14:05:35 +00001856 const uint8_t input[1] = { 0 };
1857
Jaeden Amero769ce272019-01-04 11:48:03 +00001858 /* Test each valid way of initializing the object, except for `= {0}`, as
1859 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1860 * though it's OK by the C standard. We could test for this, but we'd need
1861 * to supress the Clang warning for the test. */
1862 psa_mac_operation_t func = psa_mac_operation_init( );
1863 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1864 psa_mac_operation_t zero;
1865
1866 memset( &zero, 0, sizeof( zero ) );
1867
Jaeden Amero252ef282019-02-15 14:05:35 +00001868 /* A freshly-initialized MAC operation should not be usable. */
1869 TEST_EQUAL( psa_mac_update( &func,
1870 input, sizeof( input ) ),
1871 PSA_ERROR_BAD_STATE );
1872 TEST_EQUAL( psa_mac_update( &init,
1873 input, sizeof( input ) ),
1874 PSA_ERROR_BAD_STATE );
1875 TEST_EQUAL( psa_mac_update( &zero,
1876 input, sizeof( input ) ),
1877 PSA_ERROR_BAD_STATE );
1878
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001879 /* A default MAC operation should be abortable without error. */
1880 PSA_ASSERT( psa_mac_abort( &func ) );
1881 PSA_ASSERT( psa_mac_abort( &init ) );
1882 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001883}
1884/* END_CASE */
1885
1886/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001887void mac_setup( int key_type_arg,
1888 data_t *key,
1889 int alg_arg,
1890 int expected_status_arg )
1891{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001892 psa_key_type_t key_type = key_type_arg;
1893 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001894 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001895 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001896 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1897#if defined(KNOWN_SUPPORTED_MAC_ALG)
1898 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1899#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001900
Gilles Peskine8817f612018-12-18 00:18:46 +01001901 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001902
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001903 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1904 &operation, &status ) )
1905 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001906 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001907
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001908 /* The operation object should be reusable. */
1909#if defined(KNOWN_SUPPORTED_MAC_ALG)
1910 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1911 smoke_test_key_data,
1912 sizeof( smoke_test_key_data ),
1913 KNOWN_SUPPORTED_MAC_ALG,
1914 &operation, &status ) )
1915 goto exit;
1916 TEST_EQUAL( status, PSA_SUCCESS );
1917#endif
1918
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001919exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001920 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001921}
1922/* END_CASE */
1923
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001924/* 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 +00001925void mac_bad_order( )
1926{
Ronald Cron5425a212020-08-04 14:58:35 +02001927 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001928 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1929 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001930 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001931 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1932 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1933 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001934 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001935 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1936 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1937 size_t sign_mac_length = 0;
1938 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1939 const uint8_t verify_mac[] = {
1940 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1941 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1942 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1943
1944 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001945 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001946 psa_set_key_algorithm( &attributes, alg );
1947 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001948
Ronald Cron5425a212020-08-04 14:58:35 +02001949 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1950 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001951
Jaeden Amero252ef282019-02-15 14:05:35 +00001952 /* Call update without calling setup beforehand. */
1953 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1954 PSA_ERROR_BAD_STATE );
1955 PSA_ASSERT( psa_mac_abort( &operation ) );
1956
1957 /* Call sign finish without calling setup beforehand. */
1958 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1959 &sign_mac_length),
1960 PSA_ERROR_BAD_STATE );
1961 PSA_ASSERT( psa_mac_abort( &operation ) );
1962
1963 /* Call verify finish without calling setup beforehand. */
1964 TEST_EQUAL( psa_mac_verify_finish( &operation,
1965 verify_mac, sizeof( verify_mac ) ),
1966 PSA_ERROR_BAD_STATE );
1967 PSA_ASSERT( psa_mac_abort( &operation ) );
1968
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001969 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001970 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001971 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001972 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001973 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001974 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001975 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001976 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001977
Jaeden Amero252ef282019-02-15 14:05:35 +00001978 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001979 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001980 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1981 PSA_ASSERT( psa_mac_sign_finish( &operation,
1982 sign_mac, sizeof( sign_mac ),
1983 &sign_mac_length ) );
1984 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1985 PSA_ERROR_BAD_STATE );
1986 PSA_ASSERT( psa_mac_abort( &operation ) );
1987
1988 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001989 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001990 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1991 PSA_ASSERT( psa_mac_verify_finish( &operation,
1992 verify_mac, sizeof( verify_mac ) ) );
1993 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1994 PSA_ERROR_BAD_STATE );
1995 PSA_ASSERT( psa_mac_abort( &operation ) );
1996
1997 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001998 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001999 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2000 PSA_ASSERT( psa_mac_sign_finish( &operation,
2001 sign_mac, sizeof( sign_mac ),
2002 &sign_mac_length ) );
2003 TEST_EQUAL( psa_mac_sign_finish( &operation,
2004 sign_mac, sizeof( sign_mac ),
2005 &sign_mac_length ),
2006 PSA_ERROR_BAD_STATE );
2007 PSA_ASSERT( psa_mac_abort( &operation ) );
2008
2009 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002010 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002011 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2012 PSA_ASSERT( psa_mac_verify_finish( &operation,
2013 verify_mac, sizeof( verify_mac ) ) );
2014 TEST_EQUAL( psa_mac_verify_finish( &operation,
2015 verify_mac, sizeof( verify_mac ) ),
2016 PSA_ERROR_BAD_STATE );
2017 PSA_ASSERT( psa_mac_abort( &operation ) );
2018
2019 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002020 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002021 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002022 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002023 TEST_EQUAL( psa_mac_verify_finish( &operation,
2024 verify_mac, sizeof( verify_mac ) ),
2025 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002026 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002027 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002028 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002029
2030 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002031 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002032 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002033 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002034 TEST_EQUAL( psa_mac_sign_finish( &operation,
2035 sign_mac, sizeof( sign_mac ),
2036 &sign_mac_length ),
2037 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002038 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002039 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002040 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002041
Ronald Cron5425a212020-08-04 14:58:35 +02002042 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002043
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002044exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002045 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002046}
2047/* END_CASE */
2048
2049/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002050void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002051 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002052 int alg_arg,
2053 data_t *input,
2054 data_t *expected_mac )
2055{
Ronald Cron5425a212020-08-04 14:58:35 +02002056 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002057 psa_key_type_t key_type = key_type_arg;
2058 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002059 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002060 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002061 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002062 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002063 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002064 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002065 const size_t output_sizes_to_test[] = {
2066 0,
2067 1,
2068 expected_mac->len - 1,
2069 expected_mac->len,
2070 expected_mac->len + 1,
2071 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002072
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002073 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002074 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002075 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002076
Gilles Peskine8817f612018-12-18 00:18:46 +01002077 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002078
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002079 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002080 psa_set_key_algorithm( &attributes, alg );
2081 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002082
Ronald Cron5425a212020-08-04 14:58:35 +02002083 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2084 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002085
Gilles Peskine8b356b52020-08-25 23:44:59 +02002086 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2087 {
2088 const size_t output_size = output_sizes_to_test[i];
2089 psa_status_t expected_status =
2090 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2091 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002092
Chris Jones9634bb12021-01-20 15:56:42 +00002093 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002094 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002095
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002096 /* Calculate the MAC, one-shot case. */
2097 TEST_EQUAL( psa_mac_compute( key, alg,
2098 input->x, input->len,
2099 actual_mac, output_size, &mac_length ),
2100 expected_status );
2101 if( expected_status == PSA_SUCCESS )
2102 {
2103 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2104 actual_mac, mac_length );
2105 }
2106
2107 if( output_size > 0 )
2108 memset( actual_mac, 0, output_size );
2109
2110 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002111 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002112 PSA_ASSERT( psa_mac_update( &operation,
2113 input->x, input->len ) );
2114 TEST_EQUAL( psa_mac_sign_finish( &operation,
2115 actual_mac, output_size,
2116 &mac_length ),
2117 expected_status );
2118 PSA_ASSERT( psa_mac_abort( &operation ) );
2119
2120 if( expected_status == PSA_SUCCESS )
2121 {
2122 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2123 actual_mac, mac_length );
2124 }
2125 mbedtls_free( actual_mac );
2126 actual_mac = NULL;
2127 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002128
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002129exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002130 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002131 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002132 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002133 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002134}
2135/* END_CASE */
2136
2137/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002138void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002139 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002140 int alg_arg,
2141 data_t *input,
2142 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002143{
Ronald Cron5425a212020-08-04 14:58:35 +02002144 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002145 psa_key_type_t key_type = key_type_arg;
2146 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002147 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002148 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002149 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002150
Gilles Peskine69c12672018-06-28 00:07:19 +02002151 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2152
Gilles Peskine8817f612018-12-18 00:18:46 +01002153 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002154
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002155 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002156 psa_set_key_algorithm( &attributes, alg );
2157 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002158
Ronald Cron5425a212020-08-04 14:58:35 +02002159 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2160 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002161
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002162 /* Verify correct MAC, one-shot case. */
2163 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2164 expected_mac->x, expected_mac->len ) );
2165
2166 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002167 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002168 PSA_ASSERT( psa_mac_update( &operation,
2169 input->x, input->len ) );
2170 PSA_ASSERT( psa_mac_verify_finish( &operation,
2171 expected_mac->x,
2172 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002173
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002174 /* Test a MAC that's too short, one-shot case. */
2175 TEST_EQUAL( psa_mac_verify( key, alg,
2176 input->x, input->len,
2177 expected_mac->x,
2178 expected_mac->len - 1 ),
2179 PSA_ERROR_INVALID_SIGNATURE );
2180
2181 /* Test a MAC that's too short, 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 expected_mac->x,
2187 expected_mac->len - 1 ),
2188 PSA_ERROR_INVALID_SIGNATURE );
2189
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002190 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002191 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2192 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002193 TEST_EQUAL( psa_mac_verify( key, alg,
2194 input->x, input->len,
2195 perturbed_mac, expected_mac->len + 1 ),
2196 PSA_ERROR_INVALID_SIGNATURE );
2197
2198 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002199 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002200 PSA_ASSERT( psa_mac_update( &operation,
2201 input->x, input->len ) );
2202 TEST_EQUAL( psa_mac_verify_finish( &operation,
2203 perturbed_mac,
2204 expected_mac->len + 1 ),
2205 PSA_ERROR_INVALID_SIGNATURE );
2206
2207 /* Test changing one byte. */
2208 for( size_t i = 0; i < expected_mac->len; i++ )
2209 {
Chris Jones9634bb12021-01-20 15:56:42 +00002210 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002211 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002212
2213 TEST_EQUAL( psa_mac_verify( key, alg,
2214 input->x, input->len,
2215 perturbed_mac, expected_mac->len ),
2216 PSA_ERROR_INVALID_SIGNATURE );
2217
Ronald Cron5425a212020-08-04 14:58:35 +02002218 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002219 PSA_ASSERT( psa_mac_update( &operation,
2220 input->x, input->len ) );
2221 TEST_EQUAL( psa_mac_verify_finish( &operation,
2222 perturbed_mac,
2223 expected_mac->len ),
2224 PSA_ERROR_INVALID_SIGNATURE );
2225 perturbed_mac[i] ^= 1;
2226 }
2227
Gilles Peskine8c9def32018-02-08 10:02:12 +01002228exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002229 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002230 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002231 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002232 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002233}
2234/* END_CASE */
2235
2236/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002237void cipher_operation_init( )
2238{
Jaeden Ameroab439972019-02-15 14:12:05 +00002239 const uint8_t input[1] = { 0 };
2240 unsigned char output[1] = { 0 };
2241 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002242 /* Test each valid way of initializing the object, except for `= {0}`, as
2243 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2244 * though it's OK by the C standard. We could test for this, but we'd need
2245 * to supress the Clang warning for the test. */
2246 psa_cipher_operation_t func = psa_cipher_operation_init( );
2247 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2248 psa_cipher_operation_t zero;
2249
2250 memset( &zero, 0, sizeof( zero ) );
2251
Jaeden Ameroab439972019-02-15 14:12:05 +00002252 /* A freshly-initialized cipher operation should not be usable. */
2253 TEST_EQUAL( psa_cipher_update( &func,
2254 input, sizeof( input ),
2255 output, sizeof( output ),
2256 &output_length ),
2257 PSA_ERROR_BAD_STATE );
2258 TEST_EQUAL( psa_cipher_update( &init,
2259 input, sizeof( input ),
2260 output, sizeof( output ),
2261 &output_length ),
2262 PSA_ERROR_BAD_STATE );
2263 TEST_EQUAL( psa_cipher_update( &zero,
2264 input, sizeof( input ),
2265 output, sizeof( output ),
2266 &output_length ),
2267 PSA_ERROR_BAD_STATE );
2268
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002269 /* A default cipher operation should be abortable without error. */
2270 PSA_ASSERT( psa_cipher_abort( &func ) );
2271 PSA_ASSERT( psa_cipher_abort( &init ) );
2272 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002273}
2274/* END_CASE */
2275
2276/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002277void cipher_setup( int key_type_arg,
2278 data_t *key,
2279 int alg_arg,
2280 int expected_status_arg )
2281{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002282 psa_key_type_t key_type = key_type_arg;
2283 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002284 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002285 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002286 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002287#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002288 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2289#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002290
Gilles Peskine8817f612018-12-18 00:18:46 +01002291 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002292
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002293 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2294 &operation, &status ) )
2295 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002296 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002297
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002298 /* The operation object should be reusable. */
2299#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2300 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2301 smoke_test_key_data,
2302 sizeof( smoke_test_key_data ),
2303 KNOWN_SUPPORTED_CIPHER_ALG,
2304 &operation, &status ) )
2305 goto exit;
2306 TEST_EQUAL( status, PSA_SUCCESS );
2307#endif
2308
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002309exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002310 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002311 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002312}
2313/* END_CASE */
2314
Ronald Cronee414c72021-03-18 18:50:08 +01002315/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002316void cipher_bad_order( )
2317{
Ronald Cron5425a212020-08-04 14:58:35 +02002318 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002319 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2320 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002321 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002322 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002323 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002324 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002325 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2326 0xaa, 0xaa, 0xaa, 0xaa };
2327 const uint8_t text[] = {
2328 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2329 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002330 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002331 size_t length = 0;
2332
2333 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002334 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2335 psa_set_key_algorithm( &attributes, alg );
2336 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002337 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2338 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002339
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002340 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002341 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002342 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002343 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002344 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002345 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002346 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002347 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002348
2349 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002350 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002351 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002352 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002353 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002354 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002355 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002356 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002357
Jaeden Ameroab439972019-02-15 14:12:05 +00002358 /* Generate an IV without calling setup beforehand. */
2359 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2360 buffer, sizeof( buffer ),
2361 &length ),
2362 PSA_ERROR_BAD_STATE );
2363 PSA_ASSERT( psa_cipher_abort( &operation ) );
2364
2365 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002366 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002367 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2368 buffer, sizeof( buffer ),
2369 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002370 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002371 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2372 buffer, sizeof( buffer ),
2373 &length ),
2374 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002375 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002376 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002377 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002378
2379 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002380 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002381 PSA_ASSERT( psa_cipher_set_iv( &operation,
2382 iv, sizeof( iv ) ) );
2383 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2384 buffer, sizeof( buffer ),
2385 &length ),
2386 PSA_ERROR_BAD_STATE );
2387 PSA_ASSERT( psa_cipher_abort( &operation ) );
2388
2389 /* Set an IV without calling setup beforehand. */
2390 TEST_EQUAL( psa_cipher_set_iv( &operation,
2391 iv, sizeof( iv ) ),
2392 PSA_ERROR_BAD_STATE );
2393 PSA_ASSERT( psa_cipher_abort( &operation ) );
2394
2395 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002396 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002397 PSA_ASSERT( psa_cipher_set_iv( &operation,
2398 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002399 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002400 TEST_EQUAL( psa_cipher_set_iv( &operation,
2401 iv, sizeof( iv ) ),
2402 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002403 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002404 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002405 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002406
2407 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002408 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002409 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2410 buffer, sizeof( buffer ),
2411 &length ) );
2412 TEST_EQUAL( psa_cipher_set_iv( &operation,
2413 iv, sizeof( iv ) ),
2414 PSA_ERROR_BAD_STATE );
2415 PSA_ASSERT( psa_cipher_abort( &operation ) );
2416
2417 /* Call update without calling setup beforehand. */
2418 TEST_EQUAL( psa_cipher_update( &operation,
2419 text, sizeof( text ),
2420 buffer, sizeof( buffer ),
2421 &length ),
2422 PSA_ERROR_BAD_STATE );
2423 PSA_ASSERT( psa_cipher_abort( &operation ) );
2424
2425 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01002426 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002427 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002428 TEST_EQUAL( psa_cipher_update( &operation,
2429 text, sizeof( text ),
2430 buffer, sizeof( buffer ),
2431 &length ),
2432 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002433 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002434 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002435 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002436
2437 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002438 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002439 PSA_ASSERT( psa_cipher_set_iv( &operation,
2440 iv, sizeof( iv ) ) );
2441 PSA_ASSERT( psa_cipher_finish( &operation,
2442 buffer, sizeof( buffer ), &length ) );
2443 TEST_EQUAL( psa_cipher_update( &operation,
2444 text, sizeof( text ),
2445 buffer, sizeof( buffer ),
2446 &length ),
2447 PSA_ERROR_BAD_STATE );
2448 PSA_ASSERT( psa_cipher_abort( &operation ) );
2449
2450 /* Call finish without calling setup beforehand. */
2451 TEST_EQUAL( psa_cipher_finish( &operation,
2452 buffer, sizeof( buffer ), &length ),
2453 PSA_ERROR_BAD_STATE );
2454 PSA_ASSERT( psa_cipher_abort( &operation ) );
2455
2456 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002457 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002458 /* Not calling update means we are encrypting an empty buffer, which is OK
2459 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01002460 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002461 TEST_EQUAL( psa_cipher_finish( &operation,
2462 buffer, sizeof( buffer ), &length ),
2463 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002464 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002465 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002466 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002467
2468 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002469 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002470 PSA_ASSERT( psa_cipher_set_iv( &operation,
2471 iv, sizeof( iv ) ) );
2472 PSA_ASSERT( psa_cipher_finish( &operation,
2473 buffer, sizeof( buffer ), &length ) );
2474 TEST_EQUAL( psa_cipher_finish( &operation,
2475 buffer, sizeof( buffer ), &length ),
2476 PSA_ERROR_BAD_STATE );
2477 PSA_ASSERT( psa_cipher_abort( &operation ) );
2478
Ronald Cron5425a212020-08-04 14:58:35 +02002479 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002480
Jaeden Ameroab439972019-02-15 14:12:05 +00002481exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002482 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002483 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002484}
2485/* END_CASE */
2486
2487/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02002488void cipher_encrypt_fail( int alg_arg,
2489 int key_type_arg,
2490 data_t *key_data,
2491 data_t *input,
2492 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002493{
Ronald Cron5425a212020-08-04 14:58:35 +02002494 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002495 psa_status_t status;
2496 psa_key_type_t key_type = key_type_arg;
2497 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002498 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002499 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002500 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002501 size_t output_length = 0;
2502 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2503
2504 if ( PSA_ERROR_BAD_STATE != expected_status )
2505 {
2506 PSA_ASSERT( psa_crypto_init( ) );
2507
2508 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2509 psa_set_key_algorithm( &attributes, alg );
2510 psa_set_key_type( &attributes, key_type );
2511
2512 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
2513 input->len );
2514 ASSERT_ALLOC( output, output_buffer_size );
2515
2516 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2517 &key ) );
2518 }
2519
2520 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
2521 output_buffer_size, &output_length );
2522
2523 TEST_EQUAL( status, expected_status );
2524
2525exit:
2526 mbedtls_free( output );
2527 psa_destroy_key( key );
2528 PSA_DONE( );
2529}
2530/* END_CASE */
2531
2532/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02002533void cipher_encrypt_alg_without_iv( int alg_arg,
2534 int key_type_arg,
2535 data_t *key_data,
2536 data_t *input,
2537 data_t *expected_output )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002538{
2539 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2540 psa_key_type_t key_type = key_type_arg;
2541 psa_algorithm_t alg = alg_arg;
2542 unsigned char *output = NULL;
2543 size_t output_buffer_size = 0;
2544 size_t output_length = 0;
2545 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2546
2547 PSA_ASSERT( psa_crypto_init( ) );
2548
2549 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2550 psa_set_key_algorithm( &attributes, alg );
2551 psa_set_key_type( &attributes, key_type );
2552
2553 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2554 ASSERT_ALLOC( output, output_buffer_size );
2555
2556 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2557 &key ) );
2558
2559 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
2560 output_buffer_size, &output_length ) );
2561 TEST_ASSERT( output_length <=
2562 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2563 TEST_ASSERT( output_length <=
2564 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
2565
2566 ASSERT_COMPARE( expected_output->x, expected_output->len,
2567 output, output_length );
2568exit:
2569 mbedtls_free( output );
2570 psa_destroy_key( key );
2571 PSA_DONE( );
2572}
2573/* END_CASE */
2574
2575/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01002576void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
2577{
2578 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2579 psa_algorithm_t alg = alg_arg;
2580 psa_key_type_t key_type = key_type_arg;
2581 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2582 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2583 psa_status_t status;
2584
2585 PSA_ASSERT( psa_crypto_init( ) );
2586
2587 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2588 psa_set_key_algorithm( &attributes, alg );
2589 psa_set_key_type( &attributes, key_type );
2590
2591 /* Usage of either of these two size macros would cause divide by zero
2592 * with incorrect key types previously. Input length should be irrelevant
2593 * here. */
2594 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
2595 0 );
2596 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
2597
2598
2599 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2600 &key ) );
2601
2602 /* Should fail due to invalid alg type (to support invalid key type).
2603 * Encrypt or decrypt will end up in the same place. */
2604 status = psa_cipher_encrypt_setup( &operation, key, alg );
2605
2606 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
2607
2608exit:
2609 psa_cipher_abort( &operation );
2610 psa_destroy_key( key );
2611 PSA_DONE( );
2612}
2613/* END_CASE */
2614
2615/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002616void cipher_encrypt_validation( int alg_arg,
2617 int key_type_arg,
2618 data_t *key_data,
2619 data_t *input )
2620{
2621 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2622 psa_key_type_t key_type = key_type_arg;
2623 psa_algorithm_t alg = alg_arg;
2624 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
2625 unsigned char *output1 = NULL;
2626 size_t output1_buffer_size = 0;
2627 size_t output1_length = 0;
2628 unsigned char *output2 = NULL;
2629 size_t output2_buffer_size = 0;
2630 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002631 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002632 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002633 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002634
Gilles Peskine8817f612018-12-18 00:18:46 +01002635 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002636
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002637 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2638 psa_set_key_algorithm( &attributes, alg );
2639 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002640
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002641 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2642 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2643 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
2644 ASSERT_ALLOC( output1, output1_buffer_size );
2645 ASSERT_ALLOC( output2, output2_buffer_size );
2646
Ronald Cron5425a212020-08-04 14:58:35 +02002647 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2648 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002649
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02002650 /* The one-shot cipher encryption uses generated iv so validating
2651 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002652 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
2653 output1_buffer_size, &output1_length ) );
2654 TEST_ASSERT( output1_length <=
2655 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2656 TEST_ASSERT( output1_length <=
gabor-mezei-armceface22021-01-21 12:26:17 +01002657 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002658
2659 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2660 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002661
Gilles Peskine8817f612018-12-18 00:18:46 +01002662 PSA_ASSERT( psa_cipher_update( &operation,
2663 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002664 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01002665 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002666 TEST_ASSERT( function_output_length <=
2667 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2668 TEST_ASSERT( function_output_length <=
2669 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002670 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002671
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002672 PSA_ASSERT( psa_cipher_finish( &operation,
2673 output2 + output2_length,
2674 output2_buffer_size - output2_length,
2675 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002676 TEST_ASSERT( function_output_length <=
2677 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2678 TEST_ASSERT( function_output_length <=
2679 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002680 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002681
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002682 PSA_ASSERT( psa_cipher_abort( &operation ) );
2683 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
2684 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002685
Gilles Peskine50e586b2018-06-08 14:28:46 +02002686exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002687 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002688 mbedtls_free( output1 );
2689 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002690 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002691 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002692}
2693/* END_CASE */
2694
2695/* BEGIN_CASE */
2696void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002697 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002698 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002699 int first_part_size_arg,
2700 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002701 data_t *expected_output,
2702 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002703{
Ronald Cron5425a212020-08-04 14:58:35 +02002704 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002705 psa_key_type_t key_type = key_type_arg;
2706 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002707 psa_status_t status;
2708 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002709 size_t first_part_size = first_part_size_arg;
2710 size_t output1_length = output1_length_arg;
2711 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002712 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002713 size_t output_buffer_size = 0;
2714 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002715 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002716 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002717 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002718
Gilles Peskine8817f612018-12-18 00:18:46 +01002719 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002720
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002721 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2722 psa_set_key_algorithm( &attributes, alg );
2723 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002724
Ronald Cron5425a212020-08-04 14:58:35 +02002725 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2726 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002727
Ronald Cron5425a212020-08-04 14:58:35 +02002728 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002729
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002730 if( iv->len > 0 )
2731 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002732 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002733 }
2734
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002735 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2736 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002737 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002738
Gilles Peskinee0866522019-02-19 19:44:00 +01002739 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002740 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2741 output, output_buffer_size,
2742 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002743 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002744 TEST_ASSERT( function_output_length <=
2745 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2746 TEST_ASSERT( function_output_length <=
2747 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002748 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002749
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002750 if( first_part_size < input->len )
2751 {
2752 PSA_ASSERT( psa_cipher_update( &operation,
2753 input->x + first_part_size,
2754 input->len - first_part_size,
2755 ( output_buffer_size == 0 ? NULL :
2756 output + total_output_length ),
2757 output_buffer_size - total_output_length,
2758 &function_output_length ) );
2759 TEST_ASSERT( function_output_length == output2_length );
2760 TEST_ASSERT( function_output_length <=
2761 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2762 alg,
2763 input->len - first_part_size ) );
2764 TEST_ASSERT( function_output_length <=
2765 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2766 total_output_length += function_output_length;
2767 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002768
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002769 status = psa_cipher_finish( &operation,
2770 ( output_buffer_size == 0 ? NULL :
2771 output + total_output_length ),
2772 output_buffer_size - total_output_length,
2773 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002774 TEST_ASSERT( function_output_length <=
2775 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2776 TEST_ASSERT( function_output_length <=
2777 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002778 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002779 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002780
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002781 if( expected_status == PSA_SUCCESS )
2782 {
2783 PSA_ASSERT( psa_cipher_abort( &operation ) );
2784
2785 ASSERT_COMPARE( expected_output->x, expected_output->len,
2786 output, total_output_length );
2787 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002788
2789exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002790 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002791 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002792 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002793 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002794}
2795/* END_CASE */
2796
2797/* BEGIN_CASE */
2798void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002799 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002800 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002801 int first_part_size_arg,
2802 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002803 data_t *expected_output,
2804 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002805{
Ronald Cron5425a212020-08-04 14:58:35 +02002806 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002807 psa_key_type_t key_type = key_type_arg;
2808 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002809 psa_status_t status;
2810 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002811 size_t first_part_size = first_part_size_arg;
2812 size_t output1_length = output1_length_arg;
2813 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002814 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002815 size_t output_buffer_size = 0;
2816 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002817 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002818 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002819 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002820
Gilles Peskine8817f612018-12-18 00:18:46 +01002821 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002822
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002823 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2824 psa_set_key_algorithm( &attributes, alg );
2825 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002826
Ronald Cron5425a212020-08-04 14:58:35 +02002827 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2828 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002829
Ronald Cron5425a212020-08-04 14:58:35 +02002830 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002831
Steven Cooreman177deba2020-09-07 17:14:14 +02002832 if( iv->len > 0 )
2833 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002834 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002835 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002836
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002837 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2838 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002839 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002840
Gilles Peskinee0866522019-02-19 19:44:00 +01002841 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002842 PSA_ASSERT( psa_cipher_update( &operation,
2843 input->x, first_part_size,
2844 output, output_buffer_size,
2845 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002846 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002847 TEST_ASSERT( function_output_length <=
2848 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2849 TEST_ASSERT( function_output_length <=
2850 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002851 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002852
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002853 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02002854 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002855 PSA_ASSERT( psa_cipher_update( &operation,
2856 input->x + first_part_size,
2857 input->len - first_part_size,
2858 ( output_buffer_size == 0 ? NULL :
2859 output + total_output_length ),
2860 output_buffer_size - total_output_length,
2861 &function_output_length ) );
2862 TEST_ASSERT( function_output_length == output2_length );
2863 TEST_ASSERT( function_output_length <=
2864 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2865 alg,
2866 input->len - first_part_size ) );
2867 TEST_ASSERT( function_output_length <=
2868 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2869 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002870 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002871
Gilles Peskine50e586b2018-06-08 14:28:46 +02002872 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002873 ( output_buffer_size == 0 ? NULL :
2874 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002875 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002876 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002877 TEST_ASSERT( function_output_length <=
2878 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2879 TEST_ASSERT( function_output_length <=
2880 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002881 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002882 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002883
2884 if( expected_status == PSA_SUCCESS )
2885 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002886 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002887
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002888 ASSERT_COMPARE( expected_output->x, expected_output->len,
2889 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002890 }
2891
Gilles Peskine50e586b2018-06-08 14:28:46 +02002892exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002893 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002894 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002895 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002896 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002897}
2898/* END_CASE */
2899
Gilles Peskine50e586b2018-06-08 14:28:46 +02002900/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02002901void cipher_decrypt_fail( int alg_arg,
2902 int key_type_arg,
2903 data_t *key_data,
2904 data_t *iv,
2905 data_t *input_arg,
2906 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002907{
2908 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2909 psa_status_t status;
2910 psa_key_type_t key_type = key_type_arg;
2911 psa_algorithm_t alg = alg_arg;
2912 psa_status_t expected_status = expected_status_arg;
2913 unsigned char *input = NULL;
2914 size_t input_buffer_size = 0;
2915 unsigned char *output = NULL;
2916 size_t output_buffer_size = 0;
2917 size_t output_length = 0;
2918 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2919
2920 if ( PSA_ERROR_BAD_STATE != expected_status )
2921 {
2922 PSA_ASSERT( psa_crypto_init( ) );
2923
2924 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2925 psa_set_key_algorithm( &attributes, alg );
2926 psa_set_key_type( &attributes, key_type );
2927
2928 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2929 &key ) );
2930 }
2931
2932 /* Allocate input buffer and copy the iv and the plaintext */
2933 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
2934 if ( input_buffer_size > 0 )
2935 {
2936 ASSERT_ALLOC( input, input_buffer_size );
2937 memcpy( input, iv->x, iv->len );
2938 memcpy( input + iv->len, input_arg->x, input_arg->len );
2939 }
2940
2941 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
2942 ASSERT_ALLOC( output, output_buffer_size );
2943
2944 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
2945 output_buffer_size, &output_length );
2946 TEST_EQUAL( status, expected_status );
2947
2948exit:
2949 mbedtls_free( input );
2950 mbedtls_free( output );
2951 psa_destroy_key( key );
2952 PSA_DONE( );
2953}
2954/* END_CASE */
2955
2956/* BEGIN_CASE */
2957void cipher_decrypt( int alg_arg,
2958 int key_type_arg,
2959 data_t *key_data,
2960 data_t *iv,
2961 data_t *input_arg,
2962 data_t *expected_output )
2963{
2964 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2965 psa_key_type_t key_type = key_type_arg;
2966 psa_algorithm_t alg = alg_arg;
2967 unsigned char *input = NULL;
2968 size_t input_buffer_size = 0;
2969 unsigned char *output = NULL;
2970 size_t output_buffer_size = 0;
2971 size_t output_length = 0;
2972 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2973
2974 PSA_ASSERT( psa_crypto_init( ) );
2975
2976 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2977 psa_set_key_algorithm( &attributes, alg );
2978 psa_set_key_type( &attributes, key_type );
2979
2980 /* Allocate input buffer and copy the iv and the plaintext */
2981 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
2982 if ( input_buffer_size > 0 )
2983 {
2984 ASSERT_ALLOC( input, input_buffer_size );
2985 memcpy( input, iv->x, iv->len );
2986 memcpy( input + iv->len, input_arg->x, input_arg->len );
2987 }
2988
2989 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
2990 ASSERT_ALLOC( output, output_buffer_size );
2991
2992 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2993 &key ) );
2994
2995 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
2996 output_buffer_size, &output_length ) );
2997 TEST_ASSERT( output_length <=
2998 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
2999 TEST_ASSERT( output_length <=
3000 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
3001
3002 ASSERT_COMPARE( expected_output->x, expected_output->len,
3003 output, output_length );
3004exit:
3005 mbedtls_free( input );
3006 mbedtls_free( output );
3007 psa_destroy_key( key );
3008 PSA_DONE( );
3009}
3010/* END_CASE */
3011
3012/* BEGIN_CASE */
3013void cipher_verify_output( int alg_arg,
3014 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003015 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003016 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003017{
Ronald Cron5425a212020-08-04 14:58:35 +02003018 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003019 psa_key_type_t key_type = key_type_arg;
3020 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003021 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003022 size_t output1_size = 0;
3023 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003024 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003025 size_t output2_size = 0;
3026 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003027 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003028
Gilles Peskine8817f612018-12-18 00:18:46 +01003029 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003030
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003031 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3032 psa_set_key_algorithm( &attributes, alg );
3033 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003034
Ronald Cron5425a212020-08-04 14:58:35 +02003035 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3036 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003037 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003038 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003039
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003040 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3041 output1, output1_size,
3042 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003043 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003044 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003045 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003046 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003047
3048 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003049 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003050
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003051 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3052 output2, output2_size,
3053 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003054 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003055 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003056 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003057 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003058
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003059 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003060
3061exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003062 mbedtls_free( output1 );
3063 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003064 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003065 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003066}
3067/* END_CASE */
3068
3069/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003070void cipher_verify_output_multipart( int alg_arg,
3071 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003072 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003073 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003074 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003075{
Ronald Cron5425a212020-08-04 14:58:35 +02003076 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003077 psa_key_type_t key_type = key_type_arg;
3078 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003079 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003080 unsigned char iv[16] = {0};
3081 size_t iv_size = 16;
3082 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003083 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003084 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003085 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003086 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003087 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003088 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003089 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003090 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3091 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003092 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003093
Gilles Peskine8817f612018-12-18 00:18:46 +01003094 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003095
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003096 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3097 psa_set_key_algorithm( &attributes, alg );
3098 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003099
Ronald Cron5425a212020-08-04 14:58:35 +02003100 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3101 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003102
Ronald Cron5425a212020-08-04 14:58:35 +02003103 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3104 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003105
Steven Cooreman177deba2020-09-07 17:14:14 +02003106 if( alg != PSA_ALG_ECB_NO_PADDING )
3107 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003108 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3109 iv, iv_size,
3110 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003111 }
3112
gabor-mezei-armceface22021-01-21 12:26:17 +01003113 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3114 TEST_ASSERT( output1_buffer_size <=
3115 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003116 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003117
Gilles Peskinee0866522019-02-19 19:44:00 +01003118 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003119
Gilles Peskine8817f612018-12-18 00:18:46 +01003120 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3121 output1, output1_buffer_size,
3122 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003123 TEST_ASSERT( function_output_length <=
3124 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3125 TEST_ASSERT( function_output_length <=
3126 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003127 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003128
Gilles Peskine8817f612018-12-18 00:18:46 +01003129 PSA_ASSERT( psa_cipher_update( &operation1,
3130 input->x + first_part_size,
3131 input->len - first_part_size,
3132 output1, output1_buffer_size,
3133 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003134 TEST_ASSERT( function_output_length <=
3135 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3136 alg,
3137 input->len - first_part_size ) );
3138 TEST_ASSERT( function_output_length <=
3139 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003140 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003141
Gilles Peskine8817f612018-12-18 00:18:46 +01003142 PSA_ASSERT( psa_cipher_finish( &operation1,
3143 output1 + output1_length,
3144 output1_buffer_size - output1_length,
3145 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003146 TEST_ASSERT( function_output_length <=
3147 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3148 TEST_ASSERT( function_output_length <=
3149 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003150 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003151
Gilles Peskine8817f612018-12-18 00:18:46 +01003152 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003153
Gilles Peskine048b7f02018-06-08 14:20:49 +02003154 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003155 TEST_ASSERT( output2_buffer_size <=
3156 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3157 TEST_ASSERT( output2_buffer_size <=
3158 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003159 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003160
Steven Cooreman177deba2020-09-07 17:14:14 +02003161 if( iv_length > 0 )
3162 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003163 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3164 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003165 }
Moran Pekerded84402018-06-06 16:36:50 +03003166
Gilles Peskine8817f612018-12-18 00:18:46 +01003167 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3168 output2, output2_buffer_size,
3169 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003170 TEST_ASSERT( function_output_length <=
3171 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3172 TEST_ASSERT( function_output_length <=
3173 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003174 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003175
Gilles Peskine8817f612018-12-18 00:18:46 +01003176 PSA_ASSERT( psa_cipher_update( &operation2,
3177 output1 + first_part_size,
3178 output1_length - first_part_size,
3179 output2, output2_buffer_size,
3180 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003181 TEST_ASSERT( function_output_length <=
3182 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3183 alg,
3184 output1_length - first_part_size ) );
3185 TEST_ASSERT( function_output_length <=
3186 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003187 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003188
Gilles Peskine8817f612018-12-18 00:18:46 +01003189 PSA_ASSERT( psa_cipher_finish( &operation2,
3190 output2 + output2_length,
3191 output2_buffer_size - output2_length,
3192 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003193 TEST_ASSERT( function_output_length <=
3194 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3195 TEST_ASSERT( function_output_length <=
3196 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003197 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003198
Gilles Peskine8817f612018-12-18 00:18:46 +01003199 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003200
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003201 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003202
3203exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003204 psa_cipher_abort( &operation1 );
3205 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003206 mbedtls_free( output1 );
3207 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003208 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003209 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003210}
3211/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003212
Gilles Peskine20035e32018-02-03 22:44:14 +01003213/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003214void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003215 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003216 data_t *nonce,
3217 data_t *additional_data,
3218 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003219 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003220{
Ronald Cron5425a212020-08-04 14:58:35 +02003221 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003222 psa_key_type_t key_type = key_type_arg;
3223 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003224 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003225 unsigned char *output_data = NULL;
3226 size_t output_size = 0;
3227 size_t output_length = 0;
3228 unsigned char *output_data2 = NULL;
3229 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003230 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003231 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003232 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003233
Gilles Peskine8817f612018-12-18 00:18:46 +01003234 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003235
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003236 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3237 psa_set_key_algorithm( &attributes, alg );
3238 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003239
Gilles Peskine049c7532019-05-15 20:22:09 +02003240 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003241 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003242 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3243 key_bits = psa_get_key_bits( &attributes );
3244
3245 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3246 alg );
3247 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3248 * should be exact. */
3249 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3250 expected_result != PSA_ERROR_NOT_SUPPORTED )
3251 {
3252 TEST_EQUAL( output_size,
3253 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3254 TEST_ASSERT( output_size <=
3255 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3256 }
3257 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003258
Steven Cooremanf49478b2021-02-15 15:19:25 +01003259 status = psa_aead_encrypt( key, alg,
3260 nonce->x, nonce->len,
3261 additional_data->x,
3262 additional_data->len,
3263 input_data->x, input_data->len,
3264 output_data, output_size,
3265 &output_length );
3266
3267 /* If the operation is not supported, just skip and not fail in case the
3268 * encryption involves a common limitation of cryptography hardwares and
3269 * an alternative implementation. */
3270 if( status == PSA_ERROR_NOT_SUPPORTED )
3271 {
3272 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3273 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3274 }
3275
3276 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003277
3278 if( PSA_SUCCESS == expected_result )
3279 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003280 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003281
Gilles Peskine003a4a92019-05-14 16:09:40 +02003282 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3283 * should be exact. */
3284 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003285 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003286
gabor-mezei-armceface22021-01-21 12:26:17 +01003287 TEST_ASSERT( input_data->len <=
3288 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3289
Ronald Cron5425a212020-08-04 14:58:35 +02003290 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003291 nonce->x, nonce->len,
3292 additional_data->x,
3293 additional_data->len,
3294 output_data, output_length,
3295 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003296 &output_length2 ),
3297 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003298
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003299 ASSERT_COMPARE( input_data->x, input_data->len,
3300 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003301 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003302
Gilles Peskinea1cac842018-06-11 19:33:02 +02003303exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003304 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003305 mbedtls_free( output_data );
3306 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003307 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003308}
3309/* END_CASE */
3310
3311/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003312void aead_encrypt( int key_type_arg, data_t *key_data,
3313 int alg_arg,
3314 data_t *nonce,
3315 data_t *additional_data,
3316 data_t *input_data,
3317 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003318{
Ronald Cron5425a212020-08-04 14:58:35 +02003319 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003320 psa_key_type_t key_type = key_type_arg;
3321 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003322 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003323 unsigned char *output_data = NULL;
3324 size_t output_size = 0;
3325 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003326 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003327 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003328
Gilles Peskine8817f612018-12-18 00:18:46 +01003329 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003330
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003331 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3332 psa_set_key_algorithm( &attributes, alg );
3333 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003334
Gilles Peskine049c7532019-05-15 20:22:09 +02003335 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003336 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003337 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3338 key_bits = psa_get_key_bits( &attributes );
3339
3340 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3341 alg );
3342 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3343 * should be exact. */
3344 TEST_EQUAL( output_size,
3345 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3346 TEST_ASSERT( output_size <=
3347 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3348 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003349
Steven Cooremand588ea12021-01-11 19:36:04 +01003350 status = psa_aead_encrypt( key, alg,
3351 nonce->x, nonce->len,
3352 additional_data->x, additional_data->len,
3353 input_data->x, input_data->len,
3354 output_data, output_size,
3355 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003356
Ronald Cron28a45ed2021-02-09 20:35:42 +01003357 /* If the operation is not supported, just skip and not fail in case the
3358 * encryption involves a common limitation of cryptography hardwares and
3359 * an alternative implementation. */
3360 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003361 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003362 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3363 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003364 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003365
3366 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003367 ASSERT_COMPARE( expected_result->x, expected_result->len,
3368 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003369
Gilles Peskinea1cac842018-06-11 19:33:02 +02003370exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003371 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003372 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003373 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003374}
3375/* END_CASE */
3376
3377/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003378void aead_decrypt( int key_type_arg, data_t *key_data,
3379 int alg_arg,
3380 data_t *nonce,
3381 data_t *additional_data,
3382 data_t *input_data,
3383 data_t *expected_data,
3384 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003385{
Ronald Cron5425a212020-08-04 14:58:35 +02003386 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003387 psa_key_type_t key_type = key_type_arg;
3388 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003389 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003390 unsigned char *output_data = NULL;
3391 size_t output_size = 0;
3392 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003393 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003394 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003395 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003396
Gilles Peskine8817f612018-12-18 00:18:46 +01003397 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003398
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003399 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3400 psa_set_key_algorithm( &attributes, alg );
3401 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003402
Gilles Peskine049c7532019-05-15 20:22:09 +02003403 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003404 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003405 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3406 key_bits = psa_get_key_bits( &attributes );
3407
3408 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3409 alg );
3410 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3411 expected_result != PSA_ERROR_NOT_SUPPORTED )
3412 {
3413 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3414 * should be exact. */
3415 TEST_EQUAL( output_size,
3416 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3417 TEST_ASSERT( output_size <=
3418 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3419 }
3420 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003421
Steven Cooremand588ea12021-01-11 19:36:04 +01003422 status = psa_aead_decrypt( key, alg,
3423 nonce->x, nonce->len,
3424 additional_data->x,
3425 additional_data->len,
3426 input_data->x, input_data->len,
3427 output_data, output_size,
3428 &output_length );
3429
Ronald Cron28a45ed2021-02-09 20:35:42 +01003430 /* If the operation is not supported, just skip and not fail in case the
3431 * decryption involves a common limitation of cryptography hardwares and
3432 * an alternative implementation. */
3433 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003434 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003435 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3436 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003437 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003438
3439 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003440
Gilles Peskine2d277862018-06-18 15:41:12 +02003441 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003442 ASSERT_COMPARE( expected_data->x, expected_data->len,
3443 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003444
Gilles Peskinea1cac842018-06-11 19:33:02 +02003445exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003446 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003447 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003448 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003449}
3450/* END_CASE */
3451
3452/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003453void signature_size( int type_arg,
3454 int bits,
3455 int alg_arg,
3456 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003457{
3458 psa_key_type_t type = type_arg;
3459 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003460 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003461
Gilles Peskinefe11b722018-12-18 00:24:04 +01003462 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003463
Gilles Peskinee59236f2018-01-27 23:32:46 +01003464exit:
3465 ;
3466}
3467/* END_CASE */
3468
3469/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003470void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3471 int alg_arg, data_t *input_data,
3472 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003473{
Ronald Cron5425a212020-08-04 14:58:35 +02003474 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003475 psa_key_type_t key_type = key_type_arg;
3476 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003477 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003478 unsigned char *signature = NULL;
3479 size_t signature_size;
3480 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003481 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003482
Gilles Peskine8817f612018-12-18 00:18:46 +01003483 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003484
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003485 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003486 psa_set_key_algorithm( &attributes, alg );
3487 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003488
Gilles Peskine049c7532019-05-15 20:22:09 +02003489 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003490 &key ) );
3491 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003492 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003493
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003494 /* Allocate a buffer which has the size advertized by the
3495 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003496 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003497 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003498 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003499 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003500 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003501
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003502 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003503 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003504 input_data->x, input_data->len,
3505 signature, signature_size,
3506 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003507 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003508 ASSERT_COMPARE( output_data->x, output_data->len,
3509 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003510
3511exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003512 /*
3513 * Key attributes may have been returned by psa_get_key_attributes()
3514 * thus reset them as required.
3515 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003516 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003517
Ronald Cron5425a212020-08-04 14:58:35 +02003518 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003519 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003520 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003521}
3522/* END_CASE */
3523
3524/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003525void sign_hash_fail( int key_type_arg, data_t *key_data,
3526 int alg_arg, data_t *input_data,
3527 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003528{
Ronald Cron5425a212020-08-04 14:58:35 +02003529 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003530 psa_key_type_t key_type = key_type_arg;
3531 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003532 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003533 psa_status_t actual_status;
3534 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003535 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003536 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003537 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003538
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003539 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003540
Gilles Peskine8817f612018-12-18 00:18:46 +01003541 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003542
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003543 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003544 psa_set_key_algorithm( &attributes, alg );
3545 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003546
Gilles Peskine049c7532019-05-15 20:22:09 +02003547 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003548 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003549
Ronald Cron5425a212020-08-04 14:58:35 +02003550 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003551 input_data->x, input_data->len,
3552 signature, signature_size,
3553 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003554 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003555 /* The value of *signature_length is unspecified on error, but
3556 * whatever it is, it should be less than signature_size, so that
3557 * if the caller tries to read *signature_length bytes without
3558 * checking the error code then they don't overflow a buffer. */
3559 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003560
3561exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003562 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003563 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003564 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003565 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003566}
3567/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003568
3569/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003570void sign_verify_hash( int key_type_arg, data_t *key_data,
3571 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003572{
Ronald Cron5425a212020-08-04 14:58:35 +02003573 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003574 psa_key_type_t key_type = key_type_arg;
3575 psa_algorithm_t alg = alg_arg;
3576 size_t key_bits;
3577 unsigned char *signature = NULL;
3578 size_t signature_size;
3579 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003580 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003581
Gilles Peskine8817f612018-12-18 00:18:46 +01003582 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003583
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003584 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003585 psa_set_key_algorithm( &attributes, alg );
3586 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003587
Gilles Peskine049c7532019-05-15 20:22:09 +02003588 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003589 &key ) );
3590 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003591 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003592
3593 /* Allocate a buffer which has the size advertized by the
3594 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003595 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003596 key_bits, alg );
3597 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003598 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003599 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003600
3601 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003602 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003603 input_data->x, input_data->len,
3604 signature, signature_size,
3605 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003606 /* Check that the signature length looks sensible. */
3607 TEST_ASSERT( signature_length <= signature_size );
3608 TEST_ASSERT( signature_length > 0 );
3609
3610 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003611 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003612 input_data->x, input_data->len,
3613 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003614
3615 if( input_data->len != 0 )
3616 {
3617 /* Flip a bit in the input and verify that the signature is now
3618 * detected as invalid. Flip a bit at the beginning, not at the end,
3619 * because ECDSA may ignore the last few bits of the input. */
3620 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003621 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003622 input_data->x, input_data->len,
3623 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003624 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003625 }
3626
3627exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003628 /*
3629 * Key attributes may have been returned by psa_get_key_attributes()
3630 * thus reset them as required.
3631 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003632 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003633
Ronald Cron5425a212020-08-04 14:58:35 +02003634 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003635 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003636 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003637}
3638/* END_CASE */
3639
3640/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003641void verify_hash( int key_type_arg, data_t *key_data,
3642 int alg_arg, data_t *hash_data,
3643 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003644{
Ronald Cron5425a212020-08-04 14:58:35 +02003645 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003646 psa_key_type_t key_type = key_type_arg;
3647 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003648 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003649
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003650 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003651
Gilles Peskine8817f612018-12-18 00:18:46 +01003652 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003653
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003654 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003655 psa_set_key_algorithm( &attributes, alg );
3656 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003657
Gilles Peskine049c7532019-05-15 20:22:09 +02003658 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003659 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003660
Ronald Cron5425a212020-08-04 14:58:35 +02003661 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003662 hash_data->x, hash_data->len,
3663 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003664
itayzafrir5c753392018-05-08 11:18:38 +03003665exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003666 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003667 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003668 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003669}
3670/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003671
3672/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003673void verify_hash_fail( int key_type_arg, data_t *key_data,
3674 int alg_arg, data_t *hash_data,
3675 data_t *signature_data,
3676 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003677{
Ronald Cron5425a212020-08-04 14:58:35 +02003678 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003679 psa_key_type_t key_type = key_type_arg;
3680 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003681 psa_status_t actual_status;
3682 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003683 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003684
Gilles Peskine8817f612018-12-18 00:18:46 +01003685 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003686
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003687 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003688 psa_set_key_algorithm( &attributes, alg );
3689 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003690
Gilles Peskine049c7532019-05-15 20:22:09 +02003691 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003692 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003693
Ronald Cron5425a212020-08-04 14:58:35 +02003694 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003695 hash_data->x, hash_data->len,
3696 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003697 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003698
3699exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003700 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003701 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003702 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003703}
3704/* END_CASE */
3705
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003706/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02003707void sign_message_deterministic( int key_type_arg,
3708 data_t *key_data,
3709 int alg_arg,
3710 data_t *input_data,
3711 data_t *output_data )
3712{
3713 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3714 psa_key_type_t key_type = key_type_arg;
3715 psa_algorithm_t alg = alg_arg;
3716 size_t key_bits;
3717 unsigned char *signature = NULL;
3718 size_t signature_size;
3719 size_t signature_length = 0xdeadbeef;
3720 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3721
3722 PSA_ASSERT( psa_crypto_init( ) );
3723
3724 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3725 psa_set_key_algorithm( &attributes, alg );
3726 psa_set_key_type( &attributes, key_type );
3727
3728 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3729 &key ) );
3730 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3731 key_bits = psa_get_key_bits( &attributes );
3732
3733 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3734 TEST_ASSERT( signature_size != 0 );
3735 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3736 ASSERT_ALLOC( signature, signature_size );
3737
3738 PSA_ASSERT( psa_sign_message( key, alg,
3739 input_data->x, input_data->len,
3740 signature, signature_size,
3741 &signature_length ) );
3742
3743 ASSERT_COMPARE( output_data->x, output_data->len,
3744 signature, signature_length );
3745
3746exit:
3747 psa_reset_key_attributes( &attributes );
3748
3749 psa_destroy_key( key );
3750 mbedtls_free( signature );
3751 PSA_DONE( );
3752
3753}
3754/* END_CASE */
3755
3756/* BEGIN_CASE */
3757void sign_message_fail( int key_type_arg,
3758 data_t *key_data,
3759 int alg_arg,
3760 data_t *input_data,
3761 int signature_size_arg,
3762 int expected_status_arg )
3763{
3764 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3765 psa_key_type_t key_type = key_type_arg;
3766 psa_algorithm_t alg = alg_arg;
3767 size_t signature_size = signature_size_arg;
3768 psa_status_t actual_status;
3769 psa_status_t expected_status = expected_status_arg;
3770 unsigned char *signature = NULL;
3771 size_t signature_length = 0xdeadbeef;
3772 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3773
3774 ASSERT_ALLOC( signature, signature_size );
3775
3776 PSA_ASSERT( psa_crypto_init( ) );
3777
3778 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3779 psa_set_key_algorithm( &attributes, alg );
3780 psa_set_key_type( &attributes, key_type );
3781
3782 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3783 &key ) );
3784
3785 actual_status = psa_sign_message( key, alg,
3786 input_data->x, input_data->len,
3787 signature, signature_size,
3788 &signature_length );
3789 TEST_EQUAL( actual_status, expected_status );
3790 /* The value of *signature_length is unspecified on error, but
3791 * whatever it is, it should be less than signature_size, so that
3792 * if the caller tries to read *signature_length bytes without
3793 * checking the error code then they don't overflow a buffer. */
3794 TEST_ASSERT( signature_length <= signature_size );
3795
3796exit:
3797 psa_reset_key_attributes( &attributes );
3798 psa_destroy_key( key );
3799 mbedtls_free( signature );
3800 PSA_DONE( );
3801}
3802/* END_CASE */
3803
3804/* BEGIN_CASE */
3805void sign_verify_message( int key_type_arg,
3806 data_t *key_data,
3807 int alg_arg,
3808 data_t *input_data )
3809{
3810 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3811 psa_key_type_t key_type = key_type_arg;
3812 psa_algorithm_t alg = alg_arg;
3813 size_t key_bits;
3814 unsigned char *signature = NULL;
3815 size_t signature_size;
3816 size_t signature_length = 0xdeadbeef;
3817 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3818
3819 PSA_ASSERT( psa_crypto_init( ) );
3820
3821 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3822 PSA_KEY_USAGE_VERIFY_MESSAGE );
3823 psa_set_key_algorithm( &attributes, alg );
3824 psa_set_key_type( &attributes, key_type );
3825
3826 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3827 &key ) );
3828 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3829 key_bits = psa_get_key_bits( &attributes );
3830
3831 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3832 TEST_ASSERT( signature_size != 0 );
3833 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3834 ASSERT_ALLOC( signature, signature_size );
3835
3836 PSA_ASSERT( psa_sign_message( key, alg,
3837 input_data->x, input_data->len,
3838 signature, signature_size,
3839 &signature_length ) );
3840 TEST_ASSERT( signature_length <= signature_size );
3841 TEST_ASSERT( signature_length > 0 );
3842
3843 PSA_ASSERT( psa_verify_message( key, alg,
3844 input_data->x, input_data->len,
3845 signature, signature_length ) );
3846
3847 if( input_data->len != 0 )
3848 {
3849 /* Flip a bit in the input and verify that the signature is now
3850 * detected as invalid. Flip a bit at the beginning, not at the end,
3851 * because ECDSA may ignore the last few bits of the input. */
3852 input_data->x[0] ^= 1;
3853 TEST_EQUAL( psa_verify_message( key, alg,
3854 input_data->x, input_data->len,
3855 signature, signature_length ),
3856 PSA_ERROR_INVALID_SIGNATURE );
3857 }
3858
3859exit:
3860 psa_reset_key_attributes( &attributes );
3861
3862 psa_destroy_key( key );
3863 mbedtls_free( signature );
3864 PSA_DONE( );
3865}
3866/* END_CASE */
3867
3868/* BEGIN_CASE */
3869void verify_message( int key_type_arg,
3870 data_t *key_data,
3871 int alg_arg,
3872 data_t *input_data,
3873 data_t *signature_data )
3874{
3875 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3876 psa_key_type_t key_type = key_type_arg;
3877 psa_algorithm_t alg = alg_arg;
3878 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3879
3880 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3881
3882 PSA_ASSERT( psa_crypto_init( ) );
3883
3884 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3885 psa_set_key_algorithm( &attributes, alg );
3886 psa_set_key_type( &attributes, key_type );
3887
3888 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3889 &key ) );
3890
3891 PSA_ASSERT( psa_verify_message( key, alg,
3892 input_data->x, input_data->len,
3893 signature_data->x, signature_data->len ) );
3894
3895exit:
3896 psa_reset_key_attributes( &attributes );
3897 psa_destroy_key( key );
3898 PSA_DONE( );
3899}
3900/* END_CASE */
3901
3902/* BEGIN_CASE */
3903void verify_message_fail( int key_type_arg,
3904 data_t *key_data,
3905 int alg_arg,
3906 data_t *hash_data,
3907 data_t *signature_data,
3908 int expected_status_arg )
3909{
3910 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3911 psa_key_type_t key_type = key_type_arg;
3912 psa_algorithm_t alg = alg_arg;
3913 psa_status_t actual_status;
3914 psa_status_t expected_status = expected_status_arg;
3915 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3916
3917 PSA_ASSERT( psa_crypto_init( ) );
3918
3919 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3920 psa_set_key_algorithm( &attributes, alg );
3921 psa_set_key_type( &attributes, key_type );
3922
3923 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3924 &key ) );
3925
3926 actual_status = psa_verify_message( key, alg,
3927 hash_data->x, hash_data->len,
3928 signature_data->x,
3929 signature_data->len );
3930 TEST_EQUAL( actual_status, expected_status );
3931
3932exit:
3933 psa_reset_key_attributes( &attributes );
3934 psa_destroy_key( key );
3935 PSA_DONE( );
3936}
3937/* END_CASE */
3938
3939/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003940void asymmetric_encrypt( int key_type_arg,
3941 data_t *key_data,
3942 int alg_arg,
3943 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003944 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003945 int expected_output_length_arg,
3946 int expected_status_arg )
3947{
Ronald Cron5425a212020-08-04 14:58:35 +02003948 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003949 psa_key_type_t key_type = key_type_arg;
3950 psa_algorithm_t alg = alg_arg;
3951 size_t expected_output_length = expected_output_length_arg;
3952 size_t key_bits;
3953 unsigned char *output = NULL;
3954 size_t output_size;
3955 size_t output_length = ~0;
3956 psa_status_t actual_status;
3957 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003958 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003959
Gilles Peskine8817f612018-12-18 00:18:46 +01003960 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003961
Gilles Peskine656896e2018-06-29 19:12:28 +02003962 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003963 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3964 psa_set_key_algorithm( &attributes, alg );
3965 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003966 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003967 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003968
3969 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003970 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003971 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003972
Gilles Peskine656896e2018-06-29 19:12:28 +02003973 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003974 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003975 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003976
3977 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003978 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003979 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003980 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003981 output, output_size,
3982 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003983 TEST_EQUAL( actual_status, expected_status );
3984 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003985
Gilles Peskine68428122018-06-30 18:42:41 +02003986 /* If the label is empty, the test framework puts a non-null pointer
3987 * in label->x. Test that a null pointer works as well. */
3988 if( label->len == 0 )
3989 {
3990 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003991 if( output_size != 0 )
3992 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003993 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003994 input_data->x, input_data->len,
3995 NULL, label->len,
3996 output, output_size,
3997 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003998 TEST_EQUAL( actual_status, expected_status );
3999 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004000 }
4001
Gilles Peskine656896e2018-06-29 19:12:28 +02004002exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004003 /*
4004 * Key attributes may have been returned by psa_get_key_attributes()
4005 * thus reset them as required.
4006 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004007 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004008
Ronald Cron5425a212020-08-04 14:58:35 +02004009 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004010 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004011 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004012}
4013/* END_CASE */
4014
4015/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004016void asymmetric_encrypt_decrypt( int key_type_arg,
4017 data_t *key_data,
4018 int alg_arg,
4019 data_t *input_data,
4020 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004021{
Ronald Cron5425a212020-08-04 14:58:35 +02004022 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004023 psa_key_type_t key_type = key_type_arg;
4024 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004025 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004026 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004027 size_t output_size;
4028 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004029 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004030 size_t output2_size;
4031 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004032 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004033
Gilles Peskine8817f612018-12-18 00:18:46 +01004034 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004035
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004036 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4037 psa_set_key_algorithm( &attributes, alg );
4038 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004039
Gilles Peskine049c7532019-05-15 20:22:09 +02004040 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004041 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004042
4043 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004044 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004045 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004046
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004047 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004048 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004049 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01004050
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004051 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01004052 TEST_ASSERT( output2_size <=
4053 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
4054 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004055 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004056
Gilles Peskineeebd7382018-06-08 18:11:54 +02004057 /* We test encryption by checking that encrypt-then-decrypt gives back
4058 * the original plaintext because of the non-optional random
4059 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004060 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004061 input_data->x, input_data->len,
4062 label->x, label->len,
4063 output, output_size,
4064 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004065 /* We don't know what ciphertext length to expect, but check that
4066 * it looks sensible. */
4067 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004068
Ronald Cron5425a212020-08-04 14:58:35 +02004069 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004070 output, output_length,
4071 label->x, label->len,
4072 output2, output2_size,
4073 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004074 ASSERT_COMPARE( input_data->x, input_data->len,
4075 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004076
4077exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004078 /*
4079 * Key attributes may have been returned by psa_get_key_attributes()
4080 * thus reset them as required.
4081 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004082 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004083
Ronald Cron5425a212020-08-04 14:58:35 +02004084 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004085 mbedtls_free( output );
4086 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004087 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004088}
4089/* END_CASE */
4090
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004091/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004092void asymmetric_decrypt( int key_type_arg,
4093 data_t *key_data,
4094 int alg_arg,
4095 data_t *input_data,
4096 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004097 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004098{
Ronald Cron5425a212020-08-04 14:58:35 +02004099 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004100 psa_key_type_t key_type = key_type_arg;
4101 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004102 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004103 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004104 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004105 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004106 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004107
Gilles Peskine8817f612018-12-18 00:18:46 +01004108 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004109
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004110 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4111 psa_set_key_algorithm( &attributes, alg );
4112 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004113
Gilles Peskine049c7532019-05-15 20:22:09 +02004114 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004115 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004116
gabor-mezei-armceface22021-01-21 12:26:17 +01004117 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4118 key_bits = psa_get_key_bits( &attributes );
4119
4120 /* Determine the maximum ciphertext length */
4121 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
4122 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
4123 ASSERT_ALLOC( output, output_size );
4124
Ronald Cron5425a212020-08-04 14:58:35 +02004125 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004126 input_data->x, input_data->len,
4127 label->x, label->len,
4128 output,
4129 output_size,
4130 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004131 ASSERT_COMPARE( expected_data->x, expected_data->len,
4132 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004133
Gilles Peskine68428122018-06-30 18:42:41 +02004134 /* If the label is empty, the test framework puts a non-null pointer
4135 * in label->x. Test that a null pointer works as well. */
4136 if( label->len == 0 )
4137 {
4138 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004139 if( output_size != 0 )
4140 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004141 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004142 input_data->x, input_data->len,
4143 NULL, label->len,
4144 output,
4145 output_size,
4146 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004147 ASSERT_COMPARE( expected_data->x, expected_data->len,
4148 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004149 }
4150
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004151exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004152 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004153 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004154 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004155 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004156}
4157/* END_CASE */
4158
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004159/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004160void asymmetric_decrypt_fail( int key_type_arg,
4161 data_t *key_data,
4162 int alg_arg,
4163 data_t *input_data,
4164 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004165 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004166 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004167{
Ronald Cron5425a212020-08-04 14:58:35 +02004168 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004169 psa_key_type_t key_type = key_type_arg;
4170 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004171 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004172 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004173 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004174 psa_status_t actual_status;
4175 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004176 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004177
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004178 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004179
Gilles Peskine8817f612018-12-18 00:18:46 +01004180 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004181
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004182 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4183 psa_set_key_algorithm( &attributes, alg );
4184 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004185
Gilles Peskine049c7532019-05-15 20:22:09 +02004186 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004187 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004188
Ronald Cron5425a212020-08-04 14:58:35 +02004189 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004190 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004191 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004192 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004193 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004194 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004195 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004196
Gilles Peskine68428122018-06-30 18:42:41 +02004197 /* If the label is empty, the test framework puts a non-null pointer
4198 * in label->x. Test that a null pointer works as well. */
4199 if( label->len == 0 )
4200 {
4201 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004202 if( output_size != 0 )
4203 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004204 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004205 input_data->x, input_data->len,
4206 NULL, label->len,
4207 output, output_size,
4208 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004209 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004210 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004211 }
4212
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004213exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004214 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004215 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004216 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004217 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004218}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004219/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004220
4221/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004222void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004223{
4224 /* Test each valid way of initializing the object, except for `= {0}`, as
4225 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4226 * though it's OK by the C standard. We could test for this, but we'd need
4227 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004228 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004229 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4230 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4231 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004232
4233 memset( &zero, 0, sizeof( zero ) );
4234
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004235 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004236 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004237 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004238 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004239 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004240 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004241 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004242
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004243 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004244 PSA_ASSERT( psa_key_derivation_abort(&func) );
4245 PSA_ASSERT( psa_key_derivation_abort(&init) );
4246 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004247}
4248/* END_CASE */
4249
Janos Follath16de4a42019-06-13 16:32:24 +01004250/* BEGIN_CASE */
4251void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004252{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004253 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004254 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004255 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004256
Gilles Peskine8817f612018-12-18 00:18:46 +01004257 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004258
Janos Follath16de4a42019-06-13 16:32:24 +01004259 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004260 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004261
4262exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004263 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004264 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004265}
4266/* END_CASE */
4267
Janos Follathaf3c2a02019-06-12 12:34:34 +01004268/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004269void derive_set_capacity( int alg_arg, int capacity_arg,
4270 int expected_status_arg )
4271{
4272 psa_algorithm_t alg = alg_arg;
4273 size_t capacity = capacity_arg;
4274 psa_status_t expected_status = expected_status_arg;
4275 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4276
4277 PSA_ASSERT( psa_crypto_init( ) );
4278
4279 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4280
4281 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4282 expected_status );
4283
4284exit:
4285 psa_key_derivation_abort( &operation );
4286 PSA_DONE( );
4287}
4288/* END_CASE */
4289
4290/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004291void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004292 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004293 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004294 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004295 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004296 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004297 int expected_status_arg3,
4298 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004299{
4300 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004301 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4302 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004303 psa_status_t expected_statuses[] = {expected_status_arg1,
4304 expected_status_arg2,
4305 expected_status_arg3};
4306 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004307 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4308 MBEDTLS_SVC_KEY_ID_INIT,
4309 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004310 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4311 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4312 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004313 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004314 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004315 psa_status_t expected_output_status = expected_output_status_arg;
4316 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004317
4318 PSA_ASSERT( psa_crypto_init( ) );
4319
4320 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4321 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004322
4323 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4324
4325 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4326 {
Gilles Peskine4023c012021-05-27 13:21:20 +02004327 mbedtls_test_set_step( i );
4328 if( steps[i] == 0 )
4329 {
4330 /* Skip this step */
4331 }
4332 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004333 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004334 psa_set_key_type( &attributes, key_types[i] );
4335 PSA_ASSERT( psa_import_key( &attributes,
4336 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004337 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004338 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4339 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4340 {
4341 // When taking a private key as secret input, use key agreement
4342 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004343 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4344 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004345 expected_statuses[i] );
4346 }
4347 else
4348 {
4349 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004350 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004351 expected_statuses[i] );
4352 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004353 }
4354 else
4355 {
4356 TEST_EQUAL( psa_key_derivation_input_bytes(
4357 &operation, steps[i],
4358 inputs[i]->x, inputs[i]->len ),
4359 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004360 }
4361 }
4362
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004363 if( output_key_type != PSA_KEY_TYPE_NONE )
4364 {
4365 psa_reset_key_attributes( &attributes );
4366 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4367 psa_set_key_bits( &attributes, 8 );
4368 actual_output_status =
4369 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004370 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004371 }
4372 else
4373 {
4374 uint8_t buffer[1];
4375 actual_output_status =
4376 psa_key_derivation_output_bytes( &operation,
4377 buffer, sizeof( buffer ) );
4378 }
4379 TEST_EQUAL( actual_output_status, expected_output_status );
4380
Janos Follathaf3c2a02019-06-12 12:34:34 +01004381exit:
4382 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004383 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4384 psa_destroy_key( keys[i] );
4385 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004386 PSA_DONE( );
4387}
4388/* END_CASE */
4389
Janos Follathd958bb72019-07-03 15:02:16 +01004390/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02004391void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004392{
Janos Follathd958bb72019-07-03 15:02:16 +01004393 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004394 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004395 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004396 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004397 unsigned char input1[] = "Input 1";
4398 size_t input1_length = sizeof( input1 );
4399 unsigned char input2[] = "Input 2";
4400 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004401 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004402 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004403 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4404 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4405 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004406 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004407
Gilles Peskine8817f612018-12-18 00:18:46 +01004408 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004409
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004410 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4411 psa_set_key_algorithm( &attributes, alg );
4412 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004413
Gilles Peskine73676cb2019-05-15 20:15:10 +02004414 PSA_ASSERT( psa_import_key( &attributes,
4415 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004416 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004417
4418 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004419 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4420 input1, input1_length,
4421 input2, input2_length,
4422 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004423 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004424
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004425 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004426 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004427 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004428
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004429 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004430
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004431 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004432 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004433
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004434exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004435 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004436 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004437 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004438}
4439/* END_CASE */
4440
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004441/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02004442void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004443{
4444 uint8_t output_buffer[16];
4445 size_t buffer_size = 16;
4446 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004447 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004448
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004449 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4450 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004451 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004452
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004453 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004454 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004455
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004456 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004457
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004458 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4459 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004460 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004461
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004462 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004463 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004464
4465exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004466 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004467}
4468/* END_CASE */
4469
4470/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004471void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004472 int step1_arg, data_t *input1,
4473 int step2_arg, data_t *input2,
4474 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004475 int requested_capacity_arg,
4476 data_t *expected_output1,
4477 data_t *expected_output2 )
4478{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004479 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004480 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4481 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004482 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4483 MBEDTLS_SVC_KEY_ID_INIT,
4484 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004485 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004486 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004487 uint8_t *expected_outputs[2] =
4488 {expected_output1->x, expected_output2->x};
4489 size_t output_sizes[2] =
4490 {expected_output1->len, expected_output2->len};
4491 size_t output_buffer_size = 0;
4492 uint8_t *output_buffer = NULL;
4493 size_t expected_capacity;
4494 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004495 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004496 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004497 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004498
4499 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4500 {
4501 if( output_sizes[i] > output_buffer_size )
4502 output_buffer_size = output_sizes[i];
4503 if( output_sizes[i] == 0 )
4504 expected_outputs[i] = NULL;
4505 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004506 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004507 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004508
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004509 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4510 psa_set_key_algorithm( &attributes, alg );
4511 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004512
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004513 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004514 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4515 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4516 requested_capacity ) );
4517 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004518 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004519 switch( steps[i] )
4520 {
4521 case 0:
4522 break;
4523 case PSA_KEY_DERIVATION_INPUT_SECRET:
4524 PSA_ASSERT( psa_import_key( &attributes,
4525 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004526 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004527
4528 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4529 {
4530 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4531 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4532 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4533 }
4534
Gilles Peskine1468da72019-05-29 17:35:49 +02004535 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004536 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004537 break;
4538 default:
4539 PSA_ASSERT( psa_key_derivation_input_bytes(
4540 &operation, steps[i],
4541 inputs[i]->x, inputs[i]->len ) );
4542 break;
4543 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004544 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004545
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004546 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004547 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004548 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004549 expected_capacity = requested_capacity;
4550
4551 /* Expansion phase. */
4552 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4553 {
4554 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004555 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004556 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004557 if( expected_capacity == 0 && output_sizes[i] == 0 )
4558 {
4559 /* Reading 0 bytes when 0 bytes are available can go either way. */
4560 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004561 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004562 continue;
4563 }
4564 else if( expected_capacity == 0 ||
4565 output_sizes[i] > expected_capacity )
4566 {
4567 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004568 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004569 expected_capacity = 0;
4570 continue;
4571 }
4572 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004573 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004574 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004575 ASSERT_COMPARE( output_buffer, output_sizes[i],
4576 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004577 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004578 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004579 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004580 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004581 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004582 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004583 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004584
4585exit:
4586 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004587 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004588 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4589 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004590 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004591}
4592/* END_CASE */
4593
4594/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004595void derive_full( int alg_arg,
4596 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004597 data_t *input1,
4598 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004599 int requested_capacity_arg )
4600{
Ronald Cron5425a212020-08-04 14:58:35 +02004601 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004602 psa_algorithm_t alg = alg_arg;
4603 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004604 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004605 unsigned char output_buffer[16];
4606 size_t expected_capacity = requested_capacity;
4607 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004608 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004609
Gilles Peskine8817f612018-12-18 00:18:46 +01004610 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004611
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004612 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4613 psa_set_key_algorithm( &attributes, alg );
4614 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004615
Gilles Peskine049c7532019-05-15 20:22:09 +02004616 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004617 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004618
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004619 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4620 input1->x, input1->len,
4621 input2->x, input2->len,
4622 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004623 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004624
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004625 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004626 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004627 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004628
4629 /* Expansion phase. */
4630 while( current_capacity > 0 )
4631 {
4632 size_t read_size = sizeof( output_buffer );
4633 if( read_size > current_capacity )
4634 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004635 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004636 output_buffer,
4637 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004638 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004639 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004640 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004641 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004642 }
4643
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004644 /* Check that the operation refuses to go over capacity. */
4645 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004646 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004647
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004648 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004649
4650exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004651 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004652 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004653 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004654}
4655/* END_CASE */
4656
Janos Follathe60c9052019-07-03 13:51:30 +01004657/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004658void derive_key_exercise( int alg_arg,
4659 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004660 data_t *input1,
4661 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004662 int derived_type_arg,
4663 int derived_bits_arg,
4664 int derived_usage_arg,
4665 int derived_alg_arg )
4666{
Ronald Cron5425a212020-08-04 14:58:35 +02004667 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4668 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004669 psa_algorithm_t alg = alg_arg;
4670 psa_key_type_t derived_type = derived_type_arg;
4671 size_t derived_bits = derived_bits_arg;
4672 psa_key_usage_t derived_usage = derived_usage_arg;
4673 psa_algorithm_t derived_alg = derived_alg_arg;
4674 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004675 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004676 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004677 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004678
Gilles Peskine8817f612018-12-18 00:18:46 +01004679 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004680
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004681 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4682 psa_set_key_algorithm( &attributes, alg );
4683 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004684 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004685 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004686
4687 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004688 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4689 input1->x, input1->len,
4690 input2->x, input2->len,
4691 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004692 goto exit;
4693
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004694 psa_set_key_usage_flags( &attributes, derived_usage );
4695 psa_set_key_algorithm( &attributes, derived_alg );
4696 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004697 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004698 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004699 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004700
4701 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004702 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004703 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4704 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004705
4706 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004707 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004708 goto exit;
4709
4710exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004711 /*
4712 * Key attributes may have been returned by psa_get_key_attributes()
4713 * thus reset them as required.
4714 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004715 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004716
4717 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004718 psa_destroy_key( base_key );
4719 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004720 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004721}
4722/* END_CASE */
4723
Janos Follath42fd8882019-07-03 14:17:09 +01004724/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004725void derive_key_export( int alg_arg,
4726 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004727 data_t *input1,
4728 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004729 int bytes1_arg,
4730 int bytes2_arg )
4731{
Ronald Cron5425a212020-08-04 14:58:35 +02004732 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4733 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004734 psa_algorithm_t alg = alg_arg;
4735 size_t bytes1 = bytes1_arg;
4736 size_t bytes2 = bytes2_arg;
4737 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004738 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004739 uint8_t *output_buffer = NULL;
4740 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004741 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4742 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004743 size_t length;
4744
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004745 ASSERT_ALLOC( output_buffer, capacity );
4746 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004747 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004748
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004749 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4750 psa_set_key_algorithm( &base_attributes, alg );
4751 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004752 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004753 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004754
4755 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004756 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4757 input1->x, input1->len,
4758 input2->x, input2->len,
4759 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004760 goto exit;
4761
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004762 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004763 output_buffer,
4764 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004765 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004766
4767 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004768 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4769 input1->x, input1->len,
4770 input2->x, input2->len,
4771 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004772 goto exit;
4773
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004774 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4775 psa_set_key_algorithm( &derived_attributes, 0 );
4776 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004777 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004778 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004779 &derived_key ) );
4780 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004781 export_buffer, bytes1,
4782 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004783 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004784 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004785 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004786 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004787 &derived_key ) );
4788 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004789 export_buffer + bytes1, bytes2,
4790 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004791 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004792
4793 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004794 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4795 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004796
4797exit:
4798 mbedtls_free( output_buffer );
4799 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004800 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004801 psa_destroy_key( base_key );
4802 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004803 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004804}
4805/* END_CASE */
4806
4807/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004808void derive_key( int alg_arg,
4809 data_t *key_data, data_t *input1, data_t *input2,
4810 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004811 int expected_status_arg,
4812 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004813{
Ronald Cron5425a212020-08-04 14:58:35 +02004814 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4815 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004816 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004817 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004818 size_t bits = bits_arg;
4819 psa_status_t expected_status = expected_status_arg;
4820 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4821 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4822 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4823
4824 PSA_ASSERT( psa_crypto_init( ) );
4825
4826 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4827 psa_set_key_algorithm( &base_attributes, alg );
4828 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4829 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004830 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004831
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004832 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4833 input1->x, input1->len,
4834 input2->x, input2->len,
4835 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004836 goto exit;
4837
4838 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4839 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004840 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004841 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004842
4843 psa_status_t status =
4844 psa_key_derivation_output_key( &derived_attributes,
4845 &operation,
4846 &derived_key );
4847 if( is_large_output > 0 )
4848 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4849 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004850
4851exit:
4852 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004853 psa_destroy_key( base_key );
4854 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004855 PSA_DONE( );
4856}
4857/* END_CASE */
4858
4859/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004860void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004861 int our_key_type_arg, int our_key_alg_arg,
4862 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004863 int expected_status_arg )
4864{
Ronald Cron5425a212020-08-04 14:58:35 +02004865 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004866 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004867 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004868 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004869 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004870 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004871 psa_status_t expected_status = expected_status_arg;
4872 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004873
Gilles Peskine8817f612018-12-18 00:18:46 +01004874 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004875
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004876 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004877 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004878 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004879 PSA_ASSERT( psa_import_key( &attributes,
4880 our_key_data->x, our_key_data->len,
4881 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004882
Gilles Peskine77f40d82019-04-11 21:27:06 +02004883 /* The tests currently include inputs that should fail at either step.
4884 * Test cases that fail at the setup step should be changed to call
4885 * key_derivation_setup instead, and this function should be renamed
4886 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004887 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004888 if( status == PSA_SUCCESS )
4889 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004890 TEST_EQUAL( psa_key_derivation_key_agreement(
4891 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4892 our_key,
4893 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004894 expected_status );
4895 }
4896 else
4897 {
4898 TEST_ASSERT( status == expected_status );
4899 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004900
4901exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004902 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004903 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004904 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004905}
4906/* END_CASE */
4907
4908/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004909void raw_key_agreement( int alg_arg,
4910 int our_key_type_arg, data_t *our_key_data,
4911 data_t *peer_key_data,
4912 data_t *expected_output )
4913{
Ronald Cron5425a212020-08-04 14:58:35 +02004914 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004915 psa_algorithm_t alg = alg_arg;
4916 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004917 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004918 unsigned char *output = NULL;
4919 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004920 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004921
4922 ASSERT_ALLOC( output, expected_output->len );
4923 PSA_ASSERT( psa_crypto_init( ) );
4924
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004925 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4926 psa_set_key_algorithm( &attributes, alg );
4927 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004928 PSA_ASSERT( psa_import_key( &attributes,
4929 our_key_data->x, our_key_data->len,
4930 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004931
gabor-mezei-armceface22021-01-21 12:26:17 +01004932 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4933 key_bits = psa_get_key_bits( &attributes );
4934
Gilles Peskinebe697d82019-05-16 18:00:41 +02004935 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4936 peer_key_data->x, peer_key_data->len,
4937 output, expected_output->len,
4938 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004939 ASSERT_COMPARE( output, output_length,
4940 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01004941 TEST_ASSERT( output_length <=
4942 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
4943 TEST_ASSERT( output_length <=
4944 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004945
4946exit:
4947 mbedtls_free( output );
4948 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004949 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004950}
4951/* END_CASE */
4952
4953/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004954void key_agreement_capacity( int alg_arg,
4955 int our_key_type_arg, data_t *our_key_data,
4956 data_t *peer_key_data,
4957 int expected_capacity_arg )
4958{
Ronald Cron5425a212020-08-04 14:58:35 +02004959 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004960 psa_algorithm_t alg = alg_arg;
4961 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004962 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004963 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004964 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004965 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004966
Gilles Peskine8817f612018-12-18 00:18:46 +01004967 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004968
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004969 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4970 psa_set_key_algorithm( &attributes, alg );
4971 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004972 PSA_ASSERT( psa_import_key( &attributes,
4973 our_key_data->x, our_key_data->len,
4974 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004975
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004976 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004977 PSA_ASSERT( psa_key_derivation_key_agreement(
4978 &operation,
4979 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4980 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004981 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4982 {
4983 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004984 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004985 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004986 NULL, 0 ) );
4987 }
Gilles Peskine59685592018-09-18 12:11:34 +02004988
Gilles Peskinebf491972018-10-25 22:36:12 +02004989 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004990 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004991 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004992 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004993
Gilles Peskinebf491972018-10-25 22:36:12 +02004994 /* Test the actual capacity by reading the output. */
4995 while( actual_capacity > sizeof( output ) )
4996 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004997 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004998 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004999 actual_capacity -= sizeof( output );
5000 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005001 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005002 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005003 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005004 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005005
Gilles Peskine59685592018-09-18 12:11:34 +02005006exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005007 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005008 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005009 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005010}
5011/* END_CASE */
5012
5013/* BEGIN_CASE */
5014void key_agreement_output( int alg_arg,
5015 int our_key_type_arg, data_t *our_key_data,
5016 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005017 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005018{
Ronald Cron5425a212020-08-04 14:58:35 +02005019 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005020 psa_algorithm_t alg = alg_arg;
5021 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005022 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005023 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005024 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005025
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005026 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5027 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005028
Gilles Peskine8817f612018-12-18 00:18:46 +01005029 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005030
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005031 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5032 psa_set_key_algorithm( &attributes, alg );
5033 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005034 PSA_ASSERT( psa_import_key( &attributes,
5035 our_key_data->x, our_key_data->len,
5036 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005037
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005038 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005039 PSA_ASSERT( psa_key_derivation_key_agreement(
5040 &operation,
5041 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5042 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005043 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5044 {
5045 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005046 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005047 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005048 NULL, 0 ) );
5049 }
Gilles Peskine59685592018-09-18 12:11:34 +02005050
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005051 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005052 actual_output,
5053 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005054 ASSERT_COMPARE( actual_output, expected_output1->len,
5055 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005056 if( expected_output2->len != 0 )
5057 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005058 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005059 actual_output,
5060 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005061 ASSERT_COMPARE( actual_output, expected_output2->len,
5062 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005063 }
Gilles Peskine59685592018-09-18 12:11:34 +02005064
5065exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005066 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005067 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005068 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005069 mbedtls_free( actual_output );
5070}
5071/* END_CASE */
5072
5073/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005074void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005075{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005076 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005077 unsigned char *output = NULL;
5078 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005079 size_t i;
5080 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005081
Simon Butcher49f8e312020-03-03 15:51:50 +00005082 TEST_ASSERT( bytes_arg >= 0 );
5083
Gilles Peskine91892022021-02-08 19:50:26 +01005084 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005085 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005086
Gilles Peskine8817f612018-12-18 00:18:46 +01005087 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005088
Gilles Peskinea50d7392018-06-21 10:22:13 +02005089 /* Run several times, to ensure that every output byte will be
5090 * nonzero at least once with overwhelming probability
5091 * (2^(-8*number_of_runs)). */
5092 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005093 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005094 if( bytes != 0 )
5095 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005096 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005097
Gilles Peskinea50d7392018-06-21 10:22:13 +02005098 for( i = 0; i < bytes; i++ )
5099 {
5100 if( output[i] != 0 )
5101 ++changed[i];
5102 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005103 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005104
5105 /* Check that every byte was changed to nonzero at least once. This
5106 * validates that psa_generate_random is overwriting every byte of
5107 * the output buffer. */
5108 for( i = 0; i < bytes; i++ )
5109 {
5110 TEST_ASSERT( changed[i] != 0 );
5111 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005112
5113exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005114 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005115 mbedtls_free( output );
5116 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005117}
5118/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005119
5120/* BEGIN_CASE */
5121void generate_key( int type_arg,
5122 int bits_arg,
5123 int usage_arg,
5124 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005125 int expected_status_arg,
5126 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005127{
Ronald Cron5425a212020-08-04 14:58:35 +02005128 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005129 psa_key_type_t type = type_arg;
5130 psa_key_usage_t usage = usage_arg;
5131 size_t bits = bits_arg;
5132 psa_algorithm_t alg = alg_arg;
5133 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005134 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005135 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005136
Gilles Peskine8817f612018-12-18 00:18:46 +01005137 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005138
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005139 psa_set_key_usage_flags( &attributes, usage );
5140 psa_set_key_algorithm( &attributes, alg );
5141 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005142 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005143
5144 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005145 psa_status_t status = psa_generate_key( &attributes, &key );
5146
5147 if( is_large_key > 0 )
5148 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5149 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005150 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005151 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005152
5153 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005154 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005155 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5156 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005157
Gilles Peskine818ca122018-06-20 18:16:48 +02005158 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005159 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005160 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005161
5162exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005163 /*
5164 * Key attributes may have been returned by psa_get_key_attributes()
5165 * thus reset them as required.
5166 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005167 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005168
Ronald Cron5425a212020-08-04 14:58:35 +02005169 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005170 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005171}
5172/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005173
Ronald Cronee414c72021-03-18 18:50:08 +01005174/* 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 +02005175void generate_key_rsa( int bits_arg,
5176 data_t *e_arg,
5177 int expected_status_arg )
5178{
Ronald Cron5425a212020-08-04 14:58:35 +02005179 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005180 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005181 size_t bits = bits_arg;
5182 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5183 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5184 psa_status_t expected_status = expected_status_arg;
5185 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5186 uint8_t *exported = NULL;
5187 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005188 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005189 size_t exported_length = SIZE_MAX;
5190 uint8_t *e_read_buffer = NULL;
5191 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005192 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005193 size_t e_read_length = SIZE_MAX;
5194
5195 if( e_arg->len == 0 ||
5196 ( e_arg->len == 3 &&
5197 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5198 {
5199 is_default_public_exponent = 1;
5200 e_read_size = 0;
5201 }
5202 ASSERT_ALLOC( e_read_buffer, e_read_size );
5203 ASSERT_ALLOC( exported, exported_size );
5204
5205 PSA_ASSERT( psa_crypto_init( ) );
5206
5207 psa_set_key_usage_flags( &attributes, usage );
5208 psa_set_key_algorithm( &attributes, alg );
5209 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5210 e_arg->x, e_arg->len ) );
5211 psa_set_key_bits( &attributes, bits );
5212
5213 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005214 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005215 if( expected_status != PSA_SUCCESS )
5216 goto exit;
5217
5218 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005219 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005220 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5221 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5222 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5223 e_read_buffer, e_read_size,
5224 &e_read_length ) );
5225 if( is_default_public_exponent )
5226 TEST_EQUAL( e_read_length, 0 );
5227 else
5228 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5229
5230 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005231 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005232 goto exit;
5233
5234 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005235 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005236 exported, exported_size,
5237 &exported_length ) );
5238 {
5239 uint8_t *p = exported;
5240 uint8_t *end = exported + exported_length;
5241 size_t len;
5242 /* RSAPublicKey ::= SEQUENCE {
5243 * modulus INTEGER, -- n
5244 * publicExponent INTEGER } -- e
5245 */
5246 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005247 MBEDTLS_ASN1_SEQUENCE |
5248 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005249 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005250 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5251 MBEDTLS_ASN1_INTEGER ) );
5252 if( len >= 1 && p[0] == 0 )
5253 {
5254 ++p;
5255 --len;
5256 }
5257 if( e_arg->len == 0 )
5258 {
5259 TEST_EQUAL( len, 3 );
5260 TEST_EQUAL( p[0], 1 );
5261 TEST_EQUAL( p[1], 0 );
5262 TEST_EQUAL( p[2], 1 );
5263 }
5264 else
5265 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5266 }
5267
5268exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005269 /*
5270 * Key attributes may have been returned by psa_get_key_attributes() or
5271 * set by psa_set_key_domain_parameters() thus reset them as required.
5272 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005273 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005274
Ronald Cron5425a212020-08-04 14:58:35 +02005275 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005276 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005277 mbedtls_free( e_read_buffer );
5278 mbedtls_free( exported );
5279}
5280/* END_CASE */
5281
Darryl Greend49a4992018-06-18 17:27:26 +01005282/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005283void persistent_key_load_key_from_storage( data_t *data,
5284 int type_arg, int bits_arg,
5285 int usage_flags_arg, int alg_arg,
5286 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005287{
Ronald Cron71016a92020-08-28 19:01:50 +02005288 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005289 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005290 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5291 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005292 psa_key_type_t type = type_arg;
5293 size_t bits = bits_arg;
5294 psa_key_usage_t usage_flags = usage_flags_arg;
5295 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005296 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005297 unsigned char *first_export = NULL;
5298 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005299 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005300 size_t first_exported_length;
5301 size_t second_exported_length;
5302
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005303 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5304 {
5305 ASSERT_ALLOC( first_export, export_size );
5306 ASSERT_ALLOC( second_export, export_size );
5307 }
Darryl Greend49a4992018-06-18 17:27:26 +01005308
Gilles Peskine8817f612018-12-18 00:18:46 +01005309 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005310
Gilles Peskinec87af662019-05-15 16:12:22 +02005311 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005312 psa_set_key_usage_flags( &attributes, usage_flags );
5313 psa_set_key_algorithm( &attributes, alg );
5314 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005315 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005316
Darryl Green0c6575a2018-11-07 16:05:30 +00005317 switch( generation_method )
5318 {
5319 case IMPORT_KEY:
5320 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005321 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005322 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005323 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005324
Darryl Green0c6575a2018-11-07 16:05:30 +00005325 case GENERATE_KEY:
5326 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005327 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005328 break;
5329
5330 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005331#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005332 {
5333 /* Create base key */
5334 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5335 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5336 psa_set_key_usage_flags( &base_attributes,
5337 PSA_KEY_USAGE_DERIVE );
5338 psa_set_key_algorithm( &base_attributes, derive_alg );
5339 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005340 PSA_ASSERT( psa_import_key( &base_attributes,
5341 data->x, data->len,
5342 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005343 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005344 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005345 PSA_ASSERT( psa_key_derivation_input_key(
5346 &operation,
5347 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005348 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005349 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005350 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005351 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5352 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005353 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005354 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005355 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005356 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005357 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005358#else
5359 TEST_ASSUME( ! "KDF not supported in this configuration" );
5360#endif
5361 break;
5362
5363 default:
5364 TEST_ASSERT( ! "generation_method not implemented in test" );
5365 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005366 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005367 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005368
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005369 /* Export the key if permitted by the key policy. */
5370 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5371 {
Ronald Cron5425a212020-08-04 14:58:35 +02005372 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005373 first_export, export_size,
5374 &first_exported_length ) );
5375 if( generation_method == IMPORT_KEY )
5376 ASSERT_COMPARE( data->x, data->len,
5377 first_export, first_exported_length );
5378 }
Darryl Greend49a4992018-06-18 17:27:26 +01005379
5380 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005381 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005382 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005383 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005384
Darryl Greend49a4992018-06-18 17:27:26 +01005385 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005386 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005387 TEST_ASSERT( mbedtls_svc_key_id_equal(
5388 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005389 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5390 PSA_KEY_LIFETIME_PERSISTENT );
5391 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5392 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02005393 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02005394 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005395 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005396
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005397 /* Export the key again if permitted by the key policy. */
5398 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005399 {
Ronald Cron5425a212020-08-04 14:58:35 +02005400 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005401 second_export, export_size,
5402 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005403 ASSERT_COMPARE( first_export, first_exported_length,
5404 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005405 }
5406
5407 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005408 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005409 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005410
5411exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005412 /*
5413 * Key attributes may have been returned by psa_get_key_attributes()
5414 * thus reset them as required.
5415 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005416 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005417
Darryl Greend49a4992018-06-18 17:27:26 +01005418 mbedtls_free( first_export );
5419 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005420 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005421 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005422 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005423 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005424}
5425/* END_CASE */