blob: 85999a1c8870e1fdb960fe294ebcab60a45a8019 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010018
Gilles Peskine4023c012021-05-27 13:21:20 +020019/* If this comes up, it's a bug in the test code or in the test data. */
20#define UNUSED 0xdeadbeef
21
Dave Rodgman647791d2021-06-23 12:49:59 +010022/* Assert that an operation is (not) active.
23 * This serves as a proxy for checking if the operation is aborted. */
24#define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 )
25#define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 )
26
Jaeden Amerof24c7f82018-06-27 17:20:43 +010027/** An invalid export length that will never be set by psa_export_key(). */
28static const size_t INVALID_EXPORT_LENGTH = ~0U;
29
Gilles Peskinea7aa4422018-08-14 15:17:54 +020030/** Test if a buffer contains a constant byte value.
31 *
32 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020033 *
34 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020035 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020036 * \param size Size of the buffer in bytes.
37 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020038 * \return 1 if the buffer is all-bits-zero.
39 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020040 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042{
43 size_t i;
44 for( i = 0; i < size; i++ )
45 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020046 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020047 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020048 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020049 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020050}
Gilles Peskine818ca122018-06-20 18:16:48 +020051
Gilles Peskine0b352bc2018-06-28 00:16:11 +020052/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
53static int asn1_write_10x( unsigned char **p,
54 unsigned char *start,
55 size_t bits,
56 unsigned char x )
57{
58 int ret;
59 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020060 if( bits == 0 )
61 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
62 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020063 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030064 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020065 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
66 *p -= len;
67 ( *p )[len-1] = x;
68 if( bits % 8 == 0 )
69 ( *p )[1] |= 1;
70 else
71 ( *p )[0] |= 1 << ( bits % 8 );
72 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
73 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
74 MBEDTLS_ASN1_INTEGER ) );
75 return( len );
76}
77
78static int construct_fake_rsa_key( unsigned char *buffer,
79 size_t buffer_size,
80 unsigned char **p,
81 size_t bits,
82 int keypair )
83{
84 size_t half_bits = ( bits + 1 ) / 2;
85 int ret;
86 int len = 0;
87 /* Construct something that looks like a DER encoding of
88 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
89 * RSAPrivateKey ::= SEQUENCE {
90 * version Version,
91 * modulus INTEGER, -- n
92 * publicExponent INTEGER, -- e
93 * privateExponent INTEGER, -- d
94 * prime1 INTEGER, -- p
95 * prime2 INTEGER, -- q
96 * exponent1 INTEGER, -- d mod (p-1)
97 * exponent2 INTEGER, -- d mod (q-1)
98 * coefficient INTEGER, -- (inverse of q) mod p
99 * otherPrimeInfos OtherPrimeInfos OPTIONAL
100 * }
101 * Or, for a public key, the same structure with only
102 * version, modulus and publicExponent.
103 */
104 *p = buffer + buffer_size;
105 if( keypair )
106 {
107 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
108 asn1_write_10x( p, buffer, half_bits, 1 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
110 asn1_write_10x( p, buffer, half_bits, 1 ) );
111 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
112 asn1_write_10x( p, buffer, half_bits, 1 ) );
113 MBEDTLS_ASN1_CHK_ADD( len, /* q */
114 asn1_write_10x( p, buffer, half_bits, 1 ) );
115 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
116 asn1_write_10x( p, buffer, half_bits, 3 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* d */
118 asn1_write_10x( p, buffer, bits, 1 ) );
119 }
120 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
121 asn1_write_10x( p, buffer, 17, 1 ) );
122 MBEDTLS_ASN1_CHK_ADD( len, /* n */
123 asn1_write_10x( p, buffer, bits, 1 ) );
124 if( keypair )
125 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
126 mbedtls_asn1_write_int( p, buffer, 0 ) );
127 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
128 {
129 const unsigned char tag =
130 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
131 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
132 }
133 return( len );
134}
135
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100136int exercise_mac_setup( psa_key_type_t key_type,
137 const unsigned char *key_bytes,
138 size_t key_length,
139 psa_algorithm_t alg,
140 psa_mac_operation_t *operation,
141 psa_status_t *status )
142{
Ronald Cron5425a212020-08-04 14:58:35 +0200143 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200144 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100145
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100146 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200147 psa_set_key_algorithm( &attributes, alg );
148 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200149 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100150
Ronald Cron5425a212020-08-04 14:58:35 +0200151 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100152 /* Whether setup succeeded or failed, abort must succeed. */
153 PSA_ASSERT( psa_mac_abort( operation ) );
154 /* If setup failed, reproduce the failure, so that the caller can
155 * test the resulting state of the operation object. */
156 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100157 {
Ronald Cron5425a212020-08-04 14:58:35 +0200158 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100159 }
160
Ronald Cron5425a212020-08-04 14:58:35 +0200161 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100162 return( 1 );
163
164exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200165 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100166 return( 0 );
167}
168
169int exercise_cipher_setup( psa_key_type_t key_type,
170 const unsigned char *key_bytes,
171 size_t key_length,
172 psa_algorithm_t alg,
173 psa_cipher_operation_t *operation,
174 psa_status_t *status )
175{
Ronald Cron5425a212020-08-04 14:58:35 +0200176 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200177 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100178
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200179 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
180 psa_set_key_algorithm( &attributes, alg );
181 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200182 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100183
Ronald Cron5425a212020-08-04 14:58:35 +0200184 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100185 /* Whether setup succeeded or failed, abort must succeed. */
186 PSA_ASSERT( psa_cipher_abort( operation ) );
187 /* If setup failed, reproduce the failure, so that the caller can
188 * test the resulting state of the operation object. */
189 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100190 {
Ronald Cron5425a212020-08-04 14:58:35 +0200191 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100192 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100193 }
194
Ronald Cron5425a212020-08-04 14:58:35 +0200195 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100196 return( 1 );
197
198exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200199 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100200 return( 0 );
201}
202
Ronald Cron5425a212020-08-04 14:58:35 +0200203static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200204{
205 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200206 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200207 uint8_t buffer[1];
208 size_t length;
209 int ok = 0;
210
Ronald Cronecfb2372020-07-23 17:13:42 +0200211 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200212 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
213 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
214 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200215 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000216 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200217 TEST_EQUAL(
218 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
219 TEST_EQUAL(
220 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200221 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200222 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
223 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
224 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
225 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
226
Ronald Cron5425a212020-08-04 14:58:35 +0200227 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000228 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200229 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200230 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000231 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200232
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200233 ok = 1;
234
235exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100236 /*
237 * Key attributes may have been returned by psa_get_key_attributes()
238 * thus reset them as required.
239 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200240 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100241
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200242 return( ok );
243}
244
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200245/* Assert that a key isn't reported as having a slot number. */
246#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
247#define ASSERT_NO_SLOT_NUMBER( attributes ) \
248 do \
249 { \
250 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
251 TEST_EQUAL( psa_get_key_slot_number( \
252 attributes, \
253 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
254 PSA_ERROR_INVALID_ARGUMENT ); \
255 } \
256 while( 0 )
257#else /* MBEDTLS_PSA_CRYPTO_SE_C */
258#define ASSERT_NO_SLOT_NUMBER( attributes ) \
259 ( (void) 0 )
260#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
261
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100262/* An overapproximation of the amount of storage needed for a key of the
263 * given type and with the given content. The API doesn't make it easy
264 * to find a good value for the size. The current implementation doesn't
265 * care about the value anyway. */
266#define KEY_BITS_FROM_DATA( type, data ) \
267 ( data )->len
268
Darryl Green0c6575a2018-11-07 16:05:30 +0000269typedef enum {
270 IMPORT_KEY = 0,
271 GENERATE_KEY = 1,
272 DERIVE_KEY = 2
273} generate_method;
274
Gilles Peskinee59236f2018-01-27 23:32:46 +0100275/* END_HEADER */
276
277/* BEGIN_DEPENDENCIES
278 * depends_on:MBEDTLS_PSA_CRYPTO_C
279 * END_DEPENDENCIES
280 */
281
282/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200283void static_checks( )
284{
285 size_t max_truncated_mac_size =
286 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
287
288 /* Check that the length for a truncated MAC always fits in the algorithm
289 * encoding. The shifted mask is the maximum truncated value. The
290 * untruncated algorithm may be one byte larger. */
291 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
292}
293/* END_CASE */
294
295/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200296void import_with_policy( int type_arg,
297 int usage_arg, int alg_arg,
298 int expected_status_arg )
299{
300 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
301 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200302 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200303 psa_key_type_t type = type_arg;
304 psa_key_usage_t usage = usage_arg;
305 psa_algorithm_t alg = alg_arg;
306 psa_status_t expected_status = expected_status_arg;
307 const uint8_t key_material[16] = {0};
308 psa_status_t status;
309
310 PSA_ASSERT( psa_crypto_init( ) );
311
312 psa_set_key_type( &attributes, type );
313 psa_set_key_usage_flags( &attributes, usage );
314 psa_set_key_algorithm( &attributes, alg );
315
316 status = psa_import_key( &attributes,
317 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200318 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200319 TEST_EQUAL( status, expected_status );
320 if( status != PSA_SUCCESS )
321 goto exit;
322
Ronald Cron5425a212020-08-04 14:58:35 +0200323 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200324 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200325 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200326 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200327 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200328 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200329
Ronald Cron5425a212020-08-04 14:58:35 +0200330 PSA_ASSERT( psa_destroy_key( key ) );
331 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200332
333exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100334 /*
335 * Key attributes may have been returned by psa_get_key_attributes()
336 * thus reset them as required.
337 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200338 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100339
340 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200341 PSA_DONE( );
342}
343/* END_CASE */
344
345/* BEGIN_CASE */
346void import_with_data( data_t *data, int type_arg,
347 int attr_bits_arg,
348 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200349{
350 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
351 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200352 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200353 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200354 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200355 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100356 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100357
Gilles Peskine8817f612018-12-18 00:18:46 +0100358 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100359
Gilles Peskine4747d192019-04-17 15:05:45 +0200360 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200361 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200362
Ronald Cron5425a212020-08-04 14:58:35 +0200363 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100364 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200365 if( status != PSA_SUCCESS )
366 goto exit;
367
Ronald Cron5425a212020-08-04 14:58:35 +0200368 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200369 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200370 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200371 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200372 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200373
Ronald Cron5425a212020-08-04 14:58:35 +0200374 PSA_ASSERT( psa_destroy_key( key ) );
375 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100376
377exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100378 /*
379 * Key attributes may have been returned by psa_get_key_attributes()
380 * thus reset them as required.
381 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200382 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100383
384 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200385 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100386}
387/* END_CASE */
388
389/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200390void import_large_key( int type_arg, int byte_size_arg,
391 int expected_status_arg )
392{
393 psa_key_type_t type = type_arg;
394 size_t byte_size = byte_size_arg;
395 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
396 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200397 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200398 psa_status_t status;
399 uint8_t *buffer = NULL;
400 size_t buffer_size = byte_size + 1;
401 size_t n;
402
Steven Cooreman69967ce2021-01-18 18:01:08 +0100403 /* Skip the test case if the target running the test cannot
404 * accomodate large keys due to heap size constraints */
405 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200406 memset( buffer, 'K', byte_size );
407
408 PSA_ASSERT( psa_crypto_init( ) );
409
410 /* Try importing the key */
411 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
412 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200413 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100414 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200415 TEST_EQUAL( status, expected_status );
416
417 if( status == PSA_SUCCESS )
418 {
Ronald Cron5425a212020-08-04 14:58:35 +0200419 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200420 TEST_EQUAL( psa_get_key_type( &attributes ), type );
421 TEST_EQUAL( psa_get_key_bits( &attributes ),
422 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200423 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200424 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200425 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200426 for( n = 0; n < byte_size; n++ )
427 TEST_EQUAL( buffer[n], 'K' );
428 for( n = byte_size; n < buffer_size; n++ )
429 TEST_EQUAL( buffer[n], 0 );
430 }
431
432exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100433 /*
434 * Key attributes may have been returned by psa_get_key_attributes()
435 * thus reset them as required.
436 */
437 psa_reset_key_attributes( &attributes );
438
Ronald Cron5425a212020-08-04 14:58:35 +0200439 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200440 PSA_DONE( );
441 mbedtls_free( buffer );
442}
443/* END_CASE */
444
445/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200446void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
447{
Ronald Cron5425a212020-08-04 14:58:35 +0200448 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200449 size_t bits = bits_arg;
450 psa_status_t expected_status = expected_status_arg;
451 psa_status_t status;
452 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200453 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200454 size_t buffer_size = /* Slight overapproximations */
455 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200456 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200457 unsigned char *p;
458 int ret;
459 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200460 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200461
Gilles Peskine8817f612018-12-18 00:18:46 +0100462 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200463 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200464
465 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
466 bits, keypair ) ) >= 0 );
467 length = ret;
468
469 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200470 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200471 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100472 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200473
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200474 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200475 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200476
477exit:
478 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200479 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200480}
481/* END_CASE */
482
483/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300484void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300485 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200486 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100487 int expected_bits,
488 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200489 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100490 int canonical_input )
491{
Ronald Cron5425a212020-08-04 14:58:35 +0200492 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100493 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200494 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200495 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100496 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100497 unsigned char *exported = NULL;
498 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100499 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100500 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100501 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200502 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200503 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100504
Moran Pekercb088e72018-07-17 17:36:59 +0300505 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200506 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100507 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200508 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100509 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100510
Gilles Peskine4747d192019-04-17 15:05:45 +0200511 psa_set_key_usage_flags( &attributes, usage_arg );
512 psa_set_key_algorithm( &attributes, alg );
513 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700514
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100515 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200516 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100517
518 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200519 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200520 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
521 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200522 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100523
524 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200525 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100526 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100527
528 /* The exported length must be set by psa_export_key() to a value between 0
529 * and export_size. On errors, the exported length must be 0. */
530 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
531 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
532 TEST_ASSERT( exported_length <= export_size );
533
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200534 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200535 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100536 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200537 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100538 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100539 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200540 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100541
Gilles Peskineea38a922021-02-13 00:05:16 +0100542 /* Run sanity checks on the exported key. For non-canonical inputs,
543 * this validates the canonical representations. For canonical inputs,
544 * this doesn't directly validate the implementation, but it still helps
545 * by cross-validating the test data with the sanity check code. */
546 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200547 goto exit;
548
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100549 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200550 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100551 else
552 {
Ronald Cron5425a212020-08-04 14:58:35 +0200553 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200554 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200555 &key2 ) );
556 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100557 reexported,
558 export_size,
559 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200560 ASSERT_COMPARE( exported, exported_length,
561 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200562 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100563 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100564 TEST_ASSERT( exported_length <=
565 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
566 psa_get_key_bits( &got_attributes ) ) );
567 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100568
569destroy:
570 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200571 PSA_ASSERT( psa_destroy_key( key ) );
572 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100573
574exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100575 /*
576 * Key attributes may have been returned by psa_get_key_attributes()
577 * thus reset them as required.
578 */
579 psa_reset_key_attributes( &got_attributes );
580
itayzafrir3e02b3b2018-06-12 17:06:52 +0300581 mbedtls_free( exported );
582 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200583 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100584}
585/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100586
Moran Pekerf709f4a2018-06-06 17:26:04 +0300587/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300588void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200589 int type_arg,
590 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100591 int export_size_delta,
592 int expected_export_status_arg,
593 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300594{
Ronald Cron5425a212020-08-04 14:58:35 +0200595 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300596 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200597 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200598 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300599 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300600 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100601 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100602 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200603 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300604
Gilles Peskine8817f612018-12-18 00:18:46 +0100605 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300606
Gilles Peskine4747d192019-04-17 15:05:45 +0200607 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
608 psa_set_key_algorithm( &attributes, alg );
609 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300610
611 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200612 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300613
Gilles Peskine49c25912018-10-29 15:15:31 +0100614 /* Export the public key */
615 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200616 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200617 exported, export_size,
618 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100619 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100620 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100621 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200622 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100623 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200624 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200625 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100626 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100627 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100628 TEST_ASSERT( expected_public_key->len <=
629 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
630 TEST_ASSERT( expected_public_key->len <=
631 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100632 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
633 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100634 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300635
636exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100637 /*
638 * Key attributes may have been returned by psa_get_key_attributes()
639 * thus reset them as required.
640 */
641 psa_reset_key_attributes( &attributes );
642
itayzafrir3e02b3b2018-06-12 17:06:52 +0300643 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200644 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200645 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300646}
647/* END_CASE */
648
Gilles Peskine20035e32018-02-03 22:44:14 +0100649/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200650void import_and_exercise_key( data_t *data,
651 int type_arg,
652 int bits_arg,
653 int alg_arg )
654{
Ronald Cron5425a212020-08-04 14:58:35 +0200655 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200656 psa_key_type_t type = type_arg;
657 size_t bits = bits_arg;
658 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100659 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200660 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200661 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200662
Gilles Peskine8817f612018-12-18 00:18:46 +0100663 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200664
Gilles Peskine4747d192019-04-17 15:05:45 +0200665 psa_set_key_usage_flags( &attributes, usage );
666 psa_set_key_algorithm( &attributes, alg );
667 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200668
669 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200670 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200671
672 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200673 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200674 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
675 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200676
677 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100678 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200679 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200680
Ronald Cron5425a212020-08-04 14:58:35 +0200681 PSA_ASSERT( psa_destroy_key( key ) );
682 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200683
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200684exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100685 /*
686 * Key attributes may have been returned by psa_get_key_attributes()
687 * thus reset them as required.
688 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200689 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100690
691 psa_reset_key_attributes( &attributes );
692 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200693 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200694}
695/* END_CASE */
696
697/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100698void effective_key_attributes( int type_arg, int expected_type_arg,
699 int bits_arg, int expected_bits_arg,
700 int usage_arg, int expected_usage_arg,
701 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200702{
Ronald Cron5425a212020-08-04 14:58:35 +0200703 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100704 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100705 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100706 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100707 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200708 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100709 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200710 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100711 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200712 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200713
Gilles Peskine8817f612018-12-18 00:18:46 +0100714 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200715
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200716 psa_set_key_usage_flags( &attributes, usage );
717 psa_set_key_algorithm( &attributes, alg );
718 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +0100719 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +0200720
Ronald Cron5425a212020-08-04 14:58:35 +0200721 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +0100722 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +0200723
Ronald Cron5425a212020-08-04 14:58:35 +0200724 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +0100725 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
726 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
727 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
728 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200729
730exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100731 /*
732 * Key attributes may have been returned by psa_get_key_attributes()
733 * thus reset them as required.
734 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200735 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100736
737 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200738 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +0200739}
740/* END_CASE */
741
742/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100743void check_key_policy( int type_arg, int bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +0200744 int usage_arg, int alg_arg )
Gilles Peskine06c28892019-11-26 18:07:46 +0100745{
746 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +0200747 usage_arg,
748 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200749 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +0100750 goto exit;
751}
752/* END_CASE */
753
754/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200755void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +0000756{
757 /* Test each valid way of initializing the object, except for `= {0}`, as
758 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
759 * though it's OK by the C standard. We could test for this, but we'd need
760 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200761 psa_key_attributes_t func = psa_key_attributes_init( );
762 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
763 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000764
765 memset( &zero, 0, sizeof( zero ) );
766
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200767 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
768 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
769 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000770
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200771 TEST_EQUAL( psa_get_key_type( &func ), 0 );
772 TEST_EQUAL( psa_get_key_type( &init ), 0 );
773 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
774
775 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
776 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
777 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
778
779 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
780 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
781 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
782
783 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
784 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
785 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +0000786}
787/* END_CASE */
788
789/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200790void mac_key_policy( int policy_usage_arg,
791 int policy_alg_arg,
792 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200793 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200794 int exercise_alg_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100795 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200796{
Ronald Cron5425a212020-08-04 14:58:35 +0200797 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200798 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000799 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200800 psa_key_type_t key_type = key_type_arg;
801 psa_algorithm_t policy_alg = policy_alg_arg;
802 psa_algorithm_t exercise_alg = exercise_alg_arg;
803 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200804 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100805 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200806 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200807
Gilles Peskine8817f612018-12-18 00:18:46 +0100808 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200809
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200810 psa_set_key_usage_flags( &attributes, policy_usage );
811 psa_set_key_algorithm( &attributes, policy_alg );
812 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200813
Gilles Peskine049c7532019-05-15 20:22:09 +0200814 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200815 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200816
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +0200817 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
818 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200819
Ronald Cron5425a212020-08-04 14:58:35 +0200820 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100821 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100822 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100823 else
824 TEST_EQUAL( status, expected_status );
825
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200826 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200827
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200828 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200829 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100830 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100831 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100832 else
833 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200834
835exit:
836 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200837 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200838 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200839}
840/* END_CASE */
841
842/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200843void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200844 int policy_alg,
845 int key_type,
846 data_t *key_data,
847 int exercise_alg )
848{
Ronald Cron5425a212020-08-04 14:58:35 +0200849 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200850 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000851 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200852 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200853 psa_status_t status;
854
Gilles Peskine8817f612018-12-18 00:18:46 +0100855 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200856
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200857 psa_set_key_usage_flags( &attributes, policy_usage );
858 psa_set_key_algorithm( &attributes, policy_alg );
859 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200860
Gilles Peskine049c7532019-05-15 20:22:09 +0200861 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200862 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200863
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200864 /* Check if no key usage flag implication is done */
865 TEST_EQUAL( policy_usage,
866 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200867
Ronald Cron5425a212020-08-04 14:58:35 +0200868 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200869 if( policy_alg == exercise_alg &&
870 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100871 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200872 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100873 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200874 psa_cipher_abort( &operation );
875
Ronald Cron5425a212020-08-04 14:58:35 +0200876 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200877 if( policy_alg == exercise_alg &&
878 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100879 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200880 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100881 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200882
883exit:
884 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200885 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200886 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200887}
888/* END_CASE */
889
890/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200891void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200892 int policy_alg,
893 int key_type,
894 data_t *key_data,
895 int nonce_length_arg,
896 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100897 int exercise_alg,
898 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200899{
Ronald Cron5425a212020-08-04 14:58:35 +0200900 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200901 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200902 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200903 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100904 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200905 unsigned char nonce[16] = {0};
906 size_t nonce_length = nonce_length_arg;
907 unsigned char tag[16];
908 size_t tag_length = tag_length_arg;
909 size_t output_length;
910
911 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
912 TEST_ASSERT( tag_length <= sizeof( tag ) );
913
Gilles Peskine8817f612018-12-18 00:18:46 +0100914 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200915
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200916 psa_set_key_usage_flags( &attributes, policy_usage );
917 psa_set_key_algorithm( &attributes, policy_alg );
918 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200919
Gilles Peskine049c7532019-05-15 20:22:09 +0200920 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200921 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200922
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200923 /* Check if no key usage implication is done */
924 TEST_EQUAL( policy_usage,
925 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200926
Ronald Cron5425a212020-08-04 14:58:35 +0200927 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200928 nonce, nonce_length,
929 NULL, 0,
930 NULL, 0,
931 tag, tag_length,
932 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100933 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
934 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200935 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100936 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200937
938 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200939 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200940 nonce, nonce_length,
941 NULL, 0,
942 tag, tag_length,
943 NULL, 0,
944 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100945 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
946 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
947 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100948 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200949 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100950 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200951
952exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200953 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200954 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200955}
956/* END_CASE */
957
958/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200959void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200960 int policy_alg,
961 int key_type,
962 data_t *key_data,
963 int exercise_alg )
964{
Ronald Cron5425a212020-08-04 14:58:35 +0200965 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200966 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200967 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200968 psa_status_t status;
969 size_t key_bits;
970 size_t buffer_length;
971 unsigned char *buffer = NULL;
972 size_t output_length;
973
Gilles Peskine8817f612018-12-18 00:18:46 +0100974 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200975
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200976 psa_set_key_usage_flags( &attributes, policy_usage );
977 psa_set_key_algorithm( &attributes, policy_alg );
978 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200979
Gilles Peskine049c7532019-05-15 20:22:09 +0200980 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200981 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200982
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200983 /* Check if no key usage implication is done */
984 TEST_EQUAL( policy_usage,
985 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200986
Ronald Cron5425a212020-08-04 14:58:35 +0200987 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200988 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200989 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
990 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200991 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200992
Ronald Cron5425a212020-08-04 14:58:35 +0200993 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200994 NULL, 0,
995 NULL, 0,
996 buffer, buffer_length,
997 &output_length );
998 if( policy_alg == exercise_alg &&
999 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001000 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001001 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001002 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001003
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001004 if( buffer_length != 0 )
1005 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001006 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001007 buffer, buffer_length,
1008 NULL, 0,
1009 buffer, buffer_length,
1010 &output_length );
1011 if( policy_alg == exercise_alg &&
1012 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001013 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001014 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001015 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001016
1017exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001018 /*
1019 * Key attributes may have been returned by psa_get_key_attributes()
1020 * thus reset them as required.
1021 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001022 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001023
1024 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001025 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001026 mbedtls_free( buffer );
1027}
1028/* END_CASE */
1029
1030/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001031void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001032 int policy_alg,
1033 int key_type,
1034 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001035 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001036 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001037 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001038{
Ronald Cron5425a212020-08-04 14:58:35 +02001039 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001040 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001041 psa_key_usage_t policy_usage = policy_usage_arg;
1042 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001043 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001044 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1045 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1046 * compatible with the policy and `payload_length_arg` is supposed to be
1047 * a valid input length to sign. If `payload_length_arg <= 0`,
1048 * `exercise_alg` is supposed to be forbidden by the policy. */
1049 int compatible_alg = payload_length_arg > 0;
1050 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001051 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001052 size_t signature_length;
1053
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001054 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001055 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001056 TEST_EQUAL( expected_usage,
1057 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001058
Gilles Peskine8817f612018-12-18 00:18:46 +01001059 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001060
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001061 psa_set_key_usage_flags( &attributes, policy_usage );
1062 psa_set_key_algorithm( &attributes, policy_alg );
1063 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001064
Gilles Peskine049c7532019-05-15 20:22:09 +02001065 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001066 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001067
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001068 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1069
Ronald Cron5425a212020-08-04 14:58:35 +02001070 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001071 payload, payload_length,
1072 signature, sizeof( signature ),
1073 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001074 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001075 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001076 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001077 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001078
1079 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001080 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001081 payload, payload_length,
1082 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001083 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001084 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001085 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001086 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001087
gabor-mezei-armd851d682021-06-28 14:53:49 +02001088 if( PSA_ALG_IS_HASH_AND_SIGN( exercise_alg ) &&
1089 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001090 {
1091 status = psa_sign_message( key, exercise_alg,
1092 payload, payload_length,
1093 signature, sizeof( signature ),
1094 &signature_length );
1095 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1096 PSA_ASSERT( status );
1097 else
1098 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1099
1100 memset( signature, 0, sizeof( signature ) );
1101 status = psa_verify_message( key, exercise_alg,
1102 payload, payload_length,
1103 signature, sizeof( signature ) );
1104 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1105 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1106 else
1107 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1108 }
1109
Gilles Peskined5b33222018-06-18 22:20:03 +02001110exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001111 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001112 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001113}
1114/* END_CASE */
1115
Janos Follathba3fab92019-06-11 14:50:16 +01001116/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001117void derive_key_policy( int policy_usage,
1118 int policy_alg,
1119 int key_type,
1120 data_t *key_data,
1121 int exercise_alg )
1122{
Ronald Cron5425a212020-08-04 14:58:35 +02001123 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001124 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001125 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001126 psa_status_t status;
1127
Gilles Peskine8817f612018-12-18 00:18:46 +01001128 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001129
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001130 psa_set_key_usage_flags( &attributes, policy_usage );
1131 psa_set_key_algorithm( &attributes, policy_alg );
1132 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001133
Gilles Peskine049c7532019-05-15 20:22:09 +02001134 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001135 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001136
Janos Follathba3fab92019-06-11 14:50:16 +01001137 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1138
1139 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1140 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001141 {
Janos Follathba3fab92019-06-11 14:50:16 +01001142 PSA_ASSERT( psa_key_derivation_input_bytes(
1143 &operation,
1144 PSA_KEY_DERIVATION_INPUT_SEED,
1145 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001146 }
Janos Follathba3fab92019-06-11 14:50:16 +01001147
1148 status = psa_key_derivation_input_key( &operation,
1149 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001150 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001151
Gilles Peskineea0fb492018-07-12 17:17:20 +02001152 if( policy_alg == exercise_alg &&
1153 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001154 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001155 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001156 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001157
1158exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001159 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001160 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001161 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001162}
1163/* END_CASE */
1164
1165/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001166void agreement_key_policy( int policy_usage,
1167 int policy_alg,
1168 int key_type_arg,
1169 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001170 int exercise_alg,
1171 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001172{
Ronald Cron5425a212020-08-04 14:58:35 +02001173 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001174 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001175 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001176 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001177 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001178 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001179
Gilles Peskine8817f612018-12-18 00:18:46 +01001180 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001181
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001182 psa_set_key_usage_flags( &attributes, policy_usage );
1183 psa_set_key_algorithm( &attributes, policy_alg );
1184 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001185
Gilles Peskine049c7532019-05-15 20:22:09 +02001186 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001187 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001188
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001189 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001190 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001191
Steven Cooremance48e852020-10-05 16:02:45 +02001192 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001193
1194exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001195 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001196 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001197 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001198}
1199/* END_CASE */
1200
1201/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001202void key_policy_alg2( int key_type_arg, data_t *key_data,
1203 int usage_arg, int alg_arg, int alg2_arg )
1204{
Ronald Cron5425a212020-08-04 14:58:35 +02001205 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001206 psa_key_type_t key_type = key_type_arg;
1207 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1208 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1209 psa_key_usage_t usage = usage_arg;
1210 psa_algorithm_t alg = alg_arg;
1211 psa_algorithm_t alg2 = alg2_arg;
1212
1213 PSA_ASSERT( psa_crypto_init( ) );
1214
1215 psa_set_key_usage_flags( &attributes, usage );
1216 psa_set_key_algorithm( &attributes, alg );
1217 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1218 psa_set_key_type( &attributes, key_type );
1219 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001220 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001221
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001222 /* Update the usage flags to obtain implicit usage flags */
1223 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001224 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001225 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1226 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1227 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1228
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001229 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001230 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001231 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001232 goto exit;
1233
1234exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001235 /*
1236 * Key attributes may have been returned by psa_get_key_attributes()
1237 * thus reset them as required.
1238 */
1239 psa_reset_key_attributes( &got_attributes );
1240
Ronald Cron5425a212020-08-04 14:58:35 +02001241 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001242 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001243}
1244/* END_CASE */
1245
1246/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001247void raw_agreement_key_policy( int policy_usage,
1248 int policy_alg,
1249 int key_type_arg,
1250 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001251 int exercise_alg,
1252 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001253{
Ronald Cron5425a212020-08-04 14:58:35 +02001254 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001255 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001256 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001257 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001258 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001259 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001260
1261 PSA_ASSERT( psa_crypto_init( ) );
1262
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001263 psa_set_key_usage_flags( &attributes, policy_usage );
1264 psa_set_key_algorithm( &attributes, policy_alg );
1265 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001266
Gilles Peskine049c7532019-05-15 20:22:09 +02001267 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001268 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001269
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001270 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001271
Steven Cooremance48e852020-10-05 16:02:45 +02001272 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001273
1274exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001275 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001276 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001277 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001278}
1279/* END_CASE */
1280
1281/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001282void copy_success( int source_usage_arg,
1283 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001284 int type_arg, data_t *material,
1285 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001286 int target_usage_arg,
1287 int target_alg_arg, int target_alg2_arg,
1288 int expected_usage_arg,
1289 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001290{
Gilles Peskineca25db92019-04-19 11:43:08 +02001291 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1292 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001293 psa_key_usage_t expected_usage = expected_usage_arg;
1294 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001295 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001296 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1297 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001298 uint8_t *export_buffer = NULL;
1299
Gilles Peskine57ab7212019-01-28 13:03:09 +01001300 PSA_ASSERT( psa_crypto_init( ) );
1301
Gilles Peskineca25db92019-04-19 11:43:08 +02001302 /* Prepare the source key. */
1303 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1304 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001305 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001306 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001307 PSA_ASSERT( psa_import_key( &source_attributes,
1308 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001309 &source_key ) );
1310 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001311
Gilles Peskineca25db92019-04-19 11:43:08 +02001312 /* Prepare the target attributes. */
1313 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001314 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001315 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001316 /* Set volatile lifetime to reset the key identifier to 0. */
1317 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1318 }
1319
Gilles Peskineca25db92019-04-19 11:43:08 +02001320 if( target_usage_arg != -1 )
1321 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1322 if( target_alg_arg != -1 )
1323 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001324 if( target_alg2_arg != -1 )
1325 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001326
1327 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001328 PSA_ASSERT( psa_copy_key( source_key,
1329 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001330
1331 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001332 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001333
1334 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001335 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001336 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1337 psa_get_key_type( &target_attributes ) );
1338 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1339 psa_get_key_bits( &target_attributes ) );
1340 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1341 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001342 TEST_EQUAL( expected_alg2,
1343 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001344 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1345 {
1346 size_t length;
1347 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001348 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001349 material->len, &length ) );
1350 ASSERT_COMPARE( material->x, material->len,
1351 export_buffer, length );
1352 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001353
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001354 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001355 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001356 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001357 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001358
Ronald Cron5425a212020-08-04 14:58:35 +02001359 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001360
1361exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001362 /*
1363 * Source and target key attributes may have been returned by
1364 * psa_get_key_attributes() thus reset them as required.
1365 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001366 psa_reset_key_attributes( &source_attributes );
1367 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001368
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001369 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001370 mbedtls_free( export_buffer );
1371}
1372/* END_CASE */
1373
1374/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001375void copy_fail( int source_usage_arg,
1376 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001377 int type_arg, data_t *material,
1378 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001379 int target_usage_arg,
1380 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001381 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001382 int expected_status_arg )
1383{
1384 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1385 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001386 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1387 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001388 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001389
1390 PSA_ASSERT( psa_crypto_init( ) );
1391
1392 /* Prepare the source key. */
1393 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1394 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001395 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001396 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001397 PSA_ASSERT( psa_import_key( &source_attributes,
1398 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001399 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001400
1401 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001402 psa_set_key_id( &target_attributes, key_id );
1403 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001404 psa_set_key_type( &target_attributes, target_type_arg );
1405 psa_set_key_bits( &target_attributes, target_bits_arg );
1406 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1407 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001408 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001409
1410 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001411 TEST_EQUAL( psa_copy_key( source_key,
1412 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001413 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001414
Ronald Cron5425a212020-08-04 14:58:35 +02001415 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001416
Gilles Peskine4a644642019-05-03 17:14:08 +02001417exit:
1418 psa_reset_key_attributes( &source_attributes );
1419 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001420 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001421}
1422/* END_CASE */
1423
1424/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001425void hash_operation_init( )
1426{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001427 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001428 /* Test each valid way of initializing the object, except for `= {0}`, as
1429 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1430 * though it's OK by the C standard. We could test for this, but we'd need
1431 * to supress the Clang warning for the test. */
1432 psa_hash_operation_t func = psa_hash_operation_init( );
1433 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1434 psa_hash_operation_t zero;
1435
1436 memset( &zero, 0, sizeof( zero ) );
1437
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001438 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001439 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1440 PSA_ERROR_BAD_STATE );
1441 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1442 PSA_ERROR_BAD_STATE );
1443 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1444 PSA_ERROR_BAD_STATE );
1445
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001446 /* A default hash operation should be abortable without error. */
1447 PSA_ASSERT( psa_hash_abort( &func ) );
1448 PSA_ASSERT( psa_hash_abort( &init ) );
1449 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001450}
1451/* END_CASE */
1452
1453/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001454void hash_setup( int alg_arg,
1455 int expected_status_arg )
1456{
1457 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001458 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001459 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001460 psa_status_t status;
1461
Gilles Peskine8817f612018-12-18 00:18:46 +01001462 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001463
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001464 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001465 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001466
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001467 /* Whether setup succeeded or failed, abort must succeed. */
1468 PSA_ASSERT( psa_hash_abort( &operation ) );
1469
1470 /* If setup failed, reproduce the failure, so as to
1471 * test the resulting state of the operation object. */
1472 if( status != PSA_SUCCESS )
1473 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1474
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001475 /* Now the operation object should be reusable. */
1476#if defined(KNOWN_SUPPORTED_HASH_ALG)
1477 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1478 PSA_ASSERT( psa_hash_abort( &operation ) );
1479#endif
1480
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001481exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001482 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001483}
1484/* END_CASE */
1485
1486/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001487void hash_compute_fail( int alg_arg, data_t *input,
1488 int output_size_arg, int expected_status_arg )
1489{
1490 psa_algorithm_t alg = alg_arg;
1491 uint8_t *output = NULL;
1492 size_t output_size = output_size_arg;
1493 size_t output_length = INVALID_EXPORT_LENGTH;
1494 psa_status_t expected_status = expected_status_arg;
1495 psa_status_t status;
1496
1497 ASSERT_ALLOC( output, output_size );
1498
1499 PSA_ASSERT( psa_crypto_init( ) );
1500
1501 status = psa_hash_compute( alg, input->x, input->len,
1502 output, output_size, &output_length );
1503 TEST_EQUAL( status, expected_status );
1504 TEST_ASSERT( output_length <= output_size );
1505
1506exit:
1507 mbedtls_free( output );
1508 PSA_DONE( );
1509}
1510/* END_CASE */
1511
1512/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001513void hash_compare_fail( int alg_arg, data_t *input,
1514 data_t *reference_hash,
1515 int expected_status_arg )
1516{
1517 psa_algorithm_t alg = alg_arg;
1518 psa_status_t expected_status = expected_status_arg;
1519 psa_status_t status;
1520
1521 PSA_ASSERT( psa_crypto_init( ) );
1522
1523 status = psa_hash_compare( alg, input->x, input->len,
1524 reference_hash->x, reference_hash->len );
1525 TEST_EQUAL( status, expected_status );
1526
1527exit:
1528 PSA_DONE( );
1529}
1530/* END_CASE */
1531
1532/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001533void hash_compute_compare( int alg_arg, data_t *input,
1534 data_t *expected_output )
1535{
1536 psa_algorithm_t alg = alg_arg;
1537 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1538 size_t output_length = INVALID_EXPORT_LENGTH;
1539 size_t i;
1540
1541 PSA_ASSERT( psa_crypto_init( ) );
1542
1543 /* Compute with tight buffer */
1544 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001545 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001546 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001547 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001548 ASSERT_COMPARE( output, output_length,
1549 expected_output->x, expected_output->len );
1550
1551 /* Compute with larger buffer */
1552 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1553 output, sizeof( output ),
1554 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001555 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001556 ASSERT_COMPARE( output, output_length,
1557 expected_output->x, expected_output->len );
1558
1559 /* Compare with correct hash */
1560 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1561 output, output_length ) );
1562
1563 /* Compare with trailing garbage */
1564 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1565 output, output_length + 1 ),
1566 PSA_ERROR_INVALID_SIGNATURE );
1567
1568 /* Compare with truncated hash */
1569 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1570 output, output_length - 1 ),
1571 PSA_ERROR_INVALID_SIGNATURE );
1572
1573 /* Compare with corrupted value */
1574 for( i = 0; i < output_length; i++ )
1575 {
Chris Jones9634bb12021-01-20 15:56:42 +00001576 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001577 output[i] ^= 1;
1578 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1579 output, output_length ),
1580 PSA_ERROR_INVALID_SIGNATURE );
1581 output[i] ^= 1;
1582 }
1583
1584exit:
1585 PSA_DONE( );
1586}
1587/* END_CASE */
1588
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001589/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001590void hash_bad_order( )
1591{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001592 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001593 unsigned char input[] = "";
1594 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001595 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001596 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1597 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1598 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001599 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001600 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001601 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001602
Gilles Peskine8817f612018-12-18 00:18:46 +01001603 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001604
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001605 /* Call setup twice in a row. */
1606 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001607 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001608 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1609 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001610 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001611 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001612 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001613
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001614 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001615 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001616 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001617 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001618
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001619 /* Check that update calls abort on error. */
1620 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01001621 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001622 ASSERT_OPERATION_IS_ACTIVE( operation );
1623 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
1624 PSA_ERROR_BAD_STATE );
1625 ASSERT_OPERATION_IS_INACTIVE( operation );
1626 PSA_ASSERT( psa_hash_abort( &operation ) );
1627 ASSERT_OPERATION_IS_INACTIVE( operation );
1628
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001629 /* Call update after finish. */
1630 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1631 PSA_ASSERT( psa_hash_finish( &operation,
1632 hash, sizeof( hash ), &hash_len ) );
1633 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001634 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001635 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001636
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001637 /* Call verify without calling setup beforehand. */
1638 TEST_EQUAL( psa_hash_verify( &operation,
1639 valid_hash, sizeof( valid_hash ) ),
1640 PSA_ERROR_BAD_STATE );
1641 PSA_ASSERT( psa_hash_abort( &operation ) );
1642
1643 /* Call verify after finish. */
1644 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1645 PSA_ASSERT( psa_hash_finish( &operation,
1646 hash, sizeof( hash ), &hash_len ) );
1647 TEST_EQUAL( psa_hash_verify( &operation,
1648 valid_hash, sizeof( valid_hash ) ),
1649 PSA_ERROR_BAD_STATE );
1650 PSA_ASSERT( psa_hash_abort( &operation ) );
1651
1652 /* Call verify twice in a row. */
1653 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001654 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001655 PSA_ASSERT( psa_hash_verify( &operation,
1656 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001657 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001658 TEST_EQUAL( psa_hash_verify( &operation,
1659 valid_hash, sizeof( valid_hash ) ),
1660 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001661 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001662 PSA_ASSERT( psa_hash_abort( &operation ) );
1663
1664 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001665 TEST_EQUAL( psa_hash_finish( &operation,
1666 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001667 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001668 PSA_ASSERT( psa_hash_abort( &operation ) );
1669
1670 /* Call finish twice in a row. */
1671 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1672 PSA_ASSERT( psa_hash_finish( &operation,
1673 hash, sizeof( hash ), &hash_len ) );
1674 TEST_EQUAL( psa_hash_finish( &operation,
1675 hash, sizeof( hash ), &hash_len ),
1676 PSA_ERROR_BAD_STATE );
1677 PSA_ASSERT( psa_hash_abort( &operation ) );
1678
1679 /* Call finish after calling verify. */
1680 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1681 PSA_ASSERT( psa_hash_verify( &operation,
1682 valid_hash, sizeof( valid_hash ) ) );
1683 TEST_EQUAL( psa_hash_finish( &operation,
1684 hash, sizeof( hash ), &hash_len ),
1685 PSA_ERROR_BAD_STATE );
1686 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001687
1688exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001689 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001690}
1691/* END_CASE */
1692
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001693/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001694void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001695{
1696 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001697 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1698 * appended to it */
1699 unsigned char hash[] = {
1700 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1701 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1702 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001703 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001704 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001705
Gilles Peskine8817f612018-12-18 00:18:46 +01001706 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001707
itayzafrir27e69452018-11-01 14:26:34 +02001708 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001709 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001710 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001711 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001712 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001713 ASSERT_OPERATION_IS_INACTIVE( operation );
1714 PSA_ASSERT( psa_hash_abort( &operation ) );
1715 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03001716
itayzafrir27e69452018-11-01 14:26:34 +02001717 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001718 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001719 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001720 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001721
itayzafrir27e69452018-11-01 14:26:34 +02001722 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001723 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001724 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001725 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001726
itayzafrirec93d302018-10-18 18:01:10 +03001727exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001728 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001729}
1730/* END_CASE */
1731
Ronald Cronee414c72021-03-18 18:50:08 +01001732/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001733void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001734{
1735 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001736 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001737 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001738 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001739 size_t hash_len;
1740
Gilles Peskine8817f612018-12-18 00:18:46 +01001741 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001742
itayzafrir58028322018-10-25 10:22:01 +03001743 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001744 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001745 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001746 hash, expected_size - 1, &hash_len ),
1747 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001748
1749exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001750 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001751}
1752/* END_CASE */
1753
Ronald Cronee414c72021-03-18 18:50:08 +01001754/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001755void hash_clone_source_state( )
1756{
1757 psa_algorithm_t alg = PSA_ALG_SHA_256;
1758 unsigned char hash[PSA_HASH_MAX_SIZE];
1759 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1760 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1761 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1762 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1763 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1764 size_t hash_len;
1765
1766 PSA_ASSERT( psa_crypto_init( ) );
1767 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1768
1769 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1770 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1771 PSA_ASSERT( psa_hash_finish( &op_finished,
1772 hash, sizeof( hash ), &hash_len ) );
1773 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1774 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1775
1776 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1777 PSA_ERROR_BAD_STATE );
1778
1779 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1780 PSA_ASSERT( psa_hash_finish( &op_init,
1781 hash, sizeof( hash ), &hash_len ) );
1782 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1783 PSA_ASSERT( psa_hash_finish( &op_finished,
1784 hash, sizeof( hash ), &hash_len ) );
1785 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1786 PSA_ASSERT( psa_hash_finish( &op_aborted,
1787 hash, sizeof( hash ), &hash_len ) );
1788
1789exit:
1790 psa_hash_abort( &op_source );
1791 psa_hash_abort( &op_init );
1792 psa_hash_abort( &op_setup );
1793 psa_hash_abort( &op_finished );
1794 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001795 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001796}
1797/* END_CASE */
1798
Ronald Cronee414c72021-03-18 18:50:08 +01001799/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001800void hash_clone_target_state( )
1801{
1802 psa_algorithm_t alg = PSA_ALG_SHA_256;
1803 unsigned char hash[PSA_HASH_MAX_SIZE];
1804 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1805 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1806 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1807 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1808 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1809 size_t hash_len;
1810
1811 PSA_ASSERT( psa_crypto_init( ) );
1812
1813 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1814 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1815 PSA_ASSERT( psa_hash_finish( &op_finished,
1816 hash, sizeof( hash ), &hash_len ) );
1817 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1818 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1819
1820 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1821 PSA_ASSERT( psa_hash_finish( &op_target,
1822 hash, sizeof( hash ), &hash_len ) );
1823
1824 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1825 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1826 PSA_ERROR_BAD_STATE );
1827 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1828 PSA_ERROR_BAD_STATE );
1829
1830exit:
1831 psa_hash_abort( &op_target );
1832 psa_hash_abort( &op_init );
1833 psa_hash_abort( &op_setup );
1834 psa_hash_abort( &op_finished );
1835 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001836 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001837}
1838/* END_CASE */
1839
itayzafrir58028322018-10-25 10:22:01 +03001840/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001841void mac_operation_init( )
1842{
Jaeden Amero252ef282019-02-15 14:05:35 +00001843 const uint8_t input[1] = { 0 };
1844
Jaeden Amero769ce272019-01-04 11:48:03 +00001845 /* Test each valid way of initializing the object, except for `= {0}`, as
1846 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1847 * though it's OK by the C standard. We could test for this, but we'd need
1848 * to supress the Clang warning for the test. */
1849 psa_mac_operation_t func = psa_mac_operation_init( );
1850 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1851 psa_mac_operation_t zero;
1852
1853 memset( &zero, 0, sizeof( zero ) );
1854
Jaeden Amero252ef282019-02-15 14:05:35 +00001855 /* A freshly-initialized MAC operation should not be usable. */
1856 TEST_EQUAL( psa_mac_update( &func,
1857 input, sizeof( input ) ),
1858 PSA_ERROR_BAD_STATE );
1859 TEST_EQUAL( psa_mac_update( &init,
1860 input, sizeof( input ) ),
1861 PSA_ERROR_BAD_STATE );
1862 TEST_EQUAL( psa_mac_update( &zero,
1863 input, sizeof( input ) ),
1864 PSA_ERROR_BAD_STATE );
1865
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001866 /* A default MAC operation should be abortable without error. */
1867 PSA_ASSERT( psa_mac_abort( &func ) );
1868 PSA_ASSERT( psa_mac_abort( &init ) );
1869 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001870}
1871/* END_CASE */
1872
1873/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001874void mac_setup( int key_type_arg,
1875 data_t *key,
1876 int alg_arg,
1877 int expected_status_arg )
1878{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001879 psa_key_type_t key_type = key_type_arg;
1880 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001881 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001882 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001883 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1884#if defined(KNOWN_SUPPORTED_MAC_ALG)
1885 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1886#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001887
Gilles Peskine8817f612018-12-18 00:18:46 +01001888 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001889
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001890 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1891 &operation, &status ) )
1892 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001893 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001894
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001895 /* The operation object should be reusable. */
1896#if defined(KNOWN_SUPPORTED_MAC_ALG)
1897 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1898 smoke_test_key_data,
1899 sizeof( smoke_test_key_data ),
1900 KNOWN_SUPPORTED_MAC_ALG,
1901 &operation, &status ) )
1902 goto exit;
1903 TEST_EQUAL( status, PSA_SUCCESS );
1904#endif
1905
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001906exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001907 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001908}
1909/* END_CASE */
1910
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001911/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_HMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256 */
Jaeden Amero252ef282019-02-15 14:05:35 +00001912void mac_bad_order( )
1913{
Ronald Cron5425a212020-08-04 14:58:35 +02001914 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001915 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1916 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001917 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001918 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1919 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1920 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001921 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001922 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1923 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1924 size_t sign_mac_length = 0;
1925 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1926 const uint8_t verify_mac[] = {
1927 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1928 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1929 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1930
1931 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001932 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001933 psa_set_key_algorithm( &attributes, alg );
1934 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001935
Ronald Cron5425a212020-08-04 14:58:35 +02001936 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1937 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001938
Jaeden Amero252ef282019-02-15 14:05:35 +00001939 /* Call update without calling setup beforehand. */
1940 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1941 PSA_ERROR_BAD_STATE );
1942 PSA_ASSERT( psa_mac_abort( &operation ) );
1943
1944 /* Call sign finish without calling setup beforehand. */
1945 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1946 &sign_mac_length),
1947 PSA_ERROR_BAD_STATE );
1948 PSA_ASSERT( psa_mac_abort( &operation ) );
1949
1950 /* Call verify finish without calling setup beforehand. */
1951 TEST_EQUAL( psa_mac_verify_finish( &operation,
1952 verify_mac, sizeof( verify_mac ) ),
1953 PSA_ERROR_BAD_STATE );
1954 PSA_ASSERT( psa_mac_abort( &operation ) );
1955
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001956 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001957 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001958 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001959 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001960 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001961 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001962 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001963 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001964
Jaeden Amero252ef282019-02-15 14:05:35 +00001965 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001966 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001967 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1968 PSA_ASSERT( psa_mac_sign_finish( &operation,
1969 sign_mac, sizeof( sign_mac ),
1970 &sign_mac_length ) );
1971 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1972 PSA_ERROR_BAD_STATE );
1973 PSA_ASSERT( psa_mac_abort( &operation ) );
1974
1975 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001976 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001977 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1978 PSA_ASSERT( psa_mac_verify_finish( &operation,
1979 verify_mac, sizeof( verify_mac ) ) );
1980 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1981 PSA_ERROR_BAD_STATE );
1982 PSA_ASSERT( psa_mac_abort( &operation ) );
1983
1984 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001985 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001986 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1987 PSA_ASSERT( psa_mac_sign_finish( &operation,
1988 sign_mac, sizeof( sign_mac ),
1989 &sign_mac_length ) );
1990 TEST_EQUAL( psa_mac_sign_finish( &operation,
1991 sign_mac, sizeof( sign_mac ),
1992 &sign_mac_length ),
1993 PSA_ERROR_BAD_STATE );
1994 PSA_ASSERT( psa_mac_abort( &operation ) );
1995
1996 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001997 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001998 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1999 PSA_ASSERT( psa_mac_verify_finish( &operation,
2000 verify_mac, sizeof( verify_mac ) ) );
2001 TEST_EQUAL( psa_mac_verify_finish( &operation,
2002 verify_mac, sizeof( verify_mac ) ),
2003 PSA_ERROR_BAD_STATE );
2004 PSA_ASSERT( psa_mac_abort( &operation ) );
2005
2006 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002007 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002008 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002009 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002010 TEST_EQUAL( psa_mac_verify_finish( &operation,
2011 verify_mac, sizeof( verify_mac ) ),
2012 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002013 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002014 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002015 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002016
2017 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002018 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002019 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002020 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002021 TEST_EQUAL( psa_mac_sign_finish( &operation,
2022 sign_mac, sizeof( sign_mac ),
2023 &sign_mac_length ),
2024 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002025 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002026 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002027 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002028
Ronald Cron5425a212020-08-04 14:58:35 +02002029 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002030
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002031exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002032 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002033}
2034/* END_CASE */
2035
2036/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002037void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002038 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002039 int alg_arg,
2040 data_t *input,
2041 data_t *expected_mac )
2042{
Ronald Cron5425a212020-08-04 14:58:35 +02002043 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002044 psa_key_type_t key_type = key_type_arg;
2045 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002046 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002047 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002048 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002049 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002050 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002051 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002052 const size_t output_sizes_to_test[] = {
2053 0,
2054 1,
2055 expected_mac->len - 1,
2056 expected_mac->len,
2057 expected_mac->len + 1,
2058 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002059
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002060 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002061 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002062 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002063
Gilles Peskine8817f612018-12-18 00:18:46 +01002064 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002065
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002066 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002067 psa_set_key_algorithm( &attributes, alg );
2068 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002069
Ronald Cron5425a212020-08-04 14:58:35 +02002070 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2071 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002072
Gilles Peskine8b356b52020-08-25 23:44:59 +02002073 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2074 {
2075 const size_t output_size = output_sizes_to_test[i];
2076 psa_status_t expected_status =
2077 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2078 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002079
Chris Jones9634bb12021-01-20 15:56:42 +00002080 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002081 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002082
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002083 /* Calculate the MAC, one-shot case. */
2084 TEST_EQUAL( psa_mac_compute( key, alg,
2085 input->x, input->len,
2086 actual_mac, output_size, &mac_length ),
2087 expected_status );
2088 if( expected_status == PSA_SUCCESS )
2089 {
2090 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2091 actual_mac, mac_length );
2092 }
2093
2094 if( output_size > 0 )
2095 memset( actual_mac, 0, output_size );
2096
2097 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002098 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002099 PSA_ASSERT( psa_mac_update( &operation,
2100 input->x, input->len ) );
2101 TEST_EQUAL( psa_mac_sign_finish( &operation,
2102 actual_mac, output_size,
2103 &mac_length ),
2104 expected_status );
2105 PSA_ASSERT( psa_mac_abort( &operation ) );
2106
2107 if( expected_status == PSA_SUCCESS )
2108 {
2109 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2110 actual_mac, mac_length );
2111 }
2112 mbedtls_free( actual_mac );
2113 actual_mac = NULL;
2114 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002115
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002116exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002117 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002118 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002119 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002120 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002121}
2122/* END_CASE */
2123
2124/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002125void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002126 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002127 int alg_arg,
2128 data_t *input,
2129 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002130{
Ronald Cron5425a212020-08-04 14:58:35 +02002131 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002132 psa_key_type_t key_type = key_type_arg;
2133 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002134 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002135 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002136 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002137
Gilles Peskine69c12672018-06-28 00:07:19 +02002138 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2139
Gilles Peskine8817f612018-12-18 00:18:46 +01002140 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002141
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002142 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002143 psa_set_key_algorithm( &attributes, alg );
2144 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002145
Ronald Cron5425a212020-08-04 14:58:35 +02002146 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2147 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002148
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002149 /* Verify correct MAC, one-shot case. */
2150 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2151 expected_mac->x, expected_mac->len ) );
2152
2153 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002154 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002155 PSA_ASSERT( psa_mac_update( &operation,
2156 input->x, input->len ) );
2157 PSA_ASSERT( psa_mac_verify_finish( &operation,
2158 expected_mac->x,
2159 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002160
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002161 /* Test a MAC that's too short, one-shot case. */
2162 TEST_EQUAL( psa_mac_verify( key, alg,
2163 input->x, input->len,
2164 expected_mac->x,
2165 expected_mac->len - 1 ),
2166 PSA_ERROR_INVALID_SIGNATURE );
2167
2168 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002169 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002170 PSA_ASSERT( psa_mac_update( &operation,
2171 input->x, input->len ) );
2172 TEST_EQUAL( psa_mac_verify_finish( &operation,
2173 expected_mac->x,
2174 expected_mac->len - 1 ),
2175 PSA_ERROR_INVALID_SIGNATURE );
2176
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002177 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002178 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2179 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002180 TEST_EQUAL( psa_mac_verify( key, alg,
2181 input->x, input->len,
2182 perturbed_mac, expected_mac->len + 1 ),
2183 PSA_ERROR_INVALID_SIGNATURE );
2184
2185 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002186 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002187 PSA_ASSERT( psa_mac_update( &operation,
2188 input->x, input->len ) );
2189 TEST_EQUAL( psa_mac_verify_finish( &operation,
2190 perturbed_mac,
2191 expected_mac->len + 1 ),
2192 PSA_ERROR_INVALID_SIGNATURE );
2193
2194 /* Test changing one byte. */
2195 for( size_t i = 0; i < expected_mac->len; i++ )
2196 {
Chris Jones9634bb12021-01-20 15:56:42 +00002197 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002198 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002199
2200 TEST_EQUAL( psa_mac_verify( key, alg,
2201 input->x, input->len,
2202 perturbed_mac, expected_mac->len ),
2203 PSA_ERROR_INVALID_SIGNATURE );
2204
Ronald Cron5425a212020-08-04 14:58:35 +02002205 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002206 PSA_ASSERT( psa_mac_update( &operation,
2207 input->x, input->len ) );
2208 TEST_EQUAL( psa_mac_verify_finish( &operation,
2209 perturbed_mac,
2210 expected_mac->len ),
2211 PSA_ERROR_INVALID_SIGNATURE );
2212 perturbed_mac[i] ^= 1;
2213 }
2214
Gilles Peskine8c9def32018-02-08 10:02:12 +01002215exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002216 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002217 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002218 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002219 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002220}
2221/* END_CASE */
2222
2223/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002224void cipher_operation_init( )
2225{
Jaeden Ameroab439972019-02-15 14:12:05 +00002226 const uint8_t input[1] = { 0 };
2227 unsigned char output[1] = { 0 };
2228 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002229 /* Test each valid way of initializing the object, except for `= {0}`, as
2230 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2231 * though it's OK by the C standard. We could test for this, but we'd need
2232 * to supress the Clang warning for the test. */
2233 psa_cipher_operation_t func = psa_cipher_operation_init( );
2234 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2235 psa_cipher_operation_t zero;
2236
2237 memset( &zero, 0, sizeof( zero ) );
2238
Jaeden Ameroab439972019-02-15 14:12:05 +00002239 /* A freshly-initialized cipher operation should not be usable. */
2240 TEST_EQUAL( psa_cipher_update( &func,
2241 input, sizeof( input ),
2242 output, sizeof( output ),
2243 &output_length ),
2244 PSA_ERROR_BAD_STATE );
2245 TEST_EQUAL( psa_cipher_update( &init,
2246 input, sizeof( input ),
2247 output, sizeof( output ),
2248 &output_length ),
2249 PSA_ERROR_BAD_STATE );
2250 TEST_EQUAL( psa_cipher_update( &zero,
2251 input, sizeof( input ),
2252 output, sizeof( output ),
2253 &output_length ),
2254 PSA_ERROR_BAD_STATE );
2255
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002256 /* A default cipher operation should be abortable without error. */
2257 PSA_ASSERT( psa_cipher_abort( &func ) );
2258 PSA_ASSERT( psa_cipher_abort( &init ) );
2259 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002260}
2261/* END_CASE */
2262
2263/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002264void cipher_setup( int key_type_arg,
2265 data_t *key,
2266 int alg_arg,
2267 int expected_status_arg )
2268{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002269 psa_key_type_t key_type = key_type_arg;
2270 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002271 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002272 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002273 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002274#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002275 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2276#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002277
Gilles Peskine8817f612018-12-18 00:18:46 +01002278 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002279
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002280 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2281 &operation, &status ) )
2282 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002283 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002284
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002285 /* The operation object should be reusable. */
2286#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2287 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2288 smoke_test_key_data,
2289 sizeof( smoke_test_key_data ),
2290 KNOWN_SUPPORTED_CIPHER_ALG,
2291 &operation, &status ) )
2292 goto exit;
2293 TEST_EQUAL( status, PSA_SUCCESS );
2294#endif
2295
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002296exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002297 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002298 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002299}
2300/* END_CASE */
2301
Ronald Cronee414c72021-03-18 18:50:08 +01002302/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002303void cipher_bad_order( )
2304{
Ronald Cron5425a212020-08-04 14:58:35 +02002305 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002306 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2307 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002308 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002309 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002310 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002311 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002312 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2313 0xaa, 0xaa, 0xaa, 0xaa };
2314 const uint8_t text[] = {
2315 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2316 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002317 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002318 size_t length = 0;
2319
2320 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002321 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2322 psa_set_key_algorithm( &attributes, alg );
2323 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002324 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2325 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002326
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002327 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002328 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002329 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002330 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002331 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002332 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002333 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002334 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002335
2336 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002337 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002338 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002339 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002340 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002341 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002342 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002343 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002344
Jaeden Ameroab439972019-02-15 14:12:05 +00002345 /* Generate an IV without calling setup beforehand. */
2346 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2347 buffer, sizeof( buffer ),
2348 &length ),
2349 PSA_ERROR_BAD_STATE );
2350 PSA_ASSERT( psa_cipher_abort( &operation ) );
2351
2352 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002353 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002354 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2355 buffer, sizeof( buffer ),
2356 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002357 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002358 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2359 buffer, sizeof( buffer ),
2360 &length ),
2361 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002362 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002363 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002364 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002365
2366 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002367 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002368 PSA_ASSERT( psa_cipher_set_iv( &operation,
2369 iv, sizeof( iv ) ) );
2370 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2371 buffer, sizeof( buffer ),
2372 &length ),
2373 PSA_ERROR_BAD_STATE );
2374 PSA_ASSERT( psa_cipher_abort( &operation ) );
2375
2376 /* Set an IV without calling setup beforehand. */
2377 TEST_EQUAL( psa_cipher_set_iv( &operation,
2378 iv, sizeof( iv ) ),
2379 PSA_ERROR_BAD_STATE );
2380 PSA_ASSERT( psa_cipher_abort( &operation ) );
2381
2382 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002383 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002384 PSA_ASSERT( psa_cipher_set_iv( &operation,
2385 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002386 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002387 TEST_EQUAL( psa_cipher_set_iv( &operation,
2388 iv, sizeof( iv ) ),
2389 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002390 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002391 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002392 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002393
2394 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002395 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002396 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2397 buffer, sizeof( buffer ),
2398 &length ) );
2399 TEST_EQUAL( psa_cipher_set_iv( &operation,
2400 iv, sizeof( iv ) ),
2401 PSA_ERROR_BAD_STATE );
2402 PSA_ASSERT( psa_cipher_abort( &operation ) );
2403
2404 /* Call update without calling setup beforehand. */
2405 TEST_EQUAL( psa_cipher_update( &operation,
2406 text, sizeof( text ),
2407 buffer, sizeof( buffer ),
2408 &length ),
2409 PSA_ERROR_BAD_STATE );
2410 PSA_ASSERT( psa_cipher_abort( &operation ) );
2411
2412 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01002413 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002414 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002415 TEST_EQUAL( psa_cipher_update( &operation,
2416 text, sizeof( text ),
2417 buffer, sizeof( buffer ),
2418 &length ),
2419 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002420 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002421 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002422 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002423
2424 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002425 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002426 PSA_ASSERT( psa_cipher_set_iv( &operation,
2427 iv, sizeof( iv ) ) );
2428 PSA_ASSERT( psa_cipher_finish( &operation,
2429 buffer, sizeof( buffer ), &length ) );
2430 TEST_EQUAL( psa_cipher_update( &operation,
2431 text, sizeof( text ),
2432 buffer, sizeof( buffer ),
2433 &length ),
2434 PSA_ERROR_BAD_STATE );
2435 PSA_ASSERT( psa_cipher_abort( &operation ) );
2436
2437 /* Call finish without calling setup beforehand. */
2438 TEST_EQUAL( psa_cipher_finish( &operation,
2439 buffer, sizeof( buffer ), &length ),
2440 PSA_ERROR_BAD_STATE );
2441 PSA_ASSERT( psa_cipher_abort( &operation ) );
2442
2443 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002444 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002445 /* Not calling update means we are encrypting an empty buffer, which is OK
2446 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01002447 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002448 TEST_EQUAL( psa_cipher_finish( &operation,
2449 buffer, sizeof( buffer ), &length ),
2450 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002451 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002452 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002453 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002454
2455 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002456 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002457 PSA_ASSERT( psa_cipher_set_iv( &operation,
2458 iv, sizeof( iv ) ) );
2459 PSA_ASSERT( psa_cipher_finish( &operation,
2460 buffer, sizeof( buffer ), &length ) );
2461 TEST_EQUAL( psa_cipher_finish( &operation,
2462 buffer, sizeof( buffer ), &length ),
2463 PSA_ERROR_BAD_STATE );
2464 PSA_ASSERT( psa_cipher_abort( &operation ) );
2465
Ronald Cron5425a212020-08-04 14:58:35 +02002466 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002467
Jaeden Ameroab439972019-02-15 14:12:05 +00002468exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002469 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002470 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002471}
2472/* END_CASE */
2473
2474/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002475void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002476 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002477 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002478 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002479{
Ronald Cron5425a212020-08-04 14:58:35 +02002480 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002481 psa_status_t status;
2482 psa_key_type_t key_type = key_type_arg;
2483 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002484 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002485 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002486 size_t output_buffer_size = 0;
2487 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002488 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002489 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002490 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002491
Gilles Peskine8817f612018-12-18 00:18:46 +01002492 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002493
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002494 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2495 psa_set_key_algorithm( &attributes, alg );
2496 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002497
Ronald Cron5425a212020-08-04 14:58:35 +02002498 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2499 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002500
Ronald Cron5425a212020-08-04 14:58:35 +02002501 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002502
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002503 if( iv->len > 0 )
2504 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002505 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002506 }
2507
gabor-mezei-armceface22021-01-21 12:26:17 +01002508 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2509 TEST_ASSERT( output_buffer_size <=
2510 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002511 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002512
Gilles Peskine8817f612018-12-18 00:18:46 +01002513 PSA_ASSERT( psa_cipher_update( &operation,
2514 input->x, input->len,
2515 output, output_buffer_size,
2516 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002517 TEST_ASSERT( function_output_length <=
2518 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2519 TEST_ASSERT( function_output_length <=
2520 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002521 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002522
Gilles Peskine50e586b2018-06-08 14:28:46 +02002523 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002524 ( output_buffer_size == 0 ? NULL :
2525 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002526 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002527 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002528 TEST_ASSERT( function_output_length <=
2529 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2530 TEST_ASSERT( function_output_length <=
2531 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002532 total_output_length += function_output_length;
2533
Gilles Peskinefe11b722018-12-18 00:24:04 +01002534 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002535 if( expected_status == PSA_SUCCESS )
2536 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002537 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002538 ASSERT_COMPARE( expected_output->x, expected_output->len,
2539 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002540 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002541
Gilles Peskine50e586b2018-06-08 14:28:46 +02002542exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002543 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002544 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002545 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002546 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002547}
2548/* END_CASE */
2549
2550/* BEGIN_CASE */
2551void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002552 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002553 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002554 int first_part_size_arg,
2555 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002556 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002557{
Ronald Cron5425a212020-08-04 14:58:35 +02002558 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002559 psa_key_type_t key_type = key_type_arg;
2560 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002561 size_t first_part_size = first_part_size_arg;
2562 size_t output1_length = output1_length_arg;
2563 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002564 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002565 size_t output_buffer_size = 0;
2566 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002567 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002568 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002569 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002570
Gilles Peskine8817f612018-12-18 00:18:46 +01002571 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002572
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002573 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2574 psa_set_key_algorithm( &attributes, alg );
2575 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002576
Ronald Cron5425a212020-08-04 14:58:35 +02002577 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2578 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002579
Ronald Cron5425a212020-08-04 14:58:35 +02002580 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002581
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002582 if( iv->len > 0 )
2583 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002584 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002585 }
2586
gabor-mezei-armceface22021-01-21 12:26:17 +01002587 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2588 TEST_ASSERT( output_buffer_size <=
2589 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002590 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002591
Gilles Peskinee0866522019-02-19 19:44:00 +01002592 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002593 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2594 output, output_buffer_size,
2595 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002596 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002597 TEST_ASSERT( function_output_length <=
2598 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2599 TEST_ASSERT( function_output_length <=
2600 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002601 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002602
Gilles Peskine8817f612018-12-18 00:18:46 +01002603 PSA_ASSERT( psa_cipher_update( &operation,
2604 input->x + first_part_size,
2605 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002606 ( output_buffer_size == 0 ? NULL :
2607 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002608 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002609 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002610 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002611 TEST_ASSERT( function_output_length <=
2612 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2613 alg,
2614 input->len - first_part_size ) );
2615 TEST_ASSERT( function_output_length <=
2616 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002617 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002618
Gilles Peskine8817f612018-12-18 00:18:46 +01002619 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002620 ( output_buffer_size == 0 ? NULL :
2621 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002622 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002623 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002624 TEST_ASSERT( function_output_length <=
2625 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2626 TEST_ASSERT( function_output_length <=
2627 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002628 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002629 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002630
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002631 ASSERT_COMPARE( expected_output->x, expected_output->len,
2632 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002633
2634exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002635 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002636 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002637 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002638 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002639}
2640/* END_CASE */
2641
2642/* BEGIN_CASE */
2643void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002644 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002645 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002646 int first_part_size_arg,
2647 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002648 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002649{
Ronald Cron5425a212020-08-04 14:58:35 +02002650 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002651 psa_key_type_t key_type = key_type_arg;
2652 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002653 size_t first_part_size = first_part_size_arg;
2654 size_t output1_length = output1_length_arg;
2655 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002656 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002657 size_t output_buffer_size = 0;
2658 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002659 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002660 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002661 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002662
Gilles Peskine8817f612018-12-18 00:18:46 +01002663 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002664
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002665 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2666 psa_set_key_algorithm( &attributes, alg );
2667 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002668
Ronald Cron5425a212020-08-04 14:58:35 +02002669 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2670 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002671
Ronald Cron5425a212020-08-04 14:58:35 +02002672 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002673
Steven Cooreman177deba2020-09-07 17:14:14 +02002674 if( iv->len > 0 )
2675 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002676 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002677 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002678
gabor-mezei-armceface22021-01-21 12:26:17 +01002679 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2680 TEST_ASSERT( output_buffer_size <=
2681 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002682 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002683
Gilles Peskinee0866522019-02-19 19:44:00 +01002684 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002685 PSA_ASSERT( psa_cipher_update( &operation,
2686 input->x, first_part_size,
2687 output, output_buffer_size,
2688 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002689 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002690 TEST_ASSERT( function_output_length <=
2691 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2692 TEST_ASSERT( function_output_length <=
2693 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002694 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002695
Gilles Peskine8817f612018-12-18 00:18:46 +01002696 PSA_ASSERT( psa_cipher_update( &operation,
2697 input->x + first_part_size,
2698 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002699 ( output_buffer_size == 0 ? NULL :
2700 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002701 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002702 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002703 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002704 TEST_ASSERT( function_output_length <=
2705 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2706 alg,
2707 input->len - first_part_size ) );
2708 TEST_ASSERT( function_output_length <=
2709 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002710 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002711
Gilles Peskine8817f612018-12-18 00:18:46 +01002712 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002713 ( output_buffer_size == 0 ? NULL :
2714 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002715 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002716 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002717 TEST_ASSERT( function_output_length <=
2718 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2719 TEST_ASSERT( function_output_length <=
2720 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002721 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002722 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002723
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002724 ASSERT_COMPARE( expected_output->x, expected_output->len,
2725 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002726
2727exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002728 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002729 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002730 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002731 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002732}
2733/* END_CASE */
2734
Gilles Peskine50e586b2018-06-08 14:28:46 +02002735/* BEGIN_CASE */
2736void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002737 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002738 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002739 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002740{
Ronald Cron5425a212020-08-04 14:58:35 +02002741 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002742 psa_status_t status;
2743 psa_key_type_t key_type = key_type_arg;
2744 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002745 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002746 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002747 size_t output_buffer_size = 0;
2748 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002749 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002750 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002751 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002752
Gilles Peskine8817f612018-12-18 00:18:46 +01002753 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002754
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002755 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2756 psa_set_key_algorithm( &attributes, alg );
2757 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002758
Ronald Cron5425a212020-08-04 14:58:35 +02002759 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2760 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002761
Ronald Cron5425a212020-08-04 14:58:35 +02002762 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002763
Steven Cooreman177deba2020-09-07 17:14:14 +02002764 if( iv->len > 0 )
2765 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002766 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002767 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002768
gabor-mezei-armceface22021-01-21 12:26:17 +01002769 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2770 TEST_ASSERT( output_buffer_size <=
2771 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002772 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002773
Gilles Peskine8817f612018-12-18 00:18:46 +01002774 PSA_ASSERT( psa_cipher_update( &operation,
2775 input->x, input->len,
2776 output, output_buffer_size,
2777 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002778 TEST_ASSERT( function_output_length <=
2779 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2780 TEST_ASSERT( function_output_length <=
2781 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002782 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002783
Gilles Peskine50e586b2018-06-08 14:28:46 +02002784 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002785 ( output_buffer_size == 0 ? NULL :
2786 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002787 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002788 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002789 TEST_ASSERT( function_output_length <=
2790 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2791 TEST_ASSERT( function_output_length <=
2792 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002793 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002794 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002795
2796 if( expected_status == PSA_SUCCESS )
2797 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002798 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002799 ASSERT_COMPARE( expected_output->x, expected_output->len,
2800 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002801 }
2802
Gilles Peskine50e586b2018-06-08 14:28:46 +02002803exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002804 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002805 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002806 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002807 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002808}
2809/* END_CASE */
2810
Gilles Peskine50e586b2018-06-08 14:28:46 +02002811/* BEGIN_CASE */
2812void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002813 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002814 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002815{
Ronald Cron5425a212020-08-04 14:58:35 +02002816 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002817 psa_key_type_t key_type = key_type_arg;
2818 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002819 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002820 size_t iv_size = 16;
2821 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002822 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002823 size_t output1_size = 0;
2824 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002825 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002826 size_t output2_size = 0;
2827 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002828 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002829 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2830 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002831 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002832
Gilles Peskine8817f612018-12-18 00:18:46 +01002833 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002834
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002835 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2836 psa_set_key_algorithm( &attributes, alg );
2837 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002838
Ronald Cron5425a212020-08-04 14:58:35 +02002839 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2840 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002841
Ronald Cron5425a212020-08-04 14:58:35 +02002842 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2843 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002844
Steven Cooreman177deba2020-09-07 17:14:14 +02002845 if( alg != PSA_ALG_ECB_NO_PADDING )
2846 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002847 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2848 iv, iv_size,
2849 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002850 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002851 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2852 TEST_ASSERT( output1_size <=
2853 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002854 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002855
Gilles Peskine8817f612018-12-18 00:18:46 +01002856 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2857 output1, output1_size,
2858 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002859 TEST_ASSERT( output1_length <=
2860 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2861 TEST_ASSERT( output1_length <=
2862 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2863
Gilles Peskine8817f612018-12-18 00:18:46 +01002864 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002865 output1 + output1_length,
2866 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002867 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002868 TEST_ASSERT( function_output_length <=
2869 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2870 TEST_ASSERT( function_output_length <=
2871 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002872
Gilles Peskine048b7f02018-06-08 14:20:49 +02002873 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002874
Gilles Peskine8817f612018-12-18 00:18:46 +01002875 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002876
2877 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002878 TEST_ASSERT( output2_size <=
2879 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2880 TEST_ASSERT( output2_size <=
2881 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002882 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002883
Steven Cooreman177deba2020-09-07 17:14:14 +02002884 if( iv_length > 0 )
2885 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002886 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2887 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002888 }
2889
Gilles Peskine8817f612018-12-18 00:18:46 +01002890 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2891 output2, output2_size,
2892 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002893 TEST_ASSERT( output2_length <=
2894 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
2895 TEST_ASSERT( output2_length <=
2896 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
2897
Gilles Peskine048b7f02018-06-08 14:20:49 +02002898 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002899 PSA_ASSERT( psa_cipher_finish( &operation2,
2900 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002901 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002902 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002903 TEST_ASSERT( function_output_length <=
2904 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2905 TEST_ASSERT( function_output_length <=
2906 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03002907
Gilles Peskine048b7f02018-06-08 14:20:49 +02002908 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002909
Gilles Peskine8817f612018-12-18 00:18:46 +01002910 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002911
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002912 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002913
2914exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002915 psa_cipher_abort( &operation1 );
2916 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002917 mbedtls_free( output1 );
2918 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002919 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002920 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03002921}
2922/* END_CASE */
2923
2924/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002925void cipher_verify_output_multipart( int alg_arg,
2926 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002927 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002928 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002929 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03002930{
Ronald Cron5425a212020-08-04 14:58:35 +02002931 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002932 psa_key_type_t key_type = key_type_arg;
2933 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002934 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002935 unsigned char iv[16] = {0};
2936 size_t iv_size = 16;
2937 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002938 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002939 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002940 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002941 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002942 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002943 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002944 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002945 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2946 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002947 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002948
Gilles Peskine8817f612018-12-18 00:18:46 +01002949 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002950
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002951 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2952 psa_set_key_algorithm( &attributes, alg );
2953 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002954
Ronald Cron5425a212020-08-04 14:58:35 +02002955 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2956 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03002957
Ronald Cron5425a212020-08-04 14:58:35 +02002958 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2959 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002960
Steven Cooreman177deba2020-09-07 17:14:14 +02002961 if( alg != PSA_ALG_ECB_NO_PADDING )
2962 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002963 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2964 iv, iv_size,
2965 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002966 }
2967
gabor-mezei-armceface22021-01-21 12:26:17 +01002968 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2969 TEST_ASSERT( output1_buffer_size <=
2970 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002971 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002972
Gilles Peskinee0866522019-02-19 19:44:00 +01002973 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002974
Gilles Peskine8817f612018-12-18 00:18:46 +01002975 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2976 output1, output1_buffer_size,
2977 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002978 TEST_ASSERT( function_output_length <=
2979 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2980 TEST_ASSERT( function_output_length <=
2981 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002982 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002983
Gilles Peskine8817f612018-12-18 00:18:46 +01002984 PSA_ASSERT( psa_cipher_update( &operation1,
2985 input->x + first_part_size,
2986 input->len - first_part_size,
2987 output1, output1_buffer_size,
2988 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002989 TEST_ASSERT( function_output_length <=
2990 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2991 alg,
2992 input->len - first_part_size ) );
2993 TEST_ASSERT( function_output_length <=
2994 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002995 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002996
Gilles Peskine8817f612018-12-18 00:18:46 +01002997 PSA_ASSERT( psa_cipher_finish( &operation1,
2998 output1 + output1_length,
2999 output1_buffer_size - output1_length,
3000 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003001 TEST_ASSERT( function_output_length <=
3002 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3003 TEST_ASSERT( function_output_length <=
3004 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003005 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003006
Gilles Peskine8817f612018-12-18 00:18:46 +01003007 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003008
Gilles Peskine048b7f02018-06-08 14:20:49 +02003009 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003010 TEST_ASSERT( output2_buffer_size <=
3011 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3012 TEST_ASSERT( output2_buffer_size <=
3013 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003014 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003015
Steven Cooreman177deba2020-09-07 17:14:14 +02003016 if( iv_length > 0 )
3017 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003018 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3019 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003020 }
Moran Pekerded84402018-06-06 16:36:50 +03003021
Gilles Peskine8817f612018-12-18 00:18:46 +01003022 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3023 output2, output2_buffer_size,
3024 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003025 TEST_ASSERT( function_output_length <=
3026 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3027 TEST_ASSERT( function_output_length <=
3028 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003029 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003030
Gilles Peskine8817f612018-12-18 00:18:46 +01003031 PSA_ASSERT( psa_cipher_update( &operation2,
3032 output1 + first_part_size,
3033 output1_length - first_part_size,
3034 output2, output2_buffer_size,
3035 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003036 TEST_ASSERT( function_output_length <=
3037 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3038 alg,
3039 output1_length - first_part_size ) );
3040 TEST_ASSERT( function_output_length <=
3041 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003042 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003043
Gilles Peskine8817f612018-12-18 00:18:46 +01003044 PSA_ASSERT( psa_cipher_finish( &operation2,
3045 output2 + output2_length,
3046 output2_buffer_size - output2_length,
3047 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003048 TEST_ASSERT( function_output_length <=
3049 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3050 TEST_ASSERT( function_output_length <=
3051 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003052 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003053
Gilles Peskine8817f612018-12-18 00:18:46 +01003054 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003055
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003056 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003057
3058exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003059 psa_cipher_abort( &operation1 );
3060 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003061 mbedtls_free( output1 );
3062 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003063 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003064 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003065}
3066/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003067
Gilles Peskine20035e32018-02-03 22:44:14 +01003068/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003069void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003070 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003071 data_t *nonce,
3072 data_t *additional_data,
3073 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003074 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003075{
Ronald Cron5425a212020-08-04 14:58:35 +02003076 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003077 psa_key_type_t key_type = key_type_arg;
3078 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003079 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003080 unsigned char *output_data = NULL;
3081 size_t output_size = 0;
3082 size_t output_length = 0;
3083 unsigned char *output_data2 = NULL;
3084 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003085 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003086 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003087 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003088
Gilles Peskine8817f612018-12-18 00:18:46 +01003089 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003090
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003091 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3092 psa_set_key_algorithm( &attributes, alg );
3093 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003094
Gilles Peskine049c7532019-05-15 20:22:09 +02003095 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003096 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003097 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3098 key_bits = psa_get_key_bits( &attributes );
3099
3100 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3101 alg );
3102 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3103 * should be exact. */
3104 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3105 expected_result != PSA_ERROR_NOT_SUPPORTED )
3106 {
3107 TEST_EQUAL( output_size,
3108 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3109 TEST_ASSERT( output_size <=
3110 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3111 }
3112 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003113
Steven Cooremanf49478b2021-02-15 15:19:25 +01003114 status = psa_aead_encrypt( key, alg,
3115 nonce->x, nonce->len,
3116 additional_data->x,
3117 additional_data->len,
3118 input_data->x, input_data->len,
3119 output_data, output_size,
3120 &output_length );
3121
3122 /* If the operation is not supported, just skip and not fail in case the
3123 * encryption involves a common limitation of cryptography hardwares and
3124 * an alternative implementation. */
3125 if( status == PSA_ERROR_NOT_SUPPORTED )
3126 {
3127 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3128 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3129 }
3130
3131 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003132
3133 if( PSA_SUCCESS == expected_result )
3134 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003135 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003136
Gilles Peskine003a4a92019-05-14 16:09:40 +02003137 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3138 * should be exact. */
3139 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003140 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003141
gabor-mezei-armceface22021-01-21 12:26:17 +01003142 TEST_ASSERT( input_data->len <=
3143 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3144
Ronald Cron5425a212020-08-04 14:58:35 +02003145 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003146 nonce->x, nonce->len,
3147 additional_data->x,
3148 additional_data->len,
3149 output_data, output_length,
3150 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003151 &output_length2 ),
3152 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003153
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003154 ASSERT_COMPARE( input_data->x, input_data->len,
3155 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003156 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003157
Gilles Peskinea1cac842018-06-11 19:33:02 +02003158exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003159 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003160 mbedtls_free( output_data );
3161 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003162 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003163}
3164/* END_CASE */
3165
3166/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003167void aead_encrypt( int key_type_arg, data_t *key_data,
3168 int alg_arg,
3169 data_t *nonce,
3170 data_t *additional_data,
3171 data_t *input_data,
3172 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003173{
Ronald Cron5425a212020-08-04 14:58:35 +02003174 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003175 psa_key_type_t key_type = key_type_arg;
3176 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003177 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003178 unsigned char *output_data = NULL;
3179 size_t output_size = 0;
3180 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003181 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003182 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003183
Gilles Peskine8817f612018-12-18 00:18:46 +01003184 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003185
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003186 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3187 psa_set_key_algorithm( &attributes, alg );
3188 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003189
Gilles Peskine049c7532019-05-15 20:22:09 +02003190 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003191 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003192 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3193 key_bits = psa_get_key_bits( &attributes );
3194
3195 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3196 alg );
3197 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3198 * should be exact. */
3199 TEST_EQUAL( output_size,
3200 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3201 TEST_ASSERT( output_size <=
3202 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3203 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003204
Steven Cooremand588ea12021-01-11 19:36:04 +01003205 status = psa_aead_encrypt( key, alg,
3206 nonce->x, nonce->len,
3207 additional_data->x, additional_data->len,
3208 input_data->x, input_data->len,
3209 output_data, output_size,
3210 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003211
Ronald Cron28a45ed2021-02-09 20:35:42 +01003212 /* If the operation is not supported, just skip and not fail in case the
3213 * encryption involves a common limitation of cryptography hardwares and
3214 * an alternative implementation. */
3215 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003216 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003217 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3218 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003219 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003220
3221 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003222 ASSERT_COMPARE( expected_result->x, expected_result->len,
3223 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003224
Gilles Peskinea1cac842018-06-11 19:33:02 +02003225exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003226 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003227 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003228 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003229}
3230/* END_CASE */
3231
3232/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003233void aead_decrypt( int key_type_arg, data_t *key_data,
3234 int alg_arg,
3235 data_t *nonce,
3236 data_t *additional_data,
3237 data_t *input_data,
3238 data_t *expected_data,
3239 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003240{
Ronald Cron5425a212020-08-04 14:58:35 +02003241 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003242 psa_key_type_t key_type = key_type_arg;
3243 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003244 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003245 unsigned char *output_data = NULL;
3246 size_t output_size = 0;
3247 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003248 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003249 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003250 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003251
Gilles Peskine8817f612018-12-18 00:18:46 +01003252 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003253
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003254 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3255 psa_set_key_algorithm( &attributes, alg );
3256 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003257
Gilles Peskine049c7532019-05-15 20:22:09 +02003258 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003259 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003260 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3261 key_bits = psa_get_key_bits( &attributes );
3262
3263 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3264 alg );
3265 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3266 expected_result != PSA_ERROR_NOT_SUPPORTED )
3267 {
3268 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3269 * should be exact. */
3270 TEST_EQUAL( output_size,
3271 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3272 TEST_ASSERT( output_size <=
3273 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3274 }
3275 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003276
Steven Cooremand588ea12021-01-11 19:36:04 +01003277 status = psa_aead_decrypt( key, alg,
3278 nonce->x, nonce->len,
3279 additional_data->x,
3280 additional_data->len,
3281 input_data->x, input_data->len,
3282 output_data, output_size,
3283 &output_length );
3284
Ronald Cron28a45ed2021-02-09 20:35:42 +01003285 /* If the operation is not supported, just skip and not fail in case the
3286 * decryption involves a common limitation of cryptography hardwares and
3287 * an alternative implementation. */
3288 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003289 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003290 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3291 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003292 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003293
3294 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003295
Gilles Peskine2d277862018-06-18 15:41:12 +02003296 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003297 ASSERT_COMPARE( expected_data->x, expected_data->len,
3298 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003299
Gilles Peskinea1cac842018-06-11 19:33:02 +02003300exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003301 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003302 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003303 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003304}
3305/* END_CASE */
3306
3307/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003308void signature_size( int type_arg,
3309 int bits,
3310 int alg_arg,
3311 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003312{
3313 psa_key_type_t type = type_arg;
3314 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003315 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003316
Gilles Peskinefe11b722018-12-18 00:24:04 +01003317 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003318
Gilles Peskinee59236f2018-01-27 23:32:46 +01003319exit:
3320 ;
3321}
3322/* END_CASE */
3323
3324/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003325void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3326 int alg_arg, data_t *input_data,
3327 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003328{
Ronald Cron5425a212020-08-04 14:58:35 +02003329 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003330 psa_key_type_t key_type = key_type_arg;
3331 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003332 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003333 unsigned char *signature = NULL;
3334 size_t signature_size;
3335 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003336 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003337
Gilles Peskine8817f612018-12-18 00:18:46 +01003338 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003339
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003340 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003341 psa_set_key_algorithm( &attributes, alg );
3342 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003343
Gilles Peskine049c7532019-05-15 20:22:09 +02003344 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003345 &key ) );
3346 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003347 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003348
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003349 /* Allocate a buffer which has the size advertized by the
3350 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003351 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003352 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003353 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003354 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003355 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003356
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003357 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003358 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003359 input_data->x, input_data->len,
3360 signature, signature_size,
3361 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003362 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003363 ASSERT_COMPARE( output_data->x, output_data->len,
3364 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003365
3366exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003367 /*
3368 * Key attributes may have been returned by psa_get_key_attributes()
3369 * thus reset them as required.
3370 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003371 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003372
Ronald Cron5425a212020-08-04 14:58:35 +02003373 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003374 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003375 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003376}
3377/* END_CASE */
3378
3379/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003380void sign_hash_fail( int key_type_arg, data_t *key_data,
3381 int alg_arg, data_t *input_data,
3382 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003383{
Ronald Cron5425a212020-08-04 14:58:35 +02003384 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003385 psa_key_type_t key_type = key_type_arg;
3386 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003387 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003388 psa_status_t actual_status;
3389 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003390 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003391 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003392 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003393
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003394 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003395
Gilles Peskine8817f612018-12-18 00:18:46 +01003396 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003397
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003398 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003399 psa_set_key_algorithm( &attributes, alg );
3400 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003401
Gilles Peskine049c7532019-05-15 20:22:09 +02003402 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003403 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003404
Ronald Cron5425a212020-08-04 14:58:35 +02003405 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003406 input_data->x, input_data->len,
3407 signature, signature_size,
3408 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003409 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003410 /* The value of *signature_length is unspecified on error, but
3411 * whatever it is, it should be less than signature_size, so that
3412 * if the caller tries to read *signature_length bytes without
3413 * checking the error code then they don't overflow a buffer. */
3414 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003415
3416exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003417 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003418 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003419 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003420 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003421}
3422/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003423
3424/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003425void sign_verify_hash( int key_type_arg, data_t *key_data,
3426 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003427{
Ronald Cron5425a212020-08-04 14:58:35 +02003428 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003429 psa_key_type_t key_type = key_type_arg;
3430 psa_algorithm_t alg = alg_arg;
3431 size_t key_bits;
3432 unsigned char *signature = NULL;
3433 size_t signature_size;
3434 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003435 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003436
Gilles Peskine8817f612018-12-18 00:18:46 +01003437 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003438
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003439 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003440 psa_set_key_algorithm( &attributes, alg );
3441 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003442
Gilles Peskine049c7532019-05-15 20:22:09 +02003443 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003444 &key ) );
3445 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003446 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003447
3448 /* Allocate a buffer which has the size advertized by the
3449 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003450 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003451 key_bits, alg );
3452 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003453 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003454 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003455
3456 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003457 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003458 input_data->x, input_data->len,
3459 signature, signature_size,
3460 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003461 /* Check that the signature length looks sensible. */
3462 TEST_ASSERT( signature_length <= signature_size );
3463 TEST_ASSERT( signature_length > 0 );
3464
3465 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003466 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003467 input_data->x, input_data->len,
3468 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003469
3470 if( input_data->len != 0 )
3471 {
3472 /* Flip a bit in the input and verify that the signature is now
3473 * detected as invalid. Flip a bit at the beginning, not at the end,
3474 * because ECDSA may ignore the last few bits of the input. */
3475 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003476 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003477 input_data->x, input_data->len,
3478 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003479 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003480 }
3481
3482exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003483 /*
3484 * Key attributes may have been returned by psa_get_key_attributes()
3485 * thus reset them as required.
3486 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003487 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003488
Ronald Cron5425a212020-08-04 14:58:35 +02003489 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003490 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003491 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003492}
3493/* END_CASE */
3494
3495/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003496void verify_hash( int key_type_arg, data_t *key_data,
3497 int alg_arg, data_t *hash_data,
3498 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003499{
Ronald Cron5425a212020-08-04 14:58:35 +02003500 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003501 psa_key_type_t key_type = key_type_arg;
3502 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003503 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003504
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003505 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003506
Gilles Peskine8817f612018-12-18 00:18:46 +01003507 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003508
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003509 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003510 psa_set_key_algorithm( &attributes, alg );
3511 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003512
Gilles Peskine049c7532019-05-15 20:22:09 +02003513 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003514 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003515
Ronald Cron5425a212020-08-04 14:58:35 +02003516 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003517 hash_data->x, hash_data->len,
3518 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003519
itayzafrir5c753392018-05-08 11:18:38 +03003520exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003521 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003522 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003523 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003524}
3525/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003526
3527/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003528void verify_hash_fail( int key_type_arg, data_t *key_data,
3529 int alg_arg, data_t *hash_data,
3530 data_t *signature_data,
3531 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003532{
Ronald Cron5425a212020-08-04 14:58:35 +02003533 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003534 psa_key_type_t key_type = key_type_arg;
3535 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003536 psa_status_t actual_status;
3537 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003538 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003539
Gilles Peskine8817f612018-12-18 00:18:46 +01003540 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003541
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003542 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003543 psa_set_key_algorithm( &attributes, alg );
3544 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003545
Gilles Peskine049c7532019-05-15 20:22:09 +02003546 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003547 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003548
Ronald Cron5425a212020-08-04 14:58:35 +02003549 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003550 hash_data->x, hash_data->len,
3551 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003552 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003553
3554exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003555 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003556 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003557 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003558}
3559/* END_CASE */
3560
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003561/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02003562void sign_message_deterministic( int key_type_arg,
3563 data_t *key_data,
3564 int alg_arg,
3565 data_t *input_data,
3566 data_t *output_data )
3567{
3568 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3569 psa_key_type_t key_type = key_type_arg;
3570 psa_algorithm_t alg = alg_arg;
3571 size_t key_bits;
3572 unsigned char *signature = NULL;
3573 size_t signature_size;
3574 size_t signature_length = 0xdeadbeef;
3575 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3576
3577 PSA_ASSERT( psa_crypto_init( ) );
3578
3579 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3580 psa_set_key_algorithm( &attributes, alg );
3581 psa_set_key_type( &attributes, key_type );
3582
3583 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3584 &key ) );
3585 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3586 key_bits = psa_get_key_bits( &attributes );
3587
3588 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3589 TEST_ASSERT( signature_size != 0 );
3590 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3591 ASSERT_ALLOC( signature, signature_size );
3592
3593 PSA_ASSERT( psa_sign_message( key, alg,
3594 input_data->x, input_data->len,
3595 signature, signature_size,
3596 &signature_length ) );
3597
3598 ASSERT_COMPARE( output_data->x, output_data->len,
3599 signature, signature_length );
3600
3601exit:
3602 psa_reset_key_attributes( &attributes );
3603
3604 psa_destroy_key( key );
3605 mbedtls_free( signature );
3606 PSA_DONE( );
3607
3608}
3609/* END_CASE */
3610
3611/* BEGIN_CASE */
3612void sign_message_fail( int key_type_arg,
3613 data_t *key_data,
3614 int alg_arg,
3615 data_t *input_data,
3616 int signature_size_arg,
3617 int expected_status_arg )
3618{
3619 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3620 psa_key_type_t key_type = key_type_arg;
3621 psa_algorithm_t alg = alg_arg;
3622 size_t signature_size = signature_size_arg;
3623 psa_status_t actual_status;
3624 psa_status_t expected_status = expected_status_arg;
3625 unsigned char *signature = NULL;
3626 size_t signature_length = 0xdeadbeef;
3627 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3628
3629 ASSERT_ALLOC( signature, signature_size );
3630
3631 PSA_ASSERT( psa_crypto_init( ) );
3632
3633 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3634 psa_set_key_algorithm( &attributes, alg );
3635 psa_set_key_type( &attributes, key_type );
3636
3637 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3638 &key ) );
3639
3640 actual_status = psa_sign_message( key, alg,
3641 input_data->x, input_data->len,
3642 signature, signature_size,
3643 &signature_length );
3644 TEST_EQUAL( actual_status, expected_status );
3645 /* The value of *signature_length is unspecified on error, but
3646 * whatever it is, it should be less than signature_size, so that
3647 * if the caller tries to read *signature_length bytes without
3648 * checking the error code then they don't overflow a buffer. */
3649 TEST_ASSERT( signature_length <= signature_size );
3650
3651exit:
3652 psa_reset_key_attributes( &attributes );
3653 psa_destroy_key( key );
3654 mbedtls_free( signature );
3655 PSA_DONE( );
3656}
3657/* END_CASE */
3658
3659/* BEGIN_CASE */
3660void sign_verify_message( int key_type_arg,
3661 data_t *key_data,
3662 int alg_arg,
3663 data_t *input_data )
3664{
3665 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3666 psa_key_type_t key_type = key_type_arg;
3667 psa_algorithm_t alg = alg_arg;
3668 size_t key_bits;
3669 unsigned char *signature = NULL;
3670 size_t signature_size;
3671 size_t signature_length = 0xdeadbeef;
3672 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3673
3674 PSA_ASSERT( psa_crypto_init( ) );
3675
3676 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3677 PSA_KEY_USAGE_VERIFY_MESSAGE );
3678 psa_set_key_algorithm( &attributes, alg );
3679 psa_set_key_type( &attributes, key_type );
3680
3681 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3682 &key ) );
3683 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3684 key_bits = psa_get_key_bits( &attributes );
3685
3686 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3687 TEST_ASSERT( signature_size != 0 );
3688 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3689 ASSERT_ALLOC( signature, signature_size );
3690
3691 PSA_ASSERT( psa_sign_message( key, alg,
3692 input_data->x, input_data->len,
3693 signature, signature_size,
3694 &signature_length ) );
3695 TEST_ASSERT( signature_length <= signature_size );
3696 TEST_ASSERT( signature_length > 0 );
3697
3698 PSA_ASSERT( psa_verify_message( key, alg,
3699 input_data->x, input_data->len,
3700 signature, signature_length ) );
3701
3702 if( input_data->len != 0 )
3703 {
3704 /* Flip a bit in the input and verify that the signature is now
3705 * detected as invalid. Flip a bit at the beginning, not at the end,
3706 * because ECDSA may ignore the last few bits of the input. */
3707 input_data->x[0] ^= 1;
3708 TEST_EQUAL( psa_verify_message( key, alg,
3709 input_data->x, input_data->len,
3710 signature, signature_length ),
3711 PSA_ERROR_INVALID_SIGNATURE );
3712 }
3713
3714exit:
3715 psa_reset_key_attributes( &attributes );
3716
3717 psa_destroy_key( key );
3718 mbedtls_free( signature );
3719 PSA_DONE( );
3720}
3721/* END_CASE */
3722
3723/* BEGIN_CASE */
3724void verify_message( int key_type_arg,
3725 data_t *key_data,
3726 int alg_arg,
3727 data_t *input_data,
3728 data_t *signature_data )
3729{
3730 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3731 psa_key_type_t key_type = key_type_arg;
3732 psa_algorithm_t alg = alg_arg;
3733 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3734
3735 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3736
3737 PSA_ASSERT( psa_crypto_init( ) );
3738
3739 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3740 psa_set_key_algorithm( &attributes, alg );
3741 psa_set_key_type( &attributes, key_type );
3742
3743 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3744 &key ) );
3745
3746 PSA_ASSERT( psa_verify_message( key, alg,
3747 input_data->x, input_data->len,
3748 signature_data->x, signature_data->len ) );
3749
3750exit:
3751 psa_reset_key_attributes( &attributes );
3752 psa_destroy_key( key );
3753 PSA_DONE( );
3754}
3755/* END_CASE */
3756
3757/* BEGIN_CASE */
3758void verify_message_fail( int key_type_arg,
3759 data_t *key_data,
3760 int alg_arg,
3761 data_t *hash_data,
3762 data_t *signature_data,
3763 int expected_status_arg )
3764{
3765 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3766 psa_key_type_t key_type = key_type_arg;
3767 psa_algorithm_t alg = alg_arg;
3768 psa_status_t actual_status;
3769 psa_status_t expected_status = expected_status_arg;
3770 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3771
3772 PSA_ASSERT( psa_crypto_init( ) );
3773
3774 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3775 psa_set_key_algorithm( &attributes, alg );
3776 psa_set_key_type( &attributes, key_type );
3777
3778 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3779 &key ) );
3780
3781 actual_status = psa_verify_message( key, alg,
3782 hash_data->x, hash_data->len,
3783 signature_data->x,
3784 signature_data->len );
3785 TEST_EQUAL( actual_status, expected_status );
3786
3787exit:
3788 psa_reset_key_attributes( &attributes );
3789 psa_destroy_key( key );
3790 PSA_DONE( );
3791}
3792/* END_CASE */
3793
3794/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003795void asymmetric_encrypt( int key_type_arg,
3796 data_t *key_data,
3797 int alg_arg,
3798 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003799 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003800 int expected_output_length_arg,
3801 int expected_status_arg )
3802{
Ronald Cron5425a212020-08-04 14:58:35 +02003803 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003804 psa_key_type_t key_type = key_type_arg;
3805 psa_algorithm_t alg = alg_arg;
3806 size_t expected_output_length = expected_output_length_arg;
3807 size_t key_bits;
3808 unsigned char *output = NULL;
3809 size_t output_size;
3810 size_t output_length = ~0;
3811 psa_status_t actual_status;
3812 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003813 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003814
Gilles Peskine8817f612018-12-18 00:18:46 +01003815 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003816
Gilles Peskine656896e2018-06-29 19:12:28 +02003817 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003818 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3819 psa_set_key_algorithm( &attributes, alg );
3820 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003821 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003822 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003823
3824 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003825 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003826 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003827
Gilles Peskine656896e2018-06-29 19:12:28 +02003828 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003829 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003830 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003831
3832 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003833 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003834 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003835 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003836 output, output_size,
3837 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003838 TEST_EQUAL( actual_status, expected_status );
3839 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003840
Gilles Peskine68428122018-06-30 18:42:41 +02003841 /* If the label is empty, the test framework puts a non-null pointer
3842 * in label->x. Test that a null pointer works as well. */
3843 if( label->len == 0 )
3844 {
3845 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003846 if( output_size != 0 )
3847 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003848 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003849 input_data->x, input_data->len,
3850 NULL, label->len,
3851 output, output_size,
3852 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003853 TEST_EQUAL( actual_status, expected_status );
3854 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003855 }
3856
Gilles Peskine656896e2018-06-29 19:12:28 +02003857exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003858 /*
3859 * Key attributes may have been returned by psa_get_key_attributes()
3860 * thus reset them as required.
3861 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003862 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003863
Ronald Cron5425a212020-08-04 14:58:35 +02003864 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02003865 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003866 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003867}
3868/* END_CASE */
3869
3870/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003871void asymmetric_encrypt_decrypt( int key_type_arg,
3872 data_t *key_data,
3873 int alg_arg,
3874 data_t *input_data,
3875 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003876{
Ronald Cron5425a212020-08-04 14:58:35 +02003877 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003878 psa_key_type_t key_type = key_type_arg;
3879 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003880 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003881 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003882 size_t output_size;
3883 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003884 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003885 size_t output2_size;
3886 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003887 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003888
Gilles Peskine8817f612018-12-18 00:18:46 +01003889 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003890
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003891 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3892 psa_set_key_algorithm( &attributes, alg );
3893 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003894
Gilles Peskine049c7532019-05-15 20:22:09 +02003895 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003896 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003897
3898 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02003899 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003900 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003901
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003902 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003903 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003904 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01003905
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003906 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01003907 TEST_ASSERT( output2_size <=
3908 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
3909 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003910 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003911
Gilles Peskineeebd7382018-06-08 18:11:54 +02003912 /* We test encryption by checking that encrypt-then-decrypt gives back
3913 * the original plaintext because of the non-optional random
3914 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02003915 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003916 input_data->x, input_data->len,
3917 label->x, label->len,
3918 output, output_size,
3919 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003920 /* We don't know what ciphertext length to expect, but check that
3921 * it looks sensible. */
3922 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003923
Ronald Cron5425a212020-08-04 14:58:35 +02003924 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003925 output, output_length,
3926 label->x, label->len,
3927 output2, output2_size,
3928 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003929 ASSERT_COMPARE( input_data->x, input_data->len,
3930 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003931
3932exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003933 /*
3934 * Key attributes may have been returned by psa_get_key_attributes()
3935 * thus reset them as required.
3936 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003937 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003938
Ronald Cron5425a212020-08-04 14:58:35 +02003939 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003940 mbedtls_free( output );
3941 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003942 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003943}
3944/* END_CASE */
3945
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003946/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003947void asymmetric_decrypt( int key_type_arg,
3948 data_t *key_data,
3949 int alg_arg,
3950 data_t *input_data,
3951 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003952 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003953{
Ronald Cron5425a212020-08-04 14:58:35 +02003954 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003955 psa_key_type_t key_type = key_type_arg;
3956 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01003957 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003958 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003959 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003960 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003961 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003962
Gilles Peskine8817f612018-12-18 00:18:46 +01003963 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003964
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003965 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3966 psa_set_key_algorithm( &attributes, alg );
3967 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003968
Gilles Peskine049c7532019-05-15 20:22:09 +02003969 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003970 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003971
gabor-mezei-armceface22021-01-21 12:26:17 +01003972 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3973 key_bits = psa_get_key_bits( &attributes );
3974
3975 /* Determine the maximum ciphertext length */
3976 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
3977 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
3978 ASSERT_ALLOC( output, output_size );
3979
Ronald Cron5425a212020-08-04 14:58:35 +02003980 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003981 input_data->x, input_data->len,
3982 label->x, label->len,
3983 output,
3984 output_size,
3985 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003986 ASSERT_COMPARE( expected_data->x, expected_data->len,
3987 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003988
Gilles Peskine68428122018-06-30 18:42:41 +02003989 /* If the label is empty, the test framework puts a non-null pointer
3990 * in label->x. Test that a null pointer works as well. */
3991 if( label->len == 0 )
3992 {
3993 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003994 if( output_size != 0 )
3995 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003996 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003997 input_data->x, input_data->len,
3998 NULL, label->len,
3999 output,
4000 output_size,
4001 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004002 ASSERT_COMPARE( expected_data->x, expected_data->len,
4003 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004004 }
4005
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004006exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004007 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004008 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004009 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004010 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004011}
4012/* END_CASE */
4013
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004014/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004015void asymmetric_decrypt_fail( int key_type_arg,
4016 data_t *key_data,
4017 int alg_arg,
4018 data_t *input_data,
4019 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004020 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004021 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004022{
Ronald Cron5425a212020-08-04 14:58:35 +02004023 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004024 psa_key_type_t key_type = key_type_arg;
4025 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004026 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004027 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004028 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004029 psa_status_t actual_status;
4030 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004031 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004032
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004033 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004034
Gilles Peskine8817f612018-12-18 00:18:46 +01004035 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004036
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004037 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4038 psa_set_key_algorithm( &attributes, alg );
4039 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004040
Gilles Peskine049c7532019-05-15 20:22:09 +02004041 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004042 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004043
Ronald Cron5425a212020-08-04 14:58:35 +02004044 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004045 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004046 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004047 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004048 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004049 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004050 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004051
Gilles Peskine68428122018-06-30 18:42:41 +02004052 /* If the label is empty, the test framework puts a non-null pointer
4053 * in label->x. Test that a null pointer works as well. */
4054 if( label->len == 0 )
4055 {
4056 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004057 if( output_size != 0 )
4058 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004059 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004060 input_data->x, input_data->len,
4061 NULL, label->len,
4062 output, output_size,
4063 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004064 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004065 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004066 }
4067
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004068exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004069 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004070 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004071 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004072 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004073}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004074/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004075
4076/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004077void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004078{
4079 /* Test each valid way of initializing the object, except for `= {0}`, as
4080 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4081 * though it's OK by the C standard. We could test for this, but we'd need
4082 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004083 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004084 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4085 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4086 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004087
4088 memset( &zero, 0, sizeof( zero ) );
4089
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004090 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004091 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004092 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004093 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004094 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004095 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004096 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004097
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004098 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004099 PSA_ASSERT( psa_key_derivation_abort(&func) );
4100 PSA_ASSERT( psa_key_derivation_abort(&init) );
4101 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004102}
4103/* END_CASE */
4104
Janos Follath16de4a42019-06-13 16:32:24 +01004105/* BEGIN_CASE */
4106void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004107{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004108 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004109 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004110 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004111
Gilles Peskine8817f612018-12-18 00:18:46 +01004112 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004113
Janos Follath16de4a42019-06-13 16:32:24 +01004114 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004115 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004116
4117exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004118 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004119 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004120}
4121/* END_CASE */
4122
Janos Follathaf3c2a02019-06-12 12:34:34 +01004123/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004124void derive_set_capacity( int alg_arg, int capacity_arg,
4125 int expected_status_arg )
4126{
4127 psa_algorithm_t alg = alg_arg;
4128 size_t capacity = capacity_arg;
4129 psa_status_t expected_status = expected_status_arg;
4130 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4131
4132 PSA_ASSERT( psa_crypto_init( ) );
4133
4134 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4135
4136 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4137 expected_status );
4138
4139exit:
4140 psa_key_derivation_abort( &operation );
4141 PSA_DONE( );
4142}
4143/* END_CASE */
4144
4145/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004146void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004147 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004148 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004149 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004150 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004151 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004152 int expected_status_arg3,
4153 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004154{
4155 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004156 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4157 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004158 psa_status_t expected_statuses[] = {expected_status_arg1,
4159 expected_status_arg2,
4160 expected_status_arg3};
4161 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004162 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4163 MBEDTLS_SVC_KEY_ID_INIT,
4164 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004165 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4166 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4167 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004168 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004169 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004170 psa_status_t expected_output_status = expected_output_status_arg;
4171 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004172
4173 PSA_ASSERT( psa_crypto_init( ) );
4174
4175 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4176 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004177
4178 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4179
4180 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4181 {
Gilles Peskine4023c012021-05-27 13:21:20 +02004182 mbedtls_test_set_step( i );
4183 if( steps[i] == 0 )
4184 {
4185 /* Skip this step */
4186 }
4187 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004188 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004189 psa_set_key_type( &attributes, key_types[i] );
4190 PSA_ASSERT( psa_import_key( &attributes,
4191 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004192 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004193 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4194 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4195 {
4196 // When taking a private key as secret input, use key agreement
4197 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004198 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4199 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004200 expected_statuses[i] );
4201 }
4202 else
4203 {
4204 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004205 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004206 expected_statuses[i] );
4207 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004208 }
4209 else
4210 {
4211 TEST_EQUAL( psa_key_derivation_input_bytes(
4212 &operation, steps[i],
4213 inputs[i]->x, inputs[i]->len ),
4214 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004215 }
4216 }
4217
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004218 if( output_key_type != PSA_KEY_TYPE_NONE )
4219 {
4220 psa_reset_key_attributes( &attributes );
4221 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4222 psa_set_key_bits( &attributes, 8 );
4223 actual_output_status =
4224 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004225 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004226 }
4227 else
4228 {
4229 uint8_t buffer[1];
4230 actual_output_status =
4231 psa_key_derivation_output_bytes( &operation,
4232 buffer, sizeof( buffer ) );
4233 }
4234 TEST_EQUAL( actual_output_status, expected_output_status );
4235
Janos Follathaf3c2a02019-06-12 12:34:34 +01004236exit:
4237 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004238 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4239 psa_destroy_key( keys[i] );
4240 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004241 PSA_DONE( );
4242}
4243/* END_CASE */
4244
Janos Follathd958bb72019-07-03 15:02:16 +01004245/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02004246void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004247{
Janos Follathd958bb72019-07-03 15:02:16 +01004248 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004249 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004250 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004251 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004252 unsigned char input1[] = "Input 1";
4253 size_t input1_length = sizeof( input1 );
4254 unsigned char input2[] = "Input 2";
4255 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004256 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004257 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004258 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4259 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4260 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004261 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004262
Gilles Peskine8817f612018-12-18 00:18:46 +01004263 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004264
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004265 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4266 psa_set_key_algorithm( &attributes, alg );
4267 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004268
Gilles Peskine73676cb2019-05-15 20:15:10 +02004269 PSA_ASSERT( psa_import_key( &attributes,
4270 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004271 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004272
4273 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004274 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4275 input1, input1_length,
4276 input2, input2_length,
4277 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004278 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004279
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004280 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004281 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004282 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004283
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004284 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004285
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004286 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004287 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004288
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004289exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004290 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004291 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004292 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004293}
4294/* END_CASE */
4295
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004296/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02004297void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004298{
4299 uint8_t output_buffer[16];
4300 size_t buffer_size = 16;
4301 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004302 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004303
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004304 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4305 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004306 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004307
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004308 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004309 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004310
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004311 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004312
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004313 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4314 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004315 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004316
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004317 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004318 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004319
4320exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004321 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004322}
4323/* END_CASE */
4324
4325/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004326void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004327 int step1_arg, data_t *input1,
4328 int step2_arg, data_t *input2,
4329 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004330 int requested_capacity_arg,
4331 data_t *expected_output1,
4332 data_t *expected_output2 )
4333{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004334 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004335 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4336 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004337 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4338 MBEDTLS_SVC_KEY_ID_INIT,
4339 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004340 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004341 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004342 uint8_t *expected_outputs[2] =
4343 {expected_output1->x, expected_output2->x};
4344 size_t output_sizes[2] =
4345 {expected_output1->len, expected_output2->len};
4346 size_t output_buffer_size = 0;
4347 uint8_t *output_buffer = NULL;
4348 size_t expected_capacity;
4349 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004350 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004351 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004352 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004353
4354 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4355 {
4356 if( output_sizes[i] > output_buffer_size )
4357 output_buffer_size = output_sizes[i];
4358 if( output_sizes[i] == 0 )
4359 expected_outputs[i] = NULL;
4360 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004361 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004362 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004363
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004364 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4365 psa_set_key_algorithm( &attributes, alg );
4366 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004367
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004368 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004369 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4370 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4371 requested_capacity ) );
4372 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004373 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004374 switch( steps[i] )
4375 {
4376 case 0:
4377 break;
4378 case PSA_KEY_DERIVATION_INPUT_SECRET:
4379 PSA_ASSERT( psa_import_key( &attributes,
4380 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004381 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004382
4383 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4384 {
4385 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4386 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4387 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4388 }
4389
Gilles Peskine1468da72019-05-29 17:35:49 +02004390 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004391 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004392 break;
4393 default:
4394 PSA_ASSERT( psa_key_derivation_input_bytes(
4395 &operation, steps[i],
4396 inputs[i]->x, inputs[i]->len ) );
4397 break;
4398 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004399 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004400
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004401 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004402 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004403 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004404 expected_capacity = requested_capacity;
4405
4406 /* Expansion phase. */
4407 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4408 {
4409 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004410 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004411 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004412 if( expected_capacity == 0 && output_sizes[i] == 0 )
4413 {
4414 /* Reading 0 bytes when 0 bytes are available can go either way. */
4415 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004416 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004417 continue;
4418 }
4419 else if( expected_capacity == 0 ||
4420 output_sizes[i] > expected_capacity )
4421 {
4422 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004423 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004424 expected_capacity = 0;
4425 continue;
4426 }
4427 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004428 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004429 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004430 ASSERT_COMPARE( output_buffer, output_sizes[i],
4431 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004432 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004433 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004434 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004435 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004436 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004437 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004438 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004439
4440exit:
4441 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004442 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004443 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4444 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004445 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004446}
4447/* END_CASE */
4448
4449/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004450void derive_full( int alg_arg,
4451 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004452 data_t *input1,
4453 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004454 int requested_capacity_arg )
4455{
Ronald Cron5425a212020-08-04 14:58:35 +02004456 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004457 psa_algorithm_t alg = alg_arg;
4458 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004459 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004460 unsigned char output_buffer[16];
4461 size_t expected_capacity = requested_capacity;
4462 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004463 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004464
Gilles Peskine8817f612018-12-18 00:18:46 +01004465 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004466
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004467 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4468 psa_set_key_algorithm( &attributes, alg );
4469 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004470
Gilles Peskine049c7532019-05-15 20:22:09 +02004471 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004472 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004473
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004474 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4475 input1->x, input1->len,
4476 input2->x, input2->len,
4477 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004478 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004479
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004480 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004481 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004482 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004483
4484 /* Expansion phase. */
4485 while( current_capacity > 0 )
4486 {
4487 size_t read_size = sizeof( output_buffer );
4488 if( read_size > current_capacity )
4489 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004490 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004491 output_buffer,
4492 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004493 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004494 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004495 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004496 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004497 }
4498
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004499 /* Check that the operation refuses to go over capacity. */
4500 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004501 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004502
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004503 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004504
4505exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004506 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004507 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004508 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004509}
4510/* END_CASE */
4511
Janos Follathe60c9052019-07-03 13:51:30 +01004512/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004513void derive_key_exercise( int alg_arg,
4514 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004515 data_t *input1,
4516 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004517 int derived_type_arg,
4518 int derived_bits_arg,
4519 int derived_usage_arg,
4520 int derived_alg_arg )
4521{
Ronald Cron5425a212020-08-04 14:58:35 +02004522 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4523 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004524 psa_algorithm_t alg = alg_arg;
4525 psa_key_type_t derived_type = derived_type_arg;
4526 size_t derived_bits = derived_bits_arg;
4527 psa_key_usage_t derived_usage = derived_usage_arg;
4528 psa_algorithm_t derived_alg = derived_alg_arg;
4529 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004530 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004531 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004532 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004533
Gilles Peskine8817f612018-12-18 00:18:46 +01004534 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004535
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004536 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4537 psa_set_key_algorithm( &attributes, alg );
4538 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004539 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004540 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004541
4542 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004543 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4544 input1->x, input1->len,
4545 input2->x, input2->len,
4546 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004547 goto exit;
4548
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004549 psa_set_key_usage_flags( &attributes, derived_usage );
4550 psa_set_key_algorithm( &attributes, derived_alg );
4551 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004552 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004553 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004554 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004555
4556 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004557 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004558 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4559 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004560
4561 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004562 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004563 goto exit;
4564
4565exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004566 /*
4567 * Key attributes may have been returned by psa_get_key_attributes()
4568 * thus reset them as required.
4569 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004570 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004571
4572 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004573 psa_destroy_key( base_key );
4574 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004575 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004576}
4577/* END_CASE */
4578
Janos Follath42fd8882019-07-03 14:17:09 +01004579/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004580void derive_key_export( int alg_arg,
4581 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004582 data_t *input1,
4583 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004584 int bytes1_arg,
4585 int bytes2_arg )
4586{
Ronald Cron5425a212020-08-04 14:58:35 +02004587 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4588 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004589 psa_algorithm_t alg = alg_arg;
4590 size_t bytes1 = bytes1_arg;
4591 size_t bytes2 = bytes2_arg;
4592 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004593 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004594 uint8_t *output_buffer = NULL;
4595 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004596 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4597 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004598 size_t length;
4599
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004600 ASSERT_ALLOC( output_buffer, capacity );
4601 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004602 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004603
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004604 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4605 psa_set_key_algorithm( &base_attributes, alg );
4606 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004607 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004608 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004609
4610 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004611 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4612 input1->x, input1->len,
4613 input2->x, input2->len,
4614 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004615 goto exit;
4616
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004617 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004618 output_buffer,
4619 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004620 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004621
4622 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004623 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4624 input1->x, input1->len,
4625 input2->x, input2->len,
4626 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004627 goto exit;
4628
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004629 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4630 psa_set_key_algorithm( &derived_attributes, 0 );
4631 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004632 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004633 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004634 &derived_key ) );
4635 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004636 export_buffer, bytes1,
4637 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004638 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004639 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004640 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004641 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004642 &derived_key ) );
4643 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004644 export_buffer + bytes1, bytes2,
4645 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004646 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004647
4648 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004649 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4650 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004651
4652exit:
4653 mbedtls_free( output_buffer );
4654 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004655 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004656 psa_destroy_key( base_key );
4657 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004658 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004659}
4660/* END_CASE */
4661
4662/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004663void derive_key( int alg_arg,
4664 data_t *key_data, data_t *input1, data_t *input2,
4665 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004666 int expected_status_arg,
4667 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004668{
Ronald Cron5425a212020-08-04 14:58:35 +02004669 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4670 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004671 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004672 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004673 size_t bits = bits_arg;
4674 psa_status_t expected_status = expected_status_arg;
4675 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4676 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4677 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4678
4679 PSA_ASSERT( psa_crypto_init( ) );
4680
4681 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4682 psa_set_key_algorithm( &base_attributes, alg );
4683 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4684 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004685 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004686
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004687 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4688 input1->x, input1->len,
4689 input2->x, input2->len,
4690 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004691 goto exit;
4692
4693 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4694 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004695 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004696 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004697
4698 psa_status_t status =
4699 psa_key_derivation_output_key( &derived_attributes,
4700 &operation,
4701 &derived_key );
4702 if( is_large_output > 0 )
4703 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4704 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004705
4706exit:
4707 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004708 psa_destroy_key( base_key );
4709 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004710 PSA_DONE( );
4711}
4712/* END_CASE */
4713
4714/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004715void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004716 int our_key_type_arg, int our_key_alg_arg,
4717 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004718 int expected_status_arg )
4719{
Ronald Cron5425a212020-08-04 14:58:35 +02004720 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004721 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004722 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004723 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004724 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004725 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004726 psa_status_t expected_status = expected_status_arg;
4727 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004728
Gilles Peskine8817f612018-12-18 00:18:46 +01004729 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004730
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004731 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004732 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004733 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004734 PSA_ASSERT( psa_import_key( &attributes,
4735 our_key_data->x, our_key_data->len,
4736 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004737
Gilles Peskine77f40d82019-04-11 21:27:06 +02004738 /* The tests currently include inputs that should fail at either step.
4739 * Test cases that fail at the setup step should be changed to call
4740 * key_derivation_setup instead, and this function should be renamed
4741 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004742 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004743 if( status == PSA_SUCCESS )
4744 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004745 TEST_EQUAL( psa_key_derivation_key_agreement(
4746 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4747 our_key,
4748 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004749 expected_status );
4750 }
4751 else
4752 {
4753 TEST_ASSERT( status == expected_status );
4754 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004755
4756exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004757 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004758 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004759 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004760}
4761/* END_CASE */
4762
4763/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004764void raw_key_agreement( int alg_arg,
4765 int our_key_type_arg, data_t *our_key_data,
4766 data_t *peer_key_data,
4767 data_t *expected_output )
4768{
Ronald Cron5425a212020-08-04 14:58:35 +02004769 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004770 psa_algorithm_t alg = alg_arg;
4771 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004772 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004773 unsigned char *output = NULL;
4774 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004775 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004776
4777 ASSERT_ALLOC( output, expected_output->len );
4778 PSA_ASSERT( psa_crypto_init( ) );
4779
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004780 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4781 psa_set_key_algorithm( &attributes, alg );
4782 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004783 PSA_ASSERT( psa_import_key( &attributes,
4784 our_key_data->x, our_key_data->len,
4785 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004786
gabor-mezei-armceface22021-01-21 12:26:17 +01004787 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4788 key_bits = psa_get_key_bits( &attributes );
4789
Gilles Peskinebe697d82019-05-16 18:00:41 +02004790 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4791 peer_key_data->x, peer_key_data->len,
4792 output, expected_output->len,
4793 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004794 ASSERT_COMPARE( output, output_length,
4795 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01004796 TEST_ASSERT( output_length <=
4797 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
4798 TEST_ASSERT( output_length <=
4799 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004800
4801exit:
4802 mbedtls_free( output );
4803 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004804 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004805}
4806/* END_CASE */
4807
4808/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004809void key_agreement_capacity( int alg_arg,
4810 int our_key_type_arg, data_t *our_key_data,
4811 data_t *peer_key_data,
4812 int expected_capacity_arg )
4813{
Ronald Cron5425a212020-08-04 14:58:35 +02004814 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004815 psa_algorithm_t alg = alg_arg;
4816 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004817 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004818 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004819 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004820 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004821
Gilles Peskine8817f612018-12-18 00:18:46 +01004822 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004823
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004824 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4825 psa_set_key_algorithm( &attributes, alg );
4826 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004827 PSA_ASSERT( psa_import_key( &attributes,
4828 our_key_data->x, our_key_data->len,
4829 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004830
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004831 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004832 PSA_ASSERT( psa_key_derivation_key_agreement(
4833 &operation,
4834 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4835 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004836 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4837 {
4838 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004839 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004840 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004841 NULL, 0 ) );
4842 }
Gilles Peskine59685592018-09-18 12:11:34 +02004843
Gilles Peskinebf491972018-10-25 22:36:12 +02004844 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004845 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004846 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004847 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004848
Gilles Peskinebf491972018-10-25 22:36:12 +02004849 /* Test the actual capacity by reading the output. */
4850 while( actual_capacity > sizeof( output ) )
4851 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004852 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004853 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004854 actual_capacity -= sizeof( output );
4855 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004856 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004857 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004858 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004859 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004860
Gilles Peskine59685592018-09-18 12:11:34 +02004861exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004862 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004863 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004864 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004865}
4866/* END_CASE */
4867
4868/* BEGIN_CASE */
4869void key_agreement_output( int alg_arg,
4870 int our_key_type_arg, data_t *our_key_data,
4871 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004872 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004873{
Ronald Cron5425a212020-08-04 14:58:35 +02004874 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004875 psa_algorithm_t alg = alg_arg;
4876 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004877 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004878 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004879 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004880
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004881 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4882 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004883
Gilles Peskine8817f612018-12-18 00:18:46 +01004884 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004885
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004886 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4887 psa_set_key_algorithm( &attributes, alg );
4888 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004889 PSA_ASSERT( psa_import_key( &attributes,
4890 our_key_data->x, our_key_data->len,
4891 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004892
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004893 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004894 PSA_ASSERT( psa_key_derivation_key_agreement(
4895 &operation,
4896 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4897 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004898 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4899 {
4900 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004901 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004902 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004903 NULL, 0 ) );
4904 }
Gilles Peskine59685592018-09-18 12:11:34 +02004905
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004906 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004907 actual_output,
4908 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004909 ASSERT_COMPARE( actual_output, expected_output1->len,
4910 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004911 if( expected_output2->len != 0 )
4912 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004913 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004914 actual_output,
4915 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004916 ASSERT_COMPARE( actual_output, expected_output2->len,
4917 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004918 }
Gilles Peskine59685592018-09-18 12:11:34 +02004919
4920exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004921 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004922 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004923 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004924 mbedtls_free( actual_output );
4925}
4926/* END_CASE */
4927
4928/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004929void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004930{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004931 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004932 unsigned char *output = NULL;
4933 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004934 size_t i;
4935 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004936
Simon Butcher49f8e312020-03-03 15:51:50 +00004937 TEST_ASSERT( bytes_arg >= 0 );
4938
Gilles Peskine91892022021-02-08 19:50:26 +01004939 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004940 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02004941
Gilles Peskine8817f612018-12-18 00:18:46 +01004942 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004943
Gilles Peskinea50d7392018-06-21 10:22:13 +02004944 /* Run several times, to ensure that every output byte will be
4945 * nonzero at least once with overwhelming probability
4946 * (2^(-8*number_of_runs)). */
4947 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004948 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004949 if( bytes != 0 )
4950 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004951 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004952
Gilles Peskinea50d7392018-06-21 10:22:13 +02004953 for( i = 0; i < bytes; i++ )
4954 {
4955 if( output[i] != 0 )
4956 ++changed[i];
4957 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004958 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004959
4960 /* Check that every byte was changed to nonzero at least once. This
4961 * validates that psa_generate_random is overwriting every byte of
4962 * the output buffer. */
4963 for( i = 0; i < bytes; i++ )
4964 {
4965 TEST_ASSERT( changed[i] != 0 );
4966 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004967
4968exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004969 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004970 mbedtls_free( output );
4971 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004972}
4973/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004974
4975/* BEGIN_CASE */
4976void generate_key( int type_arg,
4977 int bits_arg,
4978 int usage_arg,
4979 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004980 int expected_status_arg,
4981 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004982{
Ronald Cron5425a212020-08-04 14:58:35 +02004983 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004984 psa_key_type_t type = type_arg;
4985 psa_key_usage_t usage = usage_arg;
4986 size_t bits = bits_arg;
4987 psa_algorithm_t alg = alg_arg;
4988 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004989 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004990 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004991
Gilles Peskine8817f612018-12-18 00:18:46 +01004992 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004993
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004994 psa_set_key_usage_flags( &attributes, usage );
4995 psa_set_key_algorithm( &attributes, alg );
4996 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004997 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004998
4999 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005000 psa_status_t status = psa_generate_key( &attributes, &key );
5001
5002 if( is_large_key > 0 )
5003 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5004 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005005 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005006 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005007
5008 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005009 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005010 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5011 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005012
Gilles Peskine818ca122018-06-20 18:16:48 +02005013 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005014 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005015 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005016
5017exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005018 /*
5019 * Key attributes may have been returned by psa_get_key_attributes()
5020 * thus reset them as required.
5021 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005022 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005023
Ronald Cron5425a212020-08-04 14:58:35 +02005024 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005025 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005026}
5027/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005028
Ronald Cronee414c72021-03-18 18:50:08 +01005029/* 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 +02005030void generate_key_rsa( int bits_arg,
5031 data_t *e_arg,
5032 int expected_status_arg )
5033{
Ronald Cron5425a212020-08-04 14:58:35 +02005034 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005035 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005036 size_t bits = bits_arg;
5037 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5038 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5039 psa_status_t expected_status = expected_status_arg;
5040 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5041 uint8_t *exported = NULL;
5042 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005043 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005044 size_t exported_length = SIZE_MAX;
5045 uint8_t *e_read_buffer = NULL;
5046 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005047 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005048 size_t e_read_length = SIZE_MAX;
5049
5050 if( e_arg->len == 0 ||
5051 ( e_arg->len == 3 &&
5052 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5053 {
5054 is_default_public_exponent = 1;
5055 e_read_size = 0;
5056 }
5057 ASSERT_ALLOC( e_read_buffer, e_read_size );
5058 ASSERT_ALLOC( exported, exported_size );
5059
5060 PSA_ASSERT( psa_crypto_init( ) );
5061
5062 psa_set_key_usage_flags( &attributes, usage );
5063 psa_set_key_algorithm( &attributes, alg );
5064 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5065 e_arg->x, e_arg->len ) );
5066 psa_set_key_bits( &attributes, bits );
5067
5068 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005069 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005070 if( expected_status != PSA_SUCCESS )
5071 goto exit;
5072
5073 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005074 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005075 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5076 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5077 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5078 e_read_buffer, e_read_size,
5079 &e_read_length ) );
5080 if( is_default_public_exponent )
5081 TEST_EQUAL( e_read_length, 0 );
5082 else
5083 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5084
5085 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005086 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005087 goto exit;
5088
5089 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005090 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005091 exported, exported_size,
5092 &exported_length ) );
5093 {
5094 uint8_t *p = exported;
5095 uint8_t *end = exported + exported_length;
5096 size_t len;
5097 /* RSAPublicKey ::= SEQUENCE {
5098 * modulus INTEGER, -- n
5099 * publicExponent INTEGER } -- e
5100 */
5101 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005102 MBEDTLS_ASN1_SEQUENCE |
5103 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005104 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005105 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5106 MBEDTLS_ASN1_INTEGER ) );
5107 if( len >= 1 && p[0] == 0 )
5108 {
5109 ++p;
5110 --len;
5111 }
5112 if( e_arg->len == 0 )
5113 {
5114 TEST_EQUAL( len, 3 );
5115 TEST_EQUAL( p[0], 1 );
5116 TEST_EQUAL( p[1], 0 );
5117 TEST_EQUAL( p[2], 1 );
5118 }
5119 else
5120 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5121 }
5122
5123exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005124 /*
5125 * Key attributes may have been returned by psa_get_key_attributes() or
5126 * set by psa_set_key_domain_parameters() thus reset them as required.
5127 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005128 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005129
Ronald Cron5425a212020-08-04 14:58:35 +02005130 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005131 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005132 mbedtls_free( e_read_buffer );
5133 mbedtls_free( exported );
5134}
5135/* END_CASE */
5136
Darryl Greend49a4992018-06-18 17:27:26 +01005137/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005138void persistent_key_load_key_from_storage( data_t *data,
5139 int type_arg, int bits_arg,
5140 int usage_flags_arg, int alg_arg,
5141 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005142{
Ronald Cron71016a92020-08-28 19:01:50 +02005143 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005144 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005145 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5146 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005147 psa_key_type_t type = type_arg;
5148 size_t bits = bits_arg;
5149 psa_key_usage_t usage_flags = usage_flags_arg;
5150 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005151 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005152 unsigned char *first_export = NULL;
5153 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005154 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005155 size_t first_exported_length;
5156 size_t second_exported_length;
5157
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005158 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5159 {
5160 ASSERT_ALLOC( first_export, export_size );
5161 ASSERT_ALLOC( second_export, export_size );
5162 }
Darryl Greend49a4992018-06-18 17:27:26 +01005163
Gilles Peskine8817f612018-12-18 00:18:46 +01005164 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005165
Gilles Peskinec87af662019-05-15 16:12:22 +02005166 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005167 psa_set_key_usage_flags( &attributes, usage_flags );
5168 psa_set_key_algorithm( &attributes, alg );
5169 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005170 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005171
Darryl Green0c6575a2018-11-07 16:05:30 +00005172 switch( generation_method )
5173 {
5174 case IMPORT_KEY:
5175 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005176 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005177 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005178 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005179
Darryl Green0c6575a2018-11-07 16:05:30 +00005180 case GENERATE_KEY:
5181 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005182 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005183 break;
5184
5185 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005186#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005187 {
5188 /* Create base key */
5189 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5190 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5191 psa_set_key_usage_flags( &base_attributes,
5192 PSA_KEY_USAGE_DERIVE );
5193 psa_set_key_algorithm( &base_attributes, derive_alg );
5194 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005195 PSA_ASSERT( psa_import_key( &base_attributes,
5196 data->x, data->len,
5197 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005198 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005199 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005200 PSA_ASSERT( psa_key_derivation_input_key(
5201 &operation,
5202 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005203 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005204 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005205 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005206 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5207 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005208 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005209 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005210 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005211 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005212 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005213#else
5214 TEST_ASSUME( ! "KDF not supported in this configuration" );
5215#endif
5216 break;
5217
5218 default:
5219 TEST_ASSERT( ! "generation_method not implemented in test" );
5220 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005221 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005222 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005223
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005224 /* Export the key if permitted by the key policy. */
5225 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5226 {
Ronald Cron5425a212020-08-04 14:58:35 +02005227 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005228 first_export, export_size,
5229 &first_exported_length ) );
5230 if( generation_method == IMPORT_KEY )
5231 ASSERT_COMPARE( data->x, data->len,
5232 first_export, first_exported_length );
5233 }
Darryl Greend49a4992018-06-18 17:27:26 +01005234
5235 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005236 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005237 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005238 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005239
Darryl Greend49a4992018-06-18 17:27:26 +01005240 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005241 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005242 TEST_ASSERT( mbedtls_svc_key_id_equal(
5243 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005244 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5245 PSA_KEY_LIFETIME_PERSISTENT );
5246 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5247 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02005248 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02005249 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005250 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005251
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005252 /* Export the key again if permitted by the key policy. */
5253 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005254 {
Ronald Cron5425a212020-08-04 14:58:35 +02005255 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005256 second_export, export_size,
5257 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005258 ASSERT_COMPARE( first_export, first_exported_length,
5259 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005260 }
5261
5262 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005263 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005264 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005265
5266exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005267 /*
5268 * Key attributes may have been returned by psa_get_key_attributes()
5269 * thus reset them as required.
5270 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005271 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005272
Darryl Greend49a4992018-06-18 17:27:26 +01005273 mbedtls_free( first_export );
5274 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005275 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005276 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005277 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005278 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005279}
5280/* END_CASE */