blob: ad9decf6de405a445269708485f8d6c59a99e30f [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 ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001548 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001549 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1550 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001551 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001552 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001553 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001554
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001555 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001556 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001557 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001558 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001559
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001560 /* Check that update calls abort on error. */
1561 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01001562 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001563 ASSERT_OPERATION_IS_ACTIVE( operation );
1564 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
1565 PSA_ERROR_BAD_STATE );
1566 ASSERT_OPERATION_IS_INACTIVE( operation );
1567 PSA_ASSERT( psa_hash_abort( &operation ) );
1568 ASSERT_OPERATION_IS_INACTIVE( operation );
1569
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001570 /* Call update after finish. */
1571 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1572 PSA_ASSERT( psa_hash_finish( &operation,
1573 hash, sizeof( hash ), &hash_len ) );
1574 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001575 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001576 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001577
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001578 /* Call verify without calling setup beforehand. */
1579 TEST_EQUAL( psa_hash_verify( &operation,
1580 valid_hash, sizeof( valid_hash ) ),
1581 PSA_ERROR_BAD_STATE );
1582 PSA_ASSERT( psa_hash_abort( &operation ) );
1583
1584 /* Call verify after finish. */
1585 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1586 PSA_ASSERT( psa_hash_finish( &operation,
1587 hash, sizeof( hash ), &hash_len ) );
1588 TEST_EQUAL( psa_hash_verify( &operation,
1589 valid_hash, sizeof( valid_hash ) ),
1590 PSA_ERROR_BAD_STATE );
1591 PSA_ASSERT( psa_hash_abort( &operation ) );
1592
1593 /* Call verify twice in a row. */
1594 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001595 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001596 PSA_ASSERT( psa_hash_verify( &operation,
1597 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001598 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001599 TEST_EQUAL( psa_hash_verify( &operation,
1600 valid_hash, sizeof( valid_hash ) ),
1601 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001602 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001603 PSA_ASSERT( psa_hash_abort( &operation ) );
1604
1605 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001606 TEST_EQUAL( psa_hash_finish( &operation,
1607 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001608 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001609 PSA_ASSERT( psa_hash_abort( &operation ) );
1610
1611 /* Call finish twice in a row. */
1612 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1613 PSA_ASSERT( psa_hash_finish( &operation,
1614 hash, sizeof( hash ), &hash_len ) );
1615 TEST_EQUAL( psa_hash_finish( &operation,
1616 hash, sizeof( hash ), &hash_len ),
1617 PSA_ERROR_BAD_STATE );
1618 PSA_ASSERT( psa_hash_abort( &operation ) );
1619
1620 /* Call finish after calling verify. */
1621 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1622 PSA_ASSERT( psa_hash_verify( &operation,
1623 valid_hash, sizeof( valid_hash ) ) );
1624 TEST_EQUAL( psa_hash_finish( &operation,
1625 hash, sizeof( hash ), &hash_len ),
1626 PSA_ERROR_BAD_STATE );
1627 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001628
1629exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001630 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001631}
1632/* END_CASE */
1633
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001634/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001635void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001636{
1637 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001638 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1639 * appended to it */
1640 unsigned char hash[] = {
1641 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1642 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1643 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001644 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001645 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001646
Gilles Peskine8817f612018-12-18 00:18:46 +01001647 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001648
itayzafrir27e69452018-11-01 14:26:34 +02001649 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001650 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001651 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001652 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001653 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001654 ASSERT_OPERATION_IS_INACTIVE( operation );
1655 PSA_ASSERT( psa_hash_abort( &operation ) );
1656 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03001657
itayzafrir27e69452018-11-01 14:26:34 +02001658 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001659 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001660 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001661 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001662
itayzafrir27e69452018-11-01 14:26:34 +02001663 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001664 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001665 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001666 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001667
itayzafrirec93d302018-10-18 18:01:10 +03001668exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001669 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001670}
1671/* END_CASE */
1672
Ronald Cronee414c72021-03-18 18:50:08 +01001673/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001674void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001675{
1676 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001677 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001678 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001679 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001680 size_t hash_len;
1681
Gilles Peskine8817f612018-12-18 00:18:46 +01001682 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001683
itayzafrir58028322018-10-25 10:22:01 +03001684 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001685 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001686 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001687 hash, expected_size - 1, &hash_len ),
1688 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001689
1690exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001691 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001692}
1693/* END_CASE */
1694
Ronald Cronee414c72021-03-18 18:50:08 +01001695/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001696void hash_clone_source_state( )
1697{
1698 psa_algorithm_t alg = PSA_ALG_SHA_256;
1699 unsigned char hash[PSA_HASH_MAX_SIZE];
1700 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1701 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1702 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1703 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1704 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1705 size_t hash_len;
1706
1707 PSA_ASSERT( psa_crypto_init( ) );
1708 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1709
1710 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1711 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1712 PSA_ASSERT( psa_hash_finish( &op_finished,
1713 hash, sizeof( hash ), &hash_len ) );
1714 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1715 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1716
1717 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1718 PSA_ERROR_BAD_STATE );
1719
1720 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1721 PSA_ASSERT( psa_hash_finish( &op_init,
1722 hash, sizeof( hash ), &hash_len ) );
1723 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1724 PSA_ASSERT( psa_hash_finish( &op_finished,
1725 hash, sizeof( hash ), &hash_len ) );
1726 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1727 PSA_ASSERT( psa_hash_finish( &op_aborted,
1728 hash, sizeof( hash ), &hash_len ) );
1729
1730exit:
1731 psa_hash_abort( &op_source );
1732 psa_hash_abort( &op_init );
1733 psa_hash_abort( &op_setup );
1734 psa_hash_abort( &op_finished );
1735 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001736 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001737}
1738/* END_CASE */
1739
Ronald Cronee414c72021-03-18 18:50:08 +01001740/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001741void hash_clone_target_state( )
1742{
1743 psa_algorithm_t alg = PSA_ALG_SHA_256;
1744 unsigned char hash[PSA_HASH_MAX_SIZE];
1745 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1746 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1747 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1748 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1749 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1750 size_t hash_len;
1751
1752 PSA_ASSERT( psa_crypto_init( ) );
1753
1754 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1755 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1756 PSA_ASSERT( psa_hash_finish( &op_finished,
1757 hash, sizeof( hash ), &hash_len ) );
1758 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1759 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1760
1761 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1762 PSA_ASSERT( psa_hash_finish( &op_target,
1763 hash, sizeof( hash ), &hash_len ) );
1764
1765 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1766 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1767 PSA_ERROR_BAD_STATE );
1768 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1769 PSA_ERROR_BAD_STATE );
1770
1771exit:
1772 psa_hash_abort( &op_target );
1773 psa_hash_abort( &op_init );
1774 psa_hash_abort( &op_setup );
1775 psa_hash_abort( &op_finished );
1776 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001777 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001778}
1779/* END_CASE */
1780
itayzafrir58028322018-10-25 10:22:01 +03001781/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001782void mac_operation_init( )
1783{
Jaeden Amero252ef282019-02-15 14:05:35 +00001784 const uint8_t input[1] = { 0 };
1785
Jaeden Amero769ce272019-01-04 11:48:03 +00001786 /* Test each valid way of initializing the object, except for `= {0}`, as
1787 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1788 * though it's OK by the C standard. We could test for this, but we'd need
1789 * to supress the Clang warning for the test. */
1790 psa_mac_operation_t func = psa_mac_operation_init( );
1791 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1792 psa_mac_operation_t zero;
1793
1794 memset( &zero, 0, sizeof( zero ) );
1795
Jaeden Amero252ef282019-02-15 14:05:35 +00001796 /* A freshly-initialized MAC operation should not be usable. */
1797 TEST_EQUAL( psa_mac_update( &func,
1798 input, sizeof( input ) ),
1799 PSA_ERROR_BAD_STATE );
1800 TEST_EQUAL( psa_mac_update( &init,
1801 input, sizeof( input ) ),
1802 PSA_ERROR_BAD_STATE );
1803 TEST_EQUAL( psa_mac_update( &zero,
1804 input, sizeof( input ) ),
1805 PSA_ERROR_BAD_STATE );
1806
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001807 /* A default MAC operation should be abortable without error. */
1808 PSA_ASSERT( psa_mac_abort( &func ) );
1809 PSA_ASSERT( psa_mac_abort( &init ) );
1810 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001811}
1812/* END_CASE */
1813
1814/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001815void mac_setup( int key_type_arg,
1816 data_t *key,
1817 int alg_arg,
1818 int expected_status_arg )
1819{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001820 psa_key_type_t key_type = key_type_arg;
1821 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001822 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001823 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001824 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1825#if defined(KNOWN_SUPPORTED_MAC_ALG)
1826 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1827#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001828
Gilles Peskine8817f612018-12-18 00:18:46 +01001829 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001830
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001831 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1832 &operation, &status ) )
1833 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001834 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001835
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001836 /* The operation object should be reusable. */
1837#if defined(KNOWN_SUPPORTED_MAC_ALG)
1838 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1839 smoke_test_key_data,
1840 sizeof( smoke_test_key_data ),
1841 KNOWN_SUPPORTED_MAC_ALG,
1842 &operation, &status ) )
1843 goto exit;
1844 TEST_EQUAL( status, PSA_SUCCESS );
1845#endif
1846
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001847exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001848 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001849}
1850/* END_CASE */
1851
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001852/* 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 +00001853void mac_bad_order( )
1854{
Ronald Cron5425a212020-08-04 14:58:35 +02001855 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001856 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1857 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001858 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001859 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1860 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1861 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001862 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001863 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1864 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1865 size_t sign_mac_length = 0;
1866 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1867 const uint8_t verify_mac[] = {
1868 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1869 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1870 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1871
1872 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001873 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001874 psa_set_key_algorithm( &attributes, alg );
1875 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001876
Ronald Cron5425a212020-08-04 14:58:35 +02001877 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1878 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001879
Jaeden Amero252ef282019-02-15 14:05:35 +00001880 /* Call update without calling setup beforehand. */
1881 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1882 PSA_ERROR_BAD_STATE );
1883 PSA_ASSERT( psa_mac_abort( &operation ) );
1884
1885 /* Call sign finish without calling setup beforehand. */
1886 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1887 &sign_mac_length),
1888 PSA_ERROR_BAD_STATE );
1889 PSA_ASSERT( psa_mac_abort( &operation ) );
1890
1891 /* Call verify finish without calling setup beforehand. */
1892 TEST_EQUAL( psa_mac_verify_finish( &operation,
1893 verify_mac, sizeof( verify_mac ) ),
1894 PSA_ERROR_BAD_STATE );
1895 PSA_ASSERT( psa_mac_abort( &operation ) );
1896
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001897 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001898 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001899 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001900 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001901 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001902 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001903 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001904 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001905
Jaeden Amero252ef282019-02-15 14:05:35 +00001906 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001907 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001908 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1909 PSA_ASSERT( psa_mac_sign_finish( &operation,
1910 sign_mac, sizeof( sign_mac ),
1911 &sign_mac_length ) );
1912 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1913 PSA_ERROR_BAD_STATE );
1914 PSA_ASSERT( psa_mac_abort( &operation ) );
1915
1916 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001917 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001918 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1919 PSA_ASSERT( psa_mac_verify_finish( &operation,
1920 verify_mac, sizeof( verify_mac ) ) );
1921 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1922 PSA_ERROR_BAD_STATE );
1923 PSA_ASSERT( psa_mac_abort( &operation ) );
1924
1925 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001926 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001927 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1928 PSA_ASSERT( psa_mac_sign_finish( &operation,
1929 sign_mac, sizeof( sign_mac ),
1930 &sign_mac_length ) );
1931 TEST_EQUAL( psa_mac_sign_finish( &operation,
1932 sign_mac, sizeof( sign_mac ),
1933 &sign_mac_length ),
1934 PSA_ERROR_BAD_STATE );
1935 PSA_ASSERT( psa_mac_abort( &operation ) );
1936
1937 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001938 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001939 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1940 PSA_ASSERT( psa_mac_verify_finish( &operation,
1941 verify_mac, sizeof( verify_mac ) ) );
1942 TEST_EQUAL( psa_mac_verify_finish( &operation,
1943 verify_mac, sizeof( verify_mac ) ),
1944 PSA_ERROR_BAD_STATE );
1945 PSA_ASSERT( psa_mac_abort( &operation ) );
1946
1947 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02001948 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001949 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01001950 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00001951 TEST_EQUAL( psa_mac_verify_finish( &operation,
1952 verify_mac, sizeof( verify_mac ) ),
1953 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01001954 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00001955 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01001956 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00001957
1958 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02001959 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001960 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01001961 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00001962 TEST_EQUAL( psa_mac_sign_finish( &operation,
1963 sign_mac, sizeof( sign_mac ),
1964 &sign_mac_length ),
1965 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01001966 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00001967 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01001968 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001969
Ronald Cron5425a212020-08-04 14:58:35 +02001970 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001971
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001972exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001973 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001974}
1975/* END_CASE */
1976
1977/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001978void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02001979 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001980 int alg_arg,
1981 data_t *input,
1982 data_t *expected_mac )
1983{
Ronald Cron5425a212020-08-04 14:58:35 +02001984 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001985 psa_key_type_t key_type = key_type_arg;
1986 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001987 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001988 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02001989 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001990 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001991 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001992 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02001993 const size_t output_sizes_to_test[] = {
1994 0,
1995 1,
1996 expected_mac->len - 1,
1997 expected_mac->len,
1998 expected_mac->len + 1,
1999 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002000
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002001 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002002 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002003 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002004
Gilles Peskine8817f612018-12-18 00:18:46 +01002005 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002006
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002007 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002008 psa_set_key_algorithm( &attributes, alg );
2009 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002010
Ronald Cron5425a212020-08-04 14:58:35 +02002011 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2012 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002013
Gilles Peskine8b356b52020-08-25 23:44:59 +02002014 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2015 {
2016 const size_t output_size = output_sizes_to_test[i];
2017 psa_status_t expected_status =
2018 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2019 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002020
Chris Jones9634bb12021-01-20 15:56:42 +00002021 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002022 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002023
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002024 /* Calculate the MAC, one-shot case. */
2025 TEST_EQUAL( psa_mac_compute( key, alg,
2026 input->x, input->len,
2027 actual_mac, output_size, &mac_length ),
2028 expected_status );
2029 if( expected_status == PSA_SUCCESS )
2030 {
2031 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2032 actual_mac, mac_length );
2033 }
2034
2035 if( output_size > 0 )
2036 memset( actual_mac, 0, output_size );
2037
2038 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002039 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002040 PSA_ASSERT( psa_mac_update( &operation,
2041 input->x, input->len ) );
2042 TEST_EQUAL( psa_mac_sign_finish( &operation,
2043 actual_mac, output_size,
2044 &mac_length ),
2045 expected_status );
2046 PSA_ASSERT( psa_mac_abort( &operation ) );
2047
2048 if( expected_status == PSA_SUCCESS )
2049 {
2050 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2051 actual_mac, mac_length );
2052 }
2053 mbedtls_free( actual_mac );
2054 actual_mac = NULL;
2055 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002056
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002057exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002058 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002059 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002060 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002061 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002062}
2063/* END_CASE */
2064
2065/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002066void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002067 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002068 int alg_arg,
2069 data_t *input,
2070 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002071{
Ronald Cron5425a212020-08-04 14:58:35 +02002072 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002073 psa_key_type_t key_type = key_type_arg;
2074 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002075 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002076 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002077 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002078
Gilles Peskine69c12672018-06-28 00:07:19 +02002079 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2080
Gilles Peskine8817f612018-12-18 00:18:46 +01002081 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002082
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002083 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002084 psa_set_key_algorithm( &attributes, alg );
2085 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002086
Ronald Cron5425a212020-08-04 14:58:35 +02002087 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2088 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002089
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002090 /* Verify correct MAC, one-shot case. */
2091 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2092 expected_mac->x, expected_mac->len ) );
2093
2094 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002095 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002096 PSA_ASSERT( psa_mac_update( &operation,
2097 input->x, input->len ) );
2098 PSA_ASSERT( psa_mac_verify_finish( &operation,
2099 expected_mac->x,
2100 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002101
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002102 /* Test a MAC that's too short, one-shot case. */
2103 TEST_EQUAL( psa_mac_verify( key, alg,
2104 input->x, input->len,
2105 expected_mac->x,
2106 expected_mac->len - 1 ),
2107 PSA_ERROR_INVALID_SIGNATURE );
2108
2109 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002110 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002111 PSA_ASSERT( psa_mac_update( &operation,
2112 input->x, input->len ) );
2113 TEST_EQUAL( psa_mac_verify_finish( &operation,
2114 expected_mac->x,
2115 expected_mac->len - 1 ),
2116 PSA_ERROR_INVALID_SIGNATURE );
2117
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002118 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002119 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2120 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002121 TEST_EQUAL( psa_mac_verify( key, alg,
2122 input->x, input->len,
2123 perturbed_mac, expected_mac->len + 1 ),
2124 PSA_ERROR_INVALID_SIGNATURE );
2125
2126 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002127 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002128 PSA_ASSERT( psa_mac_update( &operation,
2129 input->x, input->len ) );
2130 TEST_EQUAL( psa_mac_verify_finish( &operation,
2131 perturbed_mac,
2132 expected_mac->len + 1 ),
2133 PSA_ERROR_INVALID_SIGNATURE );
2134
2135 /* Test changing one byte. */
2136 for( size_t i = 0; i < expected_mac->len; i++ )
2137 {
Chris Jones9634bb12021-01-20 15:56:42 +00002138 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002139 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002140
2141 TEST_EQUAL( psa_mac_verify( key, alg,
2142 input->x, input->len,
2143 perturbed_mac, expected_mac->len ),
2144 PSA_ERROR_INVALID_SIGNATURE );
2145
Ronald Cron5425a212020-08-04 14:58:35 +02002146 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002147 PSA_ASSERT( psa_mac_update( &operation,
2148 input->x, input->len ) );
2149 TEST_EQUAL( psa_mac_verify_finish( &operation,
2150 perturbed_mac,
2151 expected_mac->len ),
2152 PSA_ERROR_INVALID_SIGNATURE );
2153 perturbed_mac[i] ^= 1;
2154 }
2155
Gilles Peskine8c9def32018-02-08 10:02:12 +01002156exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002157 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002158 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002159 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002160 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002161}
2162/* END_CASE */
2163
2164/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002165void cipher_operation_init( )
2166{
Jaeden Ameroab439972019-02-15 14:12:05 +00002167 const uint8_t input[1] = { 0 };
2168 unsigned char output[1] = { 0 };
2169 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002170 /* Test each valid way of initializing the object, except for `= {0}`, as
2171 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2172 * though it's OK by the C standard. We could test for this, but we'd need
2173 * to supress the Clang warning for the test. */
2174 psa_cipher_operation_t func = psa_cipher_operation_init( );
2175 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2176 psa_cipher_operation_t zero;
2177
2178 memset( &zero, 0, sizeof( zero ) );
2179
Jaeden Ameroab439972019-02-15 14:12:05 +00002180 /* A freshly-initialized cipher operation should not be usable. */
2181 TEST_EQUAL( psa_cipher_update( &func,
2182 input, sizeof( input ),
2183 output, sizeof( output ),
2184 &output_length ),
2185 PSA_ERROR_BAD_STATE );
2186 TEST_EQUAL( psa_cipher_update( &init,
2187 input, sizeof( input ),
2188 output, sizeof( output ),
2189 &output_length ),
2190 PSA_ERROR_BAD_STATE );
2191 TEST_EQUAL( psa_cipher_update( &zero,
2192 input, sizeof( input ),
2193 output, sizeof( output ),
2194 &output_length ),
2195 PSA_ERROR_BAD_STATE );
2196
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002197 /* A default cipher operation should be abortable without error. */
2198 PSA_ASSERT( psa_cipher_abort( &func ) );
2199 PSA_ASSERT( psa_cipher_abort( &init ) );
2200 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002201}
2202/* END_CASE */
2203
2204/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002205void cipher_setup( int key_type_arg,
2206 data_t *key,
2207 int alg_arg,
2208 int expected_status_arg )
2209{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002210 psa_key_type_t key_type = key_type_arg;
2211 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002212 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002213 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002214 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002215#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002216 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2217#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002218
Gilles Peskine8817f612018-12-18 00:18:46 +01002219 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002220
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002221 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2222 &operation, &status ) )
2223 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002224 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002225
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002226 /* The operation object should be reusable. */
2227#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2228 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2229 smoke_test_key_data,
2230 sizeof( smoke_test_key_data ),
2231 KNOWN_SUPPORTED_CIPHER_ALG,
2232 &operation, &status ) )
2233 goto exit;
2234 TEST_EQUAL( status, PSA_SUCCESS );
2235#endif
2236
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002237exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002238 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002239 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002240}
2241/* END_CASE */
2242
Ronald Cronee414c72021-03-18 18:50:08 +01002243/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002244void cipher_bad_order( )
2245{
Ronald Cron5425a212020-08-04 14:58:35 +02002246 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002247 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2248 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002249 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002250 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002251 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002252 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002253 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2254 0xaa, 0xaa, 0xaa, 0xaa };
2255 const uint8_t text[] = {
2256 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2257 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002258 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002259 size_t length = 0;
2260
2261 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002262 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2263 psa_set_key_algorithm( &attributes, alg );
2264 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002265 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2266 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002267
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002268 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002269 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002270 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002271 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002272 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002273 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002274 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002275 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002276
2277 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002278 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002279 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002280 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002281 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002282 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002283 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002284 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002285
Jaeden Ameroab439972019-02-15 14:12:05 +00002286 /* Generate an IV without calling setup beforehand. */
2287 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2288 buffer, sizeof( buffer ),
2289 &length ),
2290 PSA_ERROR_BAD_STATE );
2291 PSA_ASSERT( psa_cipher_abort( &operation ) );
2292
2293 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002294 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002295 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2296 buffer, sizeof( buffer ),
2297 &length ) );
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_generate_iv( &operation,
2300 buffer, sizeof( buffer ),
2301 &length ),
2302 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002303 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002304 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002305 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002306
2307 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002308 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002309 PSA_ASSERT( psa_cipher_set_iv( &operation,
2310 iv, sizeof( iv ) ) );
2311 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2312 buffer, sizeof( buffer ),
2313 &length ),
2314 PSA_ERROR_BAD_STATE );
2315 PSA_ASSERT( psa_cipher_abort( &operation ) );
2316
2317 /* Set an IV without calling setup beforehand. */
2318 TEST_EQUAL( psa_cipher_set_iv( &operation,
2319 iv, sizeof( iv ) ),
2320 PSA_ERROR_BAD_STATE );
2321 PSA_ASSERT( psa_cipher_abort( &operation ) );
2322
2323 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002324 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002325 PSA_ASSERT( psa_cipher_set_iv( &operation,
2326 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002327 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002328 TEST_EQUAL( psa_cipher_set_iv( &operation,
2329 iv, sizeof( iv ) ),
2330 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002331 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002332 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002333 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002334
2335 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002336 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002337 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2338 buffer, sizeof( buffer ),
2339 &length ) );
2340 TEST_EQUAL( psa_cipher_set_iv( &operation,
2341 iv, sizeof( iv ) ),
2342 PSA_ERROR_BAD_STATE );
2343 PSA_ASSERT( psa_cipher_abort( &operation ) );
2344
2345 /* Call update without calling setup beforehand. */
2346 TEST_EQUAL( psa_cipher_update( &operation,
2347 text, sizeof( text ),
2348 buffer, sizeof( buffer ),
2349 &length ),
2350 PSA_ERROR_BAD_STATE );
2351 PSA_ASSERT( psa_cipher_abort( &operation ) );
2352
2353 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01002354 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002355 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002356 TEST_EQUAL( psa_cipher_update( &operation,
2357 text, sizeof( text ),
2358 buffer, sizeof( buffer ),
2359 &length ),
2360 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002361 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002362 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002363 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002364
2365 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002366 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002367 PSA_ASSERT( psa_cipher_set_iv( &operation,
2368 iv, sizeof( iv ) ) );
2369 PSA_ASSERT( psa_cipher_finish( &operation,
2370 buffer, sizeof( buffer ), &length ) );
2371 TEST_EQUAL( psa_cipher_update( &operation,
2372 text, sizeof( text ),
2373 buffer, sizeof( buffer ),
2374 &length ),
2375 PSA_ERROR_BAD_STATE );
2376 PSA_ASSERT( psa_cipher_abort( &operation ) );
2377
2378 /* Call finish without calling setup beforehand. */
2379 TEST_EQUAL( psa_cipher_finish( &operation,
2380 buffer, sizeof( buffer ), &length ),
2381 PSA_ERROR_BAD_STATE );
2382 PSA_ASSERT( psa_cipher_abort( &operation ) );
2383
2384 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002385 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002386 /* Not calling update means we are encrypting an empty buffer, which is OK
2387 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01002388 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002389 TEST_EQUAL( psa_cipher_finish( &operation,
2390 buffer, sizeof( buffer ), &length ),
2391 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002392 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002393 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002394 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002395
2396 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002397 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002398 PSA_ASSERT( psa_cipher_set_iv( &operation,
2399 iv, sizeof( iv ) ) );
2400 PSA_ASSERT( psa_cipher_finish( &operation,
2401 buffer, sizeof( buffer ), &length ) );
2402 TEST_EQUAL( psa_cipher_finish( &operation,
2403 buffer, sizeof( buffer ), &length ),
2404 PSA_ERROR_BAD_STATE );
2405 PSA_ASSERT( psa_cipher_abort( &operation ) );
2406
Ronald Cron5425a212020-08-04 14:58:35 +02002407 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002408
Jaeden Ameroab439972019-02-15 14:12:05 +00002409exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002410 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002411 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002412}
2413/* END_CASE */
2414
2415/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002416void cipher_encrypt_error( int alg_arg,
2417 int key_type_arg,
2418 data_t *key_data,
2419 data_t *input,
2420 int expected_status_arg )
2421{
2422 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2423 psa_status_t status;
2424 psa_key_type_t key_type = key_type_arg;
2425 psa_algorithm_t alg = alg_arg;
2426 psa_status_t expected_status = expected_status_arg;
2427 unsigned char *output = NULL;
2428 size_t output_buffer_size = 0;
2429 size_t output_length = 0;
2430 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2431
2432 if ( PSA_ERROR_BAD_STATE != expected_status )
2433 {
2434 PSA_ASSERT( psa_crypto_init( ) );
2435
2436 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2437 psa_set_key_algorithm( &attributes, alg );
2438 psa_set_key_type( &attributes, key_type );
2439
2440 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
2441 input->len );
2442 ASSERT_ALLOC( output, output_buffer_size );
2443
2444 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2445 &key ) );
2446 }
2447
2448 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
2449 output_buffer_size, &output_length );
2450
2451 TEST_EQUAL( status, expected_status );
2452
2453exit:
2454 mbedtls_free( output );
2455 psa_destroy_key( key );
2456 PSA_DONE( );
2457}
2458/* END_CASE */
2459
2460/* BEGIN_CASE */
2461void cipher_encrypt_noiv( int alg_arg,
2462 int key_type_arg,
2463 data_t *key_data,
2464 data_t *input,
2465 data_t *expected_output )
2466{
2467 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2468 psa_key_type_t key_type = key_type_arg;
2469 psa_algorithm_t alg = alg_arg;
2470 unsigned char *output = NULL;
2471 size_t output_buffer_size = 0;
2472 size_t output_length = 0;
2473 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2474
2475 PSA_ASSERT( psa_crypto_init( ) );
2476
2477 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2478 psa_set_key_algorithm( &attributes, alg );
2479 psa_set_key_type( &attributes, key_type );
2480
2481 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2482 ASSERT_ALLOC( output, output_buffer_size );
2483
2484 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2485 &key ) );
2486
2487 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
2488 output_buffer_size, &output_length ) );
2489 TEST_ASSERT( output_length <=
2490 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2491 TEST_ASSERT( output_length <=
2492 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
2493
2494 ASSERT_COMPARE( expected_output->x, expected_output->len,
2495 output, output_length );
2496exit:
2497 mbedtls_free( output );
2498 psa_destroy_key( key );
2499 PSA_DONE( );
2500}
2501/* END_CASE */
2502
2503/* BEGIN_CASE */
2504void cipher_encrypt_sanity( int alg_arg,
2505 int key_type_arg,
2506 data_t *key_data,
2507 data_t *input )
2508{
2509 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2510 psa_key_type_t key_type = key_type_arg;
2511 psa_algorithm_t alg = alg_arg;
2512 unsigned char *output = NULL;
2513 size_t output_buffer_size = 0;
2514 size_t output_length = 0;
2515 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2516
2517 PSA_ASSERT( psa_crypto_init( ) );
2518
2519 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2520 psa_set_key_algorithm( &attributes, alg );
2521 psa_set_key_type( &attributes, key_type );
2522
2523 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2524 ASSERT_ALLOC( output, output_buffer_size );
2525
2526 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2527 &key ) );
2528
2529 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
2530 output_buffer_size, &output_length ) );
2531 TEST_ASSERT( output_length <=
2532 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2533 TEST_ASSERT( output_length <=
2534 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
2535
2536 /* The one-shot cipher encryption uses generated iv so validating
2537 the output is not possible. Only sanity checking is done here. */
2538exit:
2539 mbedtls_free( output );
2540 psa_destroy_key( key );
2541 PSA_DONE( );
2542}
2543/* END_CASE */
2544
2545/* BEGIN_CASE */
2546void cipher_encrypt_validation( int alg_arg,
2547 int key_type_arg,
2548 data_t *key_data,
2549 data_t *input )
2550{
2551 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2552 psa_key_type_t key_type = key_type_arg;
2553 psa_algorithm_t alg = alg_arg;
2554 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
2555 unsigned char *output1 = NULL;
2556 size_t output1_buffer_size = 0;
2557 size_t output1_length = 0;
2558 unsigned char *output2 = NULL;
2559 size_t output2_buffer_size = 0;
2560 size_t output2_length = 0;
2561 size_t function_output_length = 0;
2562 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2563 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2564
2565 PSA_ASSERT( psa_crypto_init( ) );
2566
2567 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2568 psa_set_key_algorithm( &attributes, alg );
2569 psa_set_key_type( &attributes, key_type );
2570
2571 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2572 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2573 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
2574 ASSERT_ALLOC( output1, output1_buffer_size );
2575 ASSERT_ALLOC( output2, output2_buffer_size );
2576
2577 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2578 &key ) );
2579
2580 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
2581 output1_buffer_size, &output1_length ) );
2582 TEST_ASSERT( output1_length <=
2583 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2584 TEST_ASSERT( output1_length <=
2585 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
2586
2587 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2588 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
2589
2590 PSA_ASSERT( psa_cipher_update( &operation,
2591 input->x, input->len,
2592 output2, output2_buffer_size,
2593 &function_output_length ) );
2594 TEST_ASSERT( function_output_length <=
2595 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2596 TEST_ASSERT( function_output_length <=
2597 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2598 output2_length += function_output_length;
2599
2600 PSA_ASSERT( psa_cipher_finish( &operation,
2601 output2 + output2_length,
2602 output2_buffer_size - output2_length,
2603 &function_output_length ) );
2604 TEST_ASSERT( function_output_length <=
2605 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2606 TEST_ASSERT( function_output_length <=
2607 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
2608 output2_length += function_output_length;
2609
2610 PSA_ASSERT( psa_cipher_abort( &operation ) );
2611 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
2612 output2, output2_length );
2613
2614exit:
2615 psa_cipher_abort( &operation );
2616 mbedtls_free( output1 );
2617 mbedtls_free( output2 );
2618 psa_destroy_key( key );
2619 PSA_DONE( );
2620}
2621/* END_CASE */
2622
2623/* BEGIN_CASE */
2624void cipher_encrypt_simple_multipart( int alg_arg,
2625 int key_type_arg,
2626 data_t *key_data,
2627 data_t *iv,
2628 data_t *input,
2629 data_t *expected_output,
2630 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002631{
Ronald Cron5425a212020-08-04 14:58:35 +02002632 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002633 psa_status_t status;
2634 psa_key_type_t key_type = key_type_arg;
2635 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002636 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002637 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002638 size_t output_buffer_size = 0;
2639 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002640 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002641 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002642 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002643
Gilles Peskine8817f612018-12-18 00:18:46 +01002644 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002645
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002646 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2647 psa_set_key_algorithm( &attributes, alg );
2648 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002649
Ronald Cron5425a212020-08-04 14:58:35 +02002650 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2651 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002652
Ronald Cron5425a212020-08-04 14:58:35 +02002653 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002654
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002655 if( iv->len > 0 )
2656 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002657 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002658 }
2659
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002660 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2661 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002662 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002663
Gilles Peskine8817f612018-12-18 00:18:46 +01002664 PSA_ASSERT( psa_cipher_update( &operation,
2665 input->x, input->len,
2666 output, output_buffer_size,
2667 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002668 TEST_ASSERT( function_output_length <=
2669 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2670 TEST_ASSERT( function_output_length <=
2671 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002672 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002673
Gilles Peskine50e586b2018-06-08 14:28:46 +02002674 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002675 ( output_buffer_size == 0 ? NULL :
2676 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002677 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002678 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002679 TEST_ASSERT( function_output_length <=
2680 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2681 TEST_ASSERT( function_output_length <=
2682 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002683 total_output_length += function_output_length;
2684
Gilles Peskinefe11b722018-12-18 00:24:04 +01002685 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002686 if( expected_status == PSA_SUCCESS )
2687 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002688 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002689 ASSERT_COMPARE( expected_output->x, expected_output->len,
2690 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002691 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002692
Gilles Peskine50e586b2018-06-08 14:28:46 +02002693exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002694 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002695 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002696 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002697 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002698}
2699/* END_CASE */
2700
2701/* BEGIN_CASE */
2702void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002703 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002704 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002705 int first_part_size_arg,
2706 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002707 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002708{
Ronald Cron5425a212020-08-04 14:58:35 +02002709 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002710 psa_key_type_t key_type = key_type_arg;
2711 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002712 size_t first_part_size = first_part_size_arg;
2713 size_t output1_length = output1_length_arg;
2714 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002715 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002716 size_t output_buffer_size = 0;
2717 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002718 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002719 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002720 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002721
Gilles Peskine8817f612018-12-18 00:18:46 +01002722 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002723
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002724 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2725 psa_set_key_algorithm( &attributes, alg );
2726 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002727
Ronald Cron5425a212020-08-04 14:58:35 +02002728 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2729 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002730
Ronald Cron5425a212020-08-04 14:58:35 +02002731 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002732
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002733 if( iv->len > 0 )
2734 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002735 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002736 }
2737
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002738 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2739 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002740 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002741
Gilles Peskinee0866522019-02-19 19:44:00 +01002742 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002743 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2744 output, output_buffer_size,
2745 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002746 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002747 TEST_ASSERT( function_output_length <=
2748 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2749 TEST_ASSERT( function_output_length <=
2750 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002751 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002752
Gilles Peskine8817f612018-12-18 00:18:46 +01002753 PSA_ASSERT( psa_cipher_update( &operation,
2754 input->x + first_part_size,
2755 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002756 ( output_buffer_size == 0 ? NULL :
2757 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002758 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002759 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002760 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002761 TEST_ASSERT( function_output_length <=
2762 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2763 alg,
2764 input->len - first_part_size ) );
2765 TEST_ASSERT( function_output_length <=
2766 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002767 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002768
Gilles Peskine8817f612018-12-18 00:18:46 +01002769 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002770 ( output_buffer_size == 0 ? NULL :
2771 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002772 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002773 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002774 TEST_ASSERT( function_output_length <=
2775 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2776 TEST_ASSERT( function_output_length <=
2777 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002778 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002779 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002780
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002781 ASSERT_COMPARE( expected_output->x, expected_output->len,
2782 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002783
2784exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002785 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002786 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002787 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002788 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002789}
2790/* END_CASE */
2791
2792/* BEGIN_CASE */
2793void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002794 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002795 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002796 int first_part_size_arg,
2797 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002798 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002799{
Ronald Cron5425a212020-08-04 14:58:35 +02002800 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002801 psa_key_type_t key_type = key_type_arg;
2802 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002803 size_t first_part_size = first_part_size_arg;
2804 size_t output1_length = output1_length_arg;
2805 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002806 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002807 size_t output_buffer_size = 0;
2808 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002809 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002810 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002811 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002812
Gilles Peskine8817f612018-12-18 00:18:46 +01002813 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002814
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002815 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2816 psa_set_key_algorithm( &attributes, alg );
2817 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002818
Ronald Cron5425a212020-08-04 14:58:35 +02002819 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2820 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002821
Ronald Cron5425a212020-08-04 14:58:35 +02002822 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002823
Steven Cooreman177deba2020-09-07 17:14:14 +02002824 if( iv->len > 0 )
2825 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002826 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002827 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002828
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002829 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2830 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002831 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002832
Gilles Peskinee0866522019-02-19 19:44:00 +01002833 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002834 PSA_ASSERT( psa_cipher_update( &operation,
2835 input->x, first_part_size,
2836 output, output_buffer_size,
2837 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002838 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002839 TEST_ASSERT( function_output_length <=
2840 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2841 TEST_ASSERT( function_output_length <=
2842 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002843 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002844
Gilles Peskine8817f612018-12-18 00:18:46 +01002845 PSA_ASSERT( psa_cipher_update( &operation,
2846 input->x + first_part_size,
2847 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002848 ( output_buffer_size == 0 ? NULL :
2849 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002850 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002851 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002852 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002853 TEST_ASSERT( function_output_length <=
2854 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2855 alg,
2856 input->len - first_part_size ) );
2857 TEST_ASSERT( function_output_length <=
2858 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002859 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002860
Gilles Peskine8817f612018-12-18 00:18:46 +01002861 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002862 ( output_buffer_size == 0 ? NULL :
2863 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002864 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002865 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002866 TEST_ASSERT( function_output_length <=
2867 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2868 TEST_ASSERT( function_output_length <=
2869 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002870 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002871 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002872
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002873 ASSERT_COMPARE( expected_output->x, expected_output->len,
2874 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002875
2876exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002877 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002878 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002879 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002880 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002881}
2882/* END_CASE */
2883
Gilles Peskine50e586b2018-06-08 14:28:46 +02002884/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002885void cipher_decrypt_simple_multipart( int alg_arg,
2886 int key_type_arg,
2887 data_t *key_data,
2888 data_t *iv,
2889 data_t *input,
2890 data_t *expected_output,
2891 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002892{
Ronald Cron5425a212020-08-04 14:58:35 +02002893 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002894 psa_status_t status;
2895 psa_key_type_t key_type = key_type_arg;
2896 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002897 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002898 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002899 size_t output_buffer_size = 0;
2900 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002901 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002902 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002903 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002904
Gilles Peskine8817f612018-12-18 00:18:46 +01002905 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002906
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002907 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2908 psa_set_key_algorithm( &attributes, alg );
2909 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002910
Ronald Cron5425a212020-08-04 14:58:35 +02002911 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2912 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002913
Ronald Cron5425a212020-08-04 14:58:35 +02002914 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002915
Steven Cooreman177deba2020-09-07 17:14:14 +02002916 if( iv->len > 0 )
2917 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002918 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002919 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002920
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002921 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2922 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002923 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002924
Gilles Peskine8817f612018-12-18 00:18:46 +01002925 PSA_ASSERT( psa_cipher_update( &operation,
2926 input->x, input->len,
2927 output, output_buffer_size,
2928 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002929 TEST_ASSERT( function_output_length <=
2930 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2931 TEST_ASSERT( function_output_length <=
2932 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002933 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002934
Gilles Peskine50e586b2018-06-08 14:28:46 +02002935 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002936 ( output_buffer_size == 0 ? NULL :
2937 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002938 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002939 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002940 TEST_ASSERT( function_output_length <=
2941 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2942 TEST_ASSERT( function_output_length <=
2943 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002944 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002945 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002946
2947 if( expected_status == PSA_SUCCESS )
2948 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002949 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002950 ASSERT_COMPARE( expected_output->x, expected_output->len,
2951 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002952 }
2953
Gilles Peskine50e586b2018-06-08 14:28:46 +02002954exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002955 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002956 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002957 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002958 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002959}
2960/* END_CASE */
2961
Gilles Peskine50e586b2018-06-08 14:28:46 +02002962/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002963void cipher_decrypt_error( int alg_arg,
2964 int key_type_arg,
2965 data_t *key_data,
2966 data_t *iv,
2967 data_t *input_arg,
2968 int expected_status_arg )
2969{
2970 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2971 psa_status_t status;
2972 psa_key_type_t key_type = key_type_arg;
2973 psa_algorithm_t alg = alg_arg;
2974 psa_status_t expected_status = expected_status_arg;
2975 unsigned char *input = NULL;
2976 size_t input_buffer_size = 0;
2977 unsigned char *output = NULL;
2978 size_t output_buffer_size = 0;
2979 size_t output_length = 0;
2980 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2981
2982 if ( PSA_ERROR_BAD_STATE != expected_status )
2983 {
2984 PSA_ASSERT( psa_crypto_init( ) );
2985
2986 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2987 psa_set_key_algorithm( &attributes, alg );
2988 psa_set_key_type( &attributes, key_type );
2989
2990 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2991 &key ) );
2992 }
2993
2994 /* Allocate input buffer and copy the iv and the plaintext */
2995 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
2996 if ( input_buffer_size > 0 )
2997 {
2998 ASSERT_ALLOC( input, input_buffer_size );
2999 memcpy( input, iv->x, iv->len );
3000 memcpy( input + iv->len, input_arg->x, input_arg->len );
3001 }
3002
3003 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3004 ASSERT_ALLOC( output, output_buffer_size );
3005
3006 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3007 output_buffer_size, &output_length );
3008 TEST_EQUAL( status, expected_status );
3009
3010exit:
3011 mbedtls_free( input );
3012 mbedtls_free( output );
3013 psa_destroy_key( key );
3014 PSA_DONE( );
3015}
3016/* END_CASE */
3017
3018/* BEGIN_CASE */
3019void cipher_decrypt( int alg_arg,
3020 int key_type_arg,
3021 data_t *key_data,
3022 data_t *iv,
3023 data_t *input_arg,
3024 data_t *expected_output )
3025{
3026 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3027 psa_key_type_t key_type = key_type_arg;
3028 psa_algorithm_t alg = alg_arg;
3029 unsigned char *input = NULL;
3030 size_t input_buffer_size = 0;
3031 unsigned char *output = NULL;
3032 size_t output_buffer_size = 0;
3033 size_t output_length = 0;
3034 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3035
3036 PSA_ASSERT( psa_crypto_init( ) );
3037
3038 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3039 psa_set_key_algorithm( &attributes, alg );
3040 psa_set_key_type( &attributes, key_type );
3041
3042 /* Allocate input buffer and copy the iv and the plaintext */
3043 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3044 if ( input_buffer_size > 0 )
3045 {
3046 ASSERT_ALLOC( input, input_buffer_size );
3047 memcpy( input, iv->x, iv->len );
3048 memcpy( input + iv->len, input_arg->x, input_arg->len );
3049 }
3050
3051 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3052 ASSERT_ALLOC( output, output_buffer_size );
3053
3054 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3055 &key ) );
3056
3057 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3058 output_buffer_size, &output_length ) );
3059 TEST_ASSERT( output_length <=
3060 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
3061 TEST_ASSERT( output_length <=
3062 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
3063
3064 ASSERT_COMPARE( expected_output->x, expected_output->len,
3065 output, output_length );
3066exit:
3067 mbedtls_free( input );
3068 mbedtls_free( output );
3069 psa_destroy_key( key );
3070 PSA_DONE( );
3071}
3072/* END_CASE */
3073
3074/* BEGIN_CASE */
3075void cipher_verify_output( int alg_arg,
3076 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003077 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003078 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003079{
Ronald Cron5425a212020-08-04 14:58:35 +02003080 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003081 psa_key_type_t key_type = key_type_arg;
3082 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003083 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003084 size_t output1_size = 0;
3085 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003086 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003087 size_t output2_size = 0;
3088 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003089 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003090
Gilles Peskine8817f612018-12-18 00:18:46 +01003091 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003092
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003093 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3094 psa_set_key_algorithm( &attributes, alg );
3095 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003096
Ronald Cron5425a212020-08-04 14:58:35 +02003097 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3098 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003099 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003100 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003101
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003102 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3103 output1, output1_size,
3104 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003105 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003106 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003107 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003108 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003109
3110 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003111 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003112
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003113 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3114 output2, output2_size,
3115 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003116 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003117 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003118 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003119 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003120
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003121 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003122
3123exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003124 mbedtls_free( output1 );
3125 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003126 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003127 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003128}
3129/* END_CASE */
3130
3131/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003132void cipher_verify_output_multipart( int alg_arg,
3133 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003134 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003135 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003136 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003137{
Ronald Cron5425a212020-08-04 14:58:35 +02003138 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003139 psa_key_type_t key_type = key_type_arg;
3140 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003141 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003142 unsigned char iv[16] = {0};
3143 size_t iv_size = 16;
3144 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003145 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003146 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003147 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003148 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003149 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003150 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003151 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003152 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3153 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003154 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003155
Gilles Peskine8817f612018-12-18 00:18:46 +01003156 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003157
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003158 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3159 psa_set_key_algorithm( &attributes, alg );
3160 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003161
Ronald Cron5425a212020-08-04 14:58:35 +02003162 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3163 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003164
Ronald Cron5425a212020-08-04 14:58:35 +02003165 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3166 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003167
Steven Cooreman177deba2020-09-07 17:14:14 +02003168 if( alg != PSA_ALG_ECB_NO_PADDING )
3169 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003170 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3171 iv, iv_size,
3172 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003173 }
3174
gabor-mezei-armceface22021-01-21 12:26:17 +01003175 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3176 TEST_ASSERT( output1_buffer_size <=
3177 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003178 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003179
Gilles Peskinee0866522019-02-19 19:44:00 +01003180 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003181
Gilles Peskine8817f612018-12-18 00:18:46 +01003182 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3183 output1, output1_buffer_size,
3184 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003185 TEST_ASSERT( function_output_length <=
3186 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3187 TEST_ASSERT( function_output_length <=
3188 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003189 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003190
Gilles Peskine8817f612018-12-18 00:18:46 +01003191 PSA_ASSERT( psa_cipher_update( &operation1,
3192 input->x + first_part_size,
3193 input->len - first_part_size,
3194 output1, output1_buffer_size,
3195 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003196 TEST_ASSERT( function_output_length <=
3197 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3198 alg,
3199 input->len - first_part_size ) );
3200 TEST_ASSERT( function_output_length <=
3201 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003202 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003203
Gilles Peskine8817f612018-12-18 00:18:46 +01003204 PSA_ASSERT( psa_cipher_finish( &operation1,
3205 output1 + output1_length,
3206 output1_buffer_size - output1_length,
3207 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003208 TEST_ASSERT( function_output_length <=
3209 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3210 TEST_ASSERT( function_output_length <=
3211 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003212 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003213
Gilles Peskine8817f612018-12-18 00:18:46 +01003214 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003215
Gilles Peskine048b7f02018-06-08 14:20:49 +02003216 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003217 TEST_ASSERT( output2_buffer_size <=
3218 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3219 TEST_ASSERT( output2_buffer_size <=
3220 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003221 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003222
Steven Cooreman177deba2020-09-07 17:14:14 +02003223 if( iv_length > 0 )
3224 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003225 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3226 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003227 }
Moran Pekerded84402018-06-06 16:36:50 +03003228
Gilles Peskine8817f612018-12-18 00:18:46 +01003229 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3230 output2, output2_buffer_size,
3231 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003232 TEST_ASSERT( function_output_length <=
3233 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3234 TEST_ASSERT( function_output_length <=
3235 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003236 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003237
Gilles Peskine8817f612018-12-18 00:18:46 +01003238 PSA_ASSERT( psa_cipher_update( &operation2,
3239 output1 + first_part_size,
3240 output1_length - first_part_size,
3241 output2, output2_buffer_size,
3242 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003243 TEST_ASSERT( function_output_length <=
3244 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3245 alg,
3246 output1_length - first_part_size ) );
3247 TEST_ASSERT( function_output_length <=
3248 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003249 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003250
Gilles Peskine8817f612018-12-18 00:18:46 +01003251 PSA_ASSERT( psa_cipher_finish( &operation2,
3252 output2 + output2_length,
3253 output2_buffer_size - output2_length,
3254 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003255 TEST_ASSERT( function_output_length <=
3256 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3257 TEST_ASSERT( function_output_length <=
3258 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003259 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003260
Gilles Peskine8817f612018-12-18 00:18:46 +01003261 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003262
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003263 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003264
3265exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003266 psa_cipher_abort( &operation1 );
3267 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003268 mbedtls_free( output1 );
3269 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003270 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003271 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003272}
3273/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003274
Gilles Peskine20035e32018-02-03 22:44:14 +01003275/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003276void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003277 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003278 data_t *nonce,
3279 data_t *additional_data,
3280 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003281 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003282{
Ronald Cron5425a212020-08-04 14:58:35 +02003283 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003284 psa_key_type_t key_type = key_type_arg;
3285 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003286 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003287 unsigned char *output_data = NULL;
3288 size_t output_size = 0;
3289 size_t output_length = 0;
3290 unsigned char *output_data2 = NULL;
3291 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003292 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003293 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003294 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003295
Gilles Peskine8817f612018-12-18 00:18:46 +01003296 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003297
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003298 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3299 psa_set_key_algorithm( &attributes, alg );
3300 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003301
Gilles Peskine049c7532019-05-15 20:22:09 +02003302 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003303 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003304 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3305 key_bits = psa_get_key_bits( &attributes );
3306
3307 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3308 alg );
3309 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3310 * should be exact. */
3311 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3312 expected_result != PSA_ERROR_NOT_SUPPORTED )
3313 {
3314 TEST_EQUAL( output_size,
3315 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3316 TEST_ASSERT( output_size <=
3317 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3318 }
3319 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003320
Steven Cooremanf49478b2021-02-15 15:19:25 +01003321 status = psa_aead_encrypt( key, alg,
3322 nonce->x, nonce->len,
3323 additional_data->x,
3324 additional_data->len,
3325 input_data->x, input_data->len,
3326 output_data, output_size,
3327 &output_length );
3328
3329 /* If the operation is not supported, just skip and not fail in case the
3330 * encryption involves a common limitation of cryptography hardwares and
3331 * an alternative implementation. */
3332 if( status == PSA_ERROR_NOT_SUPPORTED )
3333 {
3334 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3335 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3336 }
3337
3338 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003339
3340 if( PSA_SUCCESS == expected_result )
3341 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003342 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003343
Gilles Peskine003a4a92019-05-14 16:09:40 +02003344 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3345 * should be exact. */
3346 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003347 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003348
gabor-mezei-armceface22021-01-21 12:26:17 +01003349 TEST_ASSERT( input_data->len <=
3350 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3351
Ronald Cron5425a212020-08-04 14:58:35 +02003352 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003353 nonce->x, nonce->len,
3354 additional_data->x,
3355 additional_data->len,
3356 output_data, output_length,
3357 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003358 &output_length2 ),
3359 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003360
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003361 ASSERT_COMPARE( input_data->x, input_data->len,
3362 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003363 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003364
Gilles Peskinea1cac842018-06-11 19:33:02 +02003365exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003366 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003367 mbedtls_free( output_data );
3368 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003369 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003370}
3371/* END_CASE */
3372
3373/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003374void aead_encrypt( int key_type_arg, data_t *key_data,
3375 int alg_arg,
3376 data_t *nonce,
3377 data_t *additional_data,
3378 data_t *input_data,
3379 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003380{
Ronald Cron5425a212020-08-04 14:58:35 +02003381 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003382 psa_key_type_t key_type = key_type_arg;
3383 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003384 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003385 unsigned char *output_data = NULL;
3386 size_t output_size = 0;
3387 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003388 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003389 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003390
Gilles Peskine8817f612018-12-18 00:18:46 +01003391 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003392
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003393 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3394 psa_set_key_algorithm( &attributes, alg );
3395 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003396
Gilles Peskine049c7532019-05-15 20:22:09 +02003397 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003398 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003399 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3400 key_bits = psa_get_key_bits( &attributes );
3401
3402 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3403 alg );
3404 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3405 * should be exact. */
3406 TEST_EQUAL( output_size,
3407 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3408 TEST_ASSERT( output_size <=
3409 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3410 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003411
Steven Cooremand588ea12021-01-11 19:36:04 +01003412 status = psa_aead_encrypt( key, alg,
3413 nonce->x, nonce->len,
3414 additional_data->x, additional_data->len,
3415 input_data->x, input_data->len,
3416 output_data, output_size,
3417 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003418
Ronald Cron28a45ed2021-02-09 20:35:42 +01003419 /* If the operation is not supported, just skip and not fail in case the
3420 * encryption involves a common limitation of cryptography hardwares and
3421 * an alternative implementation. */
3422 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003423 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003424 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3425 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003426 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003427
3428 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003429 ASSERT_COMPARE( expected_result->x, expected_result->len,
3430 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003431
Gilles Peskinea1cac842018-06-11 19:33:02 +02003432exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003433 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003434 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003435 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003436}
3437/* END_CASE */
3438
3439/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003440void aead_decrypt( int key_type_arg, data_t *key_data,
3441 int alg_arg,
3442 data_t *nonce,
3443 data_t *additional_data,
3444 data_t *input_data,
3445 data_t *expected_data,
3446 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003447{
Ronald Cron5425a212020-08-04 14:58:35 +02003448 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003449 psa_key_type_t key_type = key_type_arg;
3450 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003451 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003452 unsigned char *output_data = NULL;
3453 size_t output_size = 0;
3454 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003455 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003456 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003457 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003458
Gilles Peskine8817f612018-12-18 00:18:46 +01003459 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003460
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003461 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3462 psa_set_key_algorithm( &attributes, alg );
3463 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003464
Gilles Peskine049c7532019-05-15 20:22:09 +02003465 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003466 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003467 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3468 key_bits = psa_get_key_bits( &attributes );
3469
3470 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3471 alg );
3472 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3473 expected_result != PSA_ERROR_NOT_SUPPORTED )
3474 {
3475 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3476 * should be exact. */
3477 TEST_EQUAL( output_size,
3478 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3479 TEST_ASSERT( output_size <=
3480 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3481 }
3482 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003483
Steven Cooremand588ea12021-01-11 19:36:04 +01003484 status = psa_aead_decrypt( key, alg,
3485 nonce->x, nonce->len,
3486 additional_data->x,
3487 additional_data->len,
3488 input_data->x, input_data->len,
3489 output_data, output_size,
3490 &output_length );
3491
Ronald Cron28a45ed2021-02-09 20:35:42 +01003492 /* If the operation is not supported, just skip and not fail in case the
3493 * decryption involves a common limitation of cryptography hardwares and
3494 * an alternative implementation. */
3495 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003496 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003497 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3498 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003499 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003500
3501 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003502
Gilles Peskine2d277862018-06-18 15:41:12 +02003503 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003504 ASSERT_COMPARE( expected_data->x, expected_data->len,
3505 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003506
Gilles Peskinea1cac842018-06-11 19:33:02 +02003507exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003508 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003509 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003510 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003511}
3512/* END_CASE */
3513
3514/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003515void signature_size( int type_arg,
3516 int bits,
3517 int alg_arg,
3518 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003519{
3520 psa_key_type_t type = type_arg;
3521 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003522 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003523
Gilles Peskinefe11b722018-12-18 00:24:04 +01003524 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003525
Gilles Peskinee59236f2018-01-27 23:32:46 +01003526exit:
3527 ;
3528}
3529/* END_CASE */
3530
3531/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003532void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3533 int alg_arg, data_t *input_data,
3534 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003535{
Ronald Cron5425a212020-08-04 14:58:35 +02003536 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003537 psa_key_type_t key_type = key_type_arg;
3538 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003539 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003540 unsigned char *signature = NULL;
3541 size_t signature_size;
3542 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003543 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003544
Gilles Peskine8817f612018-12-18 00:18:46 +01003545 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003546
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003547 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003548 psa_set_key_algorithm( &attributes, alg );
3549 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003550
Gilles Peskine049c7532019-05-15 20:22:09 +02003551 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003552 &key ) );
3553 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003554 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003555
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003556 /* Allocate a buffer which has the size advertized by the
3557 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003558 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003559 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003560 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003561 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003562 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003563
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003564 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003565 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003566 input_data->x, input_data->len,
3567 signature, signature_size,
3568 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003569 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003570 ASSERT_COMPARE( output_data->x, output_data->len,
3571 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003572
3573exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003574 /*
3575 * Key attributes may have been returned by psa_get_key_attributes()
3576 * thus reset them as required.
3577 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003578 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003579
Ronald Cron5425a212020-08-04 14:58:35 +02003580 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003581 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003582 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003583}
3584/* END_CASE */
3585
3586/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003587void sign_hash_fail( int key_type_arg, data_t *key_data,
3588 int alg_arg, data_t *input_data,
3589 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003590{
Ronald Cron5425a212020-08-04 14:58:35 +02003591 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003592 psa_key_type_t key_type = key_type_arg;
3593 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003594 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003595 psa_status_t actual_status;
3596 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003597 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003598 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003599 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003600
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003601 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003602
Gilles Peskine8817f612018-12-18 00:18:46 +01003603 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003604
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003605 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003606 psa_set_key_algorithm( &attributes, alg );
3607 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003608
Gilles Peskine049c7532019-05-15 20:22:09 +02003609 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003610 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003611
Ronald Cron5425a212020-08-04 14:58:35 +02003612 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003613 input_data->x, input_data->len,
3614 signature, signature_size,
3615 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003616 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003617 /* The value of *signature_length is unspecified on error, but
3618 * whatever it is, it should be less than signature_size, so that
3619 * if the caller tries to read *signature_length bytes without
3620 * checking the error code then they don't overflow a buffer. */
3621 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003622
3623exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003624 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003625 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003626 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003627 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003628}
3629/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003630
3631/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003632void sign_verify_hash( int key_type_arg, data_t *key_data,
3633 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003634{
Ronald Cron5425a212020-08-04 14:58:35 +02003635 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003636 psa_key_type_t key_type = key_type_arg;
3637 psa_algorithm_t alg = alg_arg;
3638 size_t key_bits;
3639 unsigned char *signature = NULL;
3640 size_t signature_size;
3641 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003642 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003643
Gilles Peskine8817f612018-12-18 00:18:46 +01003644 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003645
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003646 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003647 psa_set_key_algorithm( &attributes, alg );
3648 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003649
Gilles Peskine049c7532019-05-15 20:22:09 +02003650 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003651 &key ) );
3652 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003653 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003654
3655 /* Allocate a buffer which has the size advertized by the
3656 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003657 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003658 key_bits, alg );
3659 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003660 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003661 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003662
3663 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003664 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003665 input_data->x, input_data->len,
3666 signature, signature_size,
3667 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003668 /* Check that the signature length looks sensible. */
3669 TEST_ASSERT( signature_length <= signature_size );
3670 TEST_ASSERT( signature_length > 0 );
3671
3672 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003673 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003674 input_data->x, input_data->len,
3675 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003676
3677 if( input_data->len != 0 )
3678 {
3679 /* Flip a bit in the input and verify that the signature is now
3680 * detected as invalid. Flip a bit at the beginning, not at the end,
3681 * because ECDSA may ignore the last few bits of the input. */
3682 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003683 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003684 input_data->x, input_data->len,
3685 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003686 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003687 }
3688
3689exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003690 /*
3691 * Key attributes may have been returned by psa_get_key_attributes()
3692 * thus reset them as required.
3693 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003694 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003695
Ronald Cron5425a212020-08-04 14:58:35 +02003696 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003697 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003698 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003699}
3700/* END_CASE */
3701
3702/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003703void verify_hash( int key_type_arg, data_t *key_data,
3704 int alg_arg, data_t *hash_data,
3705 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003706{
Ronald Cron5425a212020-08-04 14:58:35 +02003707 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003708 psa_key_type_t key_type = key_type_arg;
3709 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003710 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003711
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003712 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003713
Gilles Peskine8817f612018-12-18 00:18:46 +01003714 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003715
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003716 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003717 psa_set_key_algorithm( &attributes, alg );
3718 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003719
Gilles Peskine049c7532019-05-15 20:22:09 +02003720 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003721 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003722
Ronald Cron5425a212020-08-04 14:58:35 +02003723 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003724 hash_data->x, hash_data->len,
3725 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003726
itayzafrir5c753392018-05-08 11:18:38 +03003727exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003728 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003729 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003730 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003731}
3732/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003733
3734/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003735void verify_hash_fail( int key_type_arg, data_t *key_data,
3736 int alg_arg, data_t *hash_data,
3737 data_t *signature_data,
3738 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003739{
Ronald Cron5425a212020-08-04 14:58:35 +02003740 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003741 psa_key_type_t key_type = key_type_arg;
3742 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003743 psa_status_t actual_status;
3744 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003745 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003746
Gilles Peskine8817f612018-12-18 00:18:46 +01003747 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003748
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003749 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003750 psa_set_key_algorithm( &attributes, alg );
3751 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003752
Gilles Peskine049c7532019-05-15 20:22:09 +02003753 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003754 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003755
Ronald Cron5425a212020-08-04 14:58:35 +02003756 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003757 hash_data->x, hash_data->len,
3758 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003759 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003760
3761exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003762 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003763 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003764 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003765}
3766/* END_CASE */
3767
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003768/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02003769void sign_message_deterministic( int key_type_arg,
3770 data_t *key_data,
3771 int alg_arg,
3772 data_t *input_data,
3773 data_t *output_data )
3774{
3775 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3776 psa_key_type_t key_type = key_type_arg;
3777 psa_algorithm_t alg = alg_arg;
3778 size_t key_bits;
3779 unsigned char *signature = NULL;
3780 size_t signature_size;
3781 size_t signature_length = 0xdeadbeef;
3782 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3783
3784 PSA_ASSERT( psa_crypto_init( ) );
3785
3786 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3787 psa_set_key_algorithm( &attributes, alg );
3788 psa_set_key_type( &attributes, key_type );
3789
3790 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3791 &key ) );
3792 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3793 key_bits = psa_get_key_bits( &attributes );
3794
3795 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3796 TEST_ASSERT( signature_size != 0 );
3797 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3798 ASSERT_ALLOC( signature, signature_size );
3799
3800 PSA_ASSERT( psa_sign_message( key, alg,
3801 input_data->x, input_data->len,
3802 signature, signature_size,
3803 &signature_length ) );
3804
3805 ASSERT_COMPARE( output_data->x, output_data->len,
3806 signature, signature_length );
3807
3808exit:
3809 psa_reset_key_attributes( &attributes );
3810
3811 psa_destroy_key( key );
3812 mbedtls_free( signature );
3813 PSA_DONE( );
3814
3815}
3816/* END_CASE */
3817
3818/* BEGIN_CASE */
3819void sign_message_fail( int key_type_arg,
3820 data_t *key_data,
3821 int alg_arg,
3822 data_t *input_data,
3823 int signature_size_arg,
3824 int expected_status_arg )
3825{
3826 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3827 psa_key_type_t key_type = key_type_arg;
3828 psa_algorithm_t alg = alg_arg;
3829 size_t signature_size = signature_size_arg;
3830 psa_status_t actual_status;
3831 psa_status_t expected_status = expected_status_arg;
3832 unsigned char *signature = NULL;
3833 size_t signature_length = 0xdeadbeef;
3834 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3835
3836 ASSERT_ALLOC( signature, signature_size );
3837
3838 PSA_ASSERT( psa_crypto_init( ) );
3839
3840 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3841 psa_set_key_algorithm( &attributes, alg );
3842 psa_set_key_type( &attributes, key_type );
3843
3844 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3845 &key ) );
3846
3847 actual_status = psa_sign_message( key, alg,
3848 input_data->x, input_data->len,
3849 signature, signature_size,
3850 &signature_length );
3851 TEST_EQUAL( actual_status, expected_status );
3852 /* The value of *signature_length is unspecified on error, but
3853 * whatever it is, it should be less than signature_size, so that
3854 * if the caller tries to read *signature_length bytes without
3855 * checking the error code then they don't overflow a buffer. */
3856 TEST_ASSERT( signature_length <= signature_size );
3857
3858exit:
3859 psa_reset_key_attributes( &attributes );
3860 psa_destroy_key( key );
3861 mbedtls_free( signature );
3862 PSA_DONE( );
3863}
3864/* END_CASE */
3865
3866/* BEGIN_CASE */
3867void sign_verify_message( int key_type_arg,
3868 data_t *key_data,
3869 int alg_arg,
3870 data_t *input_data )
3871{
3872 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3873 psa_key_type_t key_type = key_type_arg;
3874 psa_algorithm_t alg = alg_arg;
3875 size_t key_bits;
3876 unsigned char *signature = NULL;
3877 size_t signature_size;
3878 size_t signature_length = 0xdeadbeef;
3879 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3880
3881 PSA_ASSERT( psa_crypto_init( ) );
3882
3883 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3884 PSA_KEY_USAGE_VERIFY_MESSAGE );
3885 psa_set_key_algorithm( &attributes, alg );
3886 psa_set_key_type( &attributes, key_type );
3887
3888 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3889 &key ) );
3890 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3891 key_bits = psa_get_key_bits( &attributes );
3892
3893 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3894 TEST_ASSERT( signature_size != 0 );
3895 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3896 ASSERT_ALLOC( signature, signature_size );
3897
3898 PSA_ASSERT( psa_sign_message( key, alg,
3899 input_data->x, input_data->len,
3900 signature, signature_size,
3901 &signature_length ) );
3902 TEST_ASSERT( signature_length <= signature_size );
3903 TEST_ASSERT( signature_length > 0 );
3904
3905 PSA_ASSERT( psa_verify_message( key, alg,
3906 input_data->x, input_data->len,
3907 signature, signature_length ) );
3908
3909 if( input_data->len != 0 )
3910 {
3911 /* Flip a bit in the input and verify that the signature is now
3912 * detected as invalid. Flip a bit at the beginning, not at the end,
3913 * because ECDSA may ignore the last few bits of the input. */
3914 input_data->x[0] ^= 1;
3915 TEST_EQUAL( psa_verify_message( key, alg,
3916 input_data->x, input_data->len,
3917 signature, signature_length ),
3918 PSA_ERROR_INVALID_SIGNATURE );
3919 }
3920
3921exit:
3922 psa_reset_key_attributes( &attributes );
3923
3924 psa_destroy_key( key );
3925 mbedtls_free( signature );
3926 PSA_DONE( );
3927}
3928/* END_CASE */
3929
3930/* BEGIN_CASE */
3931void verify_message( int key_type_arg,
3932 data_t *key_data,
3933 int alg_arg,
3934 data_t *input_data,
3935 data_t *signature_data )
3936{
3937 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3938 psa_key_type_t key_type = key_type_arg;
3939 psa_algorithm_t alg = alg_arg;
3940 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3941
3942 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3943
3944 PSA_ASSERT( psa_crypto_init( ) );
3945
3946 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3947 psa_set_key_algorithm( &attributes, alg );
3948 psa_set_key_type( &attributes, key_type );
3949
3950 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3951 &key ) );
3952
3953 PSA_ASSERT( psa_verify_message( key, alg,
3954 input_data->x, input_data->len,
3955 signature_data->x, signature_data->len ) );
3956
3957exit:
3958 psa_reset_key_attributes( &attributes );
3959 psa_destroy_key( key );
3960 PSA_DONE( );
3961}
3962/* END_CASE */
3963
3964/* BEGIN_CASE */
3965void verify_message_fail( int key_type_arg,
3966 data_t *key_data,
3967 int alg_arg,
3968 data_t *hash_data,
3969 data_t *signature_data,
3970 int expected_status_arg )
3971{
3972 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3973 psa_key_type_t key_type = key_type_arg;
3974 psa_algorithm_t alg = alg_arg;
3975 psa_status_t actual_status;
3976 psa_status_t expected_status = expected_status_arg;
3977 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3978
3979 PSA_ASSERT( psa_crypto_init( ) );
3980
3981 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3982 psa_set_key_algorithm( &attributes, alg );
3983 psa_set_key_type( &attributes, key_type );
3984
3985 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3986 &key ) );
3987
3988 actual_status = psa_verify_message( key, alg,
3989 hash_data->x, hash_data->len,
3990 signature_data->x,
3991 signature_data->len );
3992 TEST_EQUAL( actual_status, expected_status );
3993
3994exit:
3995 psa_reset_key_attributes( &attributes );
3996 psa_destroy_key( key );
3997 PSA_DONE( );
3998}
3999/* END_CASE */
4000
4001/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004002void asymmetric_encrypt( int key_type_arg,
4003 data_t *key_data,
4004 int alg_arg,
4005 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004006 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004007 int expected_output_length_arg,
4008 int expected_status_arg )
4009{
Ronald Cron5425a212020-08-04 14:58:35 +02004010 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004011 psa_key_type_t key_type = key_type_arg;
4012 psa_algorithm_t alg = alg_arg;
4013 size_t expected_output_length = expected_output_length_arg;
4014 size_t key_bits;
4015 unsigned char *output = NULL;
4016 size_t output_size;
4017 size_t output_length = ~0;
4018 psa_status_t actual_status;
4019 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004020 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004021
Gilles Peskine8817f612018-12-18 00:18:46 +01004022 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004023
Gilles Peskine656896e2018-06-29 19:12:28 +02004024 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004025 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4026 psa_set_key_algorithm( &attributes, alg );
4027 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004028 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004029 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004030
4031 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004032 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004033 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004034
Gilles Peskine656896e2018-06-29 19:12:28 +02004035 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004036 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004037 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004038
4039 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004040 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004041 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004042 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004043 output, output_size,
4044 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004045 TEST_EQUAL( actual_status, expected_status );
4046 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004047
Gilles Peskine68428122018-06-30 18:42:41 +02004048 /* If the label is empty, the test framework puts a non-null pointer
4049 * in label->x. Test that a null pointer works as well. */
4050 if( label->len == 0 )
4051 {
4052 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004053 if( output_size != 0 )
4054 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004055 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004056 input_data->x, input_data->len,
4057 NULL, label->len,
4058 output, output_size,
4059 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004060 TEST_EQUAL( actual_status, expected_status );
4061 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004062 }
4063
Gilles Peskine656896e2018-06-29 19:12:28 +02004064exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004065 /*
4066 * Key attributes may have been returned by psa_get_key_attributes()
4067 * thus reset them as required.
4068 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004069 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004070
Ronald Cron5425a212020-08-04 14:58:35 +02004071 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004072 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004073 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004074}
4075/* END_CASE */
4076
4077/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004078void asymmetric_encrypt_decrypt( int key_type_arg,
4079 data_t *key_data,
4080 int alg_arg,
4081 data_t *input_data,
4082 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004083{
Ronald Cron5425a212020-08-04 14:58:35 +02004084 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004085 psa_key_type_t key_type = key_type_arg;
4086 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004087 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004088 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004089 size_t output_size;
4090 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004091 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004092 size_t output2_size;
4093 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004094 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004095
Gilles Peskine8817f612018-12-18 00:18:46 +01004096 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004097
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004098 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4099 psa_set_key_algorithm( &attributes, alg );
4100 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004101
Gilles Peskine049c7532019-05-15 20:22:09 +02004102 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004103 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004104
4105 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004106 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004107 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004108
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004109 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004110 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004111 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01004112
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004113 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01004114 TEST_ASSERT( output2_size <=
4115 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
4116 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004117 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004118
Gilles Peskineeebd7382018-06-08 18:11:54 +02004119 /* We test encryption by checking that encrypt-then-decrypt gives back
4120 * the original plaintext because of the non-optional random
4121 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004122 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004123 input_data->x, input_data->len,
4124 label->x, label->len,
4125 output, output_size,
4126 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004127 /* We don't know what ciphertext length to expect, but check that
4128 * it looks sensible. */
4129 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004130
Ronald Cron5425a212020-08-04 14:58:35 +02004131 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004132 output, output_length,
4133 label->x, label->len,
4134 output2, output2_size,
4135 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004136 ASSERT_COMPARE( input_data->x, input_data->len,
4137 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004138
4139exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004140 /*
4141 * Key attributes may have been returned by psa_get_key_attributes()
4142 * thus reset them as required.
4143 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004144 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004145
Ronald Cron5425a212020-08-04 14:58:35 +02004146 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004147 mbedtls_free( output );
4148 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004149 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004150}
4151/* END_CASE */
4152
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004153/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004154void asymmetric_decrypt( int key_type_arg,
4155 data_t *key_data,
4156 int alg_arg,
4157 data_t *input_data,
4158 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004159 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004160{
Ronald Cron5425a212020-08-04 14:58:35 +02004161 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004162 psa_key_type_t key_type = key_type_arg;
4163 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004164 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004165 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004166 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004167 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004168 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004169
Gilles Peskine8817f612018-12-18 00:18:46 +01004170 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004171
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004172 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4173 psa_set_key_algorithm( &attributes, alg );
4174 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004175
Gilles Peskine049c7532019-05-15 20:22:09 +02004176 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004177 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004178
gabor-mezei-armceface22021-01-21 12:26:17 +01004179 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4180 key_bits = psa_get_key_bits( &attributes );
4181
4182 /* Determine the maximum ciphertext length */
4183 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
4184 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
4185 ASSERT_ALLOC( output, output_size );
4186
Ronald Cron5425a212020-08-04 14:58:35 +02004187 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004188 input_data->x, input_data->len,
4189 label->x, label->len,
4190 output,
4191 output_size,
4192 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004193 ASSERT_COMPARE( expected_data->x, expected_data->len,
4194 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004195
Gilles Peskine68428122018-06-30 18:42:41 +02004196 /* If the label is empty, the test framework puts a non-null pointer
4197 * in label->x. Test that a null pointer works as well. */
4198 if( label->len == 0 )
4199 {
4200 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004201 if( output_size != 0 )
4202 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004203 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004204 input_data->x, input_data->len,
4205 NULL, label->len,
4206 output,
4207 output_size,
4208 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004209 ASSERT_COMPARE( expected_data->x, expected_data->len,
4210 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004211 }
4212
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004213exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004214 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004215 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004216 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004217 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004218}
4219/* END_CASE */
4220
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004221/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004222void asymmetric_decrypt_fail( int key_type_arg,
4223 data_t *key_data,
4224 int alg_arg,
4225 data_t *input_data,
4226 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004227 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004228 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004229{
Ronald Cron5425a212020-08-04 14:58:35 +02004230 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004231 psa_key_type_t key_type = key_type_arg;
4232 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004233 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004234 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004235 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004236 psa_status_t actual_status;
4237 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004238 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004239
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004240 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004241
Gilles Peskine8817f612018-12-18 00:18:46 +01004242 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004243
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004244 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4245 psa_set_key_algorithm( &attributes, alg );
4246 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004247
Gilles Peskine049c7532019-05-15 20:22:09 +02004248 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004249 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004250
Ronald Cron5425a212020-08-04 14:58:35 +02004251 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004252 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004253 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004254 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004255 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004256 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004257 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004258
Gilles Peskine68428122018-06-30 18:42:41 +02004259 /* If the label is empty, the test framework puts a non-null pointer
4260 * in label->x. Test that a null pointer works as well. */
4261 if( label->len == 0 )
4262 {
4263 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004264 if( output_size != 0 )
4265 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004266 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004267 input_data->x, input_data->len,
4268 NULL, label->len,
4269 output, output_size,
4270 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004271 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004272 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004273 }
4274
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004275exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004276 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004277 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004278 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004279 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004280}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004281/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004282
4283/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004284void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004285{
4286 /* Test each valid way of initializing the object, except for `= {0}`, as
4287 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4288 * though it's OK by the C standard. We could test for this, but we'd need
4289 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004290 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004291 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4292 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4293 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004294
4295 memset( &zero, 0, sizeof( zero ) );
4296
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004297 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004298 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004299 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004300 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004301 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004302 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004303 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004304
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004305 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004306 PSA_ASSERT( psa_key_derivation_abort(&func) );
4307 PSA_ASSERT( psa_key_derivation_abort(&init) );
4308 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004309}
4310/* END_CASE */
4311
Janos Follath16de4a42019-06-13 16:32:24 +01004312/* BEGIN_CASE */
4313void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004314{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004315 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004316 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004317 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004318
Gilles Peskine8817f612018-12-18 00:18:46 +01004319 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004320
Janos Follath16de4a42019-06-13 16:32:24 +01004321 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004322 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004323
4324exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004325 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004326 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004327}
4328/* END_CASE */
4329
Janos Follathaf3c2a02019-06-12 12:34:34 +01004330/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004331void derive_set_capacity( int alg_arg, int capacity_arg,
4332 int expected_status_arg )
4333{
4334 psa_algorithm_t alg = alg_arg;
4335 size_t capacity = capacity_arg;
4336 psa_status_t expected_status = expected_status_arg;
4337 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4338
4339 PSA_ASSERT( psa_crypto_init( ) );
4340
4341 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4342
4343 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4344 expected_status );
4345
4346exit:
4347 psa_key_derivation_abort( &operation );
4348 PSA_DONE( );
4349}
4350/* END_CASE */
4351
4352/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004353void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004354 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004355 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004356 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004357 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004358 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004359 int expected_status_arg3,
4360 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004361{
4362 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004363 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4364 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004365 psa_status_t expected_statuses[] = {expected_status_arg1,
4366 expected_status_arg2,
4367 expected_status_arg3};
4368 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004369 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4370 MBEDTLS_SVC_KEY_ID_INIT,
4371 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004372 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4373 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4374 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004375 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004376 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004377 psa_status_t expected_output_status = expected_output_status_arg;
4378 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004379
4380 PSA_ASSERT( psa_crypto_init( ) );
4381
4382 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4383 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004384
4385 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4386
4387 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4388 {
Gilles Peskine4023c012021-05-27 13:21:20 +02004389 mbedtls_test_set_step( i );
4390 if( steps[i] == 0 )
4391 {
4392 /* Skip this step */
4393 }
4394 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004395 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004396 psa_set_key_type( &attributes, key_types[i] );
4397 PSA_ASSERT( psa_import_key( &attributes,
4398 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004399 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004400 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4401 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4402 {
4403 // When taking a private key as secret input, use key agreement
4404 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004405 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4406 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004407 expected_statuses[i] );
4408 }
4409 else
4410 {
4411 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004412 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004413 expected_statuses[i] );
4414 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004415 }
4416 else
4417 {
4418 TEST_EQUAL( psa_key_derivation_input_bytes(
4419 &operation, steps[i],
4420 inputs[i]->x, inputs[i]->len ),
4421 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004422 }
4423 }
4424
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004425 if( output_key_type != PSA_KEY_TYPE_NONE )
4426 {
4427 psa_reset_key_attributes( &attributes );
4428 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4429 psa_set_key_bits( &attributes, 8 );
4430 actual_output_status =
4431 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004432 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004433 }
4434 else
4435 {
4436 uint8_t buffer[1];
4437 actual_output_status =
4438 psa_key_derivation_output_bytes( &operation,
4439 buffer, sizeof( buffer ) );
4440 }
4441 TEST_EQUAL( actual_output_status, expected_output_status );
4442
Janos Follathaf3c2a02019-06-12 12:34:34 +01004443exit:
4444 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004445 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4446 psa_destroy_key( keys[i] );
4447 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004448 PSA_DONE( );
4449}
4450/* END_CASE */
4451
Janos Follathd958bb72019-07-03 15:02:16 +01004452/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02004453void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004454{
Janos Follathd958bb72019-07-03 15:02:16 +01004455 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004456 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004457 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004458 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004459 unsigned char input1[] = "Input 1";
4460 size_t input1_length = sizeof( input1 );
4461 unsigned char input2[] = "Input 2";
4462 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004463 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004464 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004465 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4466 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4467 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004468 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004469
Gilles Peskine8817f612018-12-18 00:18:46 +01004470 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004471
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004472 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4473 psa_set_key_algorithm( &attributes, alg );
4474 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004475
Gilles Peskine73676cb2019-05-15 20:15:10 +02004476 PSA_ASSERT( psa_import_key( &attributes,
4477 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004478 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004479
4480 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004481 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4482 input1, input1_length,
4483 input2, input2_length,
4484 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004485 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004486
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004487 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004488 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004489 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004490
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004491 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004492
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004493 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004494 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004495
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004496exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004497 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004498 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004499 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004500}
4501/* END_CASE */
4502
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004503/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02004504void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004505{
4506 uint8_t output_buffer[16];
4507 size_t buffer_size = 16;
4508 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004509 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004510
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004511 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4512 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004513 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004514
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004515 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004516 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004517
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004518 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004519
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004520 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4521 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004522 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004523
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004524 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004525 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004526
4527exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004528 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004529}
4530/* END_CASE */
4531
4532/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004533void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004534 int step1_arg, data_t *input1,
4535 int step2_arg, data_t *input2,
4536 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004537 int requested_capacity_arg,
4538 data_t *expected_output1,
4539 data_t *expected_output2 )
4540{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004541 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004542 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4543 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004544 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4545 MBEDTLS_SVC_KEY_ID_INIT,
4546 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004547 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004548 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004549 uint8_t *expected_outputs[2] =
4550 {expected_output1->x, expected_output2->x};
4551 size_t output_sizes[2] =
4552 {expected_output1->len, expected_output2->len};
4553 size_t output_buffer_size = 0;
4554 uint8_t *output_buffer = NULL;
4555 size_t expected_capacity;
4556 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004557 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004558 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004559 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004560
4561 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4562 {
4563 if( output_sizes[i] > output_buffer_size )
4564 output_buffer_size = output_sizes[i];
4565 if( output_sizes[i] == 0 )
4566 expected_outputs[i] = NULL;
4567 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004568 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004569 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004570
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004571 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4572 psa_set_key_algorithm( &attributes, alg );
4573 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004574
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004575 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004576 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4577 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4578 requested_capacity ) );
4579 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004580 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004581 switch( steps[i] )
4582 {
4583 case 0:
4584 break;
4585 case PSA_KEY_DERIVATION_INPUT_SECRET:
4586 PSA_ASSERT( psa_import_key( &attributes,
4587 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004588 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004589
4590 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4591 {
4592 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4593 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4594 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4595 }
4596
Gilles Peskine1468da72019-05-29 17:35:49 +02004597 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004598 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004599 break;
4600 default:
4601 PSA_ASSERT( psa_key_derivation_input_bytes(
4602 &operation, steps[i],
4603 inputs[i]->x, inputs[i]->len ) );
4604 break;
4605 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004606 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004607
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004608 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004609 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004610 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004611 expected_capacity = requested_capacity;
4612
4613 /* Expansion phase. */
4614 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4615 {
4616 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004617 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004618 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004619 if( expected_capacity == 0 && output_sizes[i] == 0 )
4620 {
4621 /* Reading 0 bytes when 0 bytes are available can go either way. */
4622 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004623 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004624 continue;
4625 }
4626 else if( expected_capacity == 0 ||
4627 output_sizes[i] > expected_capacity )
4628 {
4629 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004630 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004631 expected_capacity = 0;
4632 continue;
4633 }
4634 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004635 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004636 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004637 ASSERT_COMPARE( output_buffer, output_sizes[i],
4638 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004639 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004640 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004641 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004642 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004643 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004644 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004645 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004646
4647exit:
4648 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004649 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004650 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4651 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004652 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004653}
4654/* END_CASE */
4655
4656/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004657void derive_full( int alg_arg,
4658 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004659 data_t *input1,
4660 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004661 int requested_capacity_arg )
4662{
Ronald Cron5425a212020-08-04 14:58:35 +02004663 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004664 psa_algorithm_t alg = alg_arg;
4665 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004666 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004667 unsigned char output_buffer[16];
4668 size_t expected_capacity = requested_capacity;
4669 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004670 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004671
Gilles Peskine8817f612018-12-18 00:18:46 +01004672 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004673
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004674 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4675 psa_set_key_algorithm( &attributes, alg );
4676 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004677
Gilles Peskine049c7532019-05-15 20:22:09 +02004678 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004679 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004680
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004681 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4682 input1->x, input1->len,
4683 input2->x, input2->len,
4684 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004685 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004686
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004687 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004688 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004689 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004690
4691 /* Expansion phase. */
4692 while( current_capacity > 0 )
4693 {
4694 size_t read_size = sizeof( output_buffer );
4695 if( read_size > current_capacity )
4696 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004697 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004698 output_buffer,
4699 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004700 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004701 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004702 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004703 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004704 }
4705
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004706 /* Check that the operation refuses to go over capacity. */
4707 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004708 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004709
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004710 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004711
4712exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004713 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004714 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004715 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004716}
4717/* END_CASE */
4718
Janos Follathe60c9052019-07-03 13:51:30 +01004719/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004720void derive_key_exercise( int alg_arg,
4721 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004722 data_t *input1,
4723 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004724 int derived_type_arg,
4725 int derived_bits_arg,
4726 int derived_usage_arg,
4727 int derived_alg_arg )
4728{
Ronald Cron5425a212020-08-04 14:58:35 +02004729 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4730 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004731 psa_algorithm_t alg = alg_arg;
4732 psa_key_type_t derived_type = derived_type_arg;
4733 size_t derived_bits = derived_bits_arg;
4734 psa_key_usage_t derived_usage = derived_usage_arg;
4735 psa_algorithm_t derived_alg = derived_alg_arg;
4736 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004737 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004738 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004739 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004740
Gilles Peskine8817f612018-12-18 00:18:46 +01004741 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004742
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004743 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4744 psa_set_key_algorithm( &attributes, alg );
4745 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004746 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004747 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004748
4749 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004750 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4751 input1->x, input1->len,
4752 input2->x, input2->len,
4753 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004754 goto exit;
4755
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004756 psa_set_key_usage_flags( &attributes, derived_usage );
4757 psa_set_key_algorithm( &attributes, derived_alg );
4758 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004759 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004760 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004761 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004762
4763 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004764 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004765 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4766 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004767
4768 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004769 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004770 goto exit;
4771
4772exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004773 /*
4774 * Key attributes may have been returned by psa_get_key_attributes()
4775 * thus reset them as required.
4776 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004777 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004778
4779 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004780 psa_destroy_key( base_key );
4781 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004782 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004783}
4784/* END_CASE */
4785
Janos Follath42fd8882019-07-03 14:17:09 +01004786/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004787void derive_key_export( int alg_arg,
4788 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004789 data_t *input1,
4790 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004791 int bytes1_arg,
4792 int bytes2_arg )
4793{
Ronald Cron5425a212020-08-04 14:58:35 +02004794 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4795 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004796 psa_algorithm_t alg = alg_arg;
4797 size_t bytes1 = bytes1_arg;
4798 size_t bytes2 = bytes2_arg;
4799 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004800 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004801 uint8_t *output_buffer = NULL;
4802 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004803 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4804 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004805 size_t length;
4806
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004807 ASSERT_ALLOC( output_buffer, capacity );
4808 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004809 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004810
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004811 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4812 psa_set_key_algorithm( &base_attributes, alg );
4813 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004814 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004815 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004816
4817 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004818 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4819 input1->x, input1->len,
4820 input2->x, input2->len,
4821 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004822 goto exit;
4823
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004824 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004825 output_buffer,
4826 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004827 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004828
4829 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004830 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4831 input1->x, input1->len,
4832 input2->x, input2->len,
4833 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004834 goto exit;
4835
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004836 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4837 psa_set_key_algorithm( &derived_attributes, 0 );
4838 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004839 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004840 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004841 &derived_key ) );
4842 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004843 export_buffer, bytes1,
4844 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004845 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004846 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004847 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004848 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004849 &derived_key ) );
4850 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004851 export_buffer + bytes1, bytes2,
4852 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004853 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004854
4855 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004856 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4857 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004858
4859exit:
4860 mbedtls_free( output_buffer );
4861 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004862 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004863 psa_destroy_key( base_key );
4864 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004865 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004866}
4867/* END_CASE */
4868
4869/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004870void derive_key( int alg_arg,
4871 data_t *key_data, data_t *input1, data_t *input2,
4872 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004873 int expected_status_arg,
4874 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004875{
Ronald Cron5425a212020-08-04 14:58:35 +02004876 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4877 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004878 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004879 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004880 size_t bits = bits_arg;
4881 psa_status_t expected_status = expected_status_arg;
4882 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4883 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4884 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4885
4886 PSA_ASSERT( psa_crypto_init( ) );
4887
4888 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4889 psa_set_key_algorithm( &base_attributes, alg );
4890 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4891 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004892 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004893
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004894 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4895 input1->x, input1->len,
4896 input2->x, input2->len,
4897 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004898 goto exit;
4899
4900 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4901 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004902 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004903 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004904
4905 psa_status_t status =
4906 psa_key_derivation_output_key( &derived_attributes,
4907 &operation,
4908 &derived_key );
4909 if( is_large_output > 0 )
4910 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4911 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004912
4913exit:
4914 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004915 psa_destroy_key( base_key );
4916 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004917 PSA_DONE( );
4918}
4919/* END_CASE */
4920
4921/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004922void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004923 int our_key_type_arg, int our_key_alg_arg,
4924 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004925 int expected_status_arg )
4926{
Ronald Cron5425a212020-08-04 14:58:35 +02004927 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004928 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004929 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004930 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004931 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004932 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004933 psa_status_t expected_status = expected_status_arg;
4934 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004935
Gilles Peskine8817f612018-12-18 00:18:46 +01004936 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004937
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004938 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004939 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004940 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004941 PSA_ASSERT( psa_import_key( &attributes,
4942 our_key_data->x, our_key_data->len,
4943 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004944
Gilles Peskine77f40d82019-04-11 21:27:06 +02004945 /* The tests currently include inputs that should fail at either step.
4946 * Test cases that fail at the setup step should be changed to call
4947 * key_derivation_setup instead, and this function should be renamed
4948 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004949 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004950 if( status == PSA_SUCCESS )
4951 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004952 TEST_EQUAL( psa_key_derivation_key_agreement(
4953 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4954 our_key,
4955 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004956 expected_status );
4957 }
4958 else
4959 {
4960 TEST_ASSERT( status == expected_status );
4961 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004962
4963exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004964 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004965 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004966 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004967}
4968/* END_CASE */
4969
4970/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004971void raw_key_agreement( int alg_arg,
4972 int our_key_type_arg, data_t *our_key_data,
4973 data_t *peer_key_data,
4974 data_t *expected_output )
4975{
Ronald Cron5425a212020-08-04 14:58:35 +02004976 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004977 psa_algorithm_t alg = alg_arg;
4978 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004979 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004980 unsigned char *output = NULL;
4981 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004982 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004983
4984 ASSERT_ALLOC( output, expected_output->len );
4985 PSA_ASSERT( psa_crypto_init( ) );
4986
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004987 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4988 psa_set_key_algorithm( &attributes, alg );
4989 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004990 PSA_ASSERT( psa_import_key( &attributes,
4991 our_key_data->x, our_key_data->len,
4992 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004993
gabor-mezei-armceface22021-01-21 12:26:17 +01004994 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4995 key_bits = psa_get_key_bits( &attributes );
4996
Gilles Peskinebe697d82019-05-16 18:00:41 +02004997 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4998 peer_key_data->x, peer_key_data->len,
4999 output, expected_output->len,
5000 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005001 ASSERT_COMPARE( output, output_length,
5002 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01005003 TEST_ASSERT( output_length <=
5004 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
5005 TEST_ASSERT( output_length <=
5006 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005007
5008exit:
5009 mbedtls_free( output );
5010 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005011 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005012}
5013/* END_CASE */
5014
5015/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005016void key_agreement_capacity( int alg_arg,
5017 int our_key_type_arg, data_t *our_key_data,
5018 data_t *peer_key_data,
5019 int expected_capacity_arg )
5020{
Ronald Cron5425a212020-08-04 14:58:35 +02005021 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005022 psa_algorithm_t alg = alg_arg;
5023 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005024 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005025 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005026 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005027 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005028
Gilles Peskine8817f612018-12-18 00:18:46 +01005029 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005030
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005031 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5032 psa_set_key_algorithm( &attributes, alg );
5033 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005034 PSA_ASSERT( psa_import_key( &attributes,
5035 our_key_data->x, our_key_data->len,
5036 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005037
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005038 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005039 PSA_ASSERT( psa_key_derivation_key_agreement(
5040 &operation,
5041 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5042 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005043 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5044 {
5045 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005046 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005047 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005048 NULL, 0 ) );
5049 }
Gilles Peskine59685592018-09-18 12:11:34 +02005050
Gilles Peskinebf491972018-10-25 22:36:12 +02005051 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005052 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005053 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005054 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005055
Gilles Peskinebf491972018-10-25 22:36:12 +02005056 /* Test the actual capacity by reading the output. */
5057 while( actual_capacity > sizeof( output ) )
5058 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005059 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005060 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005061 actual_capacity -= sizeof( output );
5062 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005063 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005064 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005065 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005066 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005067
Gilles Peskine59685592018-09-18 12:11:34 +02005068exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005069 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005070 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005071 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005072}
5073/* END_CASE */
5074
5075/* BEGIN_CASE */
5076void key_agreement_output( int alg_arg,
5077 int our_key_type_arg, data_t *our_key_data,
5078 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005079 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005080{
Ronald Cron5425a212020-08-04 14:58:35 +02005081 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005082 psa_algorithm_t alg = alg_arg;
5083 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005084 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005085 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005086 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005087
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005088 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5089 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005090
Gilles Peskine8817f612018-12-18 00:18:46 +01005091 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005092
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005093 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5094 psa_set_key_algorithm( &attributes, alg );
5095 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005096 PSA_ASSERT( psa_import_key( &attributes,
5097 our_key_data->x, our_key_data->len,
5098 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005099
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005100 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005101 PSA_ASSERT( psa_key_derivation_key_agreement(
5102 &operation,
5103 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5104 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005105 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5106 {
5107 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005108 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005109 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005110 NULL, 0 ) );
5111 }
Gilles Peskine59685592018-09-18 12:11:34 +02005112
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005113 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005114 actual_output,
5115 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005116 ASSERT_COMPARE( actual_output, expected_output1->len,
5117 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005118 if( expected_output2->len != 0 )
5119 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005120 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005121 actual_output,
5122 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005123 ASSERT_COMPARE( actual_output, expected_output2->len,
5124 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005125 }
Gilles Peskine59685592018-09-18 12:11:34 +02005126
5127exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005128 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005129 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005130 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005131 mbedtls_free( actual_output );
5132}
5133/* END_CASE */
5134
5135/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005136void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005137{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005138 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005139 unsigned char *output = NULL;
5140 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005141 size_t i;
5142 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005143
Simon Butcher49f8e312020-03-03 15:51:50 +00005144 TEST_ASSERT( bytes_arg >= 0 );
5145
Gilles Peskine91892022021-02-08 19:50:26 +01005146 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005147 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005148
Gilles Peskine8817f612018-12-18 00:18:46 +01005149 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005150
Gilles Peskinea50d7392018-06-21 10:22:13 +02005151 /* Run several times, to ensure that every output byte will be
5152 * nonzero at least once with overwhelming probability
5153 * (2^(-8*number_of_runs)). */
5154 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005155 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005156 if( bytes != 0 )
5157 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005158 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005159
Gilles Peskinea50d7392018-06-21 10:22:13 +02005160 for( i = 0; i < bytes; i++ )
5161 {
5162 if( output[i] != 0 )
5163 ++changed[i];
5164 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005165 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005166
5167 /* Check that every byte was changed to nonzero at least once. This
5168 * validates that psa_generate_random is overwriting every byte of
5169 * the output buffer. */
5170 for( i = 0; i < bytes; i++ )
5171 {
5172 TEST_ASSERT( changed[i] != 0 );
5173 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005174
5175exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005176 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005177 mbedtls_free( output );
5178 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005179}
5180/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005181
5182/* BEGIN_CASE */
5183void generate_key( int type_arg,
5184 int bits_arg,
5185 int usage_arg,
5186 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005187 int expected_status_arg,
5188 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005189{
Ronald Cron5425a212020-08-04 14:58:35 +02005190 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005191 psa_key_type_t type = type_arg;
5192 psa_key_usage_t usage = usage_arg;
5193 size_t bits = bits_arg;
5194 psa_algorithm_t alg = alg_arg;
5195 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005196 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005197 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005198
Gilles Peskine8817f612018-12-18 00:18:46 +01005199 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005200
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005201 psa_set_key_usage_flags( &attributes, usage );
5202 psa_set_key_algorithm( &attributes, alg );
5203 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005204 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005205
5206 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005207 psa_status_t status = psa_generate_key( &attributes, &key );
5208
5209 if( is_large_key > 0 )
5210 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5211 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005212 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005213 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005214
5215 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005216 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005217 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5218 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005219
Gilles Peskine818ca122018-06-20 18:16:48 +02005220 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005221 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005222 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005223
5224exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005225 /*
5226 * Key attributes may have been returned by psa_get_key_attributes()
5227 * thus reset them as required.
5228 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005229 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005230
Ronald Cron5425a212020-08-04 14:58:35 +02005231 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005232 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005233}
5234/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005235
Ronald Cronee414c72021-03-18 18:50:08 +01005236/* 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 +02005237void generate_key_rsa( int bits_arg,
5238 data_t *e_arg,
5239 int expected_status_arg )
5240{
Ronald Cron5425a212020-08-04 14:58:35 +02005241 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005242 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005243 size_t bits = bits_arg;
5244 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5245 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5246 psa_status_t expected_status = expected_status_arg;
5247 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5248 uint8_t *exported = NULL;
5249 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005250 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005251 size_t exported_length = SIZE_MAX;
5252 uint8_t *e_read_buffer = NULL;
5253 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005254 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005255 size_t e_read_length = SIZE_MAX;
5256
5257 if( e_arg->len == 0 ||
5258 ( e_arg->len == 3 &&
5259 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5260 {
5261 is_default_public_exponent = 1;
5262 e_read_size = 0;
5263 }
5264 ASSERT_ALLOC( e_read_buffer, e_read_size );
5265 ASSERT_ALLOC( exported, exported_size );
5266
5267 PSA_ASSERT( psa_crypto_init( ) );
5268
5269 psa_set_key_usage_flags( &attributes, usage );
5270 psa_set_key_algorithm( &attributes, alg );
5271 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5272 e_arg->x, e_arg->len ) );
5273 psa_set_key_bits( &attributes, bits );
5274
5275 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005276 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005277 if( expected_status != PSA_SUCCESS )
5278 goto exit;
5279
5280 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005281 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005282 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5283 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5284 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5285 e_read_buffer, e_read_size,
5286 &e_read_length ) );
5287 if( is_default_public_exponent )
5288 TEST_EQUAL( e_read_length, 0 );
5289 else
5290 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5291
5292 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005293 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005294 goto exit;
5295
5296 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005297 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005298 exported, exported_size,
5299 &exported_length ) );
5300 {
5301 uint8_t *p = exported;
5302 uint8_t *end = exported + exported_length;
5303 size_t len;
5304 /* RSAPublicKey ::= SEQUENCE {
5305 * modulus INTEGER, -- n
5306 * publicExponent INTEGER } -- e
5307 */
5308 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005309 MBEDTLS_ASN1_SEQUENCE |
5310 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005311 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005312 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5313 MBEDTLS_ASN1_INTEGER ) );
5314 if( len >= 1 && p[0] == 0 )
5315 {
5316 ++p;
5317 --len;
5318 }
5319 if( e_arg->len == 0 )
5320 {
5321 TEST_EQUAL( len, 3 );
5322 TEST_EQUAL( p[0], 1 );
5323 TEST_EQUAL( p[1], 0 );
5324 TEST_EQUAL( p[2], 1 );
5325 }
5326 else
5327 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5328 }
5329
5330exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005331 /*
5332 * Key attributes may have been returned by psa_get_key_attributes() or
5333 * set by psa_set_key_domain_parameters() thus reset them as required.
5334 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005335 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005336
Ronald Cron5425a212020-08-04 14:58:35 +02005337 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005338 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005339 mbedtls_free( e_read_buffer );
5340 mbedtls_free( exported );
5341}
5342/* END_CASE */
5343
Darryl Greend49a4992018-06-18 17:27:26 +01005344/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005345void persistent_key_load_key_from_storage( data_t *data,
5346 int type_arg, int bits_arg,
5347 int usage_flags_arg, int alg_arg,
5348 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005349{
Ronald Cron71016a92020-08-28 19:01:50 +02005350 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005351 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005352 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5353 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005354 psa_key_type_t type = type_arg;
5355 size_t bits = bits_arg;
5356 psa_key_usage_t usage_flags = usage_flags_arg;
5357 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005358 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005359 unsigned char *first_export = NULL;
5360 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005361 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005362 size_t first_exported_length;
5363 size_t second_exported_length;
5364
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005365 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5366 {
5367 ASSERT_ALLOC( first_export, export_size );
5368 ASSERT_ALLOC( second_export, export_size );
5369 }
Darryl Greend49a4992018-06-18 17:27:26 +01005370
Gilles Peskine8817f612018-12-18 00:18:46 +01005371 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005372
Gilles Peskinec87af662019-05-15 16:12:22 +02005373 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005374 psa_set_key_usage_flags( &attributes, usage_flags );
5375 psa_set_key_algorithm( &attributes, alg );
5376 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005377 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005378
Darryl Green0c6575a2018-11-07 16:05:30 +00005379 switch( generation_method )
5380 {
5381 case IMPORT_KEY:
5382 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005383 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005384 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005385 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005386
Darryl Green0c6575a2018-11-07 16:05:30 +00005387 case GENERATE_KEY:
5388 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005389 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005390 break;
5391
5392 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005393#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005394 {
5395 /* Create base key */
5396 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5397 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5398 psa_set_key_usage_flags( &base_attributes,
5399 PSA_KEY_USAGE_DERIVE );
5400 psa_set_key_algorithm( &base_attributes, derive_alg );
5401 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005402 PSA_ASSERT( psa_import_key( &base_attributes,
5403 data->x, data->len,
5404 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005405 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005406 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005407 PSA_ASSERT( psa_key_derivation_input_key(
5408 &operation,
5409 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005410 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005411 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005412 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005413 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5414 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005415 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005416 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005417 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005418 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005419 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005420#else
5421 TEST_ASSUME( ! "KDF not supported in this configuration" );
5422#endif
5423 break;
5424
5425 default:
5426 TEST_ASSERT( ! "generation_method not implemented in test" );
5427 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005428 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005429 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005430
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005431 /* Export the key if permitted by the key policy. */
5432 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5433 {
Ronald Cron5425a212020-08-04 14:58:35 +02005434 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005435 first_export, export_size,
5436 &first_exported_length ) );
5437 if( generation_method == IMPORT_KEY )
5438 ASSERT_COMPARE( data->x, data->len,
5439 first_export, first_exported_length );
5440 }
Darryl Greend49a4992018-06-18 17:27:26 +01005441
5442 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005443 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005444 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005445 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005446
Darryl Greend49a4992018-06-18 17:27:26 +01005447 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005448 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005449 TEST_ASSERT( mbedtls_svc_key_id_equal(
5450 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005451 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5452 PSA_KEY_LIFETIME_PERSISTENT );
5453 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5454 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5455 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5456 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005457
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005458 /* Export the key again if permitted by the key policy. */
5459 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005460 {
Ronald Cron5425a212020-08-04 14:58:35 +02005461 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005462 second_export, export_size,
5463 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005464 ASSERT_COMPARE( first_export, first_exported_length,
5465 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005466 }
5467
5468 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005469 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005470 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005471
5472exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005473 /*
5474 * Key attributes may have been returned by psa_get_key_attributes()
5475 * thus reset them as required.
5476 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005477 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005478
Darryl Greend49a4992018-06-18 17:27:26 +01005479 mbedtls_free( first_export );
5480 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005481 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005482 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005483 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005484 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005485}
5486/* END_CASE */