blob: 150a3f43e393d53b136bf3f7eef73191b497c36f [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010018
Gilles Peskine4023c012021-05-27 13:21:20 +020019/* If this comes up, it's a bug in the test code or in the test data. */
20#define UNUSED 0xdeadbeef
21
Dave Rodgman647791d2021-06-23 12:49:59 +010022/* Assert that an operation is (not) active.
23 * This serves as a proxy for checking if the operation is aborted. */
24#define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 )
25#define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 )
26
Jaeden Amerof24c7f82018-06-27 17:20:43 +010027/** An invalid export length that will never be set by psa_export_key(). */
28static const size_t INVALID_EXPORT_LENGTH = ~0U;
29
Gilles Peskinea7aa4422018-08-14 15:17:54 +020030/** Test if a buffer contains a constant byte value.
31 *
32 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020033 *
34 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020035 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020036 * \param size Size of the buffer in bytes.
37 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020038 * \return 1 if the buffer is all-bits-zero.
39 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020040 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042{
43 size_t i;
44 for( i = 0; i < size; i++ )
45 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020046 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020047 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020048 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020049 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020050}
Gilles Peskine818ca122018-06-20 18:16:48 +020051
Gilles Peskine0b352bc2018-06-28 00:16:11 +020052/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
53static int asn1_write_10x( unsigned char **p,
54 unsigned char *start,
55 size_t bits,
56 unsigned char x )
57{
58 int ret;
59 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020060 if( bits == 0 )
61 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
62 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020063 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030064 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020065 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
66 *p -= len;
67 ( *p )[len-1] = x;
68 if( bits % 8 == 0 )
69 ( *p )[1] |= 1;
70 else
71 ( *p )[0] |= 1 << ( bits % 8 );
72 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
73 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
74 MBEDTLS_ASN1_INTEGER ) );
75 return( len );
76}
77
78static int construct_fake_rsa_key( unsigned char *buffer,
79 size_t buffer_size,
80 unsigned char **p,
81 size_t bits,
82 int keypair )
83{
84 size_t half_bits = ( bits + 1 ) / 2;
85 int ret;
86 int len = 0;
87 /* Construct something that looks like a DER encoding of
88 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
89 * RSAPrivateKey ::= SEQUENCE {
90 * version Version,
91 * modulus INTEGER, -- n
92 * publicExponent INTEGER, -- e
93 * privateExponent INTEGER, -- d
94 * prime1 INTEGER, -- p
95 * prime2 INTEGER, -- q
96 * exponent1 INTEGER, -- d mod (p-1)
97 * exponent2 INTEGER, -- d mod (q-1)
98 * coefficient INTEGER, -- (inverse of q) mod p
99 * otherPrimeInfos OtherPrimeInfos OPTIONAL
100 * }
101 * Or, for a public key, the same structure with only
102 * version, modulus and publicExponent.
103 */
104 *p = buffer + buffer_size;
105 if( keypair )
106 {
107 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
108 asn1_write_10x( p, buffer, half_bits, 1 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
110 asn1_write_10x( p, buffer, half_bits, 1 ) );
111 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
112 asn1_write_10x( p, buffer, half_bits, 1 ) );
113 MBEDTLS_ASN1_CHK_ADD( len, /* q */
114 asn1_write_10x( p, buffer, half_bits, 1 ) );
115 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
116 asn1_write_10x( p, buffer, half_bits, 3 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* d */
118 asn1_write_10x( p, buffer, bits, 1 ) );
119 }
120 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
121 asn1_write_10x( p, buffer, 17, 1 ) );
122 MBEDTLS_ASN1_CHK_ADD( len, /* n */
123 asn1_write_10x( p, buffer, bits, 1 ) );
124 if( keypair )
125 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
126 mbedtls_asn1_write_int( p, buffer, 0 ) );
127 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
128 {
129 const unsigned char tag =
130 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
131 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
132 }
133 return( len );
134}
135
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100136int exercise_mac_setup( psa_key_type_t key_type,
137 const unsigned char *key_bytes,
138 size_t key_length,
139 psa_algorithm_t alg,
140 psa_mac_operation_t *operation,
141 psa_status_t *status )
142{
Ronald Cron5425a212020-08-04 14:58:35 +0200143 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200144 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100145
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100146 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200147 psa_set_key_algorithm( &attributes, alg );
148 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200149 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100150
Ronald Cron5425a212020-08-04 14:58:35 +0200151 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100152 /* Whether setup succeeded or failed, abort must succeed. */
153 PSA_ASSERT( psa_mac_abort( operation ) );
154 /* If setup failed, reproduce the failure, so that the caller can
155 * test the resulting state of the operation object. */
156 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100157 {
Ronald Cron5425a212020-08-04 14:58:35 +0200158 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100159 }
160
Ronald Cron5425a212020-08-04 14:58:35 +0200161 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100162 return( 1 );
163
164exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200165 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100166 return( 0 );
167}
168
169int exercise_cipher_setup( psa_key_type_t key_type,
170 const unsigned char *key_bytes,
171 size_t key_length,
172 psa_algorithm_t alg,
173 psa_cipher_operation_t *operation,
174 psa_status_t *status )
175{
Ronald Cron5425a212020-08-04 14:58:35 +0200176 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200177 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100178
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200179 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
180 psa_set_key_algorithm( &attributes, alg );
181 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200182 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100183
Ronald Cron5425a212020-08-04 14:58:35 +0200184 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100185 /* Whether setup succeeded or failed, abort must succeed. */
186 PSA_ASSERT( psa_cipher_abort( operation ) );
187 /* If setup failed, reproduce the failure, so that the caller can
188 * test the resulting state of the operation object. */
189 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100190 {
Ronald Cron5425a212020-08-04 14:58:35 +0200191 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100192 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100193 }
194
Ronald Cron5425a212020-08-04 14:58:35 +0200195 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100196 return( 1 );
197
198exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200199 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100200 return( 0 );
201}
202
Ronald Cron5425a212020-08-04 14:58:35 +0200203static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200204{
205 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200206 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200207 uint8_t buffer[1];
208 size_t length;
209 int ok = 0;
210
Ronald Cronecfb2372020-07-23 17:13:42 +0200211 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200212 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
213 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
214 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200215 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000216 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200217 TEST_EQUAL(
218 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
219 TEST_EQUAL(
220 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200221 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200222 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
223 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
224 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
225 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
226
Ronald Cron5425a212020-08-04 14:58:35 +0200227 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000228 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200229 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200230 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000231 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200232
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200233 ok = 1;
234
235exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100236 /*
237 * Key attributes may have been returned by psa_get_key_attributes()
238 * thus reset them as required.
239 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200240 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100241
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200242 return( ok );
243}
244
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200245/* Assert that a key isn't reported as having a slot number. */
246#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
247#define ASSERT_NO_SLOT_NUMBER( attributes ) \
248 do \
249 { \
250 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
251 TEST_EQUAL( psa_get_key_slot_number( \
252 attributes, \
253 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
254 PSA_ERROR_INVALID_ARGUMENT ); \
255 } \
256 while( 0 )
257#else /* MBEDTLS_PSA_CRYPTO_SE_C */
258#define ASSERT_NO_SLOT_NUMBER( attributes ) \
259 ( (void) 0 )
260#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
261
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100262/* An overapproximation of the amount of storage needed for a key of the
263 * given type and with the given content. The API doesn't make it easy
264 * to find a good value for the size. The current implementation doesn't
265 * care about the value anyway. */
266#define KEY_BITS_FROM_DATA( type, data ) \
267 ( data )->len
268
Darryl Green0c6575a2018-11-07 16:05:30 +0000269typedef enum {
270 IMPORT_KEY = 0,
271 GENERATE_KEY = 1,
272 DERIVE_KEY = 2
273} generate_method;
274
Gilles Peskinee59236f2018-01-27 23:32:46 +0100275/* END_HEADER */
276
277/* BEGIN_DEPENDENCIES
278 * depends_on:MBEDTLS_PSA_CRYPTO_C
279 * END_DEPENDENCIES
280 */
281
282/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200283void static_checks( )
284{
285 size_t max_truncated_mac_size =
286 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
287
288 /* Check that the length for a truncated MAC always fits in the algorithm
289 * encoding. The shifted mask is the maximum truncated value. The
290 * untruncated algorithm may be one byte larger. */
291 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
292}
293/* END_CASE */
294
295/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200296void import_with_policy( int type_arg,
297 int usage_arg, int alg_arg,
298 int expected_status_arg )
299{
300 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
301 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200302 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200303 psa_key_type_t type = type_arg;
304 psa_key_usage_t usage = usage_arg;
305 psa_algorithm_t alg = alg_arg;
306 psa_status_t expected_status = expected_status_arg;
307 const uint8_t key_material[16] = {0};
308 psa_status_t status;
309
310 PSA_ASSERT( psa_crypto_init( ) );
311
312 psa_set_key_type( &attributes, type );
313 psa_set_key_usage_flags( &attributes, usage );
314 psa_set_key_algorithm( &attributes, alg );
315
316 status = psa_import_key( &attributes,
317 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200318 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200319 TEST_EQUAL( status, expected_status );
320 if( status != PSA_SUCCESS )
321 goto exit;
322
Ronald Cron5425a212020-08-04 14:58:35 +0200323 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200324 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200325 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200326 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200327 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200328 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200329
Ronald Cron5425a212020-08-04 14:58:35 +0200330 PSA_ASSERT( psa_destroy_key( key ) );
331 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200332
333exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100334 /*
335 * Key attributes may have been returned by psa_get_key_attributes()
336 * thus reset them as required.
337 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200338 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100339
340 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200341 PSA_DONE( );
342}
343/* END_CASE */
344
345/* BEGIN_CASE */
346void import_with_data( data_t *data, int type_arg,
347 int attr_bits_arg,
348 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200349{
350 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
351 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200352 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200353 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200354 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200355 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100356 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100357
Gilles Peskine8817f612018-12-18 00:18:46 +0100358 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100359
Gilles Peskine4747d192019-04-17 15:05:45 +0200360 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200361 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200362
Ronald Cron5425a212020-08-04 14:58:35 +0200363 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100364 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200365 if( status != PSA_SUCCESS )
366 goto exit;
367
Ronald Cron5425a212020-08-04 14:58:35 +0200368 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200369 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200370 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200371 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200372 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200373
Ronald Cron5425a212020-08-04 14:58:35 +0200374 PSA_ASSERT( psa_destroy_key( key ) );
375 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100376
377exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100378 /*
379 * Key attributes may have been returned by psa_get_key_attributes()
380 * thus reset them as required.
381 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200382 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100383
384 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200385 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100386}
387/* END_CASE */
388
389/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200390void import_large_key( int type_arg, int byte_size_arg,
391 int expected_status_arg )
392{
393 psa_key_type_t type = type_arg;
394 size_t byte_size = byte_size_arg;
395 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
396 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200397 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200398 psa_status_t status;
399 uint8_t *buffer = NULL;
400 size_t buffer_size = byte_size + 1;
401 size_t n;
402
Steven Cooreman69967ce2021-01-18 18:01:08 +0100403 /* Skip the test case if the target running the test cannot
404 * accomodate large keys due to heap size constraints */
405 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200406 memset( buffer, 'K', byte_size );
407
408 PSA_ASSERT( psa_crypto_init( ) );
409
410 /* Try importing the key */
411 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
412 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200413 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100414 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200415 TEST_EQUAL( status, expected_status );
416
417 if( status == PSA_SUCCESS )
418 {
Ronald Cron5425a212020-08-04 14:58:35 +0200419 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200420 TEST_EQUAL( psa_get_key_type( &attributes ), type );
421 TEST_EQUAL( psa_get_key_bits( &attributes ),
422 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200423 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200424 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200425 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200426 for( n = 0; n < byte_size; n++ )
427 TEST_EQUAL( buffer[n], 'K' );
428 for( n = byte_size; n < buffer_size; n++ )
429 TEST_EQUAL( buffer[n], 0 );
430 }
431
432exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100433 /*
434 * Key attributes may have been returned by psa_get_key_attributes()
435 * thus reset them as required.
436 */
437 psa_reset_key_attributes( &attributes );
438
Ronald Cron5425a212020-08-04 14:58:35 +0200439 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200440 PSA_DONE( );
441 mbedtls_free( buffer );
442}
443/* END_CASE */
444
445/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200446void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
447{
Ronald Cron5425a212020-08-04 14:58:35 +0200448 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200449 size_t bits = bits_arg;
450 psa_status_t expected_status = expected_status_arg;
451 psa_status_t status;
452 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200453 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200454 size_t buffer_size = /* Slight overapproximations */
455 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200456 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200457 unsigned char *p;
458 int ret;
459 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200460 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200461
Gilles Peskine8817f612018-12-18 00:18:46 +0100462 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200463 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200464
465 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
466 bits, keypair ) ) >= 0 );
467 length = ret;
468
469 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200470 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200471 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100472 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200473
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200474 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200475 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200476
477exit:
478 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200479 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200480}
481/* END_CASE */
482
483/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300484void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300485 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200486 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100487 int expected_bits,
488 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200489 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100490 int canonical_input )
491{
Ronald Cron5425a212020-08-04 14:58:35 +0200492 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100493 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200494 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200495 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100496 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100497 unsigned char *exported = NULL;
498 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100499 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100500 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100501 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200502 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200503 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100504
Moran Pekercb088e72018-07-17 17:36:59 +0300505 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200506 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100507 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200508 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100509 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100510
Gilles Peskine4747d192019-04-17 15:05:45 +0200511 psa_set_key_usage_flags( &attributes, usage_arg );
512 psa_set_key_algorithm( &attributes, alg );
513 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700514
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100515 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200516 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100517
518 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200519 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200520 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
521 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200522 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100523
524 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200525 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100526 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100527
528 /* The exported length must be set by psa_export_key() to a value between 0
529 * and export_size. On errors, the exported length must be 0. */
530 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
531 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
532 TEST_ASSERT( exported_length <= export_size );
533
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200534 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200535 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100536 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200537 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100538 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100539 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200540 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100541
Gilles Peskineea38a922021-02-13 00:05:16 +0100542 /* Run sanity checks on the exported key. For non-canonical inputs,
543 * this validates the canonical representations. For canonical inputs,
544 * this doesn't directly validate the implementation, but it still helps
545 * by cross-validating the test data with the sanity check code. */
546 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200547 goto exit;
548
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100549 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200550 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100551 else
552 {
Ronald Cron5425a212020-08-04 14:58:35 +0200553 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200554 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200555 &key2 ) );
556 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100557 reexported,
558 export_size,
559 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200560 ASSERT_COMPARE( exported, exported_length,
561 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200562 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100563 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100564 TEST_ASSERT( exported_length <=
565 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
566 psa_get_key_bits( &got_attributes ) ) );
567 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100568
569destroy:
570 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200571 PSA_ASSERT( psa_destroy_key( key ) );
572 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100573
574exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100575 /*
576 * Key attributes may have been returned by psa_get_key_attributes()
577 * thus reset them as required.
578 */
579 psa_reset_key_attributes( &got_attributes );
580
itayzafrir3e02b3b2018-06-12 17:06:52 +0300581 mbedtls_free( exported );
582 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200583 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100584}
585/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100586
Moran Pekerf709f4a2018-06-06 17:26:04 +0300587/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300588void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200589 int type_arg,
590 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100591 int export_size_delta,
592 int expected_export_status_arg,
593 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300594{
Ronald Cron5425a212020-08-04 14:58:35 +0200595 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300596 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200597 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200598 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300599 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300600 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100601 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100602 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200603 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300604
Gilles Peskine8817f612018-12-18 00:18:46 +0100605 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300606
Gilles Peskine4747d192019-04-17 15:05:45 +0200607 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
608 psa_set_key_algorithm( &attributes, alg );
609 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300610
611 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200612 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300613
Gilles Peskine49c25912018-10-29 15:15:31 +0100614 /* Export the public key */
615 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200616 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200617 exported, export_size,
618 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100619 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100620 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100621 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200622 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100623 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200624 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200625 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100626 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100627 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100628 TEST_ASSERT( expected_public_key->len <=
629 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
630 TEST_ASSERT( expected_public_key->len <=
631 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100632 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
633 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100634 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300635
636exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100637 /*
638 * Key attributes may have been returned by psa_get_key_attributes()
639 * thus reset them as required.
640 */
641 psa_reset_key_attributes( &attributes );
642
itayzafrir3e02b3b2018-06-12 17:06:52 +0300643 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200644 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200645 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300646}
647/* END_CASE */
648
Gilles Peskine20035e32018-02-03 22:44:14 +0100649/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200650void import_and_exercise_key( data_t *data,
651 int type_arg,
652 int bits_arg,
653 int alg_arg )
654{
Ronald Cron5425a212020-08-04 14:58:35 +0200655 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200656 psa_key_type_t type = type_arg;
657 size_t bits = bits_arg;
658 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100659 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200660 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200661 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200662
Gilles Peskine8817f612018-12-18 00:18:46 +0100663 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200664
Gilles Peskine4747d192019-04-17 15:05:45 +0200665 psa_set_key_usage_flags( &attributes, usage );
666 psa_set_key_algorithm( &attributes, alg );
667 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200668
669 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200670 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200671
672 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200673 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200674 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
675 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200676
677 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100678 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200679 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200680
Ronald Cron5425a212020-08-04 14:58:35 +0200681 PSA_ASSERT( psa_destroy_key( key ) );
682 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200683
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200684exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100685 /*
686 * Key attributes may have been returned by psa_get_key_attributes()
687 * thus reset them as required.
688 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200689 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100690
691 psa_reset_key_attributes( &attributes );
692 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200693 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200694}
695/* END_CASE */
696
697/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100698void effective_key_attributes( int type_arg, int expected_type_arg,
699 int bits_arg, int expected_bits_arg,
700 int usage_arg, int expected_usage_arg,
701 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200702{
Ronald Cron5425a212020-08-04 14:58:35 +0200703 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100704 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100705 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100706 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100707 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200708 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100709 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200710 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100711 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200712 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200713
Gilles Peskine8817f612018-12-18 00:18:46 +0100714 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200715
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200716 psa_set_key_usage_flags( &attributes, usage );
717 psa_set_key_algorithm( &attributes, alg );
718 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +0100719 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +0200720
Ronald Cron5425a212020-08-04 14:58:35 +0200721 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +0100722 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +0200723
Ronald Cron5425a212020-08-04 14:58:35 +0200724 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +0100725 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
726 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
727 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
728 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200729
730exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100731 /*
732 * Key attributes may have been returned by psa_get_key_attributes()
733 * thus reset them as required.
734 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200735 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100736
737 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200738 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +0200739}
740/* END_CASE */
741
742/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100743void check_key_policy( int type_arg, int bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +0200744 int usage_arg, int alg_arg )
Gilles Peskine06c28892019-11-26 18:07:46 +0100745{
746 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +0200747 usage_arg,
748 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200749 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +0100750 goto exit;
751}
752/* END_CASE */
753
754/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200755void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +0000756{
757 /* Test each valid way of initializing the object, except for `= {0}`, as
758 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
759 * though it's OK by the C standard. We could test for this, but we'd need
760 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200761 psa_key_attributes_t func = psa_key_attributes_init( );
762 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
763 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000764
765 memset( &zero, 0, sizeof( zero ) );
766
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200767 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
768 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
769 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000770
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200771 TEST_EQUAL( psa_get_key_type( &func ), 0 );
772 TEST_EQUAL( psa_get_key_type( &init ), 0 );
773 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
774
775 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
776 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
777 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
778
779 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
780 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
781 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
782
783 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
784 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
785 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +0000786}
787/* END_CASE */
788
789/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200790void mac_key_policy( int policy_usage_arg,
791 int policy_alg_arg,
792 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200793 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200794 int exercise_alg_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100795 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200796{
Ronald Cron5425a212020-08-04 14:58:35 +0200797 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200798 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000799 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200800 psa_key_type_t key_type = key_type_arg;
801 psa_algorithm_t policy_alg = policy_alg_arg;
802 psa_algorithm_t exercise_alg = exercise_alg_arg;
803 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200804 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100805 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200806 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200807
Gilles Peskine8817f612018-12-18 00:18:46 +0100808 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200809
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200810 psa_set_key_usage_flags( &attributes, policy_usage );
811 psa_set_key_algorithm( &attributes, policy_alg );
812 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200813
Gilles Peskine049c7532019-05-15 20:22:09 +0200814 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200815 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200816
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +0200817 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
818 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200819
Ronald Cron5425a212020-08-04 14:58:35 +0200820 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100821 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100822 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100823 else
824 TEST_EQUAL( status, expected_status );
825
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200826 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200827
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200828 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200829 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100830 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100831 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100832 else
833 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200834
835exit:
836 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200837 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200838 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200839}
840/* END_CASE */
841
842/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200843void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200844 int policy_alg,
845 int key_type,
846 data_t *key_data,
847 int exercise_alg )
848{
Ronald Cron5425a212020-08-04 14:58:35 +0200849 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200850 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000851 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200852 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200853 psa_status_t status;
854
Gilles Peskine8817f612018-12-18 00:18:46 +0100855 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200856
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200857 psa_set_key_usage_flags( &attributes, policy_usage );
858 psa_set_key_algorithm( &attributes, policy_alg );
859 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200860
Gilles Peskine049c7532019-05-15 20:22:09 +0200861 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200862 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200863
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200864 /* Check if no key usage flag implication is done */
865 TEST_EQUAL( policy_usage,
866 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200867
Ronald Cron5425a212020-08-04 14:58:35 +0200868 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200869 if( policy_alg == exercise_alg &&
870 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100871 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200872 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100873 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200874 psa_cipher_abort( &operation );
875
Ronald Cron5425a212020-08-04 14:58:35 +0200876 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200877 if( policy_alg == exercise_alg &&
878 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100879 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200880 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100881 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200882
883exit:
884 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200885 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200886 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200887}
888/* END_CASE */
889
890/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200891void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200892 int policy_alg,
893 int key_type,
894 data_t *key_data,
895 int nonce_length_arg,
896 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100897 int exercise_alg,
898 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200899{
Ronald Cron5425a212020-08-04 14:58:35 +0200900 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200901 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200902 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200903 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100904 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200905 unsigned char nonce[16] = {0};
906 size_t nonce_length = nonce_length_arg;
907 unsigned char tag[16];
908 size_t tag_length = tag_length_arg;
909 size_t output_length;
910
911 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
912 TEST_ASSERT( tag_length <= sizeof( tag ) );
913
Gilles Peskine8817f612018-12-18 00:18:46 +0100914 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200915
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200916 psa_set_key_usage_flags( &attributes, policy_usage );
917 psa_set_key_algorithm( &attributes, policy_alg );
918 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200919
Gilles Peskine049c7532019-05-15 20:22:09 +0200920 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200921 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200922
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200923 /* Check if no key usage implication is done */
924 TEST_EQUAL( policy_usage,
925 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200926
Ronald Cron5425a212020-08-04 14:58:35 +0200927 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200928 nonce, nonce_length,
929 NULL, 0,
930 NULL, 0,
931 tag, tag_length,
932 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100933 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
934 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200935 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100936 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200937
938 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200939 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200940 nonce, nonce_length,
941 NULL, 0,
942 tag, tag_length,
943 NULL, 0,
944 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100945 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
946 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
947 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100948 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200949 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100950 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200951
952exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200953 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200954 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200955}
956/* END_CASE */
957
958/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200959void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200960 int policy_alg,
961 int key_type,
962 data_t *key_data,
963 int exercise_alg )
964{
Ronald Cron5425a212020-08-04 14:58:35 +0200965 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200966 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200967 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200968 psa_status_t status;
969 size_t key_bits;
970 size_t buffer_length;
971 unsigned char *buffer = NULL;
972 size_t output_length;
973
Gilles Peskine8817f612018-12-18 00:18:46 +0100974 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200975
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200976 psa_set_key_usage_flags( &attributes, policy_usage );
977 psa_set_key_algorithm( &attributes, policy_alg );
978 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200979
Gilles Peskine049c7532019-05-15 20:22:09 +0200980 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200981 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200982
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200983 /* Check if no key usage implication is done */
984 TEST_EQUAL( policy_usage,
985 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +0200986
Ronald Cron5425a212020-08-04 14:58:35 +0200987 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200988 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200989 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
990 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200991 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200992
Ronald Cron5425a212020-08-04 14:58:35 +0200993 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200994 NULL, 0,
995 NULL, 0,
996 buffer, buffer_length,
997 &output_length );
998 if( policy_alg == exercise_alg &&
999 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001000 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001001 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001002 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001003
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001004 if( buffer_length != 0 )
1005 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001006 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001007 buffer, buffer_length,
1008 NULL, 0,
1009 buffer, buffer_length,
1010 &output_length );
1011 if( policy_alg == exercise_alg &&
1012 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001013 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001014 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001015 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001016
1017exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001018 /*
1019 * Key attributes may have been returned by psa_get_key_attributes()
1020 * thus reset them as required.
1021 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001022 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001023
1024 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001025 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001026 mbedtls_free( buffer );
1027}
1028/* END_CASE */
1029
1030/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001031void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001032 int policy_alg,
1033 int key_type,
1034 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001035 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001036 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001037 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001038{
Ronald Cron5425a212020-08-04 14:58:35 +02001039 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001040 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001041 psa_key_usage_t policy_usage = policy_usage_arg;
1042 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001043 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001044 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1045 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1046 * compatible with the policy and `payload_length_arg` is supposed to be
1047 * a valid input length to sign. If `payload_length_arg <= 0`,
1048 * `exercise_alg` is supposed to be forbidden by the policy. */
1049 int compatible_alg = payload_length_arg > 0;
1050 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001051 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001052 size_t signature_length;
1053
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001054 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001055 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001056 TEST_EQUAL( expected_usage,
1057 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001058
Gilles Peskine8817f612018-12-18 00:18:46 +01001059 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001060
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001061 psa_set_key_usage_flags( &attributes, policy_usage );
1062 psa_set_key_algorithm( &attributes, policy_alg );
1063 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001064
Gilles Peskine049c7532019-05-15 20:22:09 +02001065 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001066 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001067
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001068 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1069
Ronald Cron5425a212020-08-04 14:58:35 +02001070 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001071 payload, payload_length,
1072 signature, sizeof( signature ),
1073 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001074 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001075 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001076 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001077 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001078
1079 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001080 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001081 payload, payload_length,
1082 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001083 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001084 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001085 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001086 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001087
gabor-mezei-armd851d682021-06-28 14:53:49 +02001088 if( PSA_ALG_IS_HASH_AND_SIGN( exercise_alg ) &&
1089 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001090 {
1091 status = psa_sign_message( key, exercise_alg,
1092 payload, payload_length,
1093 signature, sizeof( signature ),
1094 &signature_length );
1095 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1096 PSA_ASSERT( status );
1097 else
1098 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1099
1100 memset( signature, 0, sizeof( signature ) );
1101 status = psa_verify_message( key, exercise_alg,
1102 payload, payload_length,
1103 signature, sizeof( signature ) );
1104 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1105 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1106 else
1107 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1108 }
1109
Gilles Peskined5b33222018-06-18 22:20:03 +02001110exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001111 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001112 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001113}
1114/* END_CASE */
1115
Janos Follathba3fab92019-06-11 14:50:16 +01001116/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001117void derive_key_policy( int policy_usage,
1118 int policy_alg,
1119 int key_type,
1120 data_t *key_data,
1121 int exercise_alg )
1122{
Ronald Cron5425a212020-08-04 14:58:35 +02001123 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001124 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001125 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001126 psa_status_t status;
1127
Gilles Peskine8817f612018-12-18 00:18:46 +01001128 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001129
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001130 psa_set_key_usage_flags( &attributes, policy_usage );
1131 psa_set_key_algorithm( &attributes, policy_alg );
1132 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001133
Gilles Peskine049c7532019-05-15 20:22:09 +02001134 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001135 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001136
Janos Follathba3fab92019-06-11 14:50:16 +01001137 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1138
1139 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1140 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001141 {
Janos Follathba3fab92019-06-11 14:50:16 +01001142 PSA_ASSERT( psa_key_derivation_input_bytes(
1143 &operation,
1144 PSA_KEY_DERIVATION_INPUT_SEED,
1145 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001146 }
Janos Follathba3fab92019-06-11 14:50:16 +01001147
1148 status = psa_key_derivation_input_key( &operation,
1149 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001150 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001151
Gilles Peskineea0fb492018-07-12 17:17:20 +02001152 if( policy_alg == exercise_alg &&
1153 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001154 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001155 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001156 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001157
1158exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001159 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001160 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001161 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001162}
1163/* END_CASE */
1164
1165/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001166void agreement_key_policy( int policy_usage,
1167 int policy_alg,
1168 int key_type_arg,
1169 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001170 int exercise_alg,
1171 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001172{
Ronald Cron5425a212020-08-04 14:58:35 +02001173 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001174 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001175 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001176 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001177 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001178 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001179
Gilles Peskine8817f612018-12-18 00:18:46 +01001180 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001181
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001182 psa_set_key_usage_flags( &attributes, policy_usage );
1183 psa_set_key_algorithm( &attributes, policy_alg );
1184 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001185
Gilles Peskine049c7532019-05-15 20:22:09 +02001186 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001187 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001188
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001189 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001190 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001191
Steven Cooremance48e852020-10-05 16:02:45 +02001192 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001193
1194exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001195 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001196 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001197 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001198}
1199/* END_CASE */
1200
1201/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001202void key_policy_alg2( int key_type_arg, data_t *key_data,
1203 int usage_arg, int alg_arg, int alg2_arg )
1204{
Ronald Cron5425a212020-08-04 14:58:35 +02001205 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001206 psa_key_type_t key_type = key_type_arg;
1207 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1208 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1209 psa_key_usage_t usage = usage_arg;
1210 psa_algorithm_t alg = alg_arg;
1211 psa_algorithm_t alg2 = alg2_arg;
1212
1213 PSA_ASSERT( psa_crypto_init( ) );
1214
1215 psa_set_key_usage_flags( &attributes, usage );
1216 psa_set_key_algorithm( &attributes, alg );
1217 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1218 psa_set_key_type( &attributes, key_type );
1219 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001220 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001221
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001222 /* Update the usage flags to obtain implicit usage flags */
1223 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001224 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001225 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1226 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1227 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1228
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001229 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001230 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001231 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001232 goto exit;
1233
1234exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001235 /*
1236 * Key attributes may have been returned by psa_get_key_attributes()
1237 * thus reset them as required.
1238 */
1239 psa_reset_key_attributes( &got_attributes );
1240
Ronald Cron5425a212020-08-04 14:58:35 +02001241 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001242 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001243}
1244/* END_CASE */
1245
1246/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001247void raw_agreement_key_policy( int policy_usage,
1248 int policy_alg,
1249 int key_type_arg,
1250 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001251 int exercise_alg,
1252 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001253{
Ronald Cron5425a212020-08-04 14:58:35 +02001254 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001255 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001256 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001257 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001258 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001259 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001260
1261 PSA_ASSERT( psa_crypto_init( ) );
1262
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001263 psa_set_key_usage_flags( &attributes, policy_usage );
1264 psa_set_key_algorithm( &attributes, policy_alg );
1265 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001266
Gilles Peskine049c7532019-05-15 20:22:09 +02001267 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001268 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001269
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001270 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001271
Steven Cooremance48e852020-10-05 16:02:45 +02001272 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001273
1274exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001275 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001276 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001277 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001278}
1279/* END_CASE */
1280
1281/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001282void copy_success( int source_usage_arg,
1283 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001284 int type_arg, data_t *material,
1285 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001286 int target_usage_arg,
1287 int target_alg_arg, int target_alg2_arg,
1288 int expected_usage_arg,
1289 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001290{
Gilles Peskineca25db92019-04-19 11:43:08 +02001291 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1292 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001293 psa_key_usage_t expected_usage = expected_usage_arg;
1294 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001295 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001296 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1297 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001298 uint8_t *export_buffer = NULL;
1299
Gilles Peskine57ab7212019-01-28 13:03:09 +01001300 PSA_ASSERT( psa_crypto_init( ) );
1301
Gilles Peskineca25db92019-04-19 11:43:08 +02001302 /* Prepare the source key. */
1303 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1304 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001305 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001306 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001307 PSA_ASSERT( psa_import_key( &source_attributes,
1308 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001309 &source_key ) );
1310 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001311
Gilles Peskineca25db92019-04-19 11:43:08 +02001312 /* Prepare the target attributes. */
1313 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001314 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001315 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001316 /* Set volatile lifetime to reset the key identifier to 0. */
1317 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1318 }
1319
Gilles Peskineca25db92019-04-19 11:43:08 +02001320 if( target_usage_arg != -1 )
1321 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1322 if( target_alg_arg != -1 )
1323 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001324 if( target_alg2_arg != -1 )
1325 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001326
1327 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001328 PSA_ASSERT( psa_copy_key( source_key,
1329 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001330
1331 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001332 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001333
1334 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001335 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001336 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1337 psa_get_key_type( &target_attributes ) );
1338 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1339 psa_get_key_bits( &target_attributes ) );
1340 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1341 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001342 TEST_EQUAL( expected_alg2,
1343 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001344 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1345 {
1346 size_t length;
1347 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001348 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001349 material->len, &length ) );
1350 ASSERT_COMPARE( material->x, material->len,
1351 export_buffer, length );
1352 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001353
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001354 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001355 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001356 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001357 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001358
Ronald Cron5425a212020-08-04 14:58:35 +02001359 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001360
1361exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001362 /*
1363 * Source and target key attributes may have been returned by
1364 * psa_get_key_attributes() thus reset them as required.
1365 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001366 psa_reset_key_attributes( &source_attributes );
1367 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001368
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001369 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001370 mbedtls_free( export_buffer );
1371}
1372/* END_CASE */
1373
1374/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001375void copy_fail( int source_usage_arg,
1376 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001377 int type_arg, data_t *material,
1378 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001379 int target_usage_arg,
1380 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001381 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001382 int expected_status_arg )
1383{
1384 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1385 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001386 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1387 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001388 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001389
1390 PSA_ASSERT( psa_crypto_init( ) );
1391
1392 /* Prepare the source key. */
1393 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1394 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001395 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001396 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001397 PSA_ASSERT( psa_import_key( &source_attributes,
1398 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001399 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001400
1401 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001402 psa_set_key_id( &target_attributes, key_id );
1403 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001404 psa_set_key_type( &target_attributes, target_type_arg );
1405 psa_set_key_bits( &target_attributes, target_bits_arg );
1406 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1407 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001408 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001409
1410 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001411 TEST_EQUAL( psa_copy_key( source_key,
1412 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001413 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001414
Ronald Cron5425a212020-08-04 14:58:35 +02001415 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001416
Gilles Peskine4a644642019-05-03 17:14:08 +02001417exit:
1418 psa_reset_key_attributes( &source_attributes );
1419 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001420 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001421}
1422/* END_CASE */
1423
1424/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001425void hash_operation_init( )
1426{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001427 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001428 /* Test each valid way of initializing the object, except for `= {0}`, as
1429 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1430 * though it's OK by the C standard. We could test for this, but we'd need
1431 * to supress the Clang warning for the test. */
1432 psa_hash_operation_t func = psa_hash_operation_init( );
1433 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1434 psa_hash_operation_t zero;
1435
1436 memset( &zero, 0, sizeof( zero ) );
1437
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001438 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001439 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1440 PSA_ERROR_BAD_STATE );
1441 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1442 PSA_ERROR_BAD_STATE );
1443 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1444 PSA_ERROR_BAD_STATE );
1445
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001446 /* A default hash operation should be abortable without error. */
1447 PSA_ASSERT( psa_hash_abort( &func ) );
1448 PSA_ASSERT( psa_hash_abort( &init ) );
1449 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001450}
1451/* END_CASE */
1452
1453/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001454void hash_setup( int alg_arg,
1455 int expected_status_arg )
1456{
1457 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001458 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001459 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001460 psa_status_t status;
1461
Gilles Peskine8817f612018-12-18 00:18:46 +01001462 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001463
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001464 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001465 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001466
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001467 /* Whether setup succeeded or failed, abort must succeed. */
1468 PSA_ASSERT( psa_hash_abort( &operation ) );
1469
1470 /* If setup failed, reproduce the failure, so as to
1471 * test the resulting state of the operation object. */
1472 if( status != PSA_SUCCESS )
1473 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1474
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001475 /* Now the operation object should be reusable. */
1476#if defined(KNOWN_SUPPORTED_HASH_ALG)
1477 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1478 PSA_ASSERT( psa_hash_abort( &operation ) );
1479#endif
1480
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001481exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001482 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001483}
1484/* END_CASE */
1485
1486/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001487void hash_compute_fail( int alg_arg, data_t *input,
1488 int output_size_arg, int expected_status_arg )
1489{
1490 psa_algorithm_t alg = alg_arg;
1491 uint8_t *output = NULL;
1492 size_t output_size = output_size_arg;
1493 size_t output_length = INVALID_EXPORT_LENGTH;
1494 psa_status_t expected_status = expected_status_arg;
1495 psa_status_t status;
1496
1497 ASSERT_ALLOC( output, output_size );
1498
1499 PSA_ASSERT( psa_crypto_init( ) );
1500
1501 status = psa_hash_compute( alg, input->x, input->len,
1502 output, output_size, &output_length );
1503 TEST_EQUAL( status, expected_status );
1504 TEST_ASSERT( output_length <= output_size );
1505
1506exit:
1507 mbedtls_free( output );
1508 PSA_DONE( );
1509}
1510/* END_CASE */
1511
1512/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001513void hash_compare_fail( int alg_arg, data_t *input,
1514 data_t *reference_hash,
1515 int expected_status_arg )
1516{
1517 psa_algorithm_t alg = alg_arg;
1518 psa_status_t expected_status = expected_status_arg;
1519 psa_status_t status;
1520
1521 PSA_ASSERT( psa_crypto_init( ) );
1522
1523 status = psa_hash_compare( alg, input->x, input->len,
1524 reference_hash->x, reference_hash->len );
1525 TEST_EQUAL( status, expected_status );
1526
1527exit:
1528 PSA_DONE( );
1529}
1530/* END_CASE */
1531
1532/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001533void hash_compute_compare( int alg_arg, data_t *input,
1534 data_t *expected_output )
1535{
1536 psa_algorithm_t alg = alg_arg;
1537 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1538 size_t output_length = INVALID_EXPORT_LENGTH;
1539 size_t i;
1540
1541 PSA_ASSERT( psa_crypto_init( ) );
1542
1543 /* Compute with tight buffer */
1544 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001545 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001546 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001547 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001548 ASSERT_COMPARE( output, output_length,
1549 expected_output->x, expected_output->len );
1550
1551 /* Compute with larger buffer */
1552 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1553 output, sizeof( output ),
1554 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001555 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001556 ASSERT_COMPARE( output, output_length,
1557 expected_output->x, expected_output->len );
1558
1559 /* Compare with correct hash */
1560 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1561 output, output_length ) );
1562
1563 /* Compare with trailing garbage */
1564 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1565 output, output_length + 1 ),
1566 PSA_ERROR_INVALID_SIGNATURE );
1567
1568 /* Compare with truncated hash */
1569 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1570 output, output_length - 1 ),
1571 PSA_ERROR_INVALID_SIGNATURE );
1572
1573 /* Compare with corrupted value */
1574 for( i = 0; i < output_length; i++ )
1575 {
Chris Jones9634bb12021-01-20 15:56:42 +00001576 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001577 output[i] ^= 1;
1578 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1579 output, output_length ),
1580 PSA_ERROR_INVALID_SIGNATURE );
1581 output[i] ^= 1;
1582 }
1583
1584exit:
1585 PSA_DONE( );
1586}
1587/* END_CASE */
1588
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001589/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001590void hash_bad_order( )
1591{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001592 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001593 unsigned char input[] = "";
1594 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001595 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001596 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1597 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1598 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001599 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001600 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001601 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001602
Gilles Peskine8817f612018-12-18 00:18:46 +01001603 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001604
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001605 /* Call setup twice in a row. */
1606 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001607 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001608 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1609 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001610 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001611 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001612 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001613
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001614 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001615 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001616 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001617 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001618
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001619 /* Check that update calls abort on error. */
1620 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01001621 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001622 ASSERT_OPERATION_IS_ACTIVE( operation );
1623 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
1624 PSA_ERROR_BAD_STATE );
1625 ASSERT_OPERATION_IS_INACTIVE( operation );
1626 PSA_ASSERT( psa_hash_abort( &operation ) );
1627 ASSERT_OPERATION_IS_INACTIVE( operation );
1628
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001629 /* Call update after finish. */
1630 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1631 PSA_ASSERT( psa_hash_finish( &operation,
1632 hash, sizeof( hash ), &hash_len ) );
1633 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001634 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001635 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001636
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001637 /* Call verify without calling setup beforehand. */
1638 TEST_EQUAL( psa_hash_verify( &operation,
1639 valid_hash, sizeof( valid_hash ) ),
1640 PSA_ERROR_BAD_STATE );
1641 PSA_ASSERT( psa_hash_abort( &operation ) );
1642
1643 /* Call verify after finish. */
1644 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1645 PSA_ASSERT( psa_hash_finish( &operation,
1646 hash, sizeof( hash ), &hash_len ) );
1647 TEST_EQUAL( psa_hash_verify( &operation,
1648 valid_hash, sizeof( valid_hash ) ),
1649 PSA_ERROR_BAD_STATE );
1650 PSA_ASSERT( psa_hash_abort( &operation ) );
1651
1652 /* Call verify twice in a row. */
1653 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001654 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001655 PSA_ASSERT( psa_hash_verify( &operation,
1656 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001657 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001658 TEST_EQUAL( psa_hash_verify( &operation,
1659 valid_hash, sizeof( valid_hash ) ),
1660 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001661 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001662 PSA_ASSERT( psa_hash_abort( &operation ) );
1663
1664 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001665 TEST_EQUAL( psa_hash_finish( &operation,
1666 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001667 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001668 PSA_ASSERT( psa_hash_abort( &operation ) );
1669
1670 /* Call finish twice in a row. */
1671 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1672 PSA_ASSERT( psa_hash_finish( &operation,
1673 hash, sizeof( hash ), &hash_len ) );
1674 TEST_EQUAL( psa_hash_finish( &operation,
1675 hash, sizeof( hash ), &hash_len ),
1676 PSA_ERROR_BAD_STATE );
1677 PSA_ASSERT( psa_hash_abort( &operation ) );
1678
1679 /* Call finish after calling verify. */
1680 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1681 PSA_ASSERT( psa_hash_verify( &operation,
1682 valid_hash, sizeof( valid_hash ) ) );
1683 TEST_EQUAL( psa_hash_finish( &operation,
1684 hash, sizeof( hash ), &hash_len ),
1685 PSA_ERROR_BAD_STATE );
1686 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001687
1688exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001689 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001690}
1691/* END_CASE */
1692
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001693/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001694void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001695{
1696 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001697 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1698 * appended to it */
1699 unsigned char hash[] = {
1700 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1701 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1702 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001703 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001704 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001705
Gilles Peskine8817f612018-12-18 00:18:46 +01001706 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001707
itayzafrir27e69452018-11-01 14:26:34 +02001708 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001709 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001710 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001711 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001712 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001713 ASSERT_OPERATION_IS_INACTIVE( operation );
1714 PSA_ASSERT( psa_hash_abort( &operation ) );
1715 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03001716
itayzafrir27e69452018-11-01 14:26:34 +02001717 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001718 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001719 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001720 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001721
itayzafrir27e69452018-11-01 14:26:34 +02001722 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001723 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001724 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001725 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001726
itayzafrirec93d302018-10-18 18:01:10 +03001727exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001728 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001729}
1730/* END_CASE */
1731
Ronald Cronee414c72021-03-18 18:50:08 +01001732/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001733void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001734{
1735 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001736 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001737 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001738 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001739 size_t hash_len;
1740
Gilles Peskine8817f612018-12-18 00:18:46 +01001741 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001742
itayzafrir58028322018-10-25 10:22:01 +03001743 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001744 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001745 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001746 hash, expected_size - 1, &hash_len ),
1747 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001748
1749exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001750 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001751}
1752/* END_CASE */
1753
Ronald Cronee414c72021-03-18 18:50:08 +01001754/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001755void hash_clone_source_state( )
1756{
1757 psa_algorithm_t alg = PSA_ALG_SHA_256;
1758 unsigned char hash[PSA_HASH_MAX_SIZE];
1759 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1760 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1761 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1762 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1763 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1764 size_t hash_len;
1765
1766 PSA_ASSERT( psa_crypto_init( ) );
1767 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1768
1769 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1770 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1771 PSA_ASSERT( psa_hash_finish( &op_finished,
1772 hash, sizeof( hash ), &hash_len ) );
1773 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1774 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1775
1776 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1777 PSA_ERROR_BAD_STATE );
1778
1779 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1780 PSA_ASSERT( psa_hash_finish( &op_init,
1781 hash, sizeof( hash ), &hash_len ) );
1782 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1783 PSA_ASSERT( psa_hash_finish( &op_finished,
1784 hash, sizeof( hash ), &hash_len ) );
1785 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1786 PSA_ASSERT( psa_hash_finish( &op_aborted,
1787 hash, sizeof( hash ), &hash_len ) );
1788
1789exit:
1790 psa_hash_abort( &op_source );
1791 psa_hash_abort( &op_init );
1792 psa_hash_abort( &op_setup );
1793 psa_hash_abort( &op_finished );
1794 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001795 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001796}
1797/* END_CASE */
1798
Ronald Cronee414c72021-03-18 18:50:08 +01001799/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001800void hash_clone_target_state( )
1801{
1802 psa_algorithm_t alg = PSA_ALG_SHA_256;
1803 unsigned char hash[PSA_HASH_MAX_SIZE];
1804 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1805 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1806 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1807 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1808 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1809 size_t hash_len;
1810
1811 PSA_ASSERT( psa_crypto_init( ) );
1812
1813 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1814 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1815 PSA_ASSERT( psa_hash_finish( &op_finished,
1816 hash, sizeof( hash ), &hash_len ) );
1817 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1818 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1819
1820 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1821 PSA_ASSERT( psa_hash_finish( &op_target,
1822 hash, sizeof( hash ), &hash_len ) );
1823
1824 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1825 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1826 PSA_ERROR_BAD_STATE );
1827 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1828 PSA_ERROR_BAD_STATE );
1829
1830exit:
1831 psa_hash_abort( &op_target );
1832 psa_hash_abort( &op_init );
1833 psa_hash_abort( &op_setup );
1834 psa_hash_abort( &op_finished );
1835 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001836 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001837}
1838/* END_CASE */
1839
itayzafrir58028322018-10-25 10:22:01 +03001840/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001841void mac_operation_init( )
1842{
Jaeden Amero252ef282019-02-15 14:05:35 +00001843 const uint8_t input[1] = { 0 };
1844
Jaeden Amero769ce272019-01-04 11:48:03 +00001845 /* Test each valid way of initializing the object, except for `= {0}`, as
1846 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1847 * though it's OK by the C standard. We could test for this, but we'd need
1848 * to supress the Clang warning for the test. */
1849 psa_mac_operation_t func = psa_mac_operation_init( );
1850 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1851 psa_mac_operation_t zero;
1852
1853 memset( &zero, 0, sizeof( zero ) );
1854
Jaeden Amero252ef282019-02-15 14:05:35 +00001855 /* A freshly-initialized MAC operation should not be usable. */
1856 TEST_EQUAL( psa_mac_update( &func,
1857 input, sizeof( input ) ),
1858 PSA_ERROR_BAD_STATE );
1859 TEST_EQUAL( psa_mac_update( &init,
1860 input, sizeof( input ) ),
1861 PSA_ERROR_BAD_STATE );
1862 TEST_EQUAL( psa_mac_update( &zero,
1863 input, sizeof( input ) ),
1864 PSA_ERROR_BAD_STATE );
1865
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001866 /* A default MAC operation should be abortable without error. */
1867 PSA_ASSERT( psa_mac_abort( &func ) );
1868 PSA_ASSERT( psa_mac_abort( &init ) );
1869 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001870}
1871/* END_CASE */
1872
1873/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001874void mac_setup( int key_type_arg,
1875 data_t *key,
1876 int alg_arg,
1877 int expected_status_arg )
1878{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001879 psa_key_type_t key_type = key_type_arg;
1880 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001881 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001882 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001883 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1884#if defined(KNOWN_SUPPORTED_MAC_ALG)
1885 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1886#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001887
Gilles Peskine8817f612018-12-18 00:18:46 +01001888 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001889
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001890 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1891 &operation, &status ) )
1892 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001893 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001894
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001895 /* The operation object should be reusable. */
1896#if defined(KNOWN_SUPPORTED_MAC_ALG)
1897 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1898 smoke_test_key_data,
1899 sizeof( smoke_test_key_data ),
1900 KNOWN_SUPPORTED_MAC_ALG,
1901 &operation, &status ) )
1902 goto exit;
1903 TEST_EQUAL( status, PSA_SUCCESS );
1904#endif
1905
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001906exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001907 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001908}
1909/* END_CASE */
1910
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001911/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_HMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256 */
Jaeden Amero252ef282019-02-15 14:05:35 +00001912void mac_bad_order( )
1913{
Ronald Cron5425a212020-08-04 14:58:35 +02001914 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001915 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1916 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001917 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001918 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1919 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1920 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001921 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001922 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1923 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1924 size_t sign_mac_length = 0;
1925 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1926 const uint8_t verify_mac[] = {
1927 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1928 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1929 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1930
1931 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001932 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001933 psa_set_key_algorithm( &attributes, alg );
1934 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001935
Ronald Cron5425a212020-08-04 14:58:35 +02001936 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1937 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001938
Jaeden Amero252ef282019-02-15 14:05:35 +00001939 /* Call update without calling setup beforehand. */
1940 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1941 PSA_ERROR_BAD_STATE );
1942 PSA_ASSERT( psa_mac_abort( &operation ) );
1943
1944 /* Call sign finish without calling setup beforehand. */
1945 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1946 &sign_mac_length),
1947 PSA_ERROR_BAD_STATE );
1948 PSA_ASSERT( psa_mac_abort( &operation ) );
1949
1950 /* Call verify finish without calling setup beforehand. */
1951 TEST_EQUAL( psa_mac_verify_finish( &operation,
1952 verify_mac, sizeof( verify_mac ) ),
1953 PSA_ERROR_BAD_STATE );
1954 PSA_ASSERT( psa_mac_abort( &operation ) );
1955
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001956 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001957 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001958 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001959 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001960 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001961 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001962 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001963 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001964
Jaeden Amero252ef282019-02-15 14:05:35 +00001965 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001966 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001967 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1968 PSA_ASSERT( psa_mac_sign_finish( &operation,
1969 sign_mac, sizeof( sign_mac ),
1970 &sign_mac_length ) );
1971 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1972 PSA_ERROR_BAD_STATE );
1973 PSA_ASSERT( psa_mac_abort( &operation ) );
1974
1975 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001976 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001977 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1978 PSA_ASSERT( psa_mac_verify_finish( &operation,
1979 verify_mac, sizeof( verify_mac ) ) );
1980 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1981 PSA_ERROR_BAD_STATE );
1982 PSA_ASSERT( psa_mac_abort( &operation ) );
1983
1984 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001985 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001986 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1987 PSA_ASSERT( psa_mac_sign_finish( &operation,
1988 sign_mac, sizeof( sign_mac ),
1989 &sign_mac_length ) );
1990 TEST_EQUAL( psa_mac_sign_finish( &operation,
1991 sign_mac, sizeof( sign_mac ),
1992 &sign_mac_length ),
1993 PSA_ERROR_BAD_STATE );
1994 PSA_ASSERT( psa_mac_abort( &operation ) );
1995
1996 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001997 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001998 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1999 PSA_ASSERT( psa_mac_verify_finish( &operation,
2000 verify_mac, sizeof( verify_mac ) ) );
2001 TEST_EQUAL( psa_mac_verify_finish( &operation,
2002 verify_mac, sizeof( verify_mac ) ),
2003 PSA_ERROR_BAD_STATE );
2004 PSA_ASSERT( psa_mac_abort( &operation ) );
2005
2006 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002007 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002008 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002009 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002010 TEST_EQUAL( psa_mac_verify_finish( &operation,
2011 verify_mac, sizeof( verify_mac ) ),
2012 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002013 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002014 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002015 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002016
2017 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002018 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002019 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002020 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002021 TEST_EQUAL( psa_mac_sign_finish( &operation,
2022 sign_mac, sizeof( sign_mac ),
2023 &sign_mac_length ),
2024 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002025 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002026 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002027 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002028
Ronald Cron5425a212020-08-04 14:58:35 +02002029 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002030
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002031exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002032 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002033}
2034/* END_CASE */
2035
2036/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002037void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002038 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002039 int alg_arg,
2040 data_t *input,
2041 data_t *expected_mac )
2042{
Ronald Cron5425a212020-08-04 14:58:35 +02002043 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002044 psa_key_type_t key_type = key_type_arg;
2045 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002046 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002047 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002048 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002049 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002050 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002051 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002052 const size_t output_sizes_to_test[] = {
2053 0,
2054 1,
2055 expected_mac->len - 1,
2056 expected_mac->len,
2057 expected_mac->len + 1,
2058 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002059
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002060 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002061 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002062 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002063
Gilles Peskine8817f612018-12-18 00:18:46 +01002064 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002065
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002066 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002067 psa_set_key_algorithm( &attributes, alg );
2068 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002069
Ronald Cron5425a212020-08-04 14:58:35 +02002070 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2071 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002072
Gilles Peskine8b356b52020-08-25 23:44:59 +02002073 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2074 {
2075 const size_t output_size = output_sizes_to_test[i];
2076 psa_status_t expected_status =
2077 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2078 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002079
Chris Jones9634bb12021-01-20 15:56:42 +00002080 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002081 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002082
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002083 /* Calculate the MAC, one-shot case. */
2084 TEST_EQUAL( psa_mac_compute( key, alg,
2085 input->x, input->len,
2086 actual_mac, output_size, &mac_length ),
2087 expected_status );
2088 if( expected_status == PSA_SUCCESS )
2089 {
2090 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2091 actual_mac, mac_length );
2092 }
2093
2094 if( output_size > 0 )
2095 memset( actual_mac, 0, output_size );
2096
2097 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002098 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002099 PSA_ASSERT( psa_mac_update( &operation,
2100 input->x, input->len ) );
2101 TEST_EQUAL( psa_mac_sign_finish( &operation,
2102 actual_mac, output_size,
2103 &mac_length ),
2104 expected_status );
2105 PSA_ASSERT( psa_mac_abort( &operation ) );
2106
2107 if( expected_status == PSA_SUCCESS )
2108 {
2109 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2110 actual_mac, mac_length );
2111 }
2112 mbedtls_free( actual_mac );
2113 actual_mac = NULL;
2114 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002115
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002116exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002117 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002118 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002119 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002120 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002121}
2122/* END_CASE */
2123
2124/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002125void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002126 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002127 int alg_arg,
2128 data_t *input,
2129 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002130{
Ronald Cron5425a212020-08-04 14:58:35 +02002131 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002132 psa_key_type_t key_type = key_type_arg;
2133 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002134 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002135 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002136 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002137
Gilles Peskine69c12672018-06-28 00:07:19 +02002138 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2139
Gilles Peskine8817f612018-12-18 00:18:46 +01002140 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002141
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002142 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002143 psa_set_key_algorithm( &attributes, alg );
2144 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002145
Ronald Cron5425a212020-08-04 14:58:35 +02002146 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2147 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002148
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002149 /* Verify correct MAC, one-shot case. */
2150 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2151 expected_mac->x, expected_mac->len ) );
2152
2153 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002154 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002155 PSA_ASSERT( psa_mac_update( &operation,
2156 input->x, input->len ) );
2157 PSA_ASSERT( psa_mac_verify_finish( &operation,
2158 expected_mac->x,
2159 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002160
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002161 /* Test a MAC that's too short, one-shot case. */
2162 TEST_EQUAL( psa_mac_verify( key, alg,
2163 input->x, input->len,
2164 expected_mac->x,
2165 expected_mac->len - 1 ),
2166 PSA_ERROR_INVALID_SIGNATURE );
2167
2168 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002169 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002170 PSA_ASSERT( psa_mac_update( &operation,
2171 input->x, input->len ) );
2172 TEST_EQUAL( psa_mac_verify_finish( &operation,
2173 expected_mac->x,
2174 expected_mac->len - 1 ),
2175 PSA_ERROR_INVALID_SIGNATURE );
2176
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002177 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002178 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2179 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002180 TEST_EQUAL( psa_mac_verify( key, alg,
2181 input->x, input->len,
2182 perturbed_mac, expected_mac->len + 1 ),
2183 PSA_ERROR_INVALID_SIGNATURE );
2184
2185 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002186 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002187 PSA_ASSERT( psa_mac_update( &operation,
2188 input->x, input->len ) );
2189 TEST_EQUAL( psa_mac_verify_finish( &operation,
2190 perturbed_mac,
2191 expected_mac->len + 1 ),
2192 PSA_ERROR_INVALID_SIGNATURE );
2193
2194 /* Test changing one byte. */
2195 for( size_t i = 0; i < expected_mac->len; i++ )
2196 {
Chris Jones9634bb12021-01-20 15:56:42 +00002197 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002198 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002199
2200 TEST_EQUAL( psa_mac_verify( key, alg,
2201 input->x, input->len,
2202 perturbed_mac, expected_mac->len ),
2203 PSA_ERROR_INVALID_SIGNATURE );
2204
Ronald Cron5425a212020-08-04 14:58:35 +02002205 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002206 PSA_ASSERT( psa_mac_update( &operation,
2207 input->x, input->len ) );
2208 TEST_EQUAL( psa_mac_verify_finish( &operation,
2209 perturbed_mac,
2210 expected_mac->len ),
2211 PSA_ERROR_INVALID_SIGNATURE );
2212 perturbed_mac[i] ^= 1;
2213 }
2214
Gilles Peskine8c9def32018-02-08 10:02:12 +01002215exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002216 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002217 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002218 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002219 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002220}
2221/* END_CASE */
2222
2223/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002224void cipher_operation_init( )
2225{
Jaeden Ameroab439972019-02-15 14:12:05 +00002226 const uint8_t input[1] = { 0 };
2227 unsigned char output[1] = { 0 };
2228 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002229 /* Test each valid way of initializing the object, except for `= {0}`, as
2230 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2231 * though it's OK by the C standard. We could test for this, but we'd need
2232 * to supress the Clang warning for the test. */
2233 psa_cipher_operation_t func = psa_cipher_operation_init( );
2234 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2235 psa_cipher_operation_t zero;
2236
2237 memset( &zero, 0, sizeof( zero ) );
2238
Jaeden Ameroab439972019-02-15 14:12:05 +00002239 /* A freshly-initialized cipher operation should not be usable. */
2240 TEST_EQUAL( psa_cipher_update( &func,
2241 input, sizeof( input ),
2242 output, sizeof( output ),
2243 &output_length ),
2244 PSA_ERROR_BAD_STATE );
2245 TEST_EQUAL( psa_cipher_update( &init,
2246 input, sizeof( input ),
2247 output, sizeof( output ),
2248 &output_length ),
2249 PSA_ERROR_BAD_STATE );
2250 TEST_EQUAL( psa_cipher_update( &zero,
2251 input, sizeof( input ),
2252 output, sizeof( output ),
2253 &output_length ),
2254 PSA_ERROR_BAD_STATE );
2255
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002256 /* A default cipher operation should be abortable without error. */
2257 PSA_ASSERT( psa_cipher_abort( &func ) );
2258 PSA_ASSERT( psa_cipher_abort( &init ) );
2259 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002260}
2261/* END_CASE */
2262
2263/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002264void cipher_setup( int key_type_arg,
2265 data_t *key,
2266 int alg_arg,
2267 int expected_status_arg )
2268{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002269 psa_key_type_t key_type = key_type_arg;
2270 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002271 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002272 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002273 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002274#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002275 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2276#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002277
Gilles Peskine8817f612018-12-18 00:18:46 +01002278 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002279
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002280 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2281 &operation, &status ) )
2282 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002283 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002284
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002285 /* The operation object should be reusable. */
2286#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2287 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2288 smoke_test_key_data,
2289 sizeof( smoke_test_key_data ),
2290 KNOWN_SUPPORTED_CIPHER_ALG,
2291 &operation, &status ) )
2292 goto exit;
2293 TEST_EQUAL( status, PSA_SUCCESS );
2294#endif
2295
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002296exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002297 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002298 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002299}
2300/* END_CASE */
2301
Ronald Cronee414c72021-03-18 18:50:08 +01002302/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002303void cipher_bad_order( )
2304{
Ronald Cron5425a212020-08-04 14:58:35 +02002305 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002306 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2307 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002308 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002309 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002310 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002311 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002312 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2313 0xaa, 0xaa, 0xaa, 0xaa };
2314 const uint8_t text[] = {
2315 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2316 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002317 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002318 size_t length = 0;
2319
2320 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002321 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2322 psa_set_key_algorithm( &attributes, alg );
2323 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002324 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2325 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002326
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002327 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002328 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002329 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002330 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002331 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002332 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002333 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002334 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002335
2336 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002337 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002338 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002339 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002340 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002341 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002342 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002343 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002344
Jaeden Ameroab439972019-02-15 14:12:05 +00002345 /* Generate an IV without calling setup beforehand. */
2346 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2347 buffer, sizeof( buffer ),
2348 &length ),
2349 PSA_ERROR_BAD_STATE );
2350 PSA_ASSERT( psa_cipher_abort( &operation ) );
2351
2352 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002353 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002354 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2355 buffer, sizeof( buffer ),
2356 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002357 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002358 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2359 buffer, sizeof( buffer ),
2360 &length ),
2361 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002362 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002363 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002364 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002365
2366 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002367 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002368 PSA_ASSERT( psa_cipher_set_iv( &operation,
2369 iv, sizeof( iv ) ) );
2370 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2371 buffer, sizeof( buffer ),
2372 &length ),
2373 PSA_ERROR_BAD_STATE );
2374 PSA_ASSERT( psa_cipher_abort( &operation ) );
2375
2376 /* Set an IV without calling setup beforehand. */
2377 TEST_EQUAL( psa_cipher_set_iv( &operation,
2378 iv, sizeof( iv ) ),
2379 PSA_ERROR_BAD_STATE );
2380 PSA_ASSERT( psa_cipher_abort( &operation ) );
2381
2382 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002383 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002384 PSA_ASSERT( psa_cipher_set_iv( &operation,
2385 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002386 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002387 TEST_EQUAL( psa_cipher_set_iv( &operation,
2388 iv, sizeof( iv ) ),
2389 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002390 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002391 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002392 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002393
2394 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002395 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002396 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2397 buffer, sizeof( buffer ),
2398 &length ) );
2399 TEST_EQUAL( psa_cipher_set_iv( &operation,
2400 iv, sizeof( iv ) ),
2401 PSA_ERROR_BAD_STATE );
2402 PSA_ASSERT( psa_cipher_abort( &operation ) );
2403
2404 /* Call update without calling setup beforehand. */
2405 TEST_EQUAL( psa_cipher_update( &operation,
2406 text, sizeof( text ),
2407 buffer, sizeof( buffer ),
2408 &length ),
2409 PSA_ERROR_BAD_STATE );
2410 PSA_ASSERT( psa_cipher_abort( &operation ) );
2411
2412 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01002413 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002414 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002415 TEST_EQUAL( psa_cipher_update( &operation,
2416 text, sizeof( text ),
2417 buffer, sizeof( buffer ),
2418 &length ),
2419 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002420 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002421 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002422 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002423
2424 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002425 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002426 PSA_ASSERT( psa_cipher_set_iv( &operation,
2427 iv, sizeof( iv ) ) );
2428 PSA_ASSERT( psa_cipher_finish( &operation,
2429 buffer, sizeof( buffer ), &length ) );
2430 TEST_EQUAL( psa_cipher_update( &operation,
2431 text, sizeof( text ),
2432 buffer, sizeof( buffer ),
2433 &length ),
2434 PSA_ERROR_BAD_STATE );
2435 PSA_ASSERT( psa_cipher_abort( &operation ) );
2436
2437 /* Call finish without calling setup beforehand. */
2438 TEST_EQUAL( psa_cipher_finish( &operation,
2439 buffer, sizeof( buffer ), &length ),
2440 PSA_ERROR_BAD_STATE );
2441 PSA_ASSERT( psa_cipher_abort( &operation ) );
2442
2443 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002444 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002445 /* Not calling update means we are encrypting an empty buffer, which is OK
2446 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01002447 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002448 TEST_EQUAL( psa_cipher_finish( &operation,
2449 buffer, sizeof( buffer ), &length ),
2450 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002451 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002452 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002453 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002454
2455 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002456 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002457 PSA_ASSERT( psa_cipher_set_iv( &operation,
2458 iv, sizeof( iv ) ) );
2459 PSA_ASSERT( psa_cipher_finish( &operation,
2460 buffer, sizeof( buffer ), &length ) );
2461 TEST_EQUAL( psa_cipher_finish( &operation,
2462 buffer, sizeof( buffer ), &length ),
2463 PSA_ERROR_BAD_STATE );
2464 PSA_ASSERT( psa_cipher_abort( &operation ) );
2465
Ronald Cron5425a212020-08-04 14:58:35 +02002466 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002467
Jaeden Ameroab439972019-02-15 14:12:05 +00002468exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002469 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002470 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002471}
2472/* END_CASE */
2473
2474/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02002475void cipher_encrypt_fail( int alg_arg,
2476 int key_type_arg,
2477 data_t *key_data,
2478 data_t *input,
2479 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002480{
Ronald Cron5425a212020-08-04 14:58:35 +02002481 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002482 psa_status_t status;
2483 psa_key_type_t key_type = key_type_arg;
2484 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002485 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002486 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002487 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002488 size_t output_length = 0;
2489 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2490
2491 if ( PSA_ERROR_BAD_STATE != expected_status )
2492 {
2493 PSA_ASSERT( psa_crypto_init( ) );
2494
2495 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2496 psa_set_key_algorithm( &attributes, alg );
2497 psa_set_key_type( &attributes, key_type );
2498
2499 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
2500 input->len );
2501 ASSERT_ALLOC( output, output_buffer_size );
2502
2503 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2504 &key ) );
2505 }
2506
2507 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
2508 output_buffer_size, &output_length );
2509
2510 TEST_EQUAL( status, expected_status );
2511
2512exit:
2513 mbedtls_free( output );
2514 psa_destroy_key( key );
2515 PSA_DONE( );
2516}
2517/* END_CASE */
2518
2519/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02002520void cipher_encrypt_alg_without_iv( int alg_arg,
2521 int key_type_arg,
2522 data_t *key_data,
2523 data_t *input,
2524 data_t *expected_output )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002525{
2526 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2527 psa_key_type_t key_type = key_type_arg;
2528 psa_algorithm_t alg = alg_arg;
2529 unsigned char *output = NULL;
2530 size_t output_buffer_size = 0;
2531 size_t output_length = 0;
2532 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2533
2534 PSA_ASSERT( psa_crypto_init( ) );
2535
2536 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2537 psa_set_key_algorithm( &attributes, alg );
2538 psa_set_key_type( &attributes, key_type );
2539
2540 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2541 ASSERT_ALLOC( output, output_buffer_size );
2542
2543 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2544 &key ) );
2545
2546 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
2547 output_buffer_size, &output_length ) );
2548 TEST_ASSERT( output_length <=
2549 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2550 TEST_ASSERT( output_length <=
2551 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
2552
2553 ASSERT_COMPARE( expected_output->x, expected_output->len,
2554 output, output_length );
2555exit:
2556 mbedtls_free( output );
2557 psa_destroy_key( key );
2558 PSA_DONE( );
2559}
2560/* END_CASE */
2561
2562/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01002563void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
2564{
2565 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2566 psa_algorithm_t alg = alg_arg;
2567 psa_key_type_t key_type = key_type_arg;
2568 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2569 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2570 psa_status_t status;
2571
2572 PSA_ASSERT( psa_crypto_init( ) );
2573
2574 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2575 psa_set_key_algorithm( &attributes, alg );
2576 psa_set_key_type( &attributes, key_type );
2577
2578 /* Usage of either of these two size macros would cause divide by zero
2579 * with incorrect key types previously. Input length should be irrelevant
2580 * here. */
2581 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
2582 0 );
2583 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
2584
2585
2586 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2587 &key ) );
2588
2589 /* Should fail due to invalid alg type (to support invalid key type).
2590 * Encrypt or decrypt will end up in the same place. */
2591 status = psa_cipher_encrypt_setup( &operation, key, alg );
2592
2593 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
2594
2595exit:
2596 psa_cipher_abort( &operation );
2597 psa_destroy_key( key );
2598 PSA_DONE( );
2599}
2600/* END_CASE */
2601
2602/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002603void cipher_encrypt_validation( int alg_arg,
2604 int key_type_arg,
2605 data_t *key_data,
2606 data_t *input )
2607{
2608 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2609 psa_key_type_t key_type = key_type_arg;
2610 psa_algorithm_t alg = alg_arg;
2611 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
2612 unsigned char *output1 = NULL;
2613 size_t output1_buffer_size = 0;
2614 size_t output1_length = 0;
2615 unsigned char *output2 = NULL;
2616 size_t output2_buffer_size = 0;
2617 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002618 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002619 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002620 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002621
Gilles Peskine8817f612018-12-18 00:18:46 +01002622 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002623
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002624 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2625 psa_set_key_algorithm( &attributes, alg );
2626 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002627
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002628 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2629 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2630 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
2631 ASSERT_ALLOC( output1, output1_buffer_size );
2632 ASSERT_ALLOC( output2, output2_buffer_size );
2633
Ronald Cron5425a212020-08-04 14:58:35 +02002634 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2635 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002636
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02002637 /* The one-shot cipher encryption uses generated iv so validating
2638 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002639 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
2640 output1_buffer_size, &output1_length ) );
2641 TEST_ASSERT( output1_length <=
2642 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2643 TEST_ASSERT( output1_length <=
gabor-mezei-armceface22021-01-21 12:26:17 +01002644 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002645
2646 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2647 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002648
Gilles Peskine8817f612018-12-18 00:18:46 +01002649 PSA_ASSERT( psa_cipher_update( &operation,
2650 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002651 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01002652 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002653 TEST_ASSERT( function_output_length <=
2654 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2655 TEST_ASSERT( function_output_length <=
2656 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002657 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002658
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002659 PSA_ASSERT( psa_cipher_finish( &operation,
2660 output2 + output2_length,
2661 output2_buffer_size - output2_length,
2662 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002663 TEST_ASSERT( function_output_length <=
2664 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2665 TEST_ASSERT( function_output_length <=
2666 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002667 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002668
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002669 PSA_ASSERT( psa_cipher_abort( &operation ) );
2670 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
2671 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002672
Gilles Peskine50e586b2018-06-08 14:28:46 +02002673exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002674 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002675 mbedtls_free( output1 );
2676 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002677 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002678 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002679}
2680/* END_CASE */
2681
2682/* BEGIN_CASE */
2683void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002684 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002685 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002686 int first_part_size_arg,
2687 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002688 data_t *expected_output,
2689 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002690{
Ronald Cron5425a212020-08-04 14:58:35 +02002691 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002692 psa_key_type_t key_type = key_type_arg;
2693 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002694 psa_status_t status;
2695 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002696 size_t first_part_size = first_part_size_arg;
2697 size_t output1_length = output1_length_arg;
2698 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002699 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002700 size_t output_buffer_size = 0;
2701 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002702 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002703 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002704 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002705
Gilles Peskine8817f612018-12-18 00:18:46 +01002706 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002707
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002708 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2709 psa_set_key_algorithm( &attributes, alg );
2710 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002711
Ronald Cron5425a212020-08-04 14:58:35 +02002712 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2713 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002714
Ronald Cron5425a212020-08-04 14:58:35 +02002715 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002716
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002717 if( iv->len > 0 )
2718 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002719 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002720 }
2721
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002722 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2723 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002724 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002725
Gilles Peskinee0866522019-02-19 19:44:00 +01002726 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002727 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2728 output, output_buffer_size,
2729 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002730 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002731 TEST_ASSERT( function_output_length <=
2732 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2733 TEST_ASSERT( function_output_length <=
2734 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002735 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002736
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002737 if( first_part_size < input->len )
2738 {
2739 PSA_ASSERT( psa_cipher_update( &operation,
2740 input->x + first_part_size,
2741 input->len - first_part_size,
2742 ( output_buffer_size == 0 ? NULL :
2743 output + total_output_length ),
2744 output_buffer_size - total_output_length,
2745 &function_output_length ) );
2746 TEST_ASSERT( function_output_length == output2_length );
2747 TEST_ASSERT( function_output_length <=
2748 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2749 alg,
2750 input->len - first_part_size ) );
2751 TEST_ASSERT( function_output_length <=
2752 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2753 total_output_length += function_output_length;
2754 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002755
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002756 status = psa_cipher_finish( &operation,
2757 ( output_buffer_size == 0 ? NULL :
2758 output + total_output_length ),
2759 output_buffer_size - total_output_length,
2760 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002761 TEST_ASSERT( function_output_length <=
2762 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2763 TEST_ASSERT( function_output_length <=
2764 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002765 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002766 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002767
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002768 if( expected_status == PSA_SUCCESS )
2769 {
2770 PSA_ASSERT( psa_cipher_abort( &operation ) );
2771
2772 ASSERT_COMPARE( expected_output->x, expected_output->len,
2773 output, total_output_length );
2774 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002775
2776exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002777 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002778 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002779 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002780 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002781}
2782/* END_CASE */
2783
2784/* BEGIN_CASE */
2785void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002786 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002787 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002788 int first_part_size_arg,
2789 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002790 data_t *expected_output,
2791 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002792{
Ronald Cron5425a212020-08-04 14:58:35 +02002793 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002794 psa_key_type_t key_type = key_type_arg;
2795 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002796 psa_status_t status;
2797 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002798 size_t first_part_size = first_part_size_arg;
2799 size_t output1_length = output1_length_arg;
2800 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002801 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002802 size_t output_buffer_size = 0;
2803 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002804 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002805 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002806 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002807
Gilles Peskine8817f612018-12-18 00:18:46 +01002808 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002809
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002810 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2811 psa_set_key_algorithm( &attributes, alg );
2812 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002813
Ronald Cron5425a212020-08-04 14:58:35 +02002814 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2815 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002816
Ronald Cron5425a212020-08-04 14:58:35 +02002817 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002818
Steven Cooreman177deba2020-09-07 17:14:14 +02002819 if( iv->len > 0 )
2820 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002821 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002822 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002823
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002824 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2825 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002826 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002827
Gilles Peskinee0866522019-02-19 19:44:00 +01002828 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002829 PSA_ASSERT( psa_cipher_update( &operation,
2830 input->x, first_part_size,
2831 output, output_buffer_size,
2832 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002833 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002834 TEST_ASSERT( function_output_length <=
2835 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2836 TEST_ASSERT( function_output_length <=
2837 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002838 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002839
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002840 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02002841 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002842 PSA_ASSERT( psa_cipher_update( &operation,
2843 input->x + first_part_size,
2844 input->len - first_part_size,
2845 ( output_buffer_size == 0 ? NULL :
2846 output + total_output_length ),
2847 output_buffer_size - total_output_length,
2848 &function_output_length ) );
2849 TEST_ASSERT( function_output_length == output2_length );
2850 TEST_ASSERT( function_output_length <=
2851 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2852 alg,
2853 input->len - first_part_size ) );
2854 TEST_ASSERT( function_output_length <=
2855 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2856 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002857 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002858
Gilles Peskine50e586b2018-06-08 14:28:46 +02002859 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002860 ( output_buffer_size == 0 ? NULL :
2861 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002862 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002863 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002864 TEST_ASSERT( function_output_length <=
2865 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2866 TEST_ASSERT( function_output_length <=
2867 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002868 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002869 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002870
2871 if( expected_status == PSA_SUCCESS )
2872 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002873 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02002874
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002875 ASSERT_COMPARE( expected_output->x, expected_output->len,
2876 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002877 }
2878
Gilles Peskine50e586b2018-06-08 14:28:46 +02002879exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002880 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002881 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002882 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002883 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002884}
2885/* END_CASE */
2886
Gilles Peskine50e586b2018-06-08 14:28:46 +02002887/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02002888void cipher_decrypt_fail( int alg_arg,
2889 int key_type_arg,
2890 data_t *key_data,
2891 data_t *iv,
2892 data_t *input_arg,
2893 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002894{
2895 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2896 psa_status_t status;
2897 psa_key_type_t key_type = key_type_arg;
2898 psa_algorithm_t alg = alg_arg;
2899 psa_status_t expected_status = expected_status_arg;
2900 unsigned char *input = NULL;
2901 size_t input_buffer_size = 0;
2902 unsigned char *output = NULL;
2903 size_t output_buffer_size = 0;
2904 size_t output_length = 0;
2905 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2906
2907 if ( PSA_ERROR_BAD_STATE != expected_status )
2908 {
2909 PSA_ASSERT( psa_crypto_init( ) );
2910
2911 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2912 psa_set_key_algorithm( &attributes, alg );
2913 psa_set_key_type( &attributes, key_type );
2914
2915 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2916 &key ) );
2917 }
2918
2919 /* Allocate input buffer and copy the iv and the plaintext */
2920 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
2921 if ( input_buffer_size > 0 )
2922 {
2923 ASSERT_ALLOC( input, input_buffer_size );
2924 memcpy( input, iv->x, iv->len );
2925 memcpy( input + iv->len, input_arg->x, input_arg->len );
2926 }
2927
2928 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
2929 ASSERT_ALLOC( output, output_buffer_size );
2930
2931 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
2932 output_buffer_size, &output_length );
2933 TEST_EQUAL( status, expected_status );
2934
2935exit:
2936 mbedtls_free( input );
2937 mbedtls_free( output );
2938 psa_destroy_key( key );
2939 PSA_DONE( );
2940}
2941/* END_CASE */
2942
2943/* BEGIN_CASE */
2944void cipher_decrypt( int alg_arg,
2945 int key_type_arg,
2946 data_t *key_data,
2947 data_t *iv,
2948 data_t *input_arg,
2949 data_t *expected_output )
2950{
2951 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2952 psa_key_type_t key_type = key_type_arg;
2953 psa_algorithm_t alg = alg_arg;
2954 unsigned char *input = NULL;
2955 size_t input_buffer_size = 0;
2956 unsigned char *output = NULL;
2957 size_t output_buffer_size = 0;
2958 size_t output_length = 0;
2959 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2960
2961 PSA_ASSERT( psa_crypto_init( ) );
2962
2963 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2964 psa_set_key_algorithm( &attributes, alg );
2965 psa_set_key_type( &attributes, key_type );
2966
2967 /* Allocate input buffer and copy the iv and the plaintext */
2968 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
2969 if ( input_buffer_size > 0 )
2970 {
2971 ASSERT_ALLOC( input, input_buffer_size );
2972 memcpy( input, iv->x, iv->len );
2973 memcpy( input + iv->len, input_arg->x, input_arg->len );
2974 }
2975
2976 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
2977 ASSERT_ALLOC( output, output_buffer_size );
2978
2979 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2980 &key ) );
2981
2982 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
2983 output_buffer_size, &output_length ) );
2984 TEST_ASSERT( output_length <=
2985 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
2986 TEST_ASSERT( output_length <=
2987 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
2988
2989 ASSERT_COMPARE( expected_output->x, expected_output->len,
2990 output, output_length );
2991exit:
2992 mbedtls_free( input );
2993 mbedtls_free( output );
2994 psa_destroy_key( key );
2995 PSA_DONE( );
2996}
2997/* END_CASE */
2998
2999/* BEGIN_CASE */
3000void cipher_verify_output( int alg_arg,
3001 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003002 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003003 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003004{
Ronald Cron5425a212020-08-04 14:58:35 +02003005 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003006 psa_key_type_t key_type = key_type_arg;
3007 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003008 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003009 size_t output1_size = 0;
3010 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003011 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003012 size_t output2_size = 0;
3013 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003014 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003015
Gilles Peskine8817f612018-12-18 00:18:46 +01003016 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003017
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003018 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3019 psa_set_key_algorithm( &attributes, alg );
3020 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003021
Ronald Cron5425a212020-08-04 14:58:35 +02003022 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3023 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003024 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003025 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003026
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003027 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3028 output1, output1_size,
3029 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003030 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003031 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003032 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003033 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003034
3035 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003036 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003037
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003038 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3039 output2, output2_size,
3040 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003041 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003042 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003043 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003044 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003045
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003046 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003047
3048exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003049 mbedtls_free( output1 );
3050 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003051 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003052 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003053}
3054/* END_CASE */
3055
3056/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003057void cipher_verify_output_multipart( int alg_arg,
3058 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003059 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003060 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003061 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003062{
Ronald Cron5425a212020-08-04 14:58:35 +02003063 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003064 psa_key_type_t key_type = key_type_arg;
3065 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003066 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003067 unsigned char iv[16] = {0};
3068 size_t iv_size = 16;
3069 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003070 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003071 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003072 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003073 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003074 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003075 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003076 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003077 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3078 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003079 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003080
Gilles Peskine8817f612018-12-18 00:18:46 +01003081 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003082
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003083 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3084 psa_set_key_algorithm( &attributes, alg );
3085 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003086
Ronald Cron5425a212020-08-04 14:58:35 +02003087 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3088 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003089
Ronald Cron5425a212020-08-04 14:58:35 +02003090 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3091 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003092
Steven Cooreman177deba2020-09-07 17:14:14 +02003093 if( alg != PSA_ALG_ECB_NO_PADDING )
3094 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003095 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3096 iv, iv_size,
3097 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003098 }
3099
gabor-mezei-armceface22021-01-21 12:26:17 +01003100 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3101 TEST_ASSERT( output1_buffer_size <=
3102 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003103 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003104
Gilles Peskinee0866522019-02-19 19:44:00 +01003105 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003106
Gilles Peskine8817f612018-12-18 00:18:46 +01003107 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3108 output1, output1_buffer_size,
3109 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003110 TEST_ASSERT( function_output_length <=
3111 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3112 TEST_ASSERT( function_output_length <=
3113 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003114 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003115
Gilles Peskine8817f612018-12-18 00:18:46 +01003116 PSA_ASSERT( psa_cipher_update( &operation1,
3117 input->x + first_part_size,
3118 input->len - first_part_size,
3119 output1, output1_buffer_size,
3120 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003121 TEST_ASSERT( function_output_length <=
3122 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3123 alg,
3124 input->len - first_part_size ) );
3125 TEST_ASSERT( function_output_length <=
3126 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003127 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003128
Gilles Peskine8817f612018-12-18 00:18:46 +01003129 PSA_ASSERT( psa_cipher_finish( &operation1,
3130 output1 + output1_length,
3131 output1_buffer_size - output1_length,
3132 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003133 TEST_ASSERT( function_output_length <=
3134 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3135 TEST_ASSERT( function_output_length <=
3136 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003137 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003138
Gilles Peskine8817f612018-12-18 00:18:46 +01003139 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003140
Gilles Peskine048b7f02018-06-08 14:20:49 +02003141 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003142 TEST_ASSERT( output2_buffer_size <=
3143 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3144 TEST_ASSERT( output2_buffer_size <=
3145 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003146 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003147
Steven Cooreman177deba2020-09-07 17:14:14 +02003148 if( iv_length > 0 )
3149 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003150 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3151 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003152 }
Moran Pekerded84402018-06-06 16:36:50 +03003153
Gilles Peskine8817f612018-12-18 00:18:46 +01003154 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3155 output2, output2_buffer_size,
3156 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003157 TEST_ASSERT( function_output_length <=
3158 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3159 TEST_ASSERT( function_output_length <=
3160 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003161 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003162
Gilles Peskine8817f612018-12-18 00:18:46 +01003163 PSA_ASSERT( psa_cipher_update( &operation2,
3164 output1 + first_part_size,
3165 output1_length - first_part_size,
3166 output2, output2_buffer_size,
3167 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003168 TEST_ASSERT( function_output_length <=
3169 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3170 alg,
3171 output1_length - first_part_size ) );
3172 TEST_ASSERT( function_output_length <=
3173 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003174 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003175
Gilles Peskine8817f612018-12-18 00:18:46 +01003176 PSA_ASSERT( psa_cipher_finish( &operation2,
3177 output2 + output2_length,
3178 output2_buffer_size - output2_length,
3179 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003180 TEST_ASSERT( function_output_length <=
3181 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3182 TEST_ASSERT( function_output_length <=
3183 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003184 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003185
Gilles Peskine8817f612018-12-18 00:18:46 +01003186 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003187
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003188 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003189
3190exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003191 psa_cipher_abort( &operation1 );
3192 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003193 mbedtls_free( output1 );
3194 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003195 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003196 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003197}
3198/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003199
Gilles Peskine20035e32018-02-03 22:44:14 +01003200/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003201void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003202 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003203 data_t *nonce,
3204 data_t *additional_data,
3205 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003206 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003207{
Ronald Cron5425a212020-08-04 14:58:35 +02003208 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003209 psa_key_type_t key_type = key_type_arg;
3210 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003211 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003212 unsigned char *output_data = NULL;
3213 size_t output_size = 0;
3214 size_t output_length = 0;
3215 unsigned char *output_data2 = NULL;
3216 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003217 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003218 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003219 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003220
Gilles Peskine8817f612018-12-18 00:18:46 +01003221 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003222
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003223 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3224 psa_set_key_algorithm( &attributes, alg );
3225 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003226
Gilles Peskine049c7532019-05-15 20:22:09 +02003227 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003228 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003229 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3230 key_bits = psa_get_key_bits( &attributes );
3231
3232 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3233 alg );
3234 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3235 * should be exact. */
3236 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3237 expected_result != PSA_ERROR_NOT_SUPPORTED )
3238 {
3239 TEST_EQUAL( output_size,
3240 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3241 TEST_ASSERT( output_size <=
3242 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3243 }
3244 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003245
Steven Cooremanf49478b2021-02-15 15:19:25 +01003246 status = psa_aead_encrypt( key, alg,
3247 nonce->x, nonce->len,
3248 additional_data->x,
3249 additional_data->len,
3250 input_data->x, input_data->len,
3251 output_data, output_size,
3252 &output_length );
3253
3254 /* If the operation is not supported, just skip and not fail in case the
3255 * encryption involves a common limitation of cryptography hardwares and
3256 * an alternative implementation. */
3257 if( status == PSA_ERROR_NOT_SUPPORTED )
3258 {
3259 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3260 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3261 }
3262
3263 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003264
3265 if( PSA_SUCCESS == expected_result )
3266 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003267 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003268
Gilles Peskine003a4a92019-05-14 16:09:40 +02003269 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3270 * should be exact. */
3271 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003272 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003273
gabor-mezei-armceface22021-01-21 12:26:17 +01003274 TEST_ASSERT( input_data->len <=
3275 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3276
Ronald Cron5425a212020-08-04 14:58:35 +02003277 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003278 nonce->x, nonce->len,
3279 additional_data->x,
3280 additional_data->len,
3281 output_data, output_length,
3282 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003283 &output_length2 ),
3284 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003285
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003286 ASSERT_COMPARE( input_data->x, input_data->len,
3287 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003288 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003289
Gilles Peskinea1cac842018-06-11 19:33:02 +02003290exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003291 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003292 mbedtls_free( output_data );
3293 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003294 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003295}
3296/* END_CASE */
3297
3298/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003299void aead_encrypt( int key_type_arg, data_t *key_data,
3300 int alg_arg,
3301 data_t *nonce,
3302 data_t *additional_data,
3303 data_t *input_data,
3304 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003305{
Ronald Cron5425a212020-08-04 14:58:35 +02003306 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003307 psa_key_type_t key_type = key_type_arg;
3308 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003309 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003310 unsigned char *output_data = NULL;
3311 size_t output_size = 0;
3312 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003313 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003314 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003315
Gilles Peskine8817f612018-12-18 00:18:46 +01003316 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003317
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003318 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3319 psa_set_key_algorithm( &attributes, alg );
3320 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003321
Gilles Peskine049c7532019-05-15 20:22:09 +02003322 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003323 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003324 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3325 key_bits = psa_get_key_bits( &attributes );
3326
3327 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3328 alg );
3329 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3330 * should be exact. */
3331 TEST_EQUAL( output_size,
3332 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3333 TEST_ASSERT( output_size <=
3334 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3335 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003336
Steven Cooremand588ea12021-01-11 19:36:04 +01003337 status = psa_aead_encrypt( key, alg,
3338 nonce->x, nonce->len,
3339 additional_data->x, additional_data->len,
3340 input_data->x, input_data->len,
3341 output_data, output_size,
3342 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003343
Ronald Cron28a45ed2021-02-09 20:35:42 +01003344 /* If the operation is not supported, just skip and not fail in case the
3345 * encryption involves a common limitation of cryptography hardwares and
3346 * an alternative implementation. */
3347 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003348 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003349 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3350 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003351 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003352
3353 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003354 ASSERT_COMPARE( expected_result->x, expected_result->len,
3355 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003356
Gilles Peskinea1cac842018-06-11 19:33:02 +02003357exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003358 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003359 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003360 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003361}
3362/* END_CASE */
3363
3364/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003365void aead_decrypt( int key_type_arg, data_t *key_data,
3366 int alg_arg,
3367 data_t *nonce,
3368 data_t *additional_data,
3369 data_t *input_data,
3370 data_t *expected_data,
3371 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003372{
Ronald Cron5425a212020-08-04 14:58:35 +02003373 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003374 psa_key_type_t key_type = key_type_arg;
3375 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003376 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003377 unsigned char *output_data = NULL;
3378 size_t output_size = 0;
3379 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003380 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003381 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003382 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003383
Gilles Peskine8817f612018-12-18 00:18:46 +01003384 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003385
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003386 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3387 psa_set_key_algorithm( &attributes, alg );
3388 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003389
Gilles Peskine049c7532019-05-15 20:22:09 +02003390 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003391 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003392 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3393 key_bits = psa_get_key_bits( &attributes );
3394
3395 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3396 alg );
3397 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3398 expected_result != PSA_ERROR_NOT_SUPPORTED )
3399 {
3400 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3401 * should be exact. */
3402 TEST_EQUAL( output_size,
3403 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3404 TEST_ASSERT( output_size <=
3405 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3406 }
3407 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003408
Steven Cooremand588ea12021-01-11 19:36:04 +01003409 status = psa_aead_decrypt( key, alg,
3410 nonce->x, nonce->len,
3411 additional_data->x,
3412 additional_data->len,
3413 input_data->x, input_data->len,
3414 output_data, output_size,
3415 &output_length );
3416
Ronald Cron28a45ed2021-02-09 20:35:42 +01003417 /* If the operation is not supported, just skip and not fail in case the
3418 * decryption involves a common limitation of cryptography hardwares and
3419 * an alternative implementation. */
3420 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003421 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003422 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3423 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003424 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003425
3426 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003427
Gilles Peskine2d277862018-06-18 15:41:12 +02003428 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003429 ASSERT_COMPARE( expected_data->x, expected_data->len,
3430 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003431
Gilles Peskinea1cac842018-06-11 19:33:02 +02003432exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003433 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003434 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003435 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003436}
3437/* END_CASE */
3438
3439/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003440void signature_size( int type_arg,
3441 int bits,
3442 int alg_arg,
3443 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003444{
3445 psa_key_type_t type = type_arg;
3446 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003447 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003448
Gilles Peskinefe11b722018-12-18 00:24:04 +01003449 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003450
Gilles Peskinee59236f2018-01-27 23:32:46 +01003451exit:
3452 ;
3453}
3454/* END_CASE */
3455
3456/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003457void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3458 int alg_arg, data_t *input_data,
3459 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003460{
Ronald Cron5425a212020-08-04 14:58:35 +02003461 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003462 psa_key_type_t key_type = key_type_arg;
3463 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003464 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003465 unsigned char *signature = NULL;
3466 size_t signature_size;
3467 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003468 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003469
Gilles Peskine8817f612018-12-18 00:18:46 +01003470 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003471
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003472 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003473 psa_set_key_algorithm( &attributes, alg );
3474 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003475
Gilles Peskine049c7532019-05-15 20:22:09 +02003476 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003477 &key ) );
3478 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003479 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003480
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003481 /* Allocate a buffer which has the size advertized by the
3482 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003483 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003484 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003485 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003486 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003487 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003488
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003489 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003490 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003491 input_data->x, input_data->len,
3492 signature, signature_size,
3493 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003494 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003495 ASSERT_COMPARE( output_data->x, output_data->len,
3496 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003497
3498exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003499 /*
3500 * Key attributes may have been returned by psa_get_key_attributes()
3501 * thus reset them as required.
3502 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003503 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003504
Ronald Cron5425a212020-08-04 14:58:35 +02003505 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003506 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003507 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003508}
3509/* END_CASE */
3510
3511/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003512void sign_hash_fail( int key_type_arg, data_t *key_data,
3513 int alg_arg, data_t *input_data,
3514 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003515{
Ronald Cron5425a212020-08-04 14:58:35 +02003516 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003517 psa_key_type_t key_type = key_type_arg;
3518 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003519 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003520 psa_status_t actual_status;
3521 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003522 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003523 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003524 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003525
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003526 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003527
Gilles Peskine8817f612018-12-18 00:18:46 +01003528 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003529
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003530 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003531 psa_set_key_algorithm( &attributes, alg );
3532 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003533
Gilles Peskine049c7532019-05-15 20:22:09 +02003534 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003535 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003536
Ronald Cron5425a212020-08-04 14:58:35 +02003537 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003538 input_data->x, input_data->len,
3539 signature, signature_size,
3540 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003541 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003542 /* The value of *signature_length is unspecified on error, but
3543 * whatever it is, it should be less than signature_size, so that
3544 * if the caller tries to read *signature_length bytes without
3545 * checking the error code then they don't overflow a buffer. */
3546 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003547
3548exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003549 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003550 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003551 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003552 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003553}
3554/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003555
3556/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003557void sign_verify_hash( int key_type_arg, data_t *key_data,
3558 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003559{
Ronald Cron5425a212020-08-04 14:58:35 +02003560 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003561 psa_key_type_t key_type = key_type_arg;
3562 psa_algorithm_t alg = alg_arg;
3563 size_t key_bits;
3564 unsigned char *signature = NULL;
3565 size_t signature_size;
3566 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003567 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003568
Gilles Peskine8817f612018-12-18 00:18:46 +01003569 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003570
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003571 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003572 psa_set_key_algorithm( &attributes, alg );
3573 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003574
Gilles Peskine049c7532019-05-15 20:22:09 +02003575 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003576 &key ) );
3577 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003578 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003579
3580 /* Allocate a buffer which has the size advertized by the
3581 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003582 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003583 key_bits, alg );
3584 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003585 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003586 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003587
3588 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003589 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003590 input_data->x, input_data->len,
3591 signature, signature_size,
3592 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003593 /* Check that the signature length looks sensible. */
3594 TEST_ASSERT( signature_length <= signature_size );
3595 TEST_ASSERT( signature_length > 0 );
3596
3597 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003598 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003599 input_data->x, input_data->len,
3600 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003601
3602 if( input_data->len != 0 )
3603 {
3604 /* Flip a bit in the input and verify that the signature is now
3605 * detected as invalid. Flip a bit at the beginning, not at the end,
3606 * because ECDSA may ignore the last few bits of the input. */
3607 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003608 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003609 input_data->x, input_data->len,
3610 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003611 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003612 }
3613
3614exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003615 /*
3616 * Key attributes may have been returned by psa_get_key_attributes()
3617 * thus reset them as required.
3618 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003619 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003620
Ronald Cron5425a212020-08-04 14:58:35 +02003621 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003622 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003623 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003624}
3625/* END_CASE */
3626
3627/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003628void verify_hash( int key_type_arg, data_t *key_data,
3629 int alg_arg, data_t *hash_data,
3630 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003631{
Ronald Cron5425a212020-08-04 14:58:35 +02003632 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003633 psa_key_type_t key_type = key_type_arg;
3634 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003635 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003636
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003637 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003638
Gilles Peskine8817f612018-12-18 00:18:46 +01003639 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003640
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003641 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003642 psa_set_key_algorithm( &attributes, alg );
3643 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003644
Gilles Peskine049c7532019-05-15 20:22:09 +02003645 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003646 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003647
Ronald Cron5425a212020-08-04 14:58:35 +02003648 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003649 hash_data->x, hash_data->len,
3650 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003651
itayzafrir5c753392018-05-08 11:18:38 +03003652exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003653 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003654 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003655 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003656}
3657/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003658
3659/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003660void verify_hash_fail( int key_type_arg, data_t *key_data,
3661 int alg_arg, data_t *hash_data,
3662 data_t *signature_data,
3663 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003664{
Ronald Cron5425a212020-08-04 14:58:35 +02003665 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003666 psa_key_type_t key_type = key_type_arg;
3667 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003668 psa_status_t actual_status;
3669 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003670 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003671
Gilles Peskine8817f612018-12-18 00:18:46 +01003672 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003673
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003674 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003675 psa_set_key_algorithm( &attributes, alg );
3676 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003677
Gilles Peskine049c7532019-05-15 20:22:09 +02003678 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003679 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003680
Ronald Cron5425a212020-08-04 14:58:35 +02003681 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003682 hash_data->x, hash_data->len,
3683 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003684 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003685
3686exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003687 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003688 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003689 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003690}
3691/* END_CASE */
3692
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003693/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02003694void sign_message_deterministic( int key_type_arg,
3695 data_t *key_data,
3696 int alg_arg,
3697 data_t *input_data,
3698 data_t *output_data )
3699{
3700 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3701 psa_key_type_t key_type = key_type_arg;
3702 psa_algorithm_t alg = alg_arg;
3703 size_t key_bits;
3704 unsigned char *signature = NULL;
3705 size_t signature_size;
3706 size_t signature_length = 0xdeadbeef;
3707 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3708
3709 PSA_ASSERT( psa_crypto_init( ) );
3710
3711 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3712 psa_set_key_algorithm( &attributes, alg );
3713 psa_set_key_type( &attributes, key_type );
3714
3715 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3716 &key ) );
3717 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3718 key_bits = psa_get_key_bits( &attributes );
3719
3720 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3721 TEST_ASSERT( signature_size != 0 );
3722 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3723 ASSERT_ALLOC( signature, signature_size );
3724
3725 PSA_ASSERT( psa_sign_message( key, alg,
3726 input_data->x, input_data->len,
3727 signature, signature_size,
3728 &signature_length ) );
3729
3730 ASSERT_COMPARE( output_data->x, output_data->len,
3731 signature, signature_length );
3732
3733exit:
3734 psa_reset_key_attributes( &attributes );
3735
3736 psa_destroy_key( key );
3737 mbedtls_free( signature );
3738 PSA_DONE( );
3739
3740}
3741/* END_CASE */
3742
3743/* BEGIN_CASE */
3744void sign_message_fail( int key_type_arg,
3745 data_t *key_data,
3746 int alg_arg,
3747 data_t *input_data,
3748 int signature_size_arg,
3749 int expected_status_arg )
3750{
3751 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3752 psa_key_type_t key_type = key_type_arg;
3753 psa_algorithm_t alg = alg_arg;
3754 size_t signature_size = signature_size_arg;
3755 psa_status_t actual_status;
3756 psa_status_t expected_status = expected_status_arg;
3757 unsigned char *signature = NULL;
3758 size_t signature_length = 0xdeadbeef;
3759 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3760
3761 ASSERT_ALLOC( signature, signature_size );
3762
3763 PSA_ASSERT( psa_crypto_init( ) );
3764
3765 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3766 psa_set_key_algorithm( &attributes, alg );
3767 psa_set_key_type( &attributes, key_type );
3768
3769 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3770 &key ) );
3771
3772 actual_status = psa_sign_message( key, alg,
3773 input_data->x, input_data->len,
3774 signature, signature_size,
3775 &signature_length );
3776 TEST_EQUAL( actual_status, expected_status );
3777 /* The value of *signature_length is unspecified on error, but
3778 * whatever it is, it should be less than signature_size, so that
3779 * if the caller tries to read *signature_length bytes without
3780 * checking the error code then they don't overflow a buffer. */
3781 TEST_ASSERT( signature_length <= signature_size );
3782
3783exit:
3784 psa_reset_key_attributes( &attributes );
3785 psa_destroy_key( key );
3786 mbedtls_free( signature );
3787 PSA_DONE( );
3788}
3789/* END_CASE */
3790
3791/* BEGIN_CASE */
3792void sign_verify_message( int key_type_arg,
3793 data_t *key_data,
3794 int alg_arg,
3795 data_t *input_data )
3796{
3797 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3798 psa_key_type_t key_type = key_type_arg;
3799 psa_algorithm_t alg = alg_arg;
3800 size_t key_bits;
3801 unsigned char *signature = NULL;
3802 size_t signature_size;
3803 size_t signature_length = 0xdeadbeef;
3804 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3805
3806 PSA_ASSERT( psa_crypto_init( ) );
3807
3808 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3809 PSA_KEY_USAGE_VERIFY_MESSAGE );
3810 psa_set_key_algorithm( &attributes, alg );
3811 psa_set_key_type( &attributes, key_type );
3812
3813 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3814 &key ) );
3815 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3816 key_bits = psa_get_key_bits( &attributes );
3817
3818 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3819 TEST_ASSERT( signature_size != 0 );
3820 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3821 ASSERT_ALLOC( signature, signature_size );
3822
3823 PSA_ASSERT( psa_sign_message( key, alg,
3824 input_data->x, input_data->len,
3825 signature, signature_size,
3826 &signature_length ) );
3827 TEST_ASSERT( signature_length <= signature_size );
3828 TEST_ASSERT( signature_length > 0 );
3829
3830 PSA_ASSERT( psa_verify_message( key, alg,
3831 input_data->x, input_data->len,
3832 signature, signature_length ) );
3833
3834 if( input_data->len != 0 )
3835 {
3836 /* Flip a bit in the input and verify that the signature is now
3837 * detected as invalid. Flip a bit at the beginning, not at the end,
3838 * because ECDSA may ignore the last few bits of the input. */
3839 input_data->x[0] ^= 1;
3840 TEST_EQUAL( psa_verify_message( key, alg,
3841 input_data->x, input_data->len,
3842 signature, signature_length ),
3843 PSA_ERROR_INVALID_SIGNATURE );
3844 }
3845
3846exit:
3847 psa_reset_key_attributes( &attributes );
3848
3849 psa_destroy_key( key );
3850 mbedtls_free( signature );
3851 PSA_DONE( );
3852}
3853/* END_CASE */
3854
3855/* BEGIN_CASE */
3856void verify_message( int key_type_arg,
3857 data_t *key_data,
3858 int alg_arg,
3859 data_t *input_data,
3860 data_t *signature_data )
3861{
3862 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3863 psa_key_type_t key_type = key_type_arg;
3864 psa_algorithm_t alg = alg_arg;
3865 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3866
3867 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3868
3869 PSA_ASSERT( psa_crypto_init( ) );
3870
3871 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3872 psa_set_key_algorithm( &attributes, alg );
3873 psa_set_key_type( &attributes, key_type );
3874
3875 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3876 &key ) );
3877
3878 PSA_ASSERT( psa_verify_message( key, alg,
3879 input_data->x, input_data->len,
3880 signature_data->x, signature_data->len ) );
3881
3882exit:
3883 psa_reset_key_attributes( &attributes );
3884 psa_destroy_key( key );
3885 PSA_DONE( );
3886}
3887/* END_CASE */
3888
3889/* BEGIN_CASE */
3890void verify_message_fail( int key_type_arg,
3891 data_t *key_data,
3892 int alg_arg,
3893 data_t *hash_data,
3894 data_t *signature_data,
3895 int expected_status_arg )
3896{
3897 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3898 psa_key_type_t key_type = key_type_arg;
3899 psa_algorithm_t alg = alg_arg;
3900 psa_status_t actual_status;
3901 psa_status_t expected_status = expected_status_arg;
3902 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3903
3904 PSA_ASSERT( psa_crypto_init( ) );
3905
3906 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3907 psa_set_key_algorithm( &attributes, alg );
3908 psa_set_key_type( &attributes, key_type );
3909
3910 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3911 &key ) );
3912
3913 actual_status = psa_verify_message( key, alg,
3914 hash_data->x, hash_data->len,
3915 signature_data->x,
3916 signature_data->len );
3917 TEST_EQUAL( actual_status, expected_status );
3918
3919exit:
3920 psa_reset_key_attributes( &attributes );
3921 psa_destroy_key( key );
3922 PSA_DONE( );
3923}
3924/* END_CASE */
3925
3926/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003927void asymmetric_encrypt( int key_type_arg,
3928 data_t *key_data,
3929 int alg_arg,
3930 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003931 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003932 int expected_output_length_arg,
3933 int expected_status_arg )
3934{
Ronald Cron5425a212020-08-04 14:58:35 +02003935 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003936 psa_key_type_t key_type = key_type_arg;
3937 psa_algorithm_t alg = alg_arg;
3938 size_t expected_output_length = expected_output_length_arg;
3939 size_t key_bits;
3940 unsigned char *output = NULL;
3941 size_t output_size;
3942 size_t output_length = ~0;
3943 psa_status_t actual_status;
3944 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003945 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003946
Gilles Peskine8817f612018-12-18 00:18:46 +01003947 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003948
Gilles Peskine656896e2018-06-29 19:12:28 +02003949 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003950 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3951 psa_set_key_algorithm( &attributes, alg );
3952 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003953 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003954 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003955
3956 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003957 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003958 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003959
Gilles Peskine656896e2018-06-29 19:12:28 +02003960 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003961 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003962 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003963
3964 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003965 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003966 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003967 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003968 output, output_size,
3969 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003970 TEST_EQUAL( actual_status, expected_status );
3971 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003972
Gilles Peskine68428122018-06-30 18:42:41 +02003973 /* If the label is empty, the test framework puts a non-null pointer
3974 * in label->x. Test that a null pointer works as well. */
3975 if( label->len == 0 )
3976 {
3977 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003978 if( output_size != 0 )
3979 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003980 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003981 input_data->x, input_data->len,
3982 NULL, label->len,
3983 output, output_size,
3984 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003985 TEST_EQUAL( actual_status, expected_status );
3986 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003987 }
3988
Gilles Peskine656896e2018-06-29 19:12:28 +02003989exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003990 /*
3991 * Key attributes may have been returned by psa_get_key_attributes()
3992 * thus reset them as required.
3993 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003994 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003995
Ronald Cron5425a212020-08-04 14:58:35 +02003996 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02003997 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003998 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003999}
4000/* END_CASE */
4001
4002/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004003void asymmetric_encrypt_decrypt( int key_type_arg,
4004 data_t *key_data,
4005 int alg_arg,
4006 data_t *input_data,
4007 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004008{
Ronald Cron5425a212020-08-04 14:58:35 +02004009 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004010 psa_key_type_t key_type = key_type_arg;
4011 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004012 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004013 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004014 size_t output_size;
4015 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004016 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004017 size_t output2_size;
4018 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004019 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004020
Gilles Peskine8817f612018-12-18 00:18:46 +01004021 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004022
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004023 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4024 psa_set_key_algorithm( &attributes, alg );
4025 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004026
Gilles Peskine049c7532019-05-15 20:22:09 +02004027 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004028 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004029
4030 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004031 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004032 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004033
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004034 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004035 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004036 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01004037
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004038 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01004039 TEST_ASSERT( output2_size <=
4040 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
4041 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004042 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004043
Gilles Peskineeebd7382018-06-08 18:11:54 +02004044 /* We test encryption by checking that encrypt-then-decrypt gives back
4045 * the original plaintext because of the non-optional random
4046 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004047 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004048 input_data->x, input_data->len,
4049 label->x, label->len,
4050 output, output_size,
4051 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004052 /* We don't know what ciphertext length to expect, but check that
4053 * it looks sensible. */
4054 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004055
Ronald Cron5425a212020-08-04 14:58:35 +02004056 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004057 output, output_length,
4058 label->x, label->len,
4059 output2, output2_size,
4060 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004061 ASSERT_COMPARE( input_data->x, input_data->len,
4062 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004063
4064exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004065 /*
4066 * Key attributes may have been returned by psa_get_key_attributes()
4067 * thus reset them as required.
4068 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004069 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004070
Ronald Cron5425a212020-08-04 14:58:35 +02004071 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004072 mbedtls_free( output );
4073 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004074 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004075}
4076/* END_CASE */
4077
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004078/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004079void asymmetric_decrypt( int key_type_arg,
4080 data_t *key_data,
4081 int alg_arg,
4082 data_t *input_data,
4083 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004084 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004085{
Ronald Cron5425a212020-08-04 14:58:35 +02004086 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004087 psa_key_type_t key_type = key_type_arg;
4088 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004089 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004090 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004091 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004092 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004093 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004094
Gilles Peskine8817f612018-12-18 00:18:46 +01004095 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004096
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004097 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4098 psa_set_key_algorithm( &attributes, alg );
4099 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004100
Gilles Peskine049c7532019-05-15 20:22:09 +02004101 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004102 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004103
gabor-mezei-armceface22021-01-21 12:26:17 +01004104 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4105 key_bits = psa_get_key_bits( &attributes );
4106
4107 /* Determine the maximum ciphertext length */
4108 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
4109 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
4110 ASSERT_ALLOC( output, output_size );
4111
Ronald Cron5425a212020-08-04 14:58:35 +02004112 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004113 input_data->x, input_data->len,
4114 label->x, label->len,
4115 output,
4116 output_size,
4117 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004118 ASSERT_COMPARE( expected_data->x, expected_data->len,
4119 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004120
Gilles Peskine68428122018-06-30 18:42:41 +02004121 /* If the label is empty, the test framework puts a non-null pointer
4122 * in label->x. Test that a null pointer works as well. */
4123 if( label->len == 0 )
4124 {
4125 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004126 if( output_size != 0 )
4127 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004128 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004129 input_data->x, input_data->len,
4130 NULL, label->len,
4131 output,
4132 output_size,
4133 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004134 ASSERT_COMPARE( expected_data->x, expected_data->len,
4135 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004136 }
4137
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004138exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004139 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004140 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004141 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004142 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004143}
4144/* END_CASE */
4145
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004146/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004147void asymmetric_decrypt_fail( int key_type_arg,
4148 data_t *key_data,
4149 int alg_arg,
4150 data_t *input_data,
4151 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004152 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004153 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004154{
Ronald Cron5425a212020-08-04 14:58:35 +02004155 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004156 psa_key_type_t key_type = key_type_arg;
4157 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004158 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004159 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004160 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004161 psa_status_t actual_status;
4162 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004163 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004164
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004165 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004166
Gilles Peskine8817f612018-12-18 00:18:46 +01004167 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004168
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004169 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4170 psa_set_key_algorithm( &attributes, alg );
4171 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004172
Gilles Peskine049c7532019-05-15 20:22:09 +02004173 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004174 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004175
Ronald Cron5425a212020-08-04 14:58:35 +02004176 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004177 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004178 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004179 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004180 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004181 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004182 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004183
Gilles Peskine68428122018-06-30 18:42:41 +02004184 /* If the label is empty, the test framework puts a non-null pointer
4185 * in label->x. Test that a null pointer works as well. */
4186 if( label->len == 0 )
4187 {
4188 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004189 if( output_size != 0 )
4190 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004191 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004192 input_data->x, input_data->len,
4193 NULL, label->len,
4194 output, output_size,
4195 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004196 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004197 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004198 }
4199
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004200exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004201 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004202 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004203 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004204 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004205}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004206/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004207
4208/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004209void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004210{
4211 /* Test each valid way of initializing the object, except for `= {0}`, as
4212 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4213 * though it's OK by the C standard. We could test for this, but we'd need
4214 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004215 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004216 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4217 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4218 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004219
4220 memset( &zero, 0, sizeof( zero ) );
4221
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004222 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004223 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004224 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004225 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004226 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004227 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004228 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004229
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004230 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004231 PSA_ASSERT( psa_key_derivation_abort(&func) );
4232 PSA_ASSERT( psa_key_derivation_abort(&init) );
4233 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004234}
4235/* END_CASE */
4236
Janos Follath16de4a42019-06-13 16:32:24 +01004237/* BEGIN_CASE */
4238void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004239{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004240 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004241 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004242 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004243
Gilles Peskine8817f612018-12-18 00:18:46 +01004244 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004245
Janos Follath16de4a42019-06-13 16:32:24 +01004246 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004247 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004248
4249exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004250 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004251 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004252}
4253/* END_CASE */
4254
Janos Follathaf3c2a02019-06-12 12:34:34 +01004255/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004256void derive_set_capacity( int alg_arg, int capacity_arg,
4257 int expected_status_arg )
4258{
4259 psa_algorithm_t alg = alg_arg;
4260 size_t capacity = capacity_arg;
4261 psa_status_t expected_status = expected_status_arg;
4262 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4263
4264 PSA_ASSERT( psa_crypto_init( ) );
4265
4266 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4267
4268 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4269 expected_status );
4270
4271exit:
4272 psa_key_derivation_abort( &operation );
4273 PSA_DONE( );
4274}
4275/* END_CASE */
4276
4277/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004278void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004279 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004280 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004281 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004282 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004283 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004284 int expected_status_arg3,
4285 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004286{
4287 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004288 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4289 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004290 psa_status_t expected_statuses[] = {expected_status_arg1,
4291 expected_status_arg2,
4292 expected_status_arg3};
4293 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004294 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4295 MBEDTLS_SVC_KEY_ID_INIT,
4296 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004297 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4298 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4299 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004300 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004301 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004302 psa_status_t expected_output_status = expected_output_status_arg;
4303 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004304
4305 PSA_ASSERT( psa_crypto_init( ) );
4306
4307 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4308 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004309
4310 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4311
4312 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4313 {
Gilles Peskine4023c012021-05-27 13:21:20 +02004314 mbedtls_test_set_step( i );
4315 if( steps[i] == 0 )
4316 {
4317 /* Skip this step */
4318 }
4319 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004320 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004321 psa_set_key_type( &attributes, key_types[i] );
4322 PSA_ASSERT( psa_import_key( &attributes,
4323 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004324 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004325 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4326 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4327 {
4328 // When taking a private key as secret input, use key agreement
4329 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004330 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4331 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004332 expected_statuses[i] );
4333 }
4334 else
4335 {
4336 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004337 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004338 expected_statuses[i] );
4339 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004340 }
4341 else
4342 {
4343 TEST_EQUAL( psa_key_derivation_input_bytes(
4344 &operation, steps[i],
4345 inputs[i]->x, inputs[i]->len ),
4346 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004347 }
4348 }
4349
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004350 if( output_key_type != PSA_KEY_TYPE_NONE )
4351 {
4352 psa_reset_key_attributes( &attributes );
4353 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4354 psa_set_key_bits( &attributes, 8 );
4355 actual_output_status =
4356 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004357 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004358 }
4359 else
4360 {
4361 uint8_t buffer[1];
4362 actual_output_status =
4363 psa_key_derivation_output_bytes( &operation,
4364 buffer, sizeof( buffer ) );
4365 }
4366 TEST_EQUAL( actual_output_status, expected_output_status );
4367
Janos Follathaf3c2a02019-06-12 12:34:34 +01004368exit:
4369 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004370 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4371 psa_destroy_key( keys[i] );
4372 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004373 PSA_DONE( );
4374}
4375/* END_CASE */
4376
Janos Follathd958bb72019-07-03 15:02:16 +01004377/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02004378void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004379{
Janos Follathd958bb72019-07-03 15:02:16 +01004380 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004381 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004382 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004383 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004384 unsigned char input1[] = "Input 1";
4385 size_t input1_length = sizeof( input1 );
4386 unsigned char input2[] = "Input 2";
4387 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004388 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004389 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004390 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4391 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4392 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004393 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004394
Gilles Peskine8817f612018-12-18 00:18:46 +01004395 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004396
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004397 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4398 psa_set_key_algorithm( &attributes, alg );
4399 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004400
Gilles Peskine73676cb2019-05-15 20:15:10 +02004401 PSA_ASSERT( psa_import_key( &attributes,
4402 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004403 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004404
4405 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004406 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4407 input1, input1_length,
4408 input2, input2_length,
4409 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004410 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004411
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004412 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004413 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004414 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004415
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004416 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004417
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004418 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004419 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004420
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004421exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004422 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004423 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004424 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004425}
4426/* END_CASE */
4427
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004428/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02004429void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004430{
4431 uint8_t output_buffer[16];
4432 size_t buffer_size = 16;
4433 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004434 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004435
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004436 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4437 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004438 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004439
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004440 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004441 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004442
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004443 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004444
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004445 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4446 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004447 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004448
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004449 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004450 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004451
4452exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004453 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004454}
4455/* END_CASE */
4456
4457/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004458void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004459 int step1_arg, data_t *input1,
4460 int step2_arg, data_t *input2,
4461 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004462 int requested_capacity_arg,
4463 data_t *expected_output1,
4464 data_t *expected_output2 )
4465{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004466 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004467 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4468 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004469 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4470 MBEDTLS_SVC_KEY_ID_INIT,
4471 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004472 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004473 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004474 uint8_t *expected_outputs[2] =
4475 {expected_output1->x, expected_output2->x};
4476 size_t output_sizes[2] =
4477 {expected_output1->len, expected_output2->len};
4478 size_t output_buffer_size = 0;
4479 uint8_t *output_buffer = NULL;
4480 size_t expected_capacity;
4481 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004482 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004483 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004484 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004485
4486 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4487 {
4488 if( output_sizes[i] > output_buffer_size )
4489 output_buffer_size = output_sizes[i];
4490 if( output_sizes[i] == 0 )
4491 expected_outputs[i] = NULL;
4492 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004493 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004494 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004495
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004496 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4497 psa_set_key_algorithm( &attributes, alg );
4498 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004499
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004500 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004501 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4502 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4503 requested_capacity ) );
4504 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004505 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004506 switch( steps[i] )
4507 {
4508 case 0:
4509 break;
4510 case PSA_KEY_DERIVATION_INPUT_SECRET:
4511 PSA_ASSERT( psa_import_key( &attributes,
4512 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004513 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004514
4515 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4516 {
4517 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4518 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4519 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4520 }
4521
Gilles Peskine1468da72019-05-29 17:35:49 +02004522 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004523 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004524 break;
4525 default:
4526 PSA_ASSERT( psa_key_derivation_input_bytes(
4527 &operation, steps[i],
4528 inputs[i]->x, inputs[i]->len ) );
4529 break;
4530 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004531 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004532
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004533 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004534 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004535 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004536 expected_capacity = requested_capacity;
4537
4538 /* Expansion phase. */
4539 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4540 {
4541 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004542 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004543 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004544 if( expected_capacity == 0 && output_sizes[i] == 0 )
4545 {
4546 /* Reading 0 bytes when 0 bytes are available can go either way. */
4547 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004548 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004549 continue;
4550 }
4551 else if( expected_capacity == 0 ||
4552 output_sizes[i] > expected_capacity )
4553 {
4554 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004555 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004556 expected_capacity = 0;
4557 continue;
4558 }
4559 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004560 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004561 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004562 ASSERT_COMPARE( output_buffer, output_sizes[i],
4563 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004564 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004565 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004566 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004567 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004568 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004569 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004570 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004571
4572exit:
4573 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004574 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004575 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4576 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004577 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004578}
4579/* END_CASE */
4580
4581/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004582void derive_full( int alg_arg,
4583 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004584 data_t *input1,
4585 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004586 int requested_capacity_arg )
4587{
Ronald Cron5425a212020-08-04 14:58:35 +02004588 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004589 psa_algorithm_t alg = alg_arg;
4590 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004591 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004592 unsigned char output_buffer[16];
4593 size_t expected_capacity = requested_capacity;
4594 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004595 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004596
Gilles Peskine8817f612018-12-18 00:18:46 +01004597 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004598
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004599 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4600 psa_set_key_algorithm( &attributes, alg );
4601 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004602
Gilles Peskine049c7532019-05-15 20:22:09 +02004603 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004604 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004605
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004606 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4607 input1->x, input1->len,
4608 input2->x, input2->len,
4609 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004610 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004611
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004612 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004613 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004614 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004615
4616 /* Expansion phase. */
4617 while( current_capacity > 0 )
4618 {
4619 size_t read_size = sizeof( output_buffer );
4620 if( read_size > current_capacity )
4621 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004622 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004623 output_buffer,
4624 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004625 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004626 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004627 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004628 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004629 }
4630
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004631 /* Check that the operation refuses to go over capacity. */
4632 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004633 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004634
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004635 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004636
4637exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004638 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004639 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004640 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004641}
4642/* END_CASE */
4643
Janos Follathe60c9052019-07-03 13:51:30 +01004644/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004645void derive_key_exercise( int alg_arg,
4646 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004647 data_t *input1,
4648 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004649 int derived_type_arg,
4650 int derived_bits_arg,
4651 int derived_usage_arg,
4652 int derived_alg_arg )
4653{
Ronald Cron5425a212020-08-04 14:58:35 +02004654 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4655 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004656 psa_algorithm_t alg = alg_arg;
4657 psa_key_type_t derived_type = derived_type_arg;
4658 size_t derived_bits = derived_bits_arg;
4659 psa_key_usage_t derived_usage = derived_usage_arg;
4660 psa_algorithm_t derived_alg = derived_alg_arg;
4661 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004662 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004663 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004664 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004665
Gilles Peskine8817f612018-12-18 00:18:46 +01004666 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004667
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004668 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4669 psa_set_key_algorithm( &attributes, alg );
4670 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004671 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004672 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004673
4674 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004675 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4676 input1->x, input1->len,
4677 input2->x, input2->len,
4678 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004679 goto exit;
4680
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004681 psa_set_key_usage_flags( &attributes, derived_usage );
4682 psa_set_key_algorithm( &attributes, derived_alg );
4683 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004684 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004685 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004686 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004687
4688 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004689 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004690 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4691 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004692
4693 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004694 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004695 goto exit;
4696
4697exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004698 /*
4699 * Key attributes may have been returned by psa_get_key_attributes()
4700 * thus reset them as required.
4701 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004702 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004703
4704 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004705 psa_destroy_key( base_key );
4706 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004707 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004708}
4709/* END_CASE */
4710
Janos Follath42fd8882019-07-03 14:17:09 +01004711/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004712void derive_key_export( int alg_arg,
4713 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004714 data_t *input1,
4715 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004716 int bytes1_arg,
4717 int bytes2_arg )
4718{
Ronald Cron5425a212020-08-04 14:58:35 +02004719 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4720 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004721 psa_algorithm_t alg = alg_arg;
4722 size_t bytes1 = bytes1_arg;
4723 size_t bytes2 = bytes2_arg;
4724 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004725 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004726 uint8_t *output_buffer = NULL;
4727 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004728 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4729 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004730 size_t length;
4731
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004732 ASSERT_ALLOC( output_buffer, capacity );
4733 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004734 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004735
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004736 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4737 psa_set_key_algorithm( &base_attributes, alg );
4738 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004739 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004740 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004741
4742 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004743 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4744 input1->x, input1->len,
4745 input2->x, input2->len,
4746 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004747 goto exit;
4748
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004749 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004750 output_buffer,
4751 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004752 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004753
4754 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004755 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4756 input1->x, input1->len,
4757 input2->x, input2->len,
4758 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004759 goto exit;
4760
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004761 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4762 psa_set_key_algorithm( &derived_attributes, 0 );
4763 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004764 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004765 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004766 &derived_key ) );
4767 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004768 export_buffer, bytes1,
4769 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004770 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004771 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004772 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004773 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004774 &derived_key ) );
4775 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004776 export_buffer + bytes1, bytes2,
4777 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004778 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004779
4780 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004781 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4782 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004783
4784exit:
4785 mbedtls_free( output_buffer );
4786 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004787 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004788 psa_destroy_key( base_key );
4789 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004790 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004791}
4792/* END_CASE */
4793
4794/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004795void derive_key( int alg_arg,
4796 data_t *key_data, data_t *input1, data_t *input2,
4797 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004798 int expected_status_arg,
4799 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004800{
Ronald Cron5425a212020-08-04 14:58:35 +02004801 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4802 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004803 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004804 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004805 size_t bits = bits_arg;
4806 psa_status_t expected_status = expected_status_arg;
4807 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4808 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4809 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4810
4811 PSA_ASSERT( psa_crypto_init( ) );
4812
4813 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4814 psa_set_key_algorithm( &base_attributes, alg );
4815 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4816 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004817 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004818
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004819 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4820 input1->x, input1->len,
4821 input2->x, input2->len,
4822 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004823 goto exit;
4824
4825 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4826 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004827 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004828 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004829
4830 psa_status_t status =
4831 psa_key_derivation_output_key( &derived_attributes,
4832 &operation,
4833 &derived_key );
4834 if( is_large_output > 0 )
4835 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4836 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004837
4838exit:
4839 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004840 psa_destroy_key( base_key );
4841 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004842 PSA_DONE( );
4843}
4844/* END_CASE */
4845
4846/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004847void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004848 int our_key_type_arg, int our_key_alg_arg,
4849 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004850 int expected_status_arg )
4851{
Ronald Cron5425a212020-08-04 14:58:35 +02004852 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004853 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004854 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004855 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004856 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004857 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004858 psa_status_t expected_status = expected_status_arg;
4859 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004860
Gilles Peskine8817f612018-12-18 00:18:46 +01004861 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004862
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004863 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004864 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004865 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004866 PSA_ASSERT( psa_import_key( &attributes,
4867 our_key_data->x, our_key_data->len,
4868 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004869
Gilles Peskine77f40d82019-04-11 21:27:06 +02004870 /* The tests currently include inputs that should fail at either step.
4871 * Test cases that fail at the setup step should be changed to call
4872 * key_derivation_setup instead, and this function should be renamed
4873 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004874 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004875 if( status == PSA_SUCCESS )
4876 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004877 TEST_EQUAL( psa_key_derivation_key_agreement(
4878 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4879 our_key,
4880 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004881 expected_status );
4882 }
4883 else
4884 {
4885 TEST_ASSERT( status == expected_status );
4886 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004887
4888exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004889 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004890 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004891 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004892}
4893/* END_CASE */
4894
4895/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004896void raw_key_agreement( int alg_arg,
4897 int our_key_type_arg, data_t *our_key_data,
4898 data_t *peer_key_data,
4899 data_t *expected_output )
4900{
Ronald Cron5425a212020-08-04 14:58:35 +02004901 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004902 psa_algorithm_t alg = alg_arg;
4903 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004904 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004905 unsigned char *output = NULL;
4906 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004907 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004908
4909 ASSERT_ALLOC( output, expected_output->len );
4910 PSA_ASSERT( psa_crypto_init( ) );
4911
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004912 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4913 psa_set_key_algorithm( &attributes, alg );
4914 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004915 PSA_ASSERT( psa_import_key( &attributes,
4916 our_key_data->x, our_key_data->len,
4917 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004918
gabor-mezei-armceface22021-01-21 12:26:17 +01004919 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4920 key_bits = psa_get_key_bits( &attributes );
4921
Gilles Peskinebe697d82019-05-16 18:00:41 +02004922 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4923 peer_key_data->x, peer_key_data->len,
4924 output, expected_output->len,
4925 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004926 ASSERT_COMPARE( output, output_length,
4927 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01004928 TEST_ASSERT( output_length <=
4929 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
4930 TEST_ASSERT( output_length <=
4931 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004932
4933exit:
4934 mbedtls_free( output );
4935 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004936 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004937}
4938/* END_CASE */
4939
4940/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004941void key_agreement_capacity( int alg_arg,
4942 int our_key_type_arg, data_t *our_key_data,
4943 data_t *peer_key_data,
4944 int expected_capacity_arg )
4945{
Ronald Cron5425a212020-08-04 14:58:35 +02004946 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004947 psa_algorithm_t alg = alg_arg;
4948 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004949 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004950 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004951 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004952 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004953
Gilles Peskine8817f612018-12-18 00:18:46 +01004954 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004955
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004956 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4957 psa_set_key_algorithm( &attributes, alg );
4958 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004959 PSA_ASSERT( psa_import_key( &attributes,
4960 our_key_data->x, our_key_data->len,
4961 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004962
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004963 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004964 PSA_ASSERT( psa_key_derivation_key_agreement(
4965 &operation,
4966 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4967 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004968 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4969 {
4970 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004971 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004972 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004973 NULL, 0 ) );
4974 }
Gilles Peskine59685592018-09-18 12:11:34 +02004975
Gilles Peskinebf491972018-10-25 22:36:12 +02004976 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004977 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004978 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004979 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004980
Gilles Peskinebf491972018-10-25 22:36:12 +02004981 /* Test the actual capacity by reading the output. */
4982 while( actual_capacity > sizeof( output ) )
4983 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004984 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004985 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004986 actual_capacity -= sizeof( output );
4987 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004988 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004989 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004990 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004991 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004992
Gilles Peskine59685592018-09-18 12:11:34 +02004993exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004994 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004995 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004996 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004997}
4998/* END_CASE */
4999
5000/* BEGIN_CASE */
5001void key_agreement_output( int alg_arg,
5002 int our_key_type_arg, data_t *our_key_data,
5003 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005004 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005005{
Ronald Cron5425a212020-08-04 14:58:35 +02005006 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005007 psa_algorithm_t alg = alg_arg;
5008 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005009 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005010 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005011 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005012
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005013 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5014 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005015
Gilles Peskine8817f612018-12-18 00:18:46 +01005016 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005017
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005018 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5019 psa_set_key_algorithm( &attributes, alg );
5020 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005021 PSA_ASSERT( psa_import_key( &attributes,
5022 our_key_data->x, our_key_data->len,
5023 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005024
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005025 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005026 PSA_ASSERT( psa_key_derivation_key_agreement(
5027 &operation,
5028 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5029 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005030 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5031 {
5032 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005033 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005034 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005035 NULL, 0 ) );
5036 }
Gilles Peskine59685592018-09-18 12:11:34 +02005037
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005038 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005039 actual_output,
5040 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005041 ASSERT_COMPARE( actual_output, expected_output1->len,
5042 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005043 if( expected_output2->len != 0 )
5044 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005045 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005046 actual_output,
5047 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005048 ASSERT_COMPARE( actual_output, expected_output2->len,
5049 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005050 }
Gilles Peskine59685592018-09-18 12:11:34 +02005051
5052exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005053 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005054 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005055 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005056 mbedtls_free( actual_output );
5057}
5058/* END_CASE */
5059
5060/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005061void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005062{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005063 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005064 unsigned char *output = NULL;
5065 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005066 size_t i;
5067 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005068
Simon Butcher49f8e312020-03-03 15:51:50 +00005069 TEST_ASSERT( bytes_arg >= 0 );
5070
Gilles Peskine91892022021-02-08 19:50:26 +01005071 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005072 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005073
Gilles Peskine8817f612018-12-18 00:18:46 +01005074 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005075
Gilles Peskinea50d7392018-06-21 10:22:13 +02005076 /* Run several times, to ensure that every output byte will be
5077 * nonzero at least once with overwhelming probability
5078 * (2^(-8*number_of_runs)). */
5079 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005080 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005081 if( bytes != 0 )
5082 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005083 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005084
Gilles Peskinea50d7392018-06-21 10:22:13 +02005085 for( i = 0; i < bytes; i++ )
5086 {
5087 if( output[i] != 0 )
5088 ++changed[i];
5089 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005090 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005091
5092 /* Check that every byte was changed to nonzero at least once. This
5093 * validates that psa_generate_random is overwriting every byte of
5094 * the output buffer. */
5095 for( i = 0; i < bytes; i++ )
5096 {
5097 TEST_ASSERT( changed[i] != 0 );
5098 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005099
5100exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005101 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005102 mbedtls_free( output );
5103 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005104}
5105/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005106
5107/* BEGIN_CASE */
5108void generate_key( int type_arg,
5109 int bits_arg,
5110 int usage_arg,
5111 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005112 int expected_status_arg,
5113 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005114{
Ronald Cron5425a212020-08-04 14:58:35 +02005115 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005116 psa_key_type_t type = type_arg;
5117 psa_key_usage_t usage = usage_arg;
5118 size_t bits = bits_arg;
5119 psa_algorithm_t alg = alg_arg;
5120 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005121 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005122 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005123
Gilles Peskine8817f612018-12-18 00:18:46 +01005124 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005125
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005126 psa_set_key_usage_flags( &attributes, usage );
5127 psa_set_key_algorithm( &attributes, alg );
5128 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005129 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005130
5131 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005132 psa_status_t status = psa_generate_key( &attributes, &key );
5133
5134 if( is_large_key > 0 )
5135 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5136 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005137 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005138 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005139
5140 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005141 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005142 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5143 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005144
Gilles Peskine818ca122018-06-20 18:16:48 +02005145 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005146 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005147 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005148
5149exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005150 /*
5151 * Key attributes may have been returned by psa_get_key_attributes()
5152 * thus reset them as required.
5153 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005154 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005155
Ronald Cron5425a212020-08-04 14:58:35 +02005156 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005157 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005158}
5159/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005160
Ronald Cronee414c72021-03-18 18:50:08 +01005161/* 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 +02005162void generate_key_rsa( int bits_arg,
5163 data_t *e_arg,
5164 int expected_status_arg )
5165{
Ronald Cron5425a212020-08-04 14:58:35 +02005166 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005167 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005168 size_t bits = bits_arg;
5169 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5170 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5171 psa_status_t expected_status = expected_status_arg;
5172 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5173 uint8_t *exported = NULL;
5174 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005175 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005176 size_t exported_length = SIZE_MAX;
5177 uint8_t *e_read_buffer = NULL;
5178 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005179 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005180 size_t e_read_length = SIZE_MAX;
5181
5182 if( e_arg->len == 0 ||
5183 ( e_arg->len == 3 &&
5184 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5185 {
5186 is_default_public_exponent = 1;
5187 e_read_size = 0;
5188 }
5189 ASSERT_ALLOC( e_read_buffer, e_read_size );
5190 ASSERT_ALLOC( exported, exported_size );
5191
5192 PSA_ASSERT( psa_crypto_init( ) );
5193
5194 psa_set_key_usage_flags( &attributes, usage );
5195 psa_set_key_algorithm( &attributes, alg );
5196 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5197 e_arg->x, e_arg->len ) );
5198 psa_set_key_bits( &attributes, bits );
5199
5200 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005201 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005202 if( expected_status != PSA_SUCCESS )
5203 goto exit;
5204
5205 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005206 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005207 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5208 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5209 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5210 e_read_buffer, e_read_size,
5211 &e_read_length ) );
5212 if( is_default_public_exponent )
5213 TEST_EQUAL( e_read_length, 0 );
5214 else
5215 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5216
5217 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005218 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005219 goto exit;
5220
5221 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005222 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005223 exported, exported_size,
5224 &exported_length ) );
5225 {
5226 uint8_t *p = exported;
5227 uint8_t *end = exported + exported_length;
5228 size_t len;
5229 /* RSAPublicKey ::= SEQUENCE {
5230 * modulus INTEGER, -- n
5231 * publicExponent INTEGER } -- e
5232 */
5233 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005234 MBEDTLS_ASN1_SEQUENCE |
5235 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005236 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005237 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5238 MBEDTLS_ASN1_INTEGER ) );
5239 if( len >= 1 && p[0] == 0 )
5240 {
5241 ++p;
5242 --len;
5243 }
5244 if( e_arg->len == 0 )
5245 {
5246 TEST_EQUAL( len, 3 );
5247 TEST_EQUAL( p[0], 1 );
5248 TEST_EQUAL( p[1], 0 );
5249 TEST_EQUAL( p[2], 1 );
5250 }
5251 else
5252 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5253 }
5254
5255exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005256 /*
5257 * Key attributes may have been returned by psa_get_key_attributes() or
5258 * set by psa_set_key_domain_parameters() thus reset them as required.
5259 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005260 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005261
Ronald Cron5425a212020-08-04 14:58:35 +02005262 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005263 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005264 mbedtls_free( e_read_buffer );
5265 mbedtls_free( exported );
5266}
5267/* END_CASE */
5268
Darryl Greend49a4992018-06-18 17:27:26 +01005269/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005270void persistent_key_load_key_from_storage( data_t *data,
5271 int type_arg, int bits_arg,
5272 int usage_flags_arg, int alg_arg,
5273 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005274{
Ronald Cron71016a92020-08-28 19:01:50 +02005275 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005276 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005277 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5278 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005279 psa_key_type_t type = type_arg;
5280 size_t bits = bits_arg;
5281 psa_key_usage_t usage_flags = usage_flags_arg;
5282 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005283 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005284 unsigned char *first_export = NULL;
5285 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005286 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005287 size_t first_exported_length;
5288 size_t second_exported_length;
5289
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005290 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5291 {
5292 ASSERT_ALLOC( first_export, export_size );
5293 ASSERT_ALLOC( second_export, export_size );
5294 }
Darryl Greend49a4992018-06-18 17:27:26 +01005295
Gilles Peskine8817f612018-12-18 00:18:46 +01005296 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005297
Gilles Peskinec87af662019-05-15 16:12:22 +02005298 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005299 psa_set_key_usage_flags( &attributes, usage_flags );
5300 psa_set_key_algorithm( &attributes, alg );
5301 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005302 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005303
Darryl Green0c6575a2018-11-07 16:05:30 +00005304 switch( generation_method )
5305 {
5306 case IMPORT_KEY:
5307 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005308 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005309 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005310 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005311
Darryl Green0c6575a2018-11-07 16:05:30 +00005312 case GENERATE_KEY:
5313 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005314 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005315 break;
5316
5317 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005318#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005319 {
5320 /* Create base key */
5321 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5322 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5323 psa_set_key_usage_flags( &base_attributes,
5324 PSA_KEY_USAGE_DERIVE );
5325 psa_set_key_algorithm( &base_attributes, derive_alg );
5326 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005327 PSA_ASSERT( psa_import_key( &base_attributes,
5328 data->x, data->len,
5329 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005330 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005331 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005332 PSA_ASSERT( psa_key_derivation_input_key(
5333 &operation,
5334 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005335 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005336 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005337 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005338 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5339 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005340 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005341 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005342 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005343 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005344 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005345#else
5346 TEST_ASSUME( ! "KDF not supported in this configuration" );
5347#endif
5348 break;
5349
5350 default:
5351 TEST_ASSERT( ! "generation_method not implemented in test" );
5352 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005353 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005354 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005355
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005356 /* Export the key if permitted by the key policy. */
5357 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5358 {
Ronald Cron5425a212020-08-04 14:58:35 +02005359 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005360 first_export, export_size,
5361 &first_exported_length ) );
5362 if( generation_method == IMPORT_KEY )
5363 ASSERT_COMPARE( data->x, data->len,
5364 first_export, first_exported_length );
5365 }
Darryl Greend49a4992018-06-18 17:27:26 +01005366
5367 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005368 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005369 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005370 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005371
Darryl Greend49a4992018-06-18 17:27:26 +01005372 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005373 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005374 TEST_ASSERT( mbedtls_svc_key_id_equal(
5375 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005376 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5377 PSA_KEY_LIFETIME_PERSISTENT );
5378 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5379 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02005380 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02005381 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005382 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005383
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005384 /* Export the key again if permitted by the key policy. */
5385 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005386 {
Ronald Cron5425a212020-08-04 14:58:35 +02005387 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005388 second_export, export_size,
5389 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005390 ASSERT_COMPARE( first_export, first_exported_length,
5391 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005392 }
5393
5394 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005395 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005396 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005397
5398exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005399 /*
5400 * Key attributes may have been returned by psa_get_key_attributes()
5401 * thus reset them as required.
5402 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005403 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005404
Darryl Greend49a4992018-06-18 17:27:26 +01005405 mbedtls_free( first_export );
5406 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005407 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005408 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005409 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005410 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005411}
5412/* END_CASE */