blob: 5c44979cdaef6b64faeadd1ed150c7220158ec88 [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 );
325 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
326 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200327 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200328
Ronald Cron5425a212020-08-04 14:58:35 +0200329 PSA_ASSERT( psa_destroy_key( key ) );
330 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200331
332exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100333 /*
334 * Key attributes may have been returned by psa_get_key_attributes()
335 * thus reset them as required.
336 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200337 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100338
339 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200340 PSA_DONE( );
341}
342/* END_CASE */
343
344/* BEGIN_CASE */
345void import_with_data( data_t *data, int type_arg,
346 int attr_bits_arg,
347 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200348{
349 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
350 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200351 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200352 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200353 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200354 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100355 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100356
Gilles Peskine8817f612018-12-18 00:18:46 +0100357 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100358
Gilles Peskine4747d192019-04-17 15:05:45 +0200359 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200360 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200361
Ronald Cron5425a212020-08-04 14:58:35 +0200362 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100363 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200364 if( status != PSA_SUCCESS )
365 goto exit;
366
Ronald Cron5425a212020-08-04 14:58:35 +0200367 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200368 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200369 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200370 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200371 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200372
Ronald Cron5425a212020-08-04 14:58:35 +0200373 PSA_ASSERT( psa_destroy_key( key ) );
374 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100375
376exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100377 /*
378 * Key attributes may have been returned by psa_get_key_attributes()
379 * thus reset them as required.
380 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200381 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100382
383 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200384 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100385}
386/* END_CASE */
387
388/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200389void import_large_key( int type_arg, int byte_size_arg,
390 int expected_status_arg )
391{
392 psa_key_type_t type = type_arg;
393 size_t byte_size = byte_size_arg;
394 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
395 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200396 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200397 psa_status_t status;
398 uint8_t *buffer = NULL;
399 size_t buffer_size = byte_size + 1;
400 size_t n;
401
Steven Cooreman69967ce2021-01-18 18:01:08 +0100402 /* Skip the test case if the target running the test cannot
403 * accomodate large keys due to heap size constraints */
404 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200405 memset( buffer, 'K', byte_size );
406
407 PSA_ASSERT( psa_crypto_init( ) );
408
409 /* Try importing the key */
410 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
411 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200412 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100413 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200414 TEST_EQUAL( status, expected_status );
415
416 if( status == PSA_SUCCESS )
417 {
Ronald Cron5425a212020-08-04 14:58:35 +0200418 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200419 TEST_EQUAL( psa_get_key_type( &attributes ), type );
420 TEST_EQUAL( psa_get_key_bits( &attributes ),
421 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200422 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200423 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200424 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200425 for( n = 0; n < byte_size; n++ )
426 TEST_EQUAL( buffer[n], 'K' );
427 for( n = byte_size; n < buffer_size; n++ )
428 TEST_EQUAL( buffer[n], 0 );
429 }
430
431exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100432 /*
433 * Key attributes may have been returned by psa_get_key_attributes()
434 * thus reset them as required.
435 */
436 psa_reset_key_attributes( &attributes );
437
Ronald Cron5425a212020-08-04 14:58:35 +0200438 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200439 PSA_DONE( );
440 mbedtls_free( buffer );
441}
442/* END_CASE */
443
444/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200445void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
446{
Ronald Cron5425a212020-08-04 14:58:35 +0200447 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200448 size_t bits = bits_arg;
449 psa_status_t expected_status = expected_status_arg;
450 psa_status_t status;
451 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200452 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200453 size_t buffer_size = /* Slight overapproximations */
454 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200455 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200456 unsigned char *p;
457 int ret;
458 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200459 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200460
Gilles Peskine8817f612018-12-18 00:18:46 +0100461 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200462 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200463
464 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
465 bits, keypair ) ) >= 0 );
466 length = ret;
467
468 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200469 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200470 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100471 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200472
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200473 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200474 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200475
476exit:
477 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200478 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200479}
480/* END_CASE */
481
482/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300483void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300484 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200485 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100486 int expected_bits,
487 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200488 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100489 int canonical_input )
490{
Ronald Cron5425a212020-08-04 14:58:35 +0200491 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100492 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200493 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200494 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100495 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100496 unsigned char *exported = NULL;
497 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100498 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100499 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100500 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200501 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200502 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100503
Moran Pekercb088e72018-07-17 17:36:59 +0300504 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200505 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100506 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200507 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100508 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100509
Gilles Peskine4747d192019-04-17 15:05:45 +0200510 psa_set_key_usage_flags( &attributes, usage_arg );
511 psa_set_key_algorithm( &attributes, alg );
512 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700513
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100514 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200515 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100516
517 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200518 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200519 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
520 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200521 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100522
523 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200524 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100525 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100526
527 /* The exported length must be set by psa_export_key() to a value between 0
528 * and export_size. On errors, the exported length must be 0. */
529 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
530 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
531 TEST_ASSERT( exported_length <= export_size );
532
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200533 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200534 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100535 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200536 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100537 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100538 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200539 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100540
Gilles Peskineea38a922021-02-13 00:05:16 +0100541 /* Run sanity checks on the exported key. For non-canonical inputs,
542 * this validates the canonical representations. For canonical inputs,
543 * this doesn't directly validate the implementation, but it still helps
544 * by cross-validating the test data with the sanity check code. */
545 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200546 goto exit;
547
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100548 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200549 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100550 else
551 {
Ronald Cron5425a212020-08-04 14:58:35 +0200552 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200553 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200554 &key2 ) );
555 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100556 reexported,
557 export_size,
558 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200559 ASSERT_COMPARE( exported, exported_length,
560 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200561 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100562 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100563 TEST_ASSERT( exported_length <=
564 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
565 psa_get_key_bits( &got_attributes ) ) );
566 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100567
568destroy:
569 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200570 PSA_ASSERT( psa_destroy_key( key ) );
571 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100572
573exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100574 /*
575 * Key attributes may have been returned by psa_get_key_attributes()
576 * thus reset them as required.
577 */
578 psa_reset_key_attributes( &got_attributes );
579
itayzafrir3e02b3b2018-06-12 17:06:52 +0300580 mbedtls_free( exported );
581 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200582 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100583}
584/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100585
Moran Pekerf709f4a2018-06-06 17:26:04 +0300586/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300587void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200588 int type_arg,
589 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100590 int export_size_delta,
591 int expected_export_status_arg,
592 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300593{
Ronald Cron5425a212020-08-04 14:58:35 +0200594 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300595 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200596 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200597 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300598 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300599 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100600 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100601 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200602 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300603
Gilles Peskine8817f612018-12-18 00:18:46 +0100604 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300605
Gilles Peskine4747d192019-04-17 15:05:45 +0200606 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
607 psa_set_key_algorithm( &attributes, alg );
608 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300609
610 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200611 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300612
Gilles Peskine49c25912018-10-29 15:15:31 +0100613 /* Export the public key */
614 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200615 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200616 exported, export_size,
617 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100618 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100619 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100620 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200621 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100622 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200623 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200624 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100625 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100626 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100627 TEST_ASSERT( expected_public_key->len <=
628 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
629 TEST_ASSERT( expected_public_key->len <=
630 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100631 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
632 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100633 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300634
635exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100636 /*
637 * Key attributes may have been returned by psa_get_key_attributes()
638 * thus reset them as required.
639 */
640 psa_reset_key_attributes( &attributes );
641
itayzafrir3e02b3b2018-06-12 17:06:52 +0300642 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200643 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200644 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300645}
646/* END_CASE */
647
Gilles Peskine20035e32018-02-03 22:44:14 +0100648/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200649void import_and_exercise_key( data_t *data,
650 int type_arg,
651 int bits_arg,
652 int alg_arg )
653{
Ronald Cron5425a212020-08-04 14:58:35 +0200654 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200655 psa_key_type_t type = type_arg;
656 size_t bits = bits_arg;
657 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100658 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200659 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200660 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200661
Gilles Peskine8817f612018-12-18 00:18:46 +0100662 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200663
Gilles Peskine4747d192019-04-17 15:05:45 +0200664 psa_set_key_usage_flags( &attributes, usage );
665 psa_set_key_algorithm( &attributes, alg );
666 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200667
668 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200669 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200670
671 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200672 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200673 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
674 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200675
676 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100677 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200678 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200679
Ronald Cron5425a212020-08-04 14:58:35 +0200680 PSA_ASSERT( psa_destroy_key( key ) );
681 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200682
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200683exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100684 /*
685 * Key attributes may have been returned by psa_get_key_attributes()
686 * thus reset them as required.
687 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200688 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100689
690 psa_reset_key_attributes( &attributes );
691 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200692 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200693}
694/* END_CASE */
695
696/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100697void effective_key_attributes( int type_arg, int expected_type_arg,
698 int bits_arg, int expected_bits_arg,
699 int usage_arg, int expected_usage_arg,
700 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200701{
Ronald Cron5425a212020-08-04 14:58:35 +0200702 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100703 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100704 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100705 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100706 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200707 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100708 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200709 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100710 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200711 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200712
Gilles Peskine8817f612018-12-18 00:18:46 +0100713 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200714
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200715 psa_set_key_usage_flags( &attributes, usage );
716 psa_set_key_algorithm( &attributes, alg );
717 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +0100718 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +0200719
Ronald Cron5425a212020-08-04 14:58:35 +0200720 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +0100721 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +0200722
Ronald Cron5425a212020-08-04 14:58:35 +0200723 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +0100724 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
725 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
726 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
727 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200728
729exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100730 /*
731 * Key attributes may have been returned by psa_get_key_attributes()
732 * thus reset them as required.
733 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200734 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100735
736 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200737 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +0200738}
739/* END_CASE */
740
741/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100742void check_key_policy( int type_arg, int bits_arg,
743 int usage_arg, int alg_arg )
744{
745 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
746 usage_arg, usage_arg, alg_arg, alg_arg );
747 goto exit;
748}
749/* END_CASE */
750
751/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200752void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +0000753{
754 /* Test each valid way of initializing the object, except for `= {0}`, as
755 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
756 * though it's OK by the C standard. We could test for this, but we'd need
757 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200758 psa_key_attributes_t func = psa_key_attributes_init( );
759 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
760 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000761
762 memset( &zero, 0, sizeof( zero ) );
763
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200764 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
765 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
766 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000767
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200768 TEST_EQUAL( psa_get_key_type( &func ), 0 );
769 TEST_EQUAL( psa_get_key_type( &init ), 0 );
770 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
771
772 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
773 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
774 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
775
776 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
777 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
778 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
779
780 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
781 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
782 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +0000783}
784/* END_CASE */
785
786/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200787void mac_key_policy( int policy_usage,
788 int policy_alg,
789 int key_type,
790 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100791 int exercise_alg,
792 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200793{
Ronald Cron5425a212020-08-04 14:58:35 +0200794 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200795 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000796 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200797 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100798 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200799 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200800
Gilles Peskine8817f612018-12-18 00:18:46 +0100801 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200802
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200803 psa_set_key_usage_flags( &attributes, policy_usage );
804 psa_set_key_algorithm( &attributes, policy_alg );
805 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200806
Gilles Peskine049c7532019-05-15 20:22:09 +0200807 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200808 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200809
Ronald Cron5425a212020-08-04 14:58:35 +0200810 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100811 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100812 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100813 else
814 TEST_EQUAL( status, expected_status );
815
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200816 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200817
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200818 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200819 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100820 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100821 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100822 else
823 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200824
825exit:
826 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200827 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200828 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200829}
830/* END_CASE */
831
832/* BEGIN_CASE */
833void cipher_key_policy( int policy_usage,
834 int policy_alg,
835 int key_type,
836 data_t *key_data,
837 int exercise_alg )
838{
Ronald Cron5425a212020-08-04 14:58:35 +0200839 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200840 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000841 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200842 psa_status_t status;
843
Gilles Peskine8817f612018-12-18 00:18:46 +0100844 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200845
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200846 psa_set_key_usage_flags( &attributes, policy_usage );
847 psa_set_key_algorithm( &attributes, policy_alg );
848 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200849
Gilles Peskine049c7532019-05-15 20:22:09 +0200850 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200851 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200852
Ronald Cron5425a212020-08-04 14:58:35 +0200853 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200854 if( policy_alg == exercise_alg &&
855 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100856 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200857 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100858 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200859 psa_cipher_abort( &operation );
860
Ronald Cron5425a212020-08-04 14:58:35 +0200861 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200862 if( policy_alg == exercise_alg &&
863 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100864 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200865 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100866 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200867
868exit:
869 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200870 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200871 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200872}
873/* END_CASE */
874
875/* BEGIN_CASE */
876void aead_key_policy( int policy_usage,
877 int policy_alg,
878 int key_type,
879 data_t *key_data,
880 int nonce_length_arg,
881 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100882 int exercise_alg,
883 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200884{
Ronald Cron5425a212020-08-04 14:58:35 +0200885 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200886 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200887 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100888 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200889 unsigned char nonce[16] = {0};
890 size_t nonce_length = nonce_length_arg;
891 unsigned char tag[16];
892 size_t tag_length = tag_length_arg;
893 size_t output_length;
894
895 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
896 TEST_ASSERT( tag_length <= sizeof( tag ) );
897
Gilles Peskine8817f612018-12-18 00:18:46 +0100898 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200899
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200900 psa_set_key_usage_flags( &attributes, policy_usage );
901 psa_set_key_algorithm( &attributes, policy_alg );
902 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200903
Gilles Peskine049c7532019-05-15 20:22:09 +0200904 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200905 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200906
Ronald Cron5425a212020-08-04 14:58:35 +0200907 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200908 nonce, nonce_length,
909 NULL, 0,
910 NULL, 0,
911 tag, tag_length,
912 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100913 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
914 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200915 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100916 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200917
918 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200919 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200920 nonce, nonce_length,
921 NULL, 0,
922 tag, tag_length,
923 NULL, 0,
924 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100925 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
926 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
927 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100928 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200929 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100930 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200931
932exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200933 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200934 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200935}
936/* END_CASE */
937
938/* BEGIN_CASE */
939void asymmetric_encryption_key_policy( int policy_usage,
940 int policy_alg,
941 int key_type,
942 data_t *key_data,
943 int exercise_alg )
944{
Ronald Cron5425a212020-08-04 14:58:35 +0200945 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200946 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200947 psa_status_t status;
948 size_t key_bits;
949 size_t buffer_length;
950 unsigned char *buffer = NULL;
951 size_t output_length;
952
Gilles Peskine8817f612018-12-18 00:18:46 +0100953 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200954
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200955 psa_set_key_usage_flags( &attributes, policy_usage );
956 psa_set_key_algorithm( &attributes, policy_alg );
957 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200958
Gilles Peskine049c7532019-05-15 20:22:09 +0200959 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200960 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200961
Ronald Cron5425a212020-08-04 14:58:35 +0200962 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200963 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200964 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
965 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200966 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200967
Ronald Cron5425a212020-08-04 14:58:35 +0200968 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200969 NULL, 0,
970 NULL, 0,
971 buffer, buffer_length,
972 &output_length );
973 if( policy_alg == exercise_alg &&
974 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100975 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200976 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100977 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200978
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +0200979 if( buffer_length != 0 )
980 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200981 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200982 buffer, buffer_length,
983 NULL, 0,
984 buffer, buffer_length,
985 &output_length );
986 if( policy_alg == exercise_alg &&
987 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100988 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200989 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100990 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200991
992exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100993 /*
994 * Key attributes may have been returned by psa_get_key_attributes()
995 * thus reset them as required.
996 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200997 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100998
999 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001000 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001001 mbedtls_free( buffer );
1002}
1003/* END_CASE */
1004
1005/* BEGIN_CASE */
1006void asymmetric_signature_key_policy( int policy_usage,
1007 int policy_alg,
1008 int key_type,
1009 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001010 int exercise_alg,
1011 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001012{
Ronald Cron5425a212020-08-04 14:58:35 +02001013 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001014 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001015 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001016 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1017 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1018 * compatible with the policy and `payload_length_arg` is supposed to be
1019 * a valid input length to sign. If `payload_length_arg <= 0`,
1020 * `exercise_alg` is supposed to be forbidden by the policy. */
1021 int compatible_alg = payload_length_arg > 0;
1022 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001023 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001024 size_t signature_length;
1025
Gilles Peskine8817f612018-12-18 00:18:46 +01001026 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001027
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001028 psa_set_key_usage_flags( &attributes, policy_usage );
1029 psa_set_key_algorithm( &attributes, policy_alg );
1030 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001031
Gilles Peskine049c7532019-05-15 20:22:09 +02001032 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001033 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001034
Ronald Cron5425a212020-08-04 14:58:35 +02001035 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001036 payload, payload_length,
1037 signature, sizeof( signature ),
1038 &signature_length );
1039 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001040 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001041 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001042 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001043
1044 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001045 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001046 payload, payload_length,
1047 signature, sizeof( signature ) );
1048 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001049 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001050 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001051 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001052
1053exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001054 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001055 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001056}
1057/* END_CASE */
1058
Janos Follathba3fab92019-06-11 14:50:16 +01001059/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001060void derive_key_policy( int policy_usage,
1061 int policy_alg,
1062 int key_type,
1063 data_t *key_data,
1064 int exercise_alg )
1065{
Ronald Cron5425a212020-08-04 14:58:35 +02001066 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001067 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001068 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001069 psa_status_t status;
1070
Gilles Peskine8817f612018-12-18 00:18:46 +01001071 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001072
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001073 psa_set_key_usage_flags( &attributes, policy_usage );
1074 psa_set_key_algorithm( &attributes, policy_alg );
1075 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001076
Gilles Peskine049c7532019-05-15 20:22:09 +02001077 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001078 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001079
Janos Follathba3fab92019-06-11 14:50:16 +01001080 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1081
1082 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1083 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001084 {
Janos Follathba3fab92019-06-11 14:50:16 +01001085 PSA_ASSERT( psa_key_derivation_input_bytes(
1086 &operation,
1087 PSA_KEY_DERIVATION_INPUT_SEED,
1088 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001089 }
Janos Follathba3fab92019-06-11 14:50:16 +01001090
1091 status = psa_key_derivation_input_key( &operation,
1092 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001093 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001094
Gilles Peskineea0fb492018-07-12 17:17:20 +02001095 if( policy_alg == exercise_alg &&
1096 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001097 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001098 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001099 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001100
1101exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001102 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001103 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001104 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001105}
1106/* END_CASE */
1107
1108/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001109void agreement_key_policy( int policy_usage,
1110 int policy_alg,
1111 int key_type_arg,
1112 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001113 int exercise_alg,
1114 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001115{
Ronald Cron5425a212020-08-04 14:58:35 +02001116 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001117 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001118 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001119 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001120 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001121 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001122
Gilles Peskine8817f612018-12-18 00:18:46 +01001123 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001124
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001125 psa_set_key_usage_flags( &attributes, policy_usage );
1126 psa_set_key_algorithm( &attributes, policy_alg );
1127 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001128
Gilles Peskine049c7532019-05-15 20:22:09 +02001129 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001130 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001131
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001132 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001133 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001134
Steven Cooremance48e852020-10-05 16:02:45 +02001135 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001136
1137exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001138 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001139 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001140 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001141}
1142/* END_CASE */
1143
1144/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001145void key_policy_alg2( int key_type_arg, data_t *key_data,
1146 int usage_arg, int alg_arg, int alg2_arg )
1147{
Ronald Cron5425a212020-08-04 14:58:35 +02001148 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001149 psa_key_type_t key_type = key_type_arg;
1150 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1151 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1152 psa_key_usage_t usage = usage_arg;
1153 psa_algorithm_t alg = alg_arg;
1154 psa_algorithm_t alg2 = alg2_arg;
1155
1156 PSA_ASSERT( psa_crypto_init( ) );
1157
1158 psa_set_key_usage_flags( &attributes, usage );
1159 psa_set_key_algorithm( &attributes, alg );
1160 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1161 psa_set_key_type( &attributes, key_type );
1162 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001163 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001164
Ronald Cron5425a212020-08-04 14:58:35 +02001165 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001166 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1167 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1168 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1169
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001170 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001171 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001172 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001173 goto exit;
1174
1175exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001176 /*
1177 * Key attributes may have been returned by psa_get_key_attributes()
1178 * thus reset them as required.
1179 */
1180 psa_reset_key_attributes( &got_attributes );
1181
Ronald Cron5425a212020-08-04 14:58:35 +02001182 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001183 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001184}
1185/* END_CASE */
1186
1187/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001188void raw_agreement_key_policy( int policy_usage,
1189 int policy_alg,
1190 int key_type_arg,
1191 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001192 int exercise_alg,
1193 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001194{
Ronald Cron5425a212020-08-04 14:58:35 +02001195 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001196 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001197 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001198 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001199 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001200 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001201
1202 PSA_ASSERT( psa_crypto_init( ) );
1203
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001204 psa_set_key_usage_flags( &attributes, policy_usage );
1205 psa_set_key_algorithm( &attributes, policy_alg );
1206 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001207
Gilles Peskine049c7532019-05-15 20:22:09 +02001208 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001209 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001210
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001211 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001212
Steven Cooremance48e852020-10-05 16:02:45 +02001213 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001214
1215exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001216 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001217 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001218 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001219}
1220/* END_CASE */
1221
1222/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001223void copy_success( int source_usage_arg,
1224 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001225 int type_arg, data_t *material,
1226 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001227 int target_usage_arg,
1228 int target_alg_arg, int target_alg2_arg,
1229 int expected_usage_arg,
1230 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001231{
Gilles Peskineca25db92019-04-19 11:43:08 +02001232 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1233 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001234 psa_key_usage_t expected_usage = expected_usage_arg;
1235 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001236 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001237 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1238 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001239 uint8_t *export_buffer = NULL;
1240
Gilles Peskine57ab7212019-01-28 13:03:09 +01001241 PSA_ASSERT( psa_crypto_init( ) );
1242
Gilles Peskineca25db92019-04-19 11:43:08 +02001243 /* Prepare the source key. */
1244 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1245 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001246 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001247 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001248 PSA_ASSERT( psa_import_key( &source_attributes,
1249 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001250 &source_key ) );
1251 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001252
Gilles Peskineca25db92019-04-19 11:43:08 +02001253 /* Prepare the target attributes. */
1254 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001255 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001256 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001257 /* Set volatile lifetime to reset the key identifier to 0. */
1258 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1259 }
1260
Gilles Peskineca25db92019-04-19 11:43:08 +02001261 if( target_usage_arg != -1 )
1262 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1263 if( target_alg_arg != -1 )
1264 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001265 if( target_alg2_arg != -1 )
1266 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001267
1268 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001269 PSA_ASSERT( psa_copy_key( source_key,
1270 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001271
1272 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001273 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001274
1275 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001276 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001277 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1278 psa_get_key_type( &target_attributes ) );
1279 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1280 psa_get_key_bits( &target_attributes ) );
1281 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1282 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001283 TEST_EQUAL( expected_alg2,
1284 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001285 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1286 {
1287 size_t length;
1288 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001289 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001290 material->len, &length ) );
1291 ASSERT_COMPARE( material->x, material->len,
1292 export_buffer, length );
1293 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001294
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001295 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001296 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001297 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001298 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001299
Ronald Cron5425a212020-08-04 14:58:35 +02001300 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001301
1302exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001303 /*
1304 * Source and target key attributes may have been returned by
1305 * psa_get_key_attributes() thus reset them as required.
1306 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001307 psa_reset_key_attributes( &source_attributes );
1308 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001309
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001310 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001311 mbedtls_free( export_buffer );
1312}
1313/* END_CASE */
1314
1315/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001316void copy_fail( int source_usage_arg,
1317 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001318 int type_arg, data_t *material,
1319 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001320 int target_usage_arg,
1321 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001322 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001323 int expected_status_arg )
1324{
1325 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1326 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001327 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1328 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001329 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001330
1331 PSA_ASSERT( psa_crypto_init( ) );
1332
1333 /* Prepare the source key. */
1334 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1335 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001336 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001337 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001338 PSA_ASSERT( psa_import_key( &source_attributes,
1339 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001340 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001341
1342 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001343 psa_set_key_id( &target_attributes, key_id );
1344 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001345 psa_set_key_type( &target_attributes, target_type_arg );
1346 psa_set_key_bits( &target_attributes, target_bits_arg );
1347 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1348 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001349 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001350
1351 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001352 TEST_EQUAL( psa_copy_key( source_key,
1353 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001354 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001355
Ronald Cron5425a212020-08-04 14:58:35 +02001356 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001357
Gilles Peskine4a644642019-05-03 17:14:08 +02001358exit:
1359 psa_reset_key_attributes( &source_attributes );
1360 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001361 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001362}
1363/* END_CASE */
1364
1365/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001366void hash_operation_init( )
1367{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001368 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001369 /* Test each valid way of initializing the object, except for `= {0}`, as
1370 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1371 * though it's OK by the C standard. We could test for this, but we'd need
1372 * to supress the Clang warning for the test. */
1373 psa_hash_operation_t func = psa_hash_operation_init( );
1374 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1375 psa_hash_operation_t zero;
1376
1377 memset( &zero, 0, sizeof( zero ) );
1378
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001379 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001380 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1381 PSA_ERROR_BAD_STATE );
1382 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1383 PSA_ERROR_BAD_STATE );
1384 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1385 PSA_ERROR_BAD_STATE );
1386
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001387 /* A default hash operation should be abortable without error. */
1388 PSA_ASSERT( psa_hash_abort( &func ) );
1389 PSA_ASSERT( psa_hash_abort( &init ) );
1390 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001391}
1392/* END_CASE */
1393
1394/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001395void hash_setup( int alg_arg,
1396 int expected_status_arg )
1397{
1398 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001399 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001400 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001401 psa_status_t status;
1402
Gilles Peskine8817f612018-12-18 00:18:46 +01001403 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001404
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001405 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001406 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001407
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001408 /* Whether setup succeeded or failed, abort must succeed. */
1409 PSA_ASSERT( psa_hash_abort( &operation ) );
1410
1411 /* If setup failed, reproduce the failure, so as to
1412 * test the resulting state of the operation object. */
1413 if( status != PSA_SUCCESS )
1414 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1415
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001416 /* Now the operation object should be reusable. */
1417#if defined(KNOWN_SUPPORTED_HASH_ALG)
1418 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1419 PSA_ASSERT( psa_hash_abort( &operation ) );
1420#endif
1421
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001422exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001423 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001424}
1425/* END_CASE */
1426
1427/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001428void hash_compute_fail( int alg_arg, data_t *input,
1429 int output_size_arg, int expected_status_arg )
1430{
1431 psa_algorithm_t alg = alg_arg;
1432 uint8_t *output = NULL;
1433 size_t output_size = output_size_arg;
1434 size_t output_length = INVALID_EXPORT_LENGTH;
1435 psa_status_t expected_status = expected_status_arg;
1436 psa_status_t status;
1437
1438 ASSERT_ALLOC( output, output_size );
1439
1440 PSA_ASSERT( psa_crypto_init( ) );
1441
1442 status = psa_hash_compute( alg, input->x, input->len,
1443 output, output_size, &output_length );
1444 TEST_EQUAL( status, expected_status );
1445 TEST_ASSERT( output_length <= output_size );
1446
1447exit:
1448 mbedtls_free( output );
1449 PSA_DONE( );
1450}
1451/* END_CASE */
1452
1453/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001454void hash_compare_fail( int alg_arg, data_t *input,
1455 data_t *reference_hash,
1456 int expected_status_arg )
1457{
1458 psa_algorithm_t alg = alg_arg;
1459 psa_status_t expected_status = expected_status_arg;
1460 psa_status_t status;
1461
1462 PSA_ASSERT( psa_crypto_init( ) );
1463
1464 status = psa_hash_compare( alg, input->x, input->len,
1465 reference_hash->x, reference_hash->len );
1466 TEST_EQUAL( status, expected_status );
1467
1468exit:
1469 PSA_DONE( );
1470}
1471/* END_CASE */
1472
1473/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001474void hash_compute_compare( int alg_arg, data_t *input,
1475 data_t *expected_output )
1476{
1477 psa_algorithm_t alg = alg_arg;
1478 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1479 size_t output_length = INVALID_EXPORT_LENGTH;
1480 size_t i;
1481
1482 PSA_ASSERT( psa_crypto_init( ) );
1483
1484 /* Compute with tight buffer */
1485 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001486 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001487 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001488 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001489 ASSERT_COMPARE( output, output_length,
1490 expected_output->x, expected_output->len );
1491
1492 /* Compute with larger buffer */
1493 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1494 output, sizeof( output ),
1495 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001496 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001497 ASSERT_COMPARE( output, output_length,
1498 expected_output->x, expected_output->len );
1499
1500 /* Compare with correct hash */
1501 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1502 output, output_length ) );
1503
1504 /* Compare with trailing garbage */
1505 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1506 output, output_length + 1 ),
1507 PSA_ERROR_INVALID_SIGNATURE );
1508
1509 /* Compare with truncated hash */
1510 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1511 output, output_length - 1 ),
1512 PSA_ERROR_INVALID_SIGNATURE );
1513
1514 /* Compare with corrupted value */
1515 for( i = 0; i < output_length; i++ )
1516 {
Chris Jones9634bb12021-01-20 15:56:42 +00001517 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001518 output[i] ^= 1;
1519 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1520 output, output_length ),
1521 PSA_ERROR_INVALID_SIGNATURE );
1522 output[i] ^= 1;
1523 }
1524
1525exit:
1526 PSA_DONE( );
1527}
1528/* END_CASE */
1529
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001530/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001531void hash_bad_order( )
1532{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001533 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001534 unsigned char input[] = "";
1535 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001536 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001537 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1538 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1539 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001540 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001541 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001542 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001543
Gilles Peskine8817f612018-12-18 00:18:46 +01001544 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001545
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001546 /* Call setup twice in a row. */
1547 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1548 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1549 PSA_ERROR_BAD_STATE );
1550 PSA_ASSERT( psa_hash_abort( &operation ) );
1551
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001552 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001553 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001554 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001555 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001556
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001557 /* Call update after finish. */
1558 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1559 PSA_ASSERT( psa_hash_finish( &operation,
1560 hash, sizeof( hash ), &hash_len ) );
1561 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001562 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001563 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001564
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001565 /* Call verify without calling setup beforehand. */
1566 TEST_EQUAL( psa_hash_verify( &operation,
1567 valid_hash, sizeof( valid_hash ) ),
1568 PSA_ERROR_BAD_STATE );
1569 PSA_ASSERT( psa_hash_abort( &operation ) );
1570
1571 /* Call verify after finish. */
1572 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1573 PSA_ASSERT( psa_hash_finish( &operation,
1574 hash, sizeof( hash ), &hash_len ) );
1575 TEST_EQUAL( psa_hash_verify( &operation,
1576 valid_hash, sizeof( valid_hash ) ),
1577 PSA_ERROR_BAD_STATE );
1578 PSA_ASSERT( psa_hash_abort( &operation ) );
1579
1580 /* Call verify twice in a row. */
1581 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1582 PSA_ASSERT( psa_hash_verify( &operation,
1583 valid_hash, sizeof( valid_hash ) ) );
1584 TEST_EQUAL( psa_hash_verify( &operation,
1585 valid_hash, sizeof( valid_hash ) ),
1586 PSA_ERROR_BAD_STATE );
1587 PSA_ASSERT( psa_hash_abort( &operation ) );
1588
1589 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001590 TEST_EQUAL( psa_hash_finish( &operation,
1591 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001592 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001593 PSA_ASSERT( psa_hash_abort( &operation ) );
1594
1595 /* Call finish twice in a row. */
1596 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1597 PSA_ASSERT( psa_hash_finish( &operation,
1598 hash, sizeof( hash ), &hash_len ) );
1599 TEST_EQUAL( psa_hash_finish( &operation,
1600 hash, sizeof( hash ), &hash_len ),
1601 PSA_ERROR_BAD_STATE );
1602 PSA_ASSERT( psa_hash_abort( &operation ) );
1603
1604 /* Call finish after calling verify. */
1605 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1606 PSA_ASSERT( psa_hash_verify( &operation,
1607 valid_hash, sizeof( valid_hash ) ) );
1608 TEST_EQUAL( psa_hash_finish( &operation,
1609 hash, sizeof( hash ), &hash_len ),
1610 PSA_ERROR_BAD_STATE );
1611 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001612
1613exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001614 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001615}
1616/* END_CASE */
1617
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001618/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001619void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001620{
1621 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001622 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1623 * appended to it */
1624 unsigned char hash[] = {
1625 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1626 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1627 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001628 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001629 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001630
Gilles Peskine8817f612018-12-18 00:18:46 +01001631 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001632
itayzafrir27e69452018-11-01 14:26:34 +02001633 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001634 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001635 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001636 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001637
itayzafrir27e69452018-11-01 14:26:34 +02001638 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001639 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001640 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001641 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001642
itayzafrir27e69452018-11-01 14:26:34 +02001643 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001644 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001645 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001646 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001647
itayzafrirec93d302018-10-18 18:01:10 +03001648exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001649 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001650}
1651/* END_CASE */
1652
Ronald Cronee414c72021-03-18 18:50:08 +01001653/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001654void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001655{
1656 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001657 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001658 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001659 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001660 size_t hash_len;
1661
Gilles Peskine8817f612018-12-18 00:18:46 +01001662 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001663
itayzafrir58028322018-10-25 10:22:01 +03001664 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001665 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001666 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001667 hash, expected_size - 1, &hash_len ),
1668 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001669
1670exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001671 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001672}
1673/* END_CASE */
1674
Ronald Cronee414c72021-03-18 18:50:08 +01001675/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001676void hash_clone_source_state( )
1677{
1678 psa_algorithm_t alg = PSA_ALG_SHA_256;
1679 unsigned char hash[PSA_HASH_MAX_SIZE];
1680 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1681 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1682 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1683 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1684 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1685 size_t hash_len;
1686
1687 PSA_ASSERT( psa_crypto_init( ) );
1688 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1689
1690 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1691 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1692 PSA_ASSERT( psa_hash_finish( &op_finished,
1693 hash, sizeof( hash ), &hash_len ) );
1694 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1695 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1696
1697 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1698 PSA_ERROR_BAD_STATE );
1699
1700 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1701 PSA_ASSERT( psa_hash_finish( &op_init,
1702 hash, sizeof( hash ), &hash_len ) );
1703 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1704 PSA_ASSERT( psa_hash_finish( &op_finished,
1705 hash, sizeof( hash ), &hash_len ) );
1706 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1707 PSA_ASSERT( psa_hash_finish( &op_aborted,
1708 hash, sizeof( hash ), &hash_len ) );
1709
1710exit:
1711 psa_hash_abort( &op_source );
1712 psa_hash_abort( &op_init );
1713 psa_hash_abort( &op_setup );
1714 psa_hash_abort( &op_finished );
1715 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001716 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001717}
1718/* END_CASE */
1719
Ronald Cronee414c72021-03-18 18:50:08 +01001720/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001721void hash_clone_target_state( )
1722{
1723 psa_algorithm_t alg = PSA_ALG_SHA_256;
1724 unsigned char hash[PSA_HASH_MAX_SIZE];
1725 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1726 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1727 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1728 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1729 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1730 size_t hash_len;
1731
1732 PSA_ASSERT( psa_crypto_init( ) );
1733
1734 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1735 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1736 PSA_ASSERT( psa_hash_finish( &op_finished,
1737 hash, sizeof( hash ), &hash_len ) );
1738 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1739 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1740
1741 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1742 PSA_ASSERT( psa_hash_finish( &op_target,
1743 hash, sizeof( hash ), &hash_len ) );
1744
1745 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1746 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1747 PSA_ERROR_BAD_STATE );
1748 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1749 PSA_ERROR_BAD_STATE );
1750
1751exit:
1752 psa_hash_abort( &op_target );
1753 psa_hash_abort( &op_init );
1754 psa_hash_abort( &op_setup );
1755 psa_hash_abort( &op_finished );
1756 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001757 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001758}
1759/* END_CASE */
1760
itayzafrir58028322018-10-25 10:22:01 +03001761/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001762void mac_operation_init( )
1763{
Jaeden Amero252ef282019-02-15 14:05:35 +00001764 const uint8_t input[1] = { 0 };
1765
Jaeden Amero769ce272019-01-04 11:48:03 +00001766 /* Test each valid way of initializing the object, except for `= {0}`, as
1767 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1768 * though it's OK by the C standard. We could test for this, but we'd need
1769 * to supress the Clang warning for the test. */
1770 psa_mac_operation_t func = psa_mac_operation_init( );
1771 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1772 psa_mac_operation_t zero;
1773
1774 memset( &zero, 0, sizeof( zero ) );
1775
Jaeden Amero252ef282019-02-15 14:05:35 +00001776 /* A freshly-initialized MAC operation should not be usable. */
1777 TEST_EQUAL( psa_mac_update( &func,
1778 input, sizeof( input ) ),
1779 PSA_ERROR_BAD_STATE );
1780 TEST_EQUAL( psa_mac_update( &init,
1781 input, sizeof( input ) ),
1782 PSA_ERROR_BAD_STATE );
1783 TEST_EQUAL( psa_mac_update( &zero,
1784 input, sizeof( input ) ),
1785 PSA_ERROR_BAD_STATE );
1786
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001787 /* A default MAC operation should be abortable without error. */
1788 PSA_ASSERT( psa_mac_abort( &func ) );
1789 PSA_ASSERT( psa_mac_abort( &init ) );
1790 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001791}
1792/* END_CASE */
1793
1794/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001795void mac_setup( int key_type_arg,
1796 data_t *key,
1797 int alg_arg,
1798 int expected_status_arg )
1799{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001800 psa_key_type_t key_type = key_type_arg;
1801 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001802 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001803 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001804 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1805#if defined(KNOWN_SUPPORTED_MAC_ALG)
1806 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1807#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001808
Gilles Peskine8817f612018-12-18 00:18:46 +01001809 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001810
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001811 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1812 &operation, &status ) )
1813 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001814 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001815
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001816 /* The operation object should be reusable. */
1817#if defined(KNOWN_SUPPORTED_MAC_ALG)
1818 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1819 smoke_test_key_data,
1820 sizeof( smoke_test_key_data ),
1821 KNOWN_SUPPORTED_MAC_ALG,
1822 &operation, &status ) )
1823 goto exit;
1824 TEST_EQUAL( status, PSA_SUCCESS );
1825#endif
1826
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001827exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001828 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001829}
1830/* END_CASE */
1831
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001832/* 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 +00001833void mac_bad_order( )
1834{
Ronald Cron5425a212020-08-04 14:58:35 +02001835 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001836 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1837 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001838 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001839 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1840 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1841 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001842 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001843 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1844 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1845 size_t sign_mac_length = 0;
1846 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1847 const uint8_t verify_mac[] = {
1848 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1849 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1850 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1851
1852 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001853 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001854 psa_set_key_algorithm( &attributes, alg );
1855 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001856
Ronald Cron5425a212020-08-04 14:58:35 +02001857 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1858 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001859
Jaeden Amero252ef282019-02-15 14:05:35 +00001860 /* Call update without calling setup beforehand. */
1861 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1862 PSA_ERROR_BAD_STATE );
1863 PSA_ASSERT( psa_mac_abort( &operation ) );
1864
1865 /* Call sign finish without calling setup beforehand. */
1866 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1867 &sign_mac_length),
1868 PSA_ERROR_BAD_STATE );
1869 PSA_ASSERT( psa_mac_abort( &operation ) );
1870
1871 /* Call verify finish without calling setup beforehand. */
1872 TEST_EQUAL( psa_mac_verify_finish( &operation,
1873 verify_mac, sizeof( verify_mac ) ),
1874 PSA_ERROR_BAD_STATE );
1875 PSA_ASSERT( psa_mac_abort( &operation ) );
1876
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001877 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001878 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
1879 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001880 PSA_ERROR_BAD_STATE );
1881 PSA_ASSERT( psa_mac_abort( &operation ) );
1882
Jaeden Amero252ef282019-02-15 14:05:35 +00001883 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001884 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001885 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1886 PSA_ASSERT( psa_mac_sign_finish( &operation,
1887 sign_mac, sizeof( sign_mac ),
1888 &sign_mac_length ) );
1889 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1890 PSA_ERROR_BAD_STATE );
1891 PSA_ASSERT( psa_mac_abort( &operation ) );
1892
1893 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001894 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001895 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1896 PSA_ASSERT( psa_mac_verify_finish( &operation,
1897 verify_mac, sizeof( verify_mac ) ) );
1898 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1899 PSA_ERROR_BAD_STATE );
1900 PSA_ASSERT( psa_mac_abort( &operation ) );
1901
1902 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001903 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001904 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1905 PSA_ASSERT( psa_mac_sign_finish( &operation,
1906 sign_mac, sizeof( sign_mac ),
1907 &sign_mac_length ) );
1908 TEST_EQUAL( psa_mac_sign_finish( &operation,
1909 sign_mac, sizeof( sign_mac ),
1910 &sign_mac_length ),
1911 PSA_ERROR_BAD_STATE );
1912 PSA_ASSERT( psa_mac_abort( &operation ) );
1913
1914 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001915 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001916 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1917 PSA_ASSERT( psa_mac_verify_finish( &operation,
1918 verify_mac, sizeof( verify_mac ) ) );
1919 TEST_EQUAL( psa_mac_verify_finish( &operation,
1920 verify_mac, sizeof( verify_mac ) ),
1921 PSA_ERROR_BAD_STATE );
1922 PSA_ASSERT( psa_mac_abort( &operation ) );
1923
1924 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02001925 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001926 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01001927 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00001928 TEST_EQUAL( psa_mac_verify_finish( &operation,
1929 verify_mac, sizeof( verify_mac ) ),
1930 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01001931 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00001932 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01001933 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00001934
1935 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02001936 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001937 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01001938 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00001939 TEST_EQUAL( psa_mac_sign_finish( &operation,
1940 sign_mac, sizeof( sign_mac ),
1941 &sign_mac_length ),
1942 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01001943 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00001944 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01001945 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001946
Ronald Cron5425a212020-08-04 14:58:35 +02001947 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001948
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001949exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001950 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001951}
1952/* END_CASE */
1953
1954/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001955void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02001956 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001957 int alg_arg,
1958 data_t *input,
1959 data_t *expected_mac )
1960{
Ronald Cron5425a212020-08-04 14:58:35 +02001961 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001962 psa_key_type_t key_type = key_type_arg;
1963 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001964 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001965 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02001966 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001967 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001968 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001969 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02001970 const size_t output_sizes_to_test[] = {
1971 0,
1972 1,
1973 expected_mac->len - 1,
1974 expected_mac->len,
1975 expected_mac->len + 1,
1976 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001977
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001978 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001979 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02001980 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001981
Gilles Peskine8817f612018-12-18 00:18:46 +01001982 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001983
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001984 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001985 psa_set_key_algorithm( &attributes, alg );
1986 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001987
Ronald Cron5425a212020-08-04 14:58:35 +02001988 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1989 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001990
Gilles Peskine8b356b52020-08-25 23:44:59 +02001991 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
1992 {
1993 const size_t output_size = output_sizes_to_test[i];
1994 psa_status_t expected_status =
1995 ( output_size >= expected_mac->len ? PSA_SUCCESS :
1996 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02001997
Chris Jones9634bb12021-01-20 15:56:42 +00001998 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02001999 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002000
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002001 /* Calculate the MAC, one-shot case. */
2002 TEST_EQUAL( psa_mac_compute( key, alg,
2003 input->x, input->len,
2004 actual_mac, output_size, &mac_length ),
2005 expected_status );
2006 if( expected_status == PSA_SUCCESS )
2007 {
2008 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2009 actual_mac, mac_length );
2010 }
2011
2012 if( output_size > 0 )
2013 memset( actual_mac, 0, output_size );
2014
2015 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002016 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002017 PSA_ASSERT( psa_mac_update( &operation,
2018 input->x, input->len ) );
2019 TEST_EQUAL( psa_mac_sign_finish( &operation,
2020 actual_mac, output_size,
2021 &mac_length ),
2022 expected_status );
2023 PSA_ASSERT( psa_mac_abort( &operation ) );
2024
2025 if( expected_status == PSA_SUCCESS )
2026 {
2027 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2028 actual_mac, mac_length );
2029 }
2030 mbedtls_free( actual_mac );
2031 actual_mac = NULL;
2032 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002033
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002034exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002035 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002036 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002037 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002038 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002039}
2040/* END_CASE */
2041
2042/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002043void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002044 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002045 int alg_arg,
2046 data_t *input,
2047 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002048{
Ronald Cron5425a212020-08-04 14:58:35 +02002049 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002050 psa_key_type_t key_type = key_type_arg;
2051 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002052 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002053 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002054 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002055
Gilles Peskine69c12672018-06-28 00:07:19 +02002056 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2057
Gilles Peskine8817f612018-12-18 00:18:46 +01002058 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002059
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002060 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002061 psa_set_key_algorithm( &attributes, alg );
2062 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002063
Ronald Cron5425a212020-08-04 14:58:35 +02002064 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2065 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002066
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002067 /* Verify correct MAC, one-shot case. */
2068 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2069 expected_mac->x, expected_mac->len ) );
2070
2071 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002072 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002073 PSA_ASSERT( psa_mac_update( &operation,
2074 input->x, input->len ) );
2075 PSA_ASSERT( psa_mac_verify_finish( &operation,
2076 expected_mac->x,
2077 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002078
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002079 /* Test a MAC that's too short, one-shot case. */
2080 TEST_EQUAL( psa_mac_verify( key, alg,
2081 input->x, input->len,
2082 expected_mac->x,
2083 expected_mac->len - 1 ),
2084 PSA_ERROR_INVALID_SIGNATURE );
2085
2086 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002087 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002088 PSA_ASSERT( psa_mac_update( &operation,
2089 input->x, input->len ) );
2090 TEST_EQUAL( psa_mac_verify_finish( &operation,
2091 expected_mac->x,
2092 expected_mac->len - 1 ),
2093 PSA_ERROR_INVALID_SIGNATURE );
2094
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002095 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002096 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2097 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002098 TEST_EQUAL( psa_mac_verify( key, alg,
2099 input->x, input->len,
2100 perturbed_mac, expected_mac->len + 1 ),
2101 PSA_ERROR_INVALID_SIGNATURE );
2102
2103 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002104 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002105 PSA_ASSERT( psa_mac_update( &operation,
2106 input->x, input->len ) );
2107 TEST_EQUAL( psa_mac_verify_finish( &operation,
2108 perturbed_mac,
2109 expected_mac->len + 1 ),
2110 PSA_ERROR_INVALID_SIGNATURE );
2111
2112 /* Test changing one byte. */
2113 for( size_t i = 0; i < expected_mac->len; i++ )
2114 {
Chris Jones9634bb12021-01-20 15:56:42 +00002115 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002116 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002117
2118 TEST_EQUAL( psa_mac_verify( key, alg,
2119 input->x, input->len,
2120 perturbed_mac, expected_mac->len ),
2121 PSA_ERROR_INVALID_SIGNATURE );
2122
Ronald Cron5425a212020-08-04 14:58:35 +02002123 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002124 PSA_ASSERT( psa_mac_update( &operation,
2125 input->x, input->len ) );
2126 TEST_EQUAL( psa_mac_verify_finish( &operation,
2127 perturbed_mac,
2128 expected_mac->len ),
2129 PSA_ERROR_INVALID_SIGNATURE );
2130 perturbed_mac[i] ^= 1;
2131 }
2132
Gilles Peskine8c9def32018-02-08 10:02:12 +01002133exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002134 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002135 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002136 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002137 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002138}
2139/* END_CASE */
2140
2141/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002142void cipher_operation_init( )
2143{
Jaeden Ameroab439972019-02-15 14:12:05 +00002144 const uint8_t input[1] = { 0 };
2145 unsigned char output[1] = { 0 };
2146 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002147 /* Test each valid way of initializing the object, except for `= {0}`, as
2148 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2149 * though it's OK by the C standard. We could test for this, but we'd need
2150 * to supress the Clang warning for the test. */
2151 psa_cipher_operation_t func = psa_cipher_operation_init( );
2152 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2153 psa_cipher_operation_t zero;
2154
2155 memset( &zero, 0, sizeof( zero ) );
2156
Jaeden Ameroab439972019-02-15 14:12:05 +00002157 /* A freshly-initialized cipher operation should not be usable. */
2158 TEST_EQUAL( psa_cipher_update( &func,
2159 input, sizeof( input ),
2160 output, sizeof( output ),
2161 &output_length ),
2162 PSA_ERROR_BAD_STATE );
2163 TEST_EQUAL( psa_cipher_update( &init,
2164 input, sizeof( input ),
2165 output, sizeof( output ),
2166 &output_length ),
2167 PSA_ERROR_BAD_STATE );
2168 TEST_EQUAL( psa_cipher_update( &zero,
2169 input, sizeof( input ),
2170 output, sizeof( output ),
2171 &output_length ),
2172 PSA_ERROR_BAD_STATE );
2173
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002174 /* A default cipher operation should be abortable without error. */
2175 PSA_ASSERT( psa_cipher_abort( &func ) );
2176 PSA_ASSERT( psa_cipher_abort( &init ) );
2177 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002178}
2179/* END_CASE */
2180
2181/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002182void cipher_setup( int key_type_arg,
2183 data_t *key,
2184 int alg_arg,
2185 int expected_status_arg )
2186{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002187 psa_key_type_t key_type = key_type_arg;
2188 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002189 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002190 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002191 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002192#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002193 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2194#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002195
Gilles Peskine8817f612018-12-18 00:18:46 +01002196 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002197
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002198 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2199 &operation, &status ) )
2200 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002201 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002202
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002203 /* The operation object should be reusable. */
2204#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2205 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2206 smoke_test_key_data,
2207 sizeof( smoke_test_key_data ),
2208 KNOWN_SUPPORTED_CIPHER_ALG,
2209 &operation, &status ) )
2210 goto exit;
2211 TEST_EQUAL( status, PSA_SUCCESS );
2212#endif
2213
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002214exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002215 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002216 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002217}
2218/* END_CASE */
2219
Ronald Cronee414c72021-03-18 18:50:08 +01002220/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002221void cipher_bad_order( )
2222{
Ronald Cron5425a212020-08-04 14:58:35 +02002223 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002224 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2225 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002226 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002227 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002228 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002229 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002230 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2231 0xaa, 0xaa, 0xaa, 0xaa };
2232 const uint8_t text[] = {
2233 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2234 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002235 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002236 size_t length = 0;
2237
2238 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002239 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2240 psa_set_key_algorithm( &attributes, alg );
2241 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002242 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2243 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002244
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002245 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002246 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2247 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002248 PSA_ERROR_BAD_STATE );
2249 PSA_ASSERT( psa_cipher_abort( &operation ) );
2250
2251 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002252 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2253 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002254 PSA_ERROR_BAD_STATE );
2255 PSA_ASSERT( psa_cipher_abort( &operation ) );
2256
Jaeden Ameroab439972019-02-15 14:12:05 +00002257 /* Generate an IV without calling setup beforehand. */
2258 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2259 buffer, sizeof( buffer ),
2260 &length ),
2261 PSA_ERROR_BAD_STATE );
2262 PSA_ASSERT( psa_cipher_abort( &operation ) );
2263
2264 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002265 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002266 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2267 buffer, sizeof( buffer ),
2268 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002269 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002270 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2271 buffer, sizeof( buffer ),
2272 &length ),
2273 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002274 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002275 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002276 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002277
2278 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002279 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002280 PSA_ASSERT( psa_cipher_set_iv( &operation,
2281 iv, sizeof( iv ) ) );
2282 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2283 buffer, sizeof( buffer ),
2284 &length ),
2285 PSA_ERROR_BAD_STATE );
2286 PSA_ASSERT( psa_cipher_abort( &operation ) );
2287
2288 /* Set an IV without calling setup beforehand. */
2289 TEST_EQUAL( psa_cipher_set_iv( &operation,
2290 iv, sizeof( iv ) ),
2291 PSA_ERROR_BAD_STATE );
2292 PSA_ASSERT( psa_cipher_abort( &operation ) );
2293
2294 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002295 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002296 PSA_ASSERT( psa_cipher_set_iv( &operation,
2297 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002298 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002299 TEST_EQUAL( psa_cipher_set_iv( &operation,
2300 iv, sizeof( iv ) ),
2301 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002302 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002303 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002304 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002305
2306 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002307 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002308 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2309 buffer, sizeof( buffer ),
2310 &length ) );
2311 TEST_EQUAL( psa_cipher_set_iv( &operation,
2312 iv, sizeof( iv ) ),
2313 PSA_ERROR_BAD_STATE );
2314 PSA_ASSERT( psa_cipher_abort( &operation ) );
2315
2316 /* Call update without calling setup beforehand. */
2317 TEST_EQUAL( psa_cipher_update( &operation,
2318 text, sizeof( text ),
2319 buffer, sizeof( buffer ),
2320 &length ),
2321 PSA_ERROR_BAD_STATE );
2322 PSA_ASSERT( psa_cipher_abort( &operation ) );
2323
2324 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01002325 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002326 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002327 TEST_EQUAL( psa_cipher_update( &operation,
2328 text, sizeof( text ),
2329 buffer, sizeof( buffer ),
2330 &length ),
2331 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002332 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002333 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002334 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002335
2336 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002337 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002338 PSA_ASSERT( psa_cipher_set_iv( &operation,
2339 iv, sizeof( iv ) ) );
2340 PSA_ASSERT( psa_cipher_finish( &operation,
2341 buffer, sizeof( buffer ), &length ) );
2342 TEST_EQUAL( psa_cipher_update( &operation,
2343 text, sizeof( text ),
2344 buffer, sizeof( buffer ),
2345 &length ),
2346 PSA_ERROR_BAD_STATE );
2347 PSA_ASSERT( psa_cipher_abort( &operation ) );
2348
2349 /* Call finish without calling setup beforehand. */
2350 TEST_EQUAL( psa_cipher_finish( &operation,
2351 buffer, sizeof( buffer ), &length ),
2352 PSA_ERROR_BAD_STATE );
2353 PSA_ASSERT( psa_cipher_abort( &operation ) );
2354
2355 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002356 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002357 /* Not calling update means we are encrypting an empty buffer, which is OK
2358 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01002359 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002360 TEST_EQUAL( psa_cipher_finish( &operation,
2361 buffer, sizeof( buffer ), &length ),
2362 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002363 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002364 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002365 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002366
2367 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002368 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002369 PSA_ASSERT( psa_cipher_set_iv( &operation,
2370 iv, sizeof( iv ) ) );
2371 PSA_ASSERT( psa_cipher_finish( &operation,
2372 buffer, sizeof( buffer ), &length ) );
2373 TEST_EQUAL( psa_cipher_finish( &operation,
2374 buffer, sizeof( buffer ), &length ),
2375 PSA_ERROR_BAD_STATE );
2376 PSA_ASSERT( psa_cipher_abort( &operation ) );
2377
Ronald Cron5425a212020-08-04 14:58:35 +02002378 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002379
Jaeden Ameroab439972019-02-15 14:12:05 +00002380exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002381 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002382 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002383}
2384/* END_CASE */
2385
2386/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002387void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002388 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002389 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002390 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002391{
Ronald Cron5425a212020-08-04 14:58:35 +02002392 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002393 psa_status_t status;
2394 psa_key_type_t key_type = key_type_arg;
2395 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002396 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002397 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002398 size_t output_buffer_size = 0;
2399 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002400 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002401 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002402 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002403
Gilles Peskine8817f612018-12-18 00:18:46 +01002404 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002405
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002406 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2407 psa_set_key_algorithm( &attributes, alg );
2408 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002409
Ronald Cron5425a212020-08-04 14:58:35 +02002410 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2411 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002412
Ronald Cron5425a212020-08-04 14:58:35 +02002413 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002414
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002415 if( iv->len > 0 )
2416 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002417 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002418 }
2419
gabor-mezei-armceface22021-01-21 12:26:17 +01002420 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2421 TEST_ASSERT( output_buffer_size <=
2422 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002423 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002424
Gilles Peskine8817f612018-12-18 00:18:46 +01002425 PSA_ASSERT( psa_cipher_update( &operation,
2426 input->x, input->len,
2427 output, output_buffer_size,
2428 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002429 TEST_ASSERT( function_output_length <=
2430 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2431 TEST_ASSERT( function_output_length <=
2432 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002433 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002434
Gilles Peskine50e586b2018-06-08 14:28:46 +02002435 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002436 ( output_buffer_size == 0 ? NULL :
2437 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002438 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002439 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002440 TEST_ASSERT( function_output_length <=
2441 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2442 TEST_ASSERT( function_output_length <=
2443 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002444 total_output_length += function_output_length;
2445
Gilles Peskinefe11b722018-12-18 00:24:04 +01002446 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002447 if( expected_status == PSA_SUCCESS )
2448 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002449 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002450 ASSERT_COMPARE( expected_output->x, expected_output->len,
2451 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002452 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002453
Gilles Peskine50e586b2018-06-08 14:28:46 +02002454exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002455 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002456 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002457 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002458 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002459}
2460/* END_CASE */
2461
2462/* BEGIN_CASE */
2463void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002464 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002465 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002466 int first_part_size_arg,
2467 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002468 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002469{
Ronald Cron5425a212020-08-04 14:58:35 +02002470 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002471 psa_key_type_t key_type = key_type_arg;
2472 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002473 size_t first_part_size = first_part_size_arg;
2474 size_t output1_length = output1_length_arg;
2475 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002476 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002477 size_t output_buffer_size = 0;
2478 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002479 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002480 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002481 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002482
Gilles Peskine8817f612018-12-18 00:18:46 +01002483 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002484
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002485 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2486 psa_set_key_algorithm( &attributes, alg );
2487 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002488
Ronald Cron5425a212020-08-04 14:58:35 +02002489 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2490 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002491
Ronald Cron5425a212020-08-04 14:58:35 +02002492 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002493
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002494 if( iv->len > 0 )
2495 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002496 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002497 }
2498
gabor-mezei-armceface22021-01-21 12:26:17 +01002499 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2500 TEST_ASSERT( output_buffer_size <=
2501 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002502 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002503
Gilles Peskinee0866522019-02-19 19:44:00 +01002504 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002505 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2506 output, output_buffer_size,
2507 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002508 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002509 TEST_ASSERT( function_output_length <=
2510 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2511 TEST_ASSERT( function_output_length <=
2512 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002513 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002514
Gilles Peskine8817f612018-12-18 00:18:46 +01002515 PSA_ASSERT( psa_cipher_update( &operation,
2516 input->x + first_part_size,
2517 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002518 ( output_buffer_size == 0 ? NULL :
2519 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002520 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002521 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002522 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002523 TEST_ASSERT( function_output_length <=
2524 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2525 alg,
2526 input->len - first_part_size ) );
2527 TEST_ASSERT( function_output_length <=
2528 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002529 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002530
Gilles Peskine8817f612018-12-18 00:18:46 +01002531 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002532 ( output_buffer_size == 0 ? NULL :
2533 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002534 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002535 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002536 TEST_ASSERT( function_output_length <=
2537 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2538 TEST_ASSERT( function_output_length <=
2539 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002540 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002541 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002542
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002543 ASSERT_COMPARE( expected_output->x, expected_output->len,
2544 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002545
2546exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002547 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002548 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002549 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002550 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002551}
2552/* END_CASE */
2553
2554/* BEGIN_CASE */
2555void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002556 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002557 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002558 int first_part_size_arg,
2559 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002560 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002561{
Ronald Cron5425a212020-08-04 14:58:35 +02002562 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002563 psa_key_type_t key_type = key_type_arg;
2564 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002565 size_t first_part_size = first_part_size_arg;
2566 size_t output1_length = output1_length_arg;
2567 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002568 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002569 size_t output_buffer_size = 0;
2570 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002571 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002572 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002573 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002574
Gilles Peskine8817f612018-12-18 00:18:46 +01002575 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002576
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002577 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2578 psa_set_key_algorithm( &attributes, alg );
2579 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002580
Ronald Cron5425a212020-08-04 14:58:35 +02002581 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2582 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002583
Ronald Cron5425a212020-08-04 14:58:35 +02002584 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002585
Steven Cooreman177deba2020-09-07 17:14:14 +02002586 if( iv->len > 0 )
2587 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002588 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002589 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002590
gabor-mezei-armceface22021-01-21 12:26:17 +01002591 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2592 TEST_ASSERT( output_buffer_size <=
2593 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002594 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002595
Gilles Peskinee0866522019-02-19 19:44:00 +01002596 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002597 PSA_ASSERT( psa_cipher_update( &operation,
2598 input->x, first_part_size,
2599 output, output_buffer_size,
2600 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002601 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002602 TEST_ASSERT( function_output_length <=
2603 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2604 TEST_ASSERT( function_output_length <=
2605 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002606 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002607
Gilles Peskine8817f612018-12-18 00:18:46 +01002608 PSA_ASSERT( psa_cipher_update( &operation,
2609 input->x + first_part_size,
2610 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002611 ( output_buffer_size == 0 ? NULL :
2612 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002613 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002614 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002615 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002616 TEST_ASSERT( function_output_length <=
2617 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2618 alg,
2619 input->len - first_part_size ) );
2620 TEST_ASSERT( function_output_length <=
2621 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002622 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002623
Gilles Peskine8817f612018-12-18 00:18:46 +01002624 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002625 ( output_buffer_size == 0 ? NULL :
2626 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002627 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002628 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002629 TEST_ASSERT( function_output_length <=
2630 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2631 TEST_ASSERT( function_output_length <=
2632 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002633 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002634 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002635
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002636 ASSERT_COMPARE( expected_output->x, expected_output->len,
2637 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002638
2639exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002640 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002641 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002642 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002643 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002644}
2645/* END_CASE */
2646
Gilles Peskine50e586b2018-06-08 14:28:46 +02002647/* BEGIN_CASE */
2648void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002649 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002650 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002651 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002652{
Ronald Cron5425a212020-08-04 14:58:35 +02002653 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002654 psa_status_t status;
2655 psa_key_type_t key_type = key_type_arg;
2656 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002657 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002658 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002659 size_t output_buffer_size = 0;
2660 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002661 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002662 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002663 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002664
Gilles Peskine8817f612018-12-18 00:18:46 +01002665 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002666
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002667 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2668 psa_set_key_algorithm( &attributes, alg );
2669 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002670
Ronald Cron5425a212020-08-04 14:58:35 +02002671 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2672 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002673
Ronald Cron5425a212020-08-04 14:58:35 +02002674 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002675
Steven Cooreman177deba2020-09-07 17:14:14 +02002676 if( iv->len > 0 )
2677 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002678 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002679 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002680
gabor-mezei-armceface22021-01-21 12:26:17 +01002681 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2682 TEST_ASSERT( output_buffer_size <=
2683 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002684 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002685
Gilles Peskine8817f612018-12-18 00:18:46 +01002686 PSA_ASSERT( psa_cipher_update( &operation,
2687 input->x, input->len,
2688 output, output_buffer_size,
2689 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002690 TEST_ASSERT( function_output_length <=
2691 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2692 TEST_ASSERT( function_output_length <=
2693 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002694 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002695
Gilles Peskine50e586b2018-06-08 14:28:46 +02002696 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002697 ( output_buffer_size == 0 ? NULL :
2698 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002699 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002700 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002701 TEST_ASSERT( function_output_length <=
2702 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2703 TEST_ASSERT( function_output_length <=
2704 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002705 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002706 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002707
2708 if( expected_status == PSA_SUCCESS )
2709 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002710 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002711 ASSERT_COMPARE( expected_output->x, expected_output->len,
2712 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002713 }
2714
Gilles Peskine50e586b2018-06-08 14:28:46 +02002715exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002716 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002717 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002718 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002719 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002720}
2721/* END_CASE */
2722
Gilles Peskine50e586b2018-06-08 14:28:46 +02002723/* BEGIN_CASE */
2724void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002725 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002726 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002727{
Ronald Cron5425a212020-08-04 14:58:35 +02002728 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002729 psa_key_type_t key_type = key_type_arg;
2730 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002731 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002732 size_t iv_size = 16;
2733 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002734 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002735 size_t output1_size = 0;
2736 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002737 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002738 size_t output2_size = 0;
2739 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002740 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002741 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2742 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002743 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002744
Gilles Peskine8817f612018-12-18 00:18:46 +01002745 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002746
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002747 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2748 psa_set_key_algorithm( &attributes, alg );
2749 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002750
Ronald Cron5425a212020-08-04 14:58:35 +02002751 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2752 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002753
Ronald Cron5425a212020-08-04 14:58:35 +02002754 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2755 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002756
Steven Cooreman177deba2020-09-07 17:14:14 +02002757 if( alg != PSA_ALG_ECB_NO_PADDING )
2758 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002759 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2760 iv, iv_size,
2761 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002762 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002763 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2764 TEST_ASSERT( output1_size <=
2765 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002766 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002767
Gilles Peskine8817f612018-12-18 00:18:46 +01002768 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2769 output1, output1_size,
2770 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002771 TEST_ASSERT( output1_length <=
2772 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2773 TEST_ASSERT( output1_length <=
2774 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2775
Gilles Peskine8817f612018-12-18 00:18:46 +01002776 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002777 output1 + output1_length,
2778 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002779 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002780 TEST_ASSERT( function_output_length <=
2781 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2782 TEST_ASSERT( function_output_length <=
2783 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002784
Gilles Peskine048b7f02018-06-08 14:20:49 +02002785 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002786
Gilles Peskine8817f612018-12-18 00:18:46 +01002787 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002788
2789 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002790 TEST_ASSERT( output2_size <=
2791 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2792 TEST_ASSERT( output2_size <=
2793 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002794 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002795
Steven Cooreman177deba2020-09-07 17:14:14 +02002796 if( iv_length > 0 )
2797 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002798 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2799 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002800 }
2801
Gilles Peskine8817f612018-12-18 00:18:46 +01002802 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2803 output2, output2_size,
2804 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002805 TEST_ASSERT( output2_length <=
2806 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
2807 TEST_ASSERT( output2_length <=
2808 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
2809
Gilles Peskine048b7f02018-06-08 14:20:49 +02002810 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002811 PSA_ASSERT( psa_cipher_finish( &operation2,
2812 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002813 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002814 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002815 TEST_ASSERT( function_output_length <=
2816 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2817 TEST_ASSERT( function_output_length <=
2818 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03002819
Gilles Peskine048b7f02018-06-08 14:20:49 +02002820 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002821
Gilles Peskine8817f612018-12-18 00:18:46 +01002822 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002823
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002824 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002825
2826exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002827 psa_cipher_abort( &operation1 );
2828 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002829 mbedtls_free( output1 );
2830 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002831 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002832 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03002833}
2834/* END_CASE */
2835
2836/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002837void cipher_verify_output_multipart( int alg_arg,
2838 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002839 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002840 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002841 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03002842{
Ronald Cron5425a212020-08-04 14:58:35 +02002843 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002844 psa_key_type_t key_type = key_type_arg;
2845 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002846 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002847 unsigned char iv[16] = {0};
2848 size_t iv_size = 16;
2849 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002850 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002851 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002852 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002853 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002854 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002855 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002856 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002857 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2858 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002859 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002860
Gilles Peskine8817f612018-12-18 00:18:46 +01002861 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002862
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002863 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2864 psa_set_key_algorithm( &attributes, alg );
2865 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002866
Ronald Cron5425a212020-08-04 14:58:35 +02002867 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2868 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03002869
Ronald Cron5425a212020-08-04 14:58:35 +02002870 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2871 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002872
Steven Cooreman177deba2020-09-07 17:14:14 +02002873 if( alg != PSA_ALG_ECB_NO_PADDING )
2874 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002875 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2876 iv, iv_size,
2877 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002878 }
2879
gabor-mezei-armceface22021-01-21 12:26:17 +01002880 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2881 TEST_ASSERT( output1_buffer_size <=
2882 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002883 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002884
Gilles Peskinee0866522019-02-19 19:44:00 +01002885 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002886
Gilles Peskine8817f612018-12-18 00:18:46 +01002887 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2888 output1, output1_buffer_size,
2889 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002890 TEST_ASSERT( function_output_length <=
2891 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2892 TEST_ASSERT( function_output_length <=
2893 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002894 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002895
Gilles Peskine8817f612018-12-18 00:18:46 +01002896 PSA_ASSERT( psa_cipher_update( &operation1,
2897 input->x + first_part_size,
2898 input->len - first_part_size,
2899 output1, output1_buffer_size,
2900 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002901 TEST_ASSERT( function_output_length <=
2902 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2903 alg,
2904 input->len - first_part_size ) );
2905 TEST_ASSERT( function_output_length <=
2906 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002907 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002908
Gilles Peskine8817f612018-12-18 00:18:46 +01002909 PSA_ASSERT( psa_cipher_finish( &operation1,
2910 output1 + output1_length,
2911 output1_buffer_size - output1_length,
2912 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002913 TEST_ASSERT( function_output_length <=
2914 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2915 TEST_ASSERT( function_output_length <=
2916 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002917 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002918
Gilles Peskine8817f612018-12-18 00:18:46 +01002919 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002920
Gilles Peskine048b7f02018-06-08 14:20:49 +02002921 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002922 TEST_ASSERT( output2_buffer_size <=
2923 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2924 TEST_ASSERT( output2_buffer_size <=
2925 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002926 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002927
Steven Cooreman177deba2020-09-07 17:14:14 +02002928 if( iv_length > 0 )
2929 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002930 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2931 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002932 }
Moran Pekerded84402018-06-06 16:36:50 +03002933
Gilles Peskine8817f612018-12-18 00:18:46 +01002934 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2935 output2, output2_buffer_size,
2936 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002937 TEST_ASSERT( function_output_length <=
2938 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2939 TEST_ASSERT( function_output_length <=
2940 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002941 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002942
Gilles Peskine8817f612018-12-18 00:18:46 +01002943 PSA_ASSERT( psa_cipher_update( &operation2,
2944 output1 + first_part_size,
2945 output1_length - first_part_size,
2946 output2, output2_buffer_size,
2947 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002948 TEST_ASSERT( function_output_length <=
2949 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2950 alg,
2951 output1_length - first_part_size ) );
2952 TEST_ASSERT( function_output_length <=
2953 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002954 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002955
Gilles Peskine8817f612018-12-18 00:18:46 +01002956 PSA_ASSERT( psa_cipher_finish( &operation2,
2957 output2 + output2_length,
2958 output2_buffer_size - output2_length,
2959 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002960 TEST_ASSERT( function_output_length <=
2961 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2962 TEST_ASSERT( function_output_length <=
2963 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002964 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002965
Gilles Peskine8817f612018-12-18 00:18:46 +01002966 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002967
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002968 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002969
2970exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002971 psa_cipher_abort( &operation1 );
2972 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002973 mbedtls_free( output1 );
2974 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002975 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002976 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002977}
2978/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002979
Gilles Peskine20035e32018-02-03 22:44:14 +01002980/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002981void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002982 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002983 data_t *nonce,
2984 data_t *additional_data,
2985 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002986 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002987{
Ronald Cron5425a212020-08-04 14:58:35 +02002988 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002989 psa_key_type_t key_type = key_type_arg;
2990 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01002991 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002992 unsigned char *output_data = NULL;
2993 size_t output_size = 0;
2994 size_t output_length = 0;
2995 unsigned char *output_data2 = NULL;
2996 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01002997 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002998 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002999 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003000
Gilles Peskine8817f612018-12-18 00:18:46 +01003001 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003002
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003003 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3004 psa_set_key_algorithm( &attributes, alg );
3005 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003006
Gilles Peskine049c7532019-05-15 20:22:09 +02003007 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003008 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003009 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3010 key_bits = psa_get_key_bits( &attributes );
3011
3012 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3013 alg );
3014 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3015 * should be exact. */
3016 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3017 expected_result != PSA_ERROR_NOT_SUPPORTED )
3018 {
3019 TEST_EQUAL( output_size,
3020 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3021 TEST_ASSERT( output_size <=
3022 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3023 }
3024 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003025
Steven Cooremanf49478b2021-02-15 15:19:25 +01003026 status = psa_aead_encrypt( key, alg,
3027 nonce->x, nonce->len,
3028 additional_data->x,
3029 additional_data->len,
3030 input_data->x, input_data->len,
3031 output_data, output_size,
3032 &output_length );
3033
3034 /* If the operation is not supported, just skip and not fail in case the
3035 * encryption involves a common limitation of cryptography hardwares and
3036 * an alternative implementation. */
3037 if( status == PSA_ERROR_NOT_SUPPORTED )
3038 {
3039 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3040 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3041 }
3042
3043 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003044
3045 if( PSA_SUCCESS == expected_result )
3046 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003047 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003048
Gilles Peskine003a4a92019-05-14 16:09:40 +02003049 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3050 * should be exact. */
3051 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003052 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003053
gabor-mezei-armceface22021-01-21 12:26:17 +01003054 TEST_ASSERT( input_data->len <=
3055 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3056
Ronald Cron5425a212020-08-04 14:58:35 +02003057 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003058 nonce->x, nonce->len,
3059 additional_data->x,
3060 additional_data->len,
3061 output_data, output_length,
3062 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003063 &output_length2 ),
3064 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003065
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003066 ASSERT_COMPARE( input_data->x, input_data->len,
3067 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003068 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003069
Gilles Peskinea1cac842018-06-11 19:33:02 +02003070exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003071 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003072 mbedtls_free( output_data );
3073 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003074 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003075}
3076/* END_CASE */
3077
3078/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003079void aead_encrypt( int key_type_arg, data_t *key_data,
3080 int alg_arg,
3081 data_t *nonce,
3082 data_t *additional_data,
3083 data_t *input_data,
3084 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003085{
Ronald Cron5425a212020-08-04 14:58:35 +02003086 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003087 psa_key_type_t key_type = key_type_arg;
3088 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003089 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003090 unsigned char *output_data = NULL;
3091 size_t output_size = 0;
3092 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003093 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003094 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003095
Gilles Peskine8817f612018-12-18 00:18:46 +01003096 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003097
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003098 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3099 psa_set_key_algorithm( &attributes, alg );
3100 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003101
Gilles Peskine049c7532019-05-15 20:22:09 +02003102 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003103 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003104 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3105 key_bits = psa_get_key_bits( &attributes );
3106
3107 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3108 alg );
3109 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3110 * should be exact. */
3111 TEST_EQUAL( output_size,
3112 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3113 TEST_ASSERT( output_size <=
3114 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3115 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003116
Steven Cooremand588ea12021-01-11 19:36:04 +01003117 status = psa_aead_encrypt( key, alg,
3118 nonce->x, nonce->len,
3119 additional_data->x, additional_data->len,
3120 input_data->x, input_data->len,
3121 output_data, output_size,
3122 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003123
Ronald Cron28a45ed2021-02-09 20:35:42 +01003124 /* If the operation is not supported, just skip and not fail in case the
3125 * encryption involves a common limitation of cryptography hardwares and
3126 * an alternative implementation. */
3127 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003128 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003129 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3130 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003131 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003132
3133 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003134 ASSERT_COMPARE( expected_result->x, expected_result->len,
3135 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003136
Gilles Peskinea1cac842018-06-11 19:33:02 +02003137exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003138 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003139 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003140 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003141}
3142/* END_CASE */
3143
3144/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003145void aead_decrypt( int key_type_arg, data_t *key_data,
3146 int alg_arg,
3147 data_t *nonce,
3148 data_t *additional_data,
3149 data_t *input_data,
3150 data_t *expected_data,
3151 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003152{
Ronald Cron5425a212020-08-04 14:58:35 +02003153 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003154 psa_key_type_t key_type = key_type_arg;
3155 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003156 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003157 unsigned char *output_data = NULL;
3158 size_t output_size = 0;
3159 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003160 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003161 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003162 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003163
Gilles Peskine8817f612018-12-18 00:18:46 +01003164 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003165
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003166 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3167 psa_set_key_algorithm( &attributes, alg );
3168 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003169
Gilles Peskine049c7532019-05-15 20:22:09 +02003170 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003171 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003172 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3173 key_bits = psa_get_key_bits( &attributes );
3174
3175 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3176 alg );
3177 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3178 expected_result != PSA_ERROR_NOT_SUPPORTED )
3179 {
3180 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3181 * should be exact. */
3182 TEST_EQUAL( output_size,
3183 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3184 TEST_ASSERT( output_size <=
3185 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3186 }
3187 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003188
Steven Cooremand588ea12021-01-11 19:36:04 +01003189 status = psa_aead_decrypt( key, alg,
3190 nonce->x, nonce->len,
3191 additional_data->x,
3192 additional_data->len,
3193 input_data->x, input_data->len,
3194 output_data, output_size,
3195 &output_length );
3196
Ronald Cron28a45ed2021-02-09 20:35:42 +01003197 /* If the operation is not supported, just skip and not fail in case the
3198 * decryption involves a common limitation of cryptography hardwares and
3199 * an alternative implementation. */
3200 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003201 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003202 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3203 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003204 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003205
3206 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003207
Gilles Peskine2d277862018-06-18 15:41:12 +02003208 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003209 ASSERT_COMPARE( expected_data->x, expected_data->len,
3210 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003211
Gilles Peskinea1cac842018-06-11 19:33:02 +02003212exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003213 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003214 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003215 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003216}
3217/* END_CASE */
3218
3219/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003220void signature_size( int type_arg,
3221 int bits,
3222 int alg_arg,
3223 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003224{
3225 psa_key_type_t type = type_arg;
3226 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003227 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003228
Gilles Peskinefe11b722018-12-18 00:24:04 +01003229 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003230
Gilles Peskinee59236f2018-01-27 23:32:46 +01003231exit:
3232 ;
3233}
3234/* END_CASE */
3235
3236/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003237void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3238 int alg_arg, data_t *input_data,
3239 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003240{
Ronald Cron5425a212020-08-04 14:58:35 +02003241 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003242 psa_key_type_t key_type = key_type_arg;
3243 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003244 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003245 unsigned char *signature = NULL;
3246 size_t signature_size;
3247 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003248 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003249
Gilles Peskine8817f612018-12-18 00:18:46 +01003250 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003251
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003252 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003253 psa_set_key_algorithm( &attributes, alg );
3254 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003255
Gilles Peskine049c7532019-05-15 20:22:09 +02003256 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003257 &key ) );
3258 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003259 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003260
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003261 /* Allocate a buffer which has the size advertized by the
3262 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003263 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003264 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003265 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003266 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003267 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003268
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003269 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003270 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003271 input_data->x, input_data->len,
3272 signature, signature_size,
3273 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003274 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003275 ASSERT_COMPARE( output_data->x, output_data->len,
3276 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003277
3278exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003279 /*
3280 * Key attributes may have been returned by psa_get_key_attributes()
3281 * thus reset them as required.
3282 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003283 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003284
Ronald Cron5425a212020-08-04 14:58:35 +02003285 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003286 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003287 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003288}
3289/* END_CASE */
3290
3291/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003292void sign_hash_fail( int key_type_arg, data_t *key_data,
3293 int alg_arg, data_t *input_data,
3294 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003295{
Ronald Cron5425a212020-08-04 14:58:35 +02003296 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003297 psa_key_type_t key_type = key_type_arg;
3298 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003299 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003300 psa_status_t actual_status;
3301 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003302 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003303 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003304 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003305
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003306 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003307
Gilles Peskine8817f612018-12-18 00:18:46 +01003308 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003309
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003310 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003311 psa_set_key_algorithm( &attributes, alg );
3312 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003313
Gilles Peskine049c7532019-05-15 20:22:09 +02003314 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003315 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003316
Ronald Cron5425a212020-08-04 14:58:35 +02003317 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003318 input_data->x, input_data->len,
3319 signature, signature_size,
3320 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003321 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003322 /* The value of *signature_length is unspecified on error, but
3323 * whatever it is, it should be less than signature_size, so that
3324 * if the caller tries to read *signature_length bytes without
3325 * checking the error code then they don't overflow a buffer. */
3326 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003327
3328exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003329 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003330 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003331 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003332 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003333}
3334/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003335
3336/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003337void sign_verify_hash( int key_type_arg, data_t *key_data,
3338 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003339{
Ronald Cron5425a212020-08-04 14:58:35 +02003340 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003341 psa_key_type_t key_type = key_type_arg;
3342 psa_algorithm_t alg = alg_arg;
3343 size_t key_bits;
3344 unsigned char *signature = NULL;
3345 size_t signature_size;
3346 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003347 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003348
Gilles Peskine8817f612018-12-18 00:18:46 +01003349 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003350
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003351 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003352 psa_set_key_algorithm( &attributes, alg );
3353 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003354
Gilles Peskine049c7532019-05-15 20:22:09 +02003355 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003356 &key ) );
3357 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003358 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003359
3360 /* Allocate a buffer which has the size advertized by the
3361 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003362 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003363 key_bits, alg );
3364 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003365 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003366 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003367
3368 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003369 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003370 input_data->x, input_data->len,
3371 signature, signature_size,
3372 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003373 /* Check that the signature length looks sensible. */
3374 TEST_ASSERT( signature_length <= signature_size );
3375 TEST_ASSERT( signature_length > 0 );
3376
3377 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003378 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003379 input_data->x, input_data->len,
3380 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003381
3382 if( input_data->len != 0 )
3383 {
3384 /* Flip a bit in the input and verify that the signature is now
3385 * detected as invalid. Flip a bit at the beginning, not at the end,
3386 * because ECDSA may ignore the last few bits of the input. */
3387 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003388 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003389 input_data->x, input_data->len,
3390 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003391 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003392 }
3393
3394exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003395 /*
3396 * Key attributes may have been returned by psa_get_key_attributes()
3397 * thus reset them as required.
3398 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003399 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003400
Ronald Cron5425a212020-08-04 14:58:35 +02003401 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003402 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003403 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003404}
3405/* END_CASE */
3406
3407/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003408void verify_hash( int key_type_arg, data_t *key_data,
3409 int alg_arg, data_t *hash_data,
3410 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003411{
Ronald Cron5425a212020-08-04 14:58:35 +02003412 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003413 psa_key_type_t key_type = key_type_arg;
3414 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003415 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003416
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003417 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003418
Gilles Peskine8817f612018-12-18 00:18:46 +01003419 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003420
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003421 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003422 psa_set_key_algorithm( &attributes, alg );
3423 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003424
Gilles Peskine049c7532019-05-15 20:22:09 +02003425 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003426 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003427
Ronald Cron5425a212020-08-04 14:58:35 +02003428 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003429 hash_data->x, hash_data->len,
3430 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003431
itayzafrir5c753392018-05-08 11:18:38 +03003432exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003433 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003434 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003435 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003436}
3437/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003438
3439/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003440void verify_hash_fail( int key_type_arg, data_t *key_data,
3441 int alg_arg, data_t *hash_data,
3442 data_t *signature_data,
3443 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003444{
Ronald Cron5425a212020-08-04 14:58:35 +02003445 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003446 psa_key_type_t key_type = key_type_arg;
3447 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003448 psa_status_t actual_status;
3449 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003450 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003451
Gilles Peskine8817f612018-12-18 00:18:46 +01003452 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003453
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003454 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003455 psa_set_key_algorithm( &attributes, alg );
3456 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003457
Gilles Peskine049c7532019-05-15 20:22:09 +02003458 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003459 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003460
Ronald Cron5425a212020-08-04 14:58:35 +02003461 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003462 hash_data->x, hash_data->len,
3463 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003464 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003465
3466exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003467 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003468 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003469 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003470}
3471/* END_CASE */
3472
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003473/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02003474void sign_message_deterministic( int key_type_arg,
3475 data_t *key_data,
3476 int alg_arg,
3477 data_t *input_data,
3478 data_t *output_data )
3479{
3480 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3481 psa_key_type_t key_type = key_type_arg;
3482 psa_algorithm_t alg = alg_arg;
3483 size_t key_bits;
3484 unsigned char *signature = NULL;
3485 size_t signature_size;
3486 size_t signature_length = 0xdeadbeef;
3487 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3488
3489 PSA_ASSERT( psa_crypto_init( ) );
3490
3491 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3492 psa_set_key_algorithm( &attributes, alg );
3493 psa_set_key_type( &attributes, key_type );
3494
3495 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3496 &key ) );
3497 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3498 key_bits = psa_get_key_bits( &attributes );
3499
3500 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3501 TEST_ASSERT( signature_size != 0 );
3502 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3503 ASSERT_ALLOC( signature, signature_size );
3504
3505 PSA_ASSERT( psa_sign_message( key, alg,
3506 input_data->x, input_data->len,
3507 signature, signature_size,
3508 &signature_length ) );
3509
3510 ASSERT_COMPARE( output_data->x, output_data->len,
3511 signature, signature_length );
3512
3513exit:
3514 psa_reset_key_attributes( &attributes );
3515
3516 psa_destroy_key( key );
3517 mbedtls_free( signature );
3518 PSA_DONE( );
3519
3520}
3521/* END_CASE */
3522
3523/* BEGIN_CASE */
3524void sign_message_fail( int key_type_arg,
3525 data_t *key_data,
3526 int alg_arg,
3527 data_t *input_data,
3528 int signature_size_arg,
3529 int expected_status_arg )
3530{
3531 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3532 psa_key_type_t key_type = key_type_arg;
3533 psa_algorithm_t alg = alg_arg;
3534 size_t signature_size = signature_size_arg;
3535 psa_status_t actual_status;
3536 psa_status_t expected_status = expected_status_arg;
3537 unsigned char *signature = NULL;
3538 size_t signature_length = 0xdeadbeef;
3539 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3540
3541 ASSERT_ALLOC( signature, signature_size );
3542
3543 PSA_ASSERT( psa_crypto_init( ) );
3544
3545 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3546 psa_set_key_algorithm( &attributes, alg );
3547 psa_set_key_type( &attributes, key_type );
3548
3549 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3550 &key ) );
3551
3552 actual_status = psa_sign_message( key, alg,
3553 input_data->x, input_data->len,
3554 signature, signature_size,
3555 &signature_length );
3556 TEST_EQUAL( actual_status, expected_status );
3557 /* The value of *signature_length is unspecified on error, but
3558 * whatever it is, it should be less than signature_size, so that
3559 * if the caller tries to read *signature_length bytes without
3560 * checking the error code then they don't overflow a buffer. */
3561 TEST_ASSERT( signature_length <= signature_size );
3562
3563exit:
3564 psa_reset_key_attributes( &attributes );
3565 psa_destroy_key( key );
3566 mbedtls_free( signature );
3567 PSA_DONE( );
3568}
3569/* END_CASE */
3570
3571/* BEGIN_CASE */
3572void sign_verify_message( int key_type_arg,
3573 data_t *key_data,
3574 int alg_arg,
3575 data_t *input_data )
3576{
3577 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3578 psa_key_type_t key_type = key_type_arg;
3579 psa_algorithm_t alg = alg_arg;
3580 size_t key_bits;
3581 unsigned char *signature = NULL;
3582 size_t signature_size;
3583 size_t signature_length = 0xdeadbeef;
3584 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3585
3586 PSA_ASSERT( psa_crypto_init( ) );
3587
3588 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3589 PSA_KEY_USAGE_VERIFY_MESSAGE );
3590 psa_set_key_algorithm( &attributes, alg );
3591 psa_set_key_type( &attributes, key_type );
3592
3593 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3594 &key ) );
3595 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3596 key_bits = psa_get_key_bits( &attributes );
3597
3598 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3599 TEST_ASSERT( signature_size != 0 );
3600 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3601 ASSERT_ALLOC( signature, signature_size );
3602
3603 PSA_ASSERT( psa_sign_message( key, alg,
3604 input_data->x, input_data->len,
3605 signature, signature_size,
3606 &signature_length ) );
3607 TEST_ASSERT( signature_length <= signature_size );
3608 TEST_ASSERT( signature_length > 0 );
3609
3610 PSA_ASSERT( psa_verify_message( key, alg,
3611 input_data->x, input_data->len,
3612 signature, signature_length ) );
3613
3614 if( input_data->len != 0 )
3615 {
3616 /* Flip a bit in the input and verify that the signature is now
3617 * detected as invalid. Flip a bit at the beginning, not at the end,
3618 * because ECDSA may ignore the last few bits of the input. */
3619 input_data->x[0] ^= 1;
3620 TEST_EQUAL( psa_verify_message( key, alg,
3621 input_data->x, input_data->len,
3622 signature, signature_length ),
3623 PSA_ERROR_INVALID_SIGNATURE );
3624 }
3625
3626exit:
3627 psa_reset_key_attributes( &attributes );
3628
3629 psa_destroy_key( key );
3630 mbedtls_free( signature );
3631 PSA_DONE( );
3632}
3633/* END_CASE */
3634
3635/* BEGIN_CASE */
3636void verify_message( int key_type_arg,
3637 data_t *key_data,
3638 int alg_arg,
3639 data_t *input_data,
3640 data_t *signature_data )
3641{
3642 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3643 psa_key_type_t key_type = key_type_arg;
3644 psa_algorithm_t alg = alg_arg;
3645 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3646
3647 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3648
3649 PSA_ASSERT( psa_crypto_init( ) );
3650
3651 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3652 psa_set_key_algorithm( &attributes, alg );
3653 psa_set_key_type( &attributes, key_type );
3654
3655 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3656 &key ) );
3657
3658 PSA_ASSERT( psa_verify_message( key, alg,
3659 input_data->x, input_data->len,
3660 signature_data->x, signature_data->len ) );
3661
3662exit:
3663 psa_reset_key_attributes( &attributes );
3664 psa_destroy_key( key );
3665 PSA_DONE( );
3666}
3667/* END_CASE */
3668
3669/* BEGIN_CASE */
3670void verify_message_fail( int key_type_arg,
3671 data_t *key_data,
3672 int alg_arg,
3673 data_t *hash_data,
3674 data_t *signature_data,
3675 int expected_status_arg )
3676{
3677 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3678 psa_key_type_t key_type = key_type_arg;
3679 psa_algorithm_t alg = alg_arg;
3680 psa_status_t actual_status;
3681 psa_status_t expected_status = expected_status_arg;
3682 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3683
3684 PSA_ASSERT( psa_crypto_init( ) );
3685
3686 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3687 psa_set_key_algorithm( &attributes, alg );
3688 psa_set_key_type( &attributes, key_type );
3689
3690 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3691 &key ) );
3692
3693 actual_status = psa_verify_message( key, alg,
3694 hash_data->x, hash_data->len,
3695 signature_data->x,
3696 signature_data->len );
3697 TEST_EQUAL( actual_status, expected_status );
3698
3699exit:
3700 psa_reset_key_attributes( &attributes );
3701 psa_destroy_key( key );
3702 PSA_DONE( );
3703}
3704/* END_CASE */
3705
3706/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003707void asymmetric_encrypt( int key_type_arg,
3708 data_t *key_data,
3709 int alg_arg,
3710 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003711 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003712 int expected_output_length_arg,
3713 int expected_status_arg )
3714{
Ronald Cron5425a212020-08-04 14:58:35 +02003715 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003716 psa_key_type_t key_type = key_type_arg;
3717 psa_algorithm_t alg = alg_arg;
3718 size_t expected_output_length = expected_output_length_arg;
3719 size_t key_bits;
3720 unsigned char *output = NULL;
3721 size_t output_size;
3722 size_t output_length = ~0;
3723 psa_status_t actual_status;
3724 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003725 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003726
Gilles Peskine8817f612018-12-18 00:18:46 +01003727 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003728
Gilles Peskine656896e2018-06-29 19:12:28 +02003729 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003730 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3731 psa_set_key_algorithm( &attributes, alg );
3732 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003733 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003734 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003735
3736 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003737 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003738 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003739
Gilles Peskine656896e2018-06-29 19:12:28 +02003740 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003741 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003742 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003743
3744 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003745 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003746 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003747 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003748 output, output_size,
3749 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003750 TEST_EQUAL( actual_status, expected_status );
3751 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003752
Gilles Peskine68428122018-06-30 18:42:41 +02003753 /* If the label is empty, the test framework puts a non-null pointer
3754 * in label->x. Test that a null pointer works as well. */
3755 if( label->len == 0 )
3756 {
3757 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003758 if( output_size != 0 )
3759 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003760 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003761 input_data->x, input_data->len,
3762 NULL, label->len,
3763 output, output_size,
3764 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003765 TEST_EQUAL( actual_status, expected_status );
3766 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003767 }
3768
Gilles Peskine656896e2018-06-29 19:12:28 +02003769exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003770 /*
3771 * Key attributes may have been returned by psa_get_key_attributes()
3772 * thus reset them as required.
3773 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003774 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003775
Ronald Cron5425a212020-08-04 14:58:35 +02003776 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02003777 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003778 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003779}
3780/* END_CASE */
3781
3782/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003783void asymmetric_encrypt_decrypt( int key_type_arg,
3784 data_t *key_data,
3785 int alg_arg,
3786 data_t *input_data,
3787 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003788{
Ronald Cron5425a212020-08-04 14:58:35 +02003789 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003790 psa_key_type_t key_type = key_type_arg;
3791 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003792 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003793 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003794 size_t output_size;
3795 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003796 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003797 size_t output2_size;
3798 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003799 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003800
Gilles Peskine8817f612018-12-18 00:18:46 +01003801 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003802
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003803 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3804 psa_set_key_algorithm( &attributes, alg );
3805 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003806
Gilles Peskine049c7532019-05-15 20:22:09 +02003807 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003808 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003809
3810 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02003811 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003812 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003813
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003814 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003815 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003816 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01003817
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003818 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01003819 TEST_ASSERT( output2_size <=
3820 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
3821 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003822 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003823
Gilles Peskineeebd7382018-06-08 18:11:54 +02003824 /* We test encryption by checking that encrypt-then-decrypt gives back
3825 * the original plaintext because of the non-optional random
3826 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02003827 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003828 input_data->x, input_data->len,
3829 label->x, label->len,
3830 output, output_size,
3831 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003832 /* We don't know what ciphertext length to expect, but check that
3833 * it looks sensible. */
3834 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003835
Ronald Cron5425a212020-08-04 14:58:35 +02003836 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003837 output, output_length,
3838 label->x, label->len,
3839 output2, output2_size,
3840 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003841 ASSERT_COMPARE( input_data->x, input_data->len,
3842 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003843
3844exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003845 /*
3846 * Key attributes may have been returned by psa_get_key_attributes()
3847 * thus reset them as required.
3848 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003849 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003850
Ronald Cron5425a212020-08-04 14:58:35 +02003851 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003852 mbedtls_free( output );
3853 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003854 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003855}
3856/* END_CASE */
3857
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003858/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003859void asymmetric_decrypt( int key_type_arg,
3860 data_t *key_data,
3861 int alg_arg,
3862 data_t *input_data,
3863 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003864 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003865{
Ronald Cron5425a212020-08-04 14:58:35 +02003866 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003867 psa_key_type_t key_type = key_type_arg;
3868 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01003869 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003870 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003871 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003872 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003873 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003874
Gilles Peskine8817f612018-12-18 00:18:46 +01003875 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003876
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003877 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3878 psa_set_key_algorithm( &attributes, alg );
3879 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003880
Gilles Peskine049c7532019-05-15 20:22:09 +02003881 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003882 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003883
gabor-mezei-armceface22021-01-21 12:26:17 +01003884 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3885 key_bits = psa_get_key_bits( &attributes );
3886
3887 /* Determine the maximum ciphertext length */
3888 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
3889 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
3890 ASSERT_ALLOC( output, output_size );
3891
Ronald Cron5425a212020-08-04 14:58:35 +02003892 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003893 input_data->x, input_data->len,
3894 label->x, label->len,
3895 output,
3896 output_size,
3897 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003898 ASSERT_COMPARE( expected_data->x, expected_data->len,
3899 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003900
Gilles Peskine68428122018-06-30 18:42:41 +02003901 /* If the label is empty, the test framework puts a non-null pointer
3902 * in label->x. Test that a null pointer works as well. */
3903 if( label->len == 0 )
3904 {
3905 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003906 if( output_size != 0 )
3907 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003908 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003909 input_data->x, input_data->len,
3910 NULL, label->len,
3911 output,
3912 output_size,
3913 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003914 ASSERT_COMPARE( expected_data->x, expected_data->len,
3915 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003916 }
3917
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003918exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003919 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003920 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003921 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003922 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003923}
3924/* END_CASE */
3925
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003926/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003927void asymmetric_decrypt_fail( int key_type_arg,
3928 data_t *key_data,
3929 int alg_arg,
3930 data_t *input_data,
3931 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00003932 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02003933 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003934{
Ronald Cron5425a212020-08-04 14:58:35 +02003935 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003936 psa_key_type_t key_type = key_type_arg;
3937 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003938 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00003939 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003940 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003941 psa_status_t actual_status;
3942 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003943 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003944
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003945 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003946
Gilles Peskine8817f612018-12-18 00:18:46 +01003947 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003948
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003949 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3950 psa_set_key_algorithm( &attributes, alg );
3951 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003952
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 ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003955
Ronald Cron5425a212020-08-04 14:58:35 +02003956 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003957 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003958 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003959 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003960 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003961 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003962 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003963
Gilles Peskine68428122018-06-30 18:42:41 +02003964 /* If the label is empty, the test framework puts a non-null pointer
3965 * in label->x. Test that a null pointer works as well. */
3966 if( label->len == 0 )
3967 {
3968 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003969 if( output_size != 0 )
3970 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003971 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003972 input_data->x, input_data->len,
3973 NULL, label->len,
3974 output, output_size,
3975 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003976 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003977 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003978 }
3979
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003980exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003981 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003982 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02003983 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003984 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003985}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003986/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003987
3988/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003989void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00003990{
3991 /* Test each valid way of initializing the object, except for `= {0}`, as
3992 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3993 * though it's OK by the C standard. We could test for this, but we'd need
3994 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003995 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003996 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
3997 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
3998 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00003999
4000 memset( &zero, 0, sizeof( zero ) );
4001
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004002 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004003 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004004 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004005 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004006 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004007 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004008 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004009
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004010 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004011 PSA_ASSERT( psa_key_derivation_abort(&func) );
4012 PSA_ASSERT( psa_key_derivation_abort(&init) );
4013 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004014}
4015/* END_CASE */
4016
Janos Follath16de4a42019-06-13 16:32:24 +01004017/* BEGIN_CASE */
4018void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004019{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004020 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004021 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004022 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004023
Gilles Peskine8817f612018-12-18 00:18:46 +01004024 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004025
Janos Follath16de4a42019-06-13 16:32:24 +01004026 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004027 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004028
4029exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004030 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004031 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004032}
4033/* END_CASE */
4034
Janos Follathaf3c2a02019-06-12 12:34:34 +01004035/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004036void derive_set_capacity( int alg_arg, int capacity_arg,
4037 int expected_status_arg )
4038{
4039 psa_algorithm_t alg = alg_arg;
4040 size_t capacity = capacity_arg;
4041 psa_status_t expected_status = expected_status_arg;
4042 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4043
4044 PSA_ASSERT( psa_crypto_init( ) );
4045
4046 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4047
4048 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4049 expected_status );
4050
4051exit:
4052 psa_key_derivation_abort( &operation );
4053 PSA_DONE( );
4054}
4055/* END_CASE */
4056
4057/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004058void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004059 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004060 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004061 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004062 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004063 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004064 int expected_status_arg3,
4065 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004066{
4067 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004068 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4069 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004070 psa_status_t expected_statuses[] = {expected_status_arg1,
4071 expected_status_arg2,
4072 expected_status_arg3};
4073 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004074 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4075 MBEDTLS_SVC_KEY_ID_INIT,
4076 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004077 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4078 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4079 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004080 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004081 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004082 psa_status_t expected_output_status = expected_output_status_arg;
4083 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004084
4085 PSA_ASSERT( psa_crypto_init( ) );
4086
4087 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4088 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004089
4090 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4091
4092 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4093 {
Gilles Peskine4023c012021-05-27 13:21:20 +02004094 mbedtls_test_set_step( i );
4095 if( steps[i] == 0 )
4096 {
4097 /* Skip this step */
4098 }
4099 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004100 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004101 psa_set_key_type( &attributes, key_types[i] );
4102 PSA_ASSERT( psa_import_key( &attributes,
4103 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004104 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004105 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4106 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4107 {
4108 // When taking a private key as secret input, use key agreement
4109 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004110 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4111 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004112 expected_statuses[i] );
4113 }
4114 else
4115 {
4116 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004117 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004118 expected_statuses[i] );
4119 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004120 }
4121 else
4122 {
4123 TEST_EQUAL( psa_key_derivation_input_bytes(
4124 &operation, steps[i],
4125 inputs[i]->x, inputs[i]->len ),
4126 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004127 }
4128 }
4129
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004130 if( output_key_type != PSA_KEY_TYPE_NONE )
4131 {
4132 psa_reset_key_attributes( &attributes );
4133 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4134 psa_set_key_bits( &attributes, 8 );
4135 actual_output_status =
4136 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004137 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004138 }
4139 else
4140 {
4141 uint8_t buffer[1];
4142 actual_output_status =
4143 psa_key_derivation_output_bytes( &operation,
4144 buffer, sizeof( buffer ) );
4145 }
4146 TEST_EQUAL( actual_output_status, expected_output_status );
4147
Janos Follathaf3c2a02019-06-12 12:34:34 +01004148exit:
4149 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004150 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4151 psa_destroy_key( keys[i] );
4152 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004153 PSA_DONE( );
4154}
4155/* END_CASE */
4156
Janos Follathd958bb72019-07-03 15:02:16 +01004157/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02004158void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004159{
Janos Follathd958bb72019-07-03 15:02:16 +01004160 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004161 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004162 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004163 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004164 unsigned char input1[] = "Input 1";
4165 size_t input1_length = sizeof( input1 );
4166 unsigned char input2[] = "Input 2";
4167 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004168 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004169 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004170 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4171 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4172 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004173 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004174
Gilles Peskine8817f612018-12-18 00:18:46 +01004175 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004176
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004177 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4178 psa_set_key_algorithm( &attributes, alg );
4179 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004180
Gilles Peskine73676cb2019-05-15 20:15:10 +02004181 PSA_ASSERT( psa_import_key( &attributes,
4182 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004183 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004184
4185 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004186 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4187 input1, input1_length,
4188 input2, input2_length,
4189 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004190 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004191
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004192 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004193 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004194 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004195
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004196 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004197
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004198 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004199 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004200
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004201exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004202 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004203 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004204 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004205}
4206/* END_CASE */
4207
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004208/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02004209void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004210{
4211 uint8_t output_buffer[16];
4212 size_t buffer_size = 16;
4213 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004214 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004215
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004216 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4217 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004218 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004219
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004220 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004221 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004222
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004223 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004224
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004225 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4226 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004227 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004228
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004229 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004230 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004231
4232exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004233 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004234}
4235/* END_CASE */
4236
4237/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004238void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004239 int step1_arg, data_t *input1,
4240 int step2_arg, data_t *input2,
4241 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004242 int requested_capacity_arg,
4243 data_t *expected_output1,
4244 data_t *expected_output2 )
4245{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004246 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004247 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4248 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004249 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4250 MBEDTLS_SVC_KEY_ID_INIT,
4251 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004252 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004253 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004254 uint8_t *expected_outputs[2] =
4255 {expected_output1->x, expected_output2->x};
4256 size_t output_sizes[2] =
4257 {expected_output1->len, expected_output2->len};
4258 size_t output_buffer_size = 0;
4259 uint8_t *output_buffer = NULL;
4260 size_t expected_capacity;
4261 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004262 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004263 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004264 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004265
4266 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4267 {
4268 if( output_sizes[i] > output_buffer_size )
4269 output_buffer_size = output_sizes[i];
4270 if( output_sizes[i] == 0 )
4271 expected_outputs[i] = NULL;
4272 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004273 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004274 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004275
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004276 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4277 psa_set_key_algorithm( &attributes, alg );
4278 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004279
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004280 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004281 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4282 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4283 requested_capacity ) );
4284 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004285 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004286 switch( steps[i] )
4287 {
4288 case 0:
4289 break;
4290 case PSA_KEY_DERIVATION_INPUT_SECRET:
4291 PSA_ASSERT( psa_import_key( &attributes,
4292 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004293 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004294
4295 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4296 {
4297 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4298 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4299 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4300 }
4301
Gilles Peskine1468da72019-05-29 17:35:49 +02004302 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004303 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004304 break;
4305 default:
4306 PSA_ASSERT( psa_key_derivation_input_bytes(
4307 &operation, steps[i],
4308 inputs[i]->x, inputs[i]->len ) );
4309 break;
4310 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004311 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004312
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004313 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004314 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004315 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004316 expected_capacity = requested_capacity;
4317
4318 /* Expansion phase. */
4319 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4320 {
4321 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004322 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004323 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004324 if( expected_capacity == 0 && output_sizes[i] == 0 )
4325 {
4326 /* Reading 0 bytes when 0 bytes are available can go either way. */
4327 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004328 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004329 continue;
4330 }
4331 else if( expected_capacity == 0 ||
4332 output_sizes[i] > expected_capacity )
4333 {
4334 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004335 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004336 expected_capacity = 0;
4337 continue;
4338 }
4339 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004340 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004341 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004342 ASSERT_COMPARE( output_buffer, output_sizes[i],
4343 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004344 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004345 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004346 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004347 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004348 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004349 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004350 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004351
4352exit:
4353 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004354 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004355 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4356 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004357 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004358}
4359/* END_CASE */
4360
4361/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004362void derive_full( int alg_arg,
4363 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004364 data_t *input1,
4365 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004366 int requested_capacity_arg )
4367{
Ronald Cron5425a212020-08-04 14:58:35 +02004368 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004369 psa_algorithm_t alg = alg_arg;
4370 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004371 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004372 unsigned char output_buffer[16];
4373 size_t expected_capacity = requested_capacity;
4374 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004375 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004376
Gilles Peskine8817f612018-12-18 00:18:46 +01004377 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004378
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004379 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4380 psa_set_key_algorithm( &attributes, alg );
4381 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004382
Gilles Peskine049c7532019-05-15 20:22:09 +02004383 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004384 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004385
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004386 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4387 input1->x, input1->len,
4388 input2->x, input2->len,
4389 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004390 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004391
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004392 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004393 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004394 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004395
4396 /* Expansion phase. */
4397 while( current_capacity > 0 )
4398 {
4399 size_t read_size = sizeof( output_buffer );
4400 if( read_size > current_capacity )
4401 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004402 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004403 output_buffer,
4404 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004405 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004406 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004407 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004408 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004409 }
4410
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004411 /* Check that the operation refuses to go over capacity. */
4412 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004413 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004414
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004415 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004416
4417exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004418 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004419 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004420 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004421}
4422/* END_CASE */
4423
Janos Follathe60c9052019-07-03 13:51:30 +01004424/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004425void derive_key_exercise( int alg_arg,
4426 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004427 data_t *input1,
4428 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004429 int derived_type_arg,
4430 int derived_bits_arg,
4431 int derived_usage_arg,
4432 int derived_alg_arg )
4433{
Ronald Cron5425a212020-08-04 14:58:35 +02004434 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4435 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004436 psa_algorithm_t alg = alg_arg;
4437 psa_key_type_t derived_type = derived_type_arg;
4438 size_t derived_bits = derived_bits_arg;
4439 psa_key_usage_t derived_usage = derived_usage_arg;
4440 psa_algorithm_t derived_alg = derived_alg_arg;
4441 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004442 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004443 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004444 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004445
Gilles Peskine8817f612018-12-18 00:18:46 +01004446 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004447
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004448 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4449 psa_set_key_algorithm( &attributes, alg );
4450 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004451 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004452 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004453
4454 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004455 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4456 input1->x, input1->len,
4457 input2->x, input2->len,
4458 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004459 goto exit;
4460
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004461 psa_set_key_usage_flags( &attributes, derived_usage );
4462 psa_set_key_algorithm( &attributes, derived_alg );
4463 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004464 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004465 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004466 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004467
4468 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004469 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004470 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4471 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004472
4473 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004474 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004475 goto exit;
4476
4477exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004478 /*
4479 * Key attributes may have been returned by psa_get_key_attributes()
4480 * thus reset them as required.
4481 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004482 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004483
4484 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004485 psa_destroy_key( base_key );
4486 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004487 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004488}
4489/* END_CASE */
4490
Janos Follath42fd8882019-07-03 14:17:09 +01004491/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004492void derive_key_export( int alg_arg,
4493 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004494 data_t *input1,
4495 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004496 int bytes1_arg,
4497 int bytes2_arg )
4498{
Ronald Cron5425a212020-08-04 14:58:35 +02004499 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4500 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004501 psa_algorithm_t alg = alg_arg;
4502 size_t bytes1 = bytes1_arg;
4503 size_t bytes2 = bytes2_arg;
4504 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004505 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004506 uint8_t *output_buffer = NULL;
4507 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004508 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4509 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004510 size_t length;
4511
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004512 ASSERT_ALLOC( output_buffer, capacity );
4513 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004514 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004515
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004516 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4517 psa_set_key_algorithm( &base_attributes, alg );
4518 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004519 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004520 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004521
4522 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004523 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4524 input1->x, input1->len,
4525 input2->x, input2->len,
4526 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004527 goto exit;
4528
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004529 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004530 output_buffer,
4531 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004532 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004533
4534 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004535 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4536 input1->x, input1->len,
4537 input2->x, input2->len,
4538 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004539 goto exit;
4540
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004541 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4542 psa_set_key_algorithm( &derived_attributes, 0 );
4543 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004544 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004545 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004546 &derived_key ) );
4547 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004548 export_buffer, bytes1,
4549 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004550 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004551 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004552 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004553 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004554 &derived_key ) );
4555 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004556 export_buffer + bytes1, bytes2,
4557 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004558 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004559
4560 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004561 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4562 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004563
4564exit:
4565 mbedtls_free( output_buffer );
4566 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004567 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004568 psa_destroy_key( base_key );
4569 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004570 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004571}
4572/* END_CASE */
4573
4574/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004575void derive_key( int alg_arg,
4576 data_t *key_data, data_t *input1, data_t *input2,
4577 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004578 int expected_status_arg,
4579 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004580{
Ronald Cron5425a212020-08-04 14:58:35 +02004581 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4582 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004583 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004584 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004585 size_t bits = bits_arg;
4586 psa_status_t expected_status = expected_status_arg;
4587 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4588 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4589 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4590
4591 PSA_ASSERT( psa_crypto_init( ) );
4592
4593 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4594 psa_set_key_algorithm( &base_attributes, alg );
4595 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4596 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004597 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004598
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004599 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4600 input1->x, input1->len,
4601 input2->x, input2->len,
4602 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004603 goto exit;
4604
4605 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4606 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004607 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004608 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004609
4610 psa_status_t status =
4611 psa_key_derivation_output_key( &derived_attributes,
4612 &operation,
4613 &derived_key );
4614 if( is_large_output > 0 )
4615 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4616 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004617
4618exit:
4619 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004620 psa_destroy_key( base_key );
4621 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004622 PSA_DONE( );
4623}
4624/* END_CASE */
4625
4626/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004627void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004628 int our_key_type_arg, int our_key_alg_arg,
4629 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004630 int expected_status_arg )
4631{
Ronald Cron5425a212020-08-04 14:58:35 +02004632 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004633 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004634 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004635 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004636 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004637 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004638 psa_status_t expected_status = expected_status_arg;
4639 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004640
Gilles Peskine8817f612018-12-18 00:18:46 +01004641 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004642
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004643 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004644 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004645 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004646 PSA_ASSERT( psa_import_key( &attributes,
4647 our_key_data->x, our_key_data->len,
4648 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004649
Gilles Peskine77f40d82019-04-11 21:27:06 +02004650 /* The tests currently include inputs that should fail at either step.
4651 * Test cases that fail at the setup step should be changed to call
4652 * key_derivation_setup instead, and this function should be renamed
4653 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004654 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004655 if( status == PSA_SUCCESS )
4656 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004657 TEST_EQUAL( psa_key_derivation_key_agreement(
4658 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4659 our_key,
4660 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004661 expected_status );
4662 }
4663 else
4664 {
4665 TEST_ASSERT( status == expected_status );
4666 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004667
4668exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004669 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004670 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004671 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004672}
4673/* END_CASE */
4674
4675/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004676void raw_key_agreement( int alg_arg,
4677 int our_key_type_arg, data_t *our_key_data,
4678 data_t *peer_key_data,
4679 data_t *expected_output )
4680{
Ronald Cron5425a212020-08-04 14:58:35 +02004681 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004682 psa_algorithm_t alg = alg_arg;
4683 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004684 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004685 unsigned char *output = NULL;
4686 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004687 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004688
4689 ASSERT_ALLOC( output, expected_output->len );
4690 PSA_ASSERT( psa_crypto_init( ) );
4691
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004692 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4693 psa_set_key_algorithm( &attributes, alg );
4694 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004695 PSA_ASSERT( psa_import_key( &attributes,
4696 our_key_data->x, our_key_data->len,
4697 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004698
gabor-mezei-armceface22021-01-21 12:26:17 +01004699 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4700 key_bits = psa_get_key_bits( &attributes );
4701
Gilles Peskinebe697d82019-05-16 18:00:41 +02004702 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4703 peer_key_data->x, peer_key_data->len,
4704 output, expected_output->len,
4705 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004706 ASSERT_COMPARE( output, output_length,
4707 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01004708 TEST_ASSERT( output_length <=
4709 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
4710 TEST_ASSERT( output_length <=
4711 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004712
4713exit:
4714 mbedtls_free( output );
4715 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004716 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004717}
4718/* END_CASE */
4719
4720/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004721void key_agreement_capacity( int alg_arg,
4722 int our_key_type_arg, data_t *our_key_data,
4723 data_t *peer_key_data,
4724 int expected_capacity_arg )
4725{
Ronald Cron5425a212020-08-04 14:58:35 +02004726 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004727 psa_algorithm_t alg = alg_arg;
4728 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004729 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004730 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004731 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004732 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004733
Gilles Peskine8817f612018-12-18 00:18:46 +01004734 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004735
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004736 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4737 psa_set_key_algorithm( &attributes, alg );
4738 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004739 PSA_ASSERT( psa_import_key( &attributes,
4740 our_key_data->x, our_key_data->len,
4741 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004742
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004743 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004744 PSA_ASSERT( psa_key_derivation_key_agreement(
4745 &operation,
4746 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4747 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004748 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4749 {
4750 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004751 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004752 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004753 NULL, 0 ) );
4754 }
Gilles Peskine59685592018-09-18 12:11:34 +02004755
Gilles Peskinebf491972018-10-25 22:36:12 +02004756 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004757 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004758 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004759 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004760
Gilles Peskinebf491972018-10-25 22:36:12 +02004761 /* Test the actual capacity by reading the output. */
4762 while( actual_capacity > sizeof( output ) )
4763 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004764 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004765 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004766 actual_capacity -= sizeof( output );
4767 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004768 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004769 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004770 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004771 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004772
Gilles Peskine59685592018-09-18 12:11:34 +02004773exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004774 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004775 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004776 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004777}
4778/* END_CASE */
4779
4780/* BEGIN_CASE */
4781void key_agreement_output( int alg_arg,
4782 int our_key_type_arg, data_t *our_key_data,
4783 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004784 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004785{
Ronald Cron5425a212020-08-04 14:58:35 +02004786 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004787 psa_algorithm_t alg = alg_arg;
4788 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004789 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004790 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004791 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004792
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004793 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4794 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004795
Gilles Peskine8817f612018-12-18 00:18:46 +01004796 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004797
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004798 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4799 psa_set_key_algorithm( &attributes, alg );
4800 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004801 PSA_ASSERT( psa_import_key( &attributes,
4802 our_key_data->x, our_key_data->len,
4803 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004804
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004805 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004806 PSA_ASSERT( psa_key_derivation_key_agreement(
4807 &operation,
4808 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4809 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004810 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4811 {
4812 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004813 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004814 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004815 NULL, 0 ) );
4816 }
Gilles Peskine59685592018-09-18 12:11:34 +02004817
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004818 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004819 actual_output,
4820 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004821 ASSERT_COMPARE( actual_output, expected_output1->len,
4822 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004823 if( expected_output2->len != 0 )
4824 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004825 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004826 actual_output,
4827 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004828 ASSERT_COMPARE( actual_output, expected_output2->len,
4829 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004830 }
Gilles Peskine59685592018-09-18 12:11:34 +02004831
4832exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004833 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004834 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004835 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004836 mbedtls_free( actual_output );
4837}
4838/* END_CASE */
4839
4840/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004841void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004842{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004843 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004844 unsigned char *output = NULL;
4845 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004846 size_t i;
4847 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004848
Simon Butcher49f8e312020-03-03 15:51:50 +00004849 TEST_ASSERT( bytes_arg >= 0 );
4850
Gilles Peskine91892022021-02-08 19:50:26 +01004851 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004852 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02004853
Gilles Peskine8817f612018-12-18 00:18:46 +01004854 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004855
Gilles Peskinea50d7392018-06-21 10:22:13 +02004856 /* Run several times, to ensure that every output byte will be
4857 * nonzero at least once with overwhelming probability
4858 * (2^(-8*number_of_runs)). */
4859 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004860 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004861 if( bytes != 0 )
4862 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004863 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004864
Gilles Peskinea50d7392018-06-21 10:22:13 +02004865 for( i = 0; i < bytes; i++ )
4866 {
4867 if( output[i] != 0 )
4868 ++changed[i];
4869 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004870 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004871
4872 /* Check that every byte was changed to nonzero at least once. This
4873 * validates that psa_generate_random is overwriting every byte of
4874 * the output buffer. */
4875 for( i = 0; i < bytes; i++ )
4876 {
4877 TEST_ASSERT( changed[i] != 0 );
4878 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004879
4880exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004881 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004882 mbedtls_free( output );
4883 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004884}
4885/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004886
4887/* BEGIN_CASE */
4888void generate_key( int type_arg,
4889 int bits_arg,
4890 int usage_arg,
4891 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004892 int expected_status_arg,
4893 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004894{
Ronald Cron5425a212020-08-04 14:58:35 +02004895 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004896 psa_key_type_t type = type_arg;
4897 psa_key_usage_t usage = usage_arg;
4898 size_t bits = bits_arg;
4899 psa_algorithm_t alg = alg_arg;
4900 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004901 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004902 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004903
Gilles Peskine8817f612018-12-18 00:18:46 +01004904 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004905
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004906 psa_set_key_usage_flags( &attributes, usage );
4907 psa_set_key_algorithm( &attributes, alg );
4908 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004909 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004910
4911 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01004912 psa_status_t status = psa_generate_key( &attributes, &key );
4913
4914 if( is_large_key > 0 )
4915 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4916 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004917 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004918 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004919
4920 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004921 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004922 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
4923 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004924
Gilles Peskine818ca122018-06-20 18:16:48 +02004925 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004926 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004927 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004928
4929exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004930 /*
4931 * Key attributes may have been returned by psa_get_key_attributes()
4932 * thus reset them as required.
4933 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004934 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004935
Ronald Cron5425a212020-08-04 14:58:35 +02004936 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004937 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004938}
4939/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004940
Ronald Cronee414c72021-03-18 18:50:08 +01004941/* 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 +02004942void generate_key_rsa( int bits_arg,
4943 data_t *e_arg,
4944 int expected_status_arg )
4945{
Ronald Cron5425a212020-08-04 14:58:35 +02004946 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004947 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02004948 size_t bits = bits_arg;
4949 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
4950 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
4951 psa_status_t expected_status = expected_status_arg;
4952 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4953 uint8_t *exported = NULL;
4954 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004955 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004956 size_t exported_length = SIZE_MAX;
4957 uint8_t *e_read_buffer = NULL;
4958 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02004959 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004960 size_t e_read_length = SIZE_MAX;
4961
4962 if( e_arg->len == 0 ||
4963 ( e_arg->len == 3 &&
4964 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
4965 {
4966 is_default_public_exponent = 1;
4967 e_read_size = 0;
4968 }
4969 ASSERT_ALLOC( e_read_buffer, e_read_size );
4970 ASSERT_ALLOC( exported, exported_size );
4971
4972 PSA_ASSERT( psa_crypto_init( ) );
4973
4974 psa_set_key_usage_flags( &attributes, usage );
4975 psa_set_key_algorithm( &attributes, alg );
4976 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
4977 e_arg->x, e_arg->len ) );
4978 psa_set_key_bits( &attributes, bits );
4979
4980 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02004981 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004982 if( expected_status != PSA_SUCCESS )
4983 goto exit;
4984
4985 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004986 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004987 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4988 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4989 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
4990 e_read_buffer, e_read_size,
4991 &e_read_length ) );
4992 if( is_default_public_exponent )
4993 TEST_EQUAL( e_read_length, 0 );
4994 else
4995 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
4996
4997 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004998 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02004999 goto exit;
5000
5001 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005002 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005003 exported, exported_size,
5004 &exported_length ) );
5005 {
5006 uint8_t *p = exported;
5007 uint8_t *end = exported + exported_length;
5008 size_t len;
5009 /* RSAPublicKey ::= SEQUENCE {
5010 * modulus INTEGER, -- n
5011 * publicExponent INTEGER } -- e
5012 */
5013 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005014 MBEDTLS_ASN1_SEQUENCE |
5015 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005016 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005017 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5018 MBEDTLS_ASN1_INTEGER ) );
5019 if( len >= 1 && p[0] == 0 )
5020 {
5021 ++p;
5022 --len;
5023 }
5024 if( e_arg->len == 0 )
5025 {
5026 TEST_EQUAL( len, 3 );
5027 TEST_EQUAL( p[0], 1 );
5028 TEST_EQUAL( p[1], 0 );
5029 TEST_EQUAL( p[2], 1 );
5030 }
5031 else
5032 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5033 }
5034
5035exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005036 /*
5037 * Key attributes may have been returned by psa_get_key_attributes() or
5038 * set by psa_set_key_domain_parameters() thus reset them as required.
5039 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005040 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005041
Ronald Cron5425a212020-08-04 14:58:35 +02005042 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005043 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005044 mbedtls_free( e_read_buffer );
5045 mbedtls_free( exported );
5046}
5047/* END_CASE */
5048
Darryl Greend49a4992018-06-18 17:27:26 +01005049/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005050void persistent_key_load_key_from_storage( data_t *data,
5051 int type_arg, int bits_arg,
5052 int usage_flags_arg, int alg_arg,
5053 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005054{
Ronald Cron71016a92020-08-28 19:01:50 +02005055 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005056 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005057 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5058 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005059 psa_key_type_t type = type_arg;
5060 size_t bits = bits_arg;
5061 psa_key_usage_t usage_flags = usage_flags_arg;
5062 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005063 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005064 unsigned char *first_export = NULL;
5065 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005066 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005067 size_t first_exported_length;
5068 size_t second_exported_length;
5069
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005070 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5071 {
5072 ASSERT_ALLOC( first_export, export_size );
5073 ASSERT_ALLOC( second_export, export_size );
5074 }
Darryl Greend49a4992018-06-18 17:27:26 +01005075
Gilles Peskine8817f612018-12-18 00:18:46 +01005076 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005077
Gilles Peskinec87af662019-05-15 16:12:22 +02005078 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005079 psa_set_key_usage_flags( &attributes, usage_flags );
5080 psa_set_key_algorithm( &attributes, alg );
5081 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005082 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005083
Darryl Green0c6575a2018-11-07 16:05:30 +00005084 switch( generation_method )
5085 {
5086 case IMPORT_KEY:
5087 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005088 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005089 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005090 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005091
Darryl Green0c6575a2018-11-07 16:05:30 +00005092 case GENERATE_KEY:
5093 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005094 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005095 break;
5096
5097 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005098#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005099 {
5100 /* Create base key */
5101 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5102 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5103 psa_set_key_usage_flags( &base_attributes,
5104 PSA_KEY_USAGE_DERIVE );
5105 psa_set_key_algorithm( &base_attributes, derive_alg );
5106 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005107 PSA_ASSERT( psa_import_key( &base_attributes,
5108 data->x, data->len,
5109 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005110 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005111 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005112 PSA_ASSERT( psa_key_derivation_input_key(
5113 &operation,
5114 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005115 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005116 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005117 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005118 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5119 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005120 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005121 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005122 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005123 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005124 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005125#else
5126 TEST_ASSUME( ! "KDF not supported in this configuration" );
5127#endif
5128 break;
5129
5130 default:
5131 TEST_ASSERT( ! "generation_method not implemented in test" );
5132 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005133 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005134 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005135
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005136 /* Export the key if permitted by the key policy. */
5137 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5138 {
Ronald Cron5425a212020-08-04 14:58:35 +02005139 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005140 first_export, export_size,
5141 &first_exported_length ) );
5142 if( generation_method == IMPORT_KEY )
5143 ASSERT_COMPARE( data->x, data->len,
5144 first_export, first_exported_length );
5145 }
Darryl Greend49a4992018-06-18 17:27:26 +01005146
5147 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005148 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005149 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005150 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005151
Darryl Greend49a4992018-06-18 17:27:26 +01005152 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005153 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005154 TEST_ASSERT( mbedtls_svc_key_id_equal(
5155 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005156 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5157 PSA_KEY_LIFETIME_PERSISTENT );
5158 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5159 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5160 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5161 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005162
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005163 /* Export the key again if permitted by the key policy. */
5164 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005165 {
Ronald Cron5425a212020-08-04 14:58:35 +02005166 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005167 second_export, export_size,
5168 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005169 ASSERT_COMPARE( first_export, first_exported_length,
5170 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005171 }
5172
5173 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005174 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005175 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005176
5177exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005178 /*
5179 * Key attributes may have been returned by psa_get_key_attributes()
5180 * thus reset them as required.
5181 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005182 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005183
Darryl Greend49a4992018-06-18 17:27:26 +01005184 mbedtls_free( first_export );
5185 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005186 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005187 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005188 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005189 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005190}
5191/* END_CASE */