blob: f9f013a227478ba2950ff3c0b18ac176f19de951 [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
Jaeden Amerof24c7f82018-06-27 17:20:43 +010019/** An invalid export length that will never be set by psa_export_key(). */
20static const size_t INVALID_EXPORT_LENGTH = ~0U;
21
Gilles Peskinea7aa4422018-08-14 15:17:54 +020022/** Test if a buffer contains a constant byte value.
23 *
24 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020025 *
26 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020027 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020028 * \param size Size of the buffer in bytes.
29 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020030 * \return 1 if the buffer is all-bits-zero.
31 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020032 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020033static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020034{
35 size_t i;
36 for( i = 0; i < size; i++ )
37 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020038 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020039 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020040 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020041 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042}
Gilles Peskine818ca122018-06-20 18:16:48 +020043
Gilles Peskine0b352bc2018-06-28 00:16:11 +020044/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
45static int asn1_write_10x( unsigned char **p,
46 unsigned char *start,
47 size_t bits,
48 unsigned char x )
49{
50 int ret;
51 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020052 if( bits == 0 )
53 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
54 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020055 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030056 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020057 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
58 *p -= len;
59 ( *p )[len-1] = x;
60 if( bits % 8 == 0 )
61 ( *p )[1] |= 1;
62 else
63 ( *p )[0] |= 1 << ( bits % 8 );
64 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
65 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
66 MBEDTLS_ASN1_INTEGER ) );
67 return( len );
68}
69
70static int construct_fake_rsa_key( unsigned char *buffer,
71 size_t buffer_size,
72 unsigned char **p,
73 size_t bits,
74 int keypair )
75{
76 size_t half_bits = ( bits + 1 ) / 2;
77 int ret;
78 int len = 0;
79 /* Construct something that looks like a DER encoding of
80 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
81 * RSAPrivateKey ::= SEQUENCE {
82 * version Version,
83 * modulus INTEGER, -- n
84 * publicExponent INTEGER, -- e
85 * privateExponent INTEGER, -- d
86 * prime1 INTEGER, -- p
87 * prime2 INTEGER, -- q
88 * exponent1 INTEGER, -- d mod (p-1)
89 * exponent2 INTEGER, -- d mod (q-1)
90 * coefficient INTEGER, -- (inverse of q) mod p
91 * otherPrimeInfos OtherPrimeInfos OPTIONAL
92 * }
93 * Or, for a public key, the same structure with only
94 * version, modulus and publicExponent.
95 */
96 *p = buffer + buffer_size;
97 if( keypair )
98 {
99 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
100 asn1_write_10x( p, buffer, half_bits, 1 ) );
101 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
102 asn1_write_10x( p, buffer, half_bits, 1 ) );
103 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
104 asn1_write_10x( p, buffer, half_bits, 1 ) );
105 MBEDTLS_ASN1_CHK_ADD( len, /* q */
106 asn1_write_10x( p, buffer, half_bits, 1 ) );
107 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
108 asn1_write_10x( p, buffer, half_bits, 3 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* d */
110 asn1_write_10x( p, buffer, bits, 1 ) );
111 }
112 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
113 asn1_write_10x( p, buffer, 17, 1 ) );
114 MBEDTLS_ASN1_CHK_ADD( len, /* n */
115 asn1_write_10x( p, buffer, bits, 1 ) );
116 if( keypair )
117 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
118 mbedtls_asn1_write_int( p, buffer, 0 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
120 {
121 const unsigned char tag =
122 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
123 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
124 }
125 return( len );
126}
127
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100128int exercise_mac_setup( psa_key_type_t key_type,
129 const unsigned char *key_bytes,
130 size_t key_length,
131 psa_algorithm_t alg,
132 psa_mac_operation_t *operation,
133 psa_status_t *status )
134{
Ronald Cron5425a212020-08-04 14:58:35 +0200135 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200136 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100137
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100138 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200139 psa_set_key_algorithm( &attributes, alg );
140 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200141 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100142
Ronald Cron5425a212020-08-04 14:58:35 +0200143 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100144 /* Whether setup succeeded or failed, abort must succeed. */
145 PSA_ASSERT( psa_mac_abort( operation ) );
146 /* If setup failed, reproduce the failure, so that the caller can
147 * test the resulting state of the operation object. */
148 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100149 {
Ronald Cron5425a212020-08-04 14:58:35 +0200150 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100151 }
152
Ronald Cron5425a212020-08-04 14:58:35 +0200153 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100154 return( 1 );
155
156exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200157 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100158 return( 0 );
159}
160
161int exercise_cipher_setup( psa_key_type_t key_type,
162 const unsigned char *key_bytes,
163 size_t key_length,
164 psa_algorithm_t alg,
165 psa_cipher_operation_t *operation,
166 psa_status_t *status )
167{
Ronald Cron5425a212020-08-04 14:58:35 +0200168 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200169 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100170
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200171 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
172 psa_set_key_algorithm( &attributes, alg );
173 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200174 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100175
Ronald Cron5425a212020-08-04 14:58:35 +0200176 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100177 /* Whether setup succeeded or failed, abort must succeed. */
178 PSA_ASSERT( psa_cipher_abort( operation ) );
179 /* If setup failed, reproduce the failure, so that the caller can
180 * test the resulting state of the operation object. */
181 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100182 {
Ronald Cron5425a212020-08-04 14:58:35 +0200183 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100184 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185 }
186
Ronald Cron5425a212020-08-04 14:58:35 +0200187 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100188 return( 1 );
189
190exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200191 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100192 return( 0 );
193}
194
Ronald Cron5425a212020-08-04 14:58:35 +0200195static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200196{
197 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200198 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200199 uint8_t buffer[1];
200 size_t length;
201 int ok = 0;
202
Ronald Cronecfb2372020-07-23 17:13:42 +0200203 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200204 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
205 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
206 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200207 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000208 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200209 TEST_EQUAL(
210 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
211 TEST_EQUAL(
212 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200213 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200214 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
215 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
216 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
217 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
218
Ronald Cron5425a212020-08-04 14:58:35 +0200219 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000220 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200221 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200222 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000223 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200224
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200225 ok = 1;
226
227exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100228 /*
229 * Key attributes may have been returned by psa_get_key_attributes()
230 * thus reset them as required.
231 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200232 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100233
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200234 return( ok );
235}
236
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200237/* Assert that a key isn't reported as having a slot number. */
238#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
239#define ASSERT_NO_SLOT_NUMBER( attributes ) \
240 do \
241 { \
242 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
243 TEST_EQUAL( psa_get_key_slot_number( \
244 attributes, \
245 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
246 PSA_ERROR_INVALID_ARGUMENT ); \
247 } \
248 while( 0 )
249#else /* MBEDTLS_PSA_CRYPTO_SE_C */
250#define ASSERT_NO_SLOT_NUMBER( attributes ) \
251 ( (void) 0 )
252#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
253
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100254/* An overapproximation of the amount of storage needed for a key of the
255 * given type and with the given content. The API doesn't make it easy
256 * to find a good value for the size. The current implementation doesn't
257 * care about the value anyway. */
258#define KEY_BITS_FROM_DATA( type, data ) \
259 ( data )->len
260
Darryl Green0c6575a2018-11-07 16:05:30 +0000261typedef enum {
262 IMPORT_KEY = 0,
263 GENERATE_KEY = 1,
264 DERIVE_KEY = 2
265} generate_method;
266
Paul Elliott33746aa2021-09-15 16:40:40 +0100267typedef enum
268{
269 DO_NOT_SET_LENGTHS = 0,
270 SET_LENGTHS_BEFORE_NONCE = 1,
271 SET_LENGTHS_AFTER_NONCE = 2
272} setlengths_method;
273
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100274/*!
275 * \brief Internal Function for AEAD multipart tests.
276 *
277 * \param key_type_arg Type of key passed in
278 * \param key_data The encryption / decryption key data
279 * \param alg_arg The type of algorithm used
280 * \param nonce Nonce data
281 * \param additional_data Additional data
282 * \param ad_part_len If not -1, the length of chunks to
283 * feed additional data in to be encrypted /
284 * decrypted. If -1, no chunking.
285 * \param input_data Data to encrypt / decrypt
286 * \param data_part_len If not -1, the length of chunks to feed the
287 * data in to be encrypted / decrypted. If -1,
288 * no chunking
289 * \param do_set_lengths If non-zero, then set lengths prior to
290 * calling encryption / decryption.
291 * \param expected_output Expected output
Paul Elliott41ffae12021-07-22 21:52:01 +0100292 * \param expect_valid_signature If non zero, we expect the signature to be
293 * valid
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100294 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100295 * \param do_zero_parts If non-zero, interleave zero length chunks
296 * with normal length chunks
297 * \param swap_set_functions If non-zero, swap the order of set lengths
298 * and set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100299 *
300 * \return int Zero on failure, non-zero on success.
301 *
302 */
303static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
304 int alg_arg,
305 data_t *nonce,
306 data_t *additional_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100307 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100308 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100309 int data_part_len_arg,
Paul Elliott33746aa2021-09-15 16:40:40 +0100310 setlengths_method set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100311 data_t *expected_output,
312 int expect_valid_signature,
Paul Elliott329d5382021-07-22 17:10:45 +0100313 int is_encrypt,
Paul Elliott33746aa2021-09-15 16:40:40 +0100314 int do_zero_parts )
Paul Elliottd3f82412021-06-16 16:52:21 +0100315{
316 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
317 psa_key_type_t key_type = key_type_arg;
318 psa_algorithm_t alg = alg_arg;
319 psa_aead_operation_t operation;
320 unsigned char *output_data = NULL;
321 unsigned char *part_data = NULL;
322 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100323 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100324 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100325 size_t output_size = 0;
326 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100327 size_t output_length = 0;
328 size_t key_bits = 0;
329 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100330 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100331 size_t part_length = 0;
332 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100333 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100334 size_t ad_part_len = 0;
335 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100336 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100337 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
338 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
339
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100340 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100341 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100342
Paul Elliottd3f82412021-06-16 16:52:21 +0100343 PSA_ASSERT( psa_crypto_init( ) );
344
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100345 if( is_encrypt )
346 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
347 else
348 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
349
Paul Elliottd3f82412021-06-16 16:52:21 +0100350 psa_set_key_algorithm( &attributes, alg );
351 psa_set_key_type( &attributes, key_type );
352
353 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
354 &key ) );
355
356 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
357 key_bits = psa_get_key_bits( &attributes );
358
359 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
360
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100361 if( is_encrypt )
362 {
363 /* Tag gets written at end of buffer. */
364 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
365 ( input_data->len +
366 tag_length ) );
367 data_true_size = input_data->len;
368 }
369 else
370 {
371 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
372 ( input_data->len -
373 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100374
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100375 /* Do not want to attempt to decrypt tag. */
376 data_true_size = input_data->len - tag_length;
377 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100378
379 ASSERT_ALLOC( output_data, output_size );
380
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100381 if( is_encrypt )
382 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100383 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
384 TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100385 }
386 else
387 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100388 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
389 TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100390 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100391
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100392 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100393
394 operation = psa_aead_operation_init( );
395
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100396
397 if( is_encrypt )
398 status = psa_aead_encrypt_setup( &operation, key, alg );
399 else
400 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100401
402 /* If the operation is not supported, just skip and not fail in case the
403 * encryption involves a common limitation of cryptography hardwares and
404 * an alternative implementation. */
405 if( status == PSA_ERROR_NOT_SUPPORTED )
406 {
407 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
408 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
409 }
410
411 PSA_ASSERT( status );
412
Paul Elliott33746aa2021-09-15 16:40:40 +0100413 if( set_lengths_method == DO_NOT_SET_LENGTHS )
414 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
415 else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
Paul Elliottd3f82412021-06-16 16:52:21 +0100416 {
Paul Elliott33746aa2021-09-15 16:40:40 +0100417 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
418 data_true_size ) );
Paul Elliottebf91632021-07-22 17:54:42 +0100419 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
420 }
Paul Elliott33746aa2021-09-15 16:40:40 +0100421 else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
Paul Elliottebf91632021-07-22 17:54:42 +0100422 {
423 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
424
Paul Elliott33746aa2021-09-15 16:40:40 +0100425 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
426 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100427 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100428
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100429 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100430 {
431 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100432 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100433
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100434 for( part_offset = 0, part_count = 0;
435 part_offset < additional_data->len;
436 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100437 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100438 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100439 {
Paul Elliott329d5382021-07-22 17:10:45 +0100440 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100441 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100442 else if( additional_data->len - part_offset < ad_part_len )
443 {
444 part_length = additional_data->len - part_offset;
445 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100446 else
447 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100448 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100449 }
450
451 PSA_ASSERT( psa_aead_update_ad( &operation,
452 additional_data->x + part_offset,
453 part_length ) );
454
Paul Elliottd3f82412021-06-16 16:52:21 +0100455 }
456 }
457 else
458 {
459 /* Pass additional data in one go. */
460 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
461 additional_data->len ) );
462 }
463
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100464 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100465 {
466 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100467 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100468 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100469 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100470
471 ASSERT_ALLOC( part_data, part_data_size );
472
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100473 for( part_offset = 0, part_count = 0;
474 part_offset < data_true_size;
475 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100476 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100477 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100478 {
Paul Elliott329d5382021-07-22 17:10:45 +0100479 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100480 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100481 else if( ( data_true_size - part_offset ) < data_part_len )
482 {
483 part_length = ( data_true_size - part_offset );
484 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100485 else
486 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100487 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100488 }
489
490 PSA_ASSERT( psa_aead_update( &operation,
491 ( input_data->x + part_offset ),
492 part_length, part_data,
493 part_data_size,
494 &output_part_length ) );
495
496 if( output_data && output_part_length )
497 {
498 memcpy( ( output_data + part_offset ), part_data,
499 output_part_length );
500 }
501
Paul Elliottd3f82412021-06-16 16:52:21 +0100502 output_length += output_part_length;
503 }
504 }
505 else
506 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100507 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100508 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100509 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100510 output_size, &output_length ) );
511 }
512
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100513 if( is_encrypt )
514 PSA_ASSERT( psa_aead_finish( &operation, final_data,
515 final_output_size,
516 &output_part_length,
517 tag_buffer, tag_length,
518 &tag_size ) );
519 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100520 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100521 status = psa_aead_verify( &operation, final_data,
522 final_output_size,
523 &output_part_length,
524 ( input_data->x + data_true_size ),
525 tag_length );
526
Paul Elliottf38adbe2021-09-15 17:04:19 +0100527 if( expect_valid_signature )
528 PSA_ASSERT( status );
529 else
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100530 {
Paul Elliottf38adbe2021-09-15 17:04:19 +0100531 TEST_ASSERT( status != PSA_SUCCESS );
532
533 if( status != PSA_SUCCESS )
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100534 {
535 /* Expected failure. */
536 test_ok = 1;
537 goto exit;
538 }
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100539 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100540 }
541
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100542 if( output_data && output_part_length )
543 memcpy( ( output_data + output_length ), final_data,
544 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100545
546 output_length += output_part_length;
547
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100548
549 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
550 * should be exact.*/
551 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100552 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100553 TEST_EQUAL( tag_length, tag_size );
554
555 if( output_data && tag_length )
556 memcpy( ( output_data + output_length ), tag_buffer,
557 tag_length );
558
559 output_length += tag_length;
560
561 TEST_EQUAL( output_length,
562 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
563 input_data->len ) );
564 TEST_ASSERT( output_length <=
565 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
566 }
567 else
568 {
569 TEST_EQUAL( output_length,
570 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
571 input_data->len ) );
572 TEST_ASSERT( output_length <=
573 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100574 }
575
Paul Elliottd3f82412021-06-16 16:52:21 +0100576
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100577 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100578 output_data, output_length );
579
Paul Elliottd3f82412021-06-16 16:52:21 +0100580
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100581 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100582
583exit:
584 psa_destroy_key( key );
585 psa_aead_abort( &operation );
586 mbedtls_free( output_data );
587 mbedtls_free( part_data );
588 mbedtls_free( final_data );
589 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100590
591 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100592}
593
Gilles Peskinee59236f2018-01-27 23:32:46 +0100594/* END_HEADER */
595
596/* BEGIN_DEPENDENCIES
597 * depends_on:MBEDTLS_PSA_CRYPTO_C
598 * END_DEPENDENCIES
599 */
600
601/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200602void static_checks( )
603{
604 size_t max_truncated_mac_size =
605 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
606
607 /* Check that the length for a truncated MAC always fits in the algorithm
608 * encoding. The shifted mask is the maximum truncated value. The
609 * untruncated algorithm may be one byte larger. */
610 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
611}
612/* END_CASE */
613
614/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200615void import_with_policy( int type_arg,
616 int usage_arg, int alg_arg,
617 int expected_status_arg )
618{
619 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
620 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200621 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200622 psa_key_type_t type = type_arg;
623 psa_key_usage_t usage = usage_arg;
624 psa_algorithm_t alg = alg_arg;
625 psa_status_t expected_status = expected_status_arg;
626 const uint8_t key_material[16] = {0};
627 psa_status_t status;
628
629 PSA_ASSERT( psa_crypto_init( ) );
630
631 psa_set_key_type( &attributes, type );
632 psa_set_key_usage_flags( &attributes, usage );
633 psa_set_key_algorithm( &attributes, alg );
634
635 status = psa_import_key( &attributes,
636 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200637 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200638 TEST_EQUAL( status, expected_status );
639 if( status != PSA_SUCCESS )
640 goto exit;
641
Ronald Cron5425a212020-08-04 14:58:35 +0200642 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200643 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
644 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
645 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200646 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200647
Ronald Cron5425a212020-08-04 14:58:35 +0200648 PSA_ASSERT( psa_destroy_key( key ) );
649 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200650
651exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100652 /*
653 * Key attributes may have been returned by psa_get_key_attributes()
654 * thus reset them as required.
655 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200656 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100657
658 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200659 PSA_DONE( );
660}
661/* END_CASE */
662
663/* BEGIN_CASE */
664void import_with_data( data_t *data, int type_arg,
665 int attr_bits_arg,
666 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200667{
668 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
669 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200670 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200671 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200672 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200673 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100674 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100675
Gilles Peskine8817f612018-12-18 00:18:46 +0100676 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100677
Gilles Peskine4747d192019-04-17 15:05:45 +0200678 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200679 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200680
Ronald Cron5425a212020-08-04 14:58:35 +0200681 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100682 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200683 if( status != PSA_SUCCESS )
684 goto exit;
685
Ronald Cron5425a212020-08-04 14:58:35 +0200686 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200687 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200688 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200689 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200690 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200691
Ronald Cron5425a212020-08-04 14:58:35 +0200692 PSA_ASSERT( psa_destroy_key( key ) );
693 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100694
695exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100696 /*
697 * Key attributes may have been returned by psa_get_key_attributes()
698 * thus reset them as required.
699 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200700 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100701
702 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200703 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100704}
705/* END_CASE */
706
707/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200708void import_large_key( int type_arg, int byte_size_arg,
709 int expected_status_arg )
710{
711 psa_key_type_t type = type_arg;
712 size_t byte_size = byte_size_arg;
713 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
714 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200715 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200716 psa_status_t status;
717 uint8_t *buffer = NULL;
718 size_t buffer_size = byte_size + 1;
719 size_t n;
720
Steven Cooreman69967ce2021-01-18 18:01:08 +0100721 /* Skip the test case if the target running the test cannot
722 * accomodate large keys due to heap size constraints */
723 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200724 memset( buffer, 'K', byte_size );
725
726 PSA_ASSERT( psa_crypto_init( ) );
727
728 /* Try importing the key */
729 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
730 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200731 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100732 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200733 TEST_EQUAL( status, expected_status );
734
735 if( status == PSA_SUCCESS )
736 {
Ronald Cron5425a212020-08-04 14:58:35 +0200737 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200738 TEST_EQUAL( psa_get_key_type( &attributes ), type );
739 TEST_EQUAL( psa_get_key_bits( &attributes ),
740 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200741 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200742 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200743 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200744 for( n = 0; n < byte_size; n++ )
745 TEST_EQUAL( buffer[n], 'K' );
746 for( n = byte_size; n < buffer_size; n++ )
747 TEST_EQUAL( buffer[n], 0 );
748 }
749
750exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100751 /*
752 * Key attributes may have been returned by psa_get_key_attributes()
753 * thus reset them as required.
754 */
755 psa_reset_key_attributes( &attributes );
756
Ronald Cron5425a212020-08-04 14:58:35 +0200757 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200758 PSA_DONE( );
759 mbedtls_free( buffer );
760}
761/* END_CASE */
762
763/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200764void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
765{
Ronald Cron5425a212020-08-04 14:58:35 +0200766 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200767 size_t bits = bits_arg;
768 psa_status_t expected_status = expected_status_arg;
769 psa_status_t status;
770 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200771 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200772 size_t buffer_size = /* Slight overapproximations */
773 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200774 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200775 unsigned char *p;
776 int ret;
777 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200778 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200779
Gilles Peskine8817f612018-12-18 00:18:46 +0100780 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200781 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200782
783 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
784 bits, keypair ) ) >= 0 );
785 length = ret;
786
787 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200788 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200789 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100790 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200791
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200792 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200793 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200794
795exit:
796 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200797 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200798}
799/* END_CASE */
800
801/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300802void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300803 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200804 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100805 int expected_bits,
806 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200807 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100808 int canonical_input )
809{
Ronald Cron5425a212020-08-04 14:58:35 +0200810 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100811 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200812 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200813 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100814 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100815 unsigned char *exported = NULL;
816 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100817 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100818 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100819 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200820 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200821 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100822
Moran Pekercb088e72018-07-17 17:36:59 +0300823 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200824 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100825 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200826 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100827 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100828
Gilles Peskine4747d192019-04-17 15:05:45 +0200829 psa_set_key_usage_flags( &attributes, usage_arg );
830 psa_set_key_algorithm( &attributes, alg );
831 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700832
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100833 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200834 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100835
836 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200837 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200838 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
839 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200840 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100841
842 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200843 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100844 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100845
846 /* The exported length must be set by psa_export_key() to a value between 0
847 * and export_size. On errors, the exported length must be 0. */
848 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
849 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
850 TEST_ASSERT( exported_length <= export_size );
851
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200852 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200853 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100854 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200855 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100856 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100857 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200858 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100859
Gilles Peskineea38a922021-02-13 00:05:16 +0100860 /* Run sanity checks on the exported key. For non-canonical inputs,
861 * this validates the canonical representations. For canonical inputs,
862 * this doesn't directly validate the implementation, but it still helps
863 * by cross-validating the test data with the sanity check code. */
864 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200865 goto exit;
866
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100867 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200868 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100869 else
870 {
Ronald Cron5425a212020-08-04 14:58:35 +0200871 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200872 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200873 &key2 ) );
874 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100875 reexported,
876 export_size,
877 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200878 ASSERT_COMPARE( exported, exported_length,
879 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200880 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100881 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100882 TEST_ASSERT( exported_length <=
883 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
884 psa_get_key_bits( &got_attributes ) ) );
885 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100886
887destroy:
888 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200889 PSA_ASSERT( psa_destroy_key( key ) );
890 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100891
892exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100893 /*
894 * Key attributes may have been returned by psa_get_key_attributes()
895 * thus reset them as required.
896 */
897 psa_reset_key_attributes( &got_attributes );
898
itayzafrir3e02b3b2018-06-12 17:06:52 +0300899 mbedtls_free( exported );
900 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200901 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100902}
903/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100904
Moran Pekerf709f4a2018-06-06 17:26:04 +0300905/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300906void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200907 int type_arg,
908 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100909 int export_size_delta,
910 int expected_export_status_arg,
911 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300912{
Ronald Cron5425a212020-08-04 14:58:35 +0200913 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300914 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200915 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200916 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300917 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300918 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100919 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100920 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200921 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300922
Gilles Peskine8817f612018-12-18 00:18:46 +0100923 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300924
Gilles Peskine4747d192019-04-17 15:05:45 +0200925 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
926 psa_set_key_algorithm( &attributes, alg );
927 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300928
929 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200930 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300931
Gilles Peskine49c25912018-10-29 15:15:31 +0100932 /* Export the public key */
933 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200934 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200935 exported, export_size,
936 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100937 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100938 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100939 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200940 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100941 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200942 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200943 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100944 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100945 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100946 TEST_ASSERT( expected_public_key->len <=
947 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
948 TEST_ASSERT( expected_public_key->len <=
949 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100950 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
951 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100952 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300953
954exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100955 /*
956 * Key attributes may have been returned by psa_get_key_attributes()
957 * thus reset them as required.
958 */
959 psa_reset_key_attributes( &attributes );
960
itayzafrir3e02b3b2018-06-12 17:06:52 +0300961 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200962 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200963 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300964}
965/* END_CASE */
966
Gilles Peskine20035e32018-02-03 22:44:14 +0100967/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200968void import_and_exercise_key( data_t *data,
969 int type_arg,
970 int bits_arg,
971 int alg_arg )
972{
Ronald Cron5425a212020-08-04 14:58:35 +0200973 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200974 psa_key_type_t type = type_arg;
975 size_t bits = bits_arg;
976 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100977 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200978 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200979 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200980
Gilles Peskine8817f612018-12-18 00:18:46 +0100981 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200982
Gilles Peskine4747d192019-04-17 15:05:45 +0200983 psa_set_key_usage_flags( &attributes, usage );
984 psa_set_key_algorithm( &attributes, alg );
985 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200986
987 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200988 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200989
990 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200991 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200992 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
993 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200994
995 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100996 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200997 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200998
Ronald Cron5425a212020-08-04 14:58:35 +0200999 PSA_ASSERT( psa_destroy_key( key ) );
1000 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001001
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001002exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001003 /*
1004 * Key attributes may have been returned by psa_get_key_attributes()
1005 * thus reset them as required.
1006 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001007 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001008
1009 psa_reset_key_attributes( &attributes );
1010 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001011 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001012}
1013/* END_CASE */
1014
1015/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001016void effective_key_attributes( int type_arg, int expected_type_arg,
1017 int bits_arg, int expected_bits_arg,
1018 int usage_arg, int expected_usage_arg,
1019 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001020{
Ronald Cron5425a212020-08-04 14:58:35 +02001021 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001022 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001023 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001024 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001025 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001026 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001027 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001028 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001029 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001030 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001031
Gilles Peskine8817f612018-12-18 00:18:46 +01001032 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001033
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001034 psa_set_key_usage_flags( &attributes, usage );
1035 psa_set_key_algorithm( &attributes, alg );
1036 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001037 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001038
Ronald Cron5425a212020-08-04 14:58:35 +02001039 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001040 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001041
Ronald Cron5425a212020-08-04 14:58:35 +02001042 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001043 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1044 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1045 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1046 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001047
1048exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001049 /*
1050 * Key attributes may have been returned by psa_get_key_attributes()
1051 * thus reset them as required.
1052 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001053 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001054
1055 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001056 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001057}
1058/* END_CASE */
1059
1060/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001061void check_key_policy( int type_arg, int bits_arg,
1062 int usage_arg, int alg_arg )
1063{
1064 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1065 usage_arg, usage_arg, alg_arg, alg_arg );
1066 goto exit;
1067}
1068/* END_CASE */
1069
1070/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001071void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001072{
1073 /* Test each valid way of initializing the object, except for `= {0}`, as
1074 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1075 * though it's OK by the C standard. We could test for this, but we'd need
1076 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001077 psa_key_attributes_t func = psa_key_attributes_init( );
1078 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1079 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001080
1081 memset( &zero, 0, sizeof( zero ) );
1082
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001083 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1084 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1085 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001086
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001087 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1088 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1089 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1090
1091 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1092 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1093 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1094
1095 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1096 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1097 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1098
1099 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1100 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1101 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001102}
1103/* END_CASE */
1104
1105/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001106void mac_key_policy( int policy_usage,
1107 int policy_alg,
1108 int key_type,
1109 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001110 int exercise_alg,
1111 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001112{
Ronald Cron5425a212020-08-04 14:58:35 +02001113 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001114 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001115 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001116 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001117 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001118 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001119
Gilles Peskine8817f612018-12-18 00:18:46 +01001120 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001121
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001122 psa_set_key_usage_flags( &attributes, policy_usage );
1123 psa_set_key_algorithm( &attributes, policy_alg );
1124 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001125
Gilles Peskine049c7532019-05-15 20:22:09 +02001126 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001127 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001128
Ronald Cron5425a212020-08-04 14:58:35 +02001129 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001130 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001131 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001132 else
1133 TEST_EQUAL( status, expected_status );
1134
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001135 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001136
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001137 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001138 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001139 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001140 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001141 else
1142 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001143
1144exit:
1145 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001146 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001147 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001148}
1149/* END_CASE */
1150
1151/* BEGIN_CASE */
1152void cipher_key_policy( int policy_usage,
1153 int policy_alg,
1154 int key_type,
1155 data_t *key_data,
1156 int exercise_alg )
1157{
Ronald Cron5425a212020-08-04 14:58:35 +02001158 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001159 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001160 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001161 psa_status_t status;
1162
Gilles Peskine8817f612018-12-18 00:18:46 +01001163 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001164
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001165 psa_set_key_usage_flags( &attributes, policy_usage );
1166 psa_set_key_algorithm( &attributes, policy_alg );
1167 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001168
Gilles Peskine049c7532019-05-15 20:22:09 +02001169 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001170 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001171
Ronald Cron5425a212020-08-04 14:58:35 +02001172 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001173 if( policy_alg == exercise_alg &&
1174 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001175 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001176 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001177 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001178 psa_cipher_abort( &operation );
1179
Ronald Cron5425a212020-08-04 14:58:35 +02001180 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001181 if( policy_alg == exercise_alg &&
1182 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001183 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001184 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001185 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001186
1187exit:
1188 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001189 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001190 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001191}
1192/* END_CASE */
1193
1194/* BEGIN_CASE */
1195void aead_key_policy( int policy_usage,
1196 int policy_alg,
1197 int key_type,
1198 data_t *key_data,
1199 int nonce_length_arg,
1200 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001201 int exercise_alg,
1202 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001203{
Ronald Cron5425a212020-08-04 14:58:35 +02001204 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001205 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001206 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001207 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001208 unsigned char nonce[16] = {0};
1209 size_t nonce_length = nonce_length_arg;
1210 unsigned char tag[16];
1211 size_t tag_length = tag_length_arg;
1212 size_t output_length;
1213
1214 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1215 TEST_ASSERT( tag_length <= sizeof( tag ) );
1216
Gilles Peskine8817f612018-12-18 00:18:46 +01001217 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001218
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001219 psa_set_key_usage_flags( &attributes, policy_usage );
1220 psa_set_key_algorithm( &attributes, policy_alg );
1221 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001222
Gilles Peskine049c7532019-05-15 20:22:09 +02001223 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001224 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001225
Ronald Cron5425a212020-08-04 14:58:35 +02001226 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001227 nonce, nonce_length,
1228 NULL, 0,
1229 NULL, 0,
1230 tag, tag_length,
1231 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001232 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1233 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001234 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001235 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001236
1237 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001238 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001239 nonce, nonce_length,
1240 NULL, 0,
1241 tag, tag_length,
1242 NULL, 0,
1243 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001244 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1245 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1246 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001247 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001248 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001249 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001250
1251exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001252 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001253 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001254}
1255/* END_CASE */
1256
1257/* BEGIN_CASE */
1258void asymmetric_encryption_key_policy( int policy_usage,
1259 int policy_alg,
1260 int key_type,
1261 data_t *key_data,
1262 int exercise_alg )
1263{
Ronald Cron5425a212020-08-04 14:58:35 +02001264 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001265 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001266 psa_status_t status;
1267 size_t key_bits;
1268 size_t buffer_length;
1269 unsigned char *buffer = NULL;
1270 size_t output_length;
1271
Gilles Peskine8817f612018-12-18 00:18:46 +01001272 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001273
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001274 psa_set_key_usage_flags( &attributes, policy_usage );
1275 psa_set_key_algorithm( &attributes, policy_alg );
1276 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001277
Gilles Peskine049c7532019-05-15 20:22:09 +02001278 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001279 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001280
Ronald Cron5425a212020-08-04 14:58:35 +02001281 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001282 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001283 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1284 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001285 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001286
Ronald Cron5425a212020-08-04 14:58:35 +02001287 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001288 NULL, 0,
1289 NULL, 0,
1290 buffer, buffer_length,
1291 &output_length );
1292 if( policy_alg == exercise_alg &&
1293 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001294 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001295 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001296 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001297
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001298 if( buffer_length != 0 )
1299 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001300 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001301 buffer, buffer_length,
1302 NULL, 0,
1303 buffer, buffer_length,
1304 &output_length );
1305 if( policy_alg == exercise_alg &&
1306 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001307 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001308 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001309 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001310
1311exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001312 /*
1313 * Key attributes may have been returned by psa_get_key_attributes()
1314 * thus reset them as required.
1315 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001316 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001317
1318 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001319 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001320 mbedtls_free( buffer );
1321}
1322/* END_CASE */
1323
1324/* BEGIN_CASE */
1325void asymmetric_signature_key_policy( int policy_usage,
1326 int policy_alg,
1327 int key_type,
1328 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001329 int exercise_alg,
1330 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001331{
Ronald Cron5425a212020-08-04 14:58:35 +02001332 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001333 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001334 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001335 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1336 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1337 * compatible with the policy and `payload_length_arg` is supposed to be
1338 * a valid input length to sign. If `payload_length_arg <= 0`,
1339 * `exercise_alg` is supposed to be forbidden by the policy. */
1340 int compatible_alg = payload_length_arg > 0;
1341 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001342 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001343 size_t signature_length;
1344
Gilles Peskine8817f612018-12-18 00:18:46 +01001345 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001346
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001347 psa_set_key_usage_flags( &attributes, policy_usage );
1348 psa_set_key_algorithm( &attributes, policy_alg );
1349 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001350
Gilles Peskine049c7532019-05-15 20:22:09 +02001351 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001352 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001353
Ronald Cron5425a212020-08-04 14:58:35 +02001354 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001355 payload, payload_length,
1356 signature, sizeof( signature ),
1357 &signature_length );
1358 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001359 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001360 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001361 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001362
1363 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001364 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001365 payload, payload_length,
1366 signature, sizeof( signature ) );
1367 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001368 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001369 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001370 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001371
1372exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001373 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001374 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001375}
1376/* END_CASE */
1377
Janos Follathba3fab92019-06-11 14:50:16 +01001378/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001379void derive_key_policy( int policy_usage,
1380 int policy_alg,
1381 int key_type,
1382 data_t *key_data,
1383 int exercise_alg )
1384{
Ronald Cron5425a212020-08-04 14:58:35 +02001385 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001386 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001387 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001388 psa_status_t status;
1389
Gilles Peskine8817f612018-12-18 00:18:46 +01001390 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001391
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001392 psa_set_key_usage_flags( &attributes, policy_usage );
1393 psa_set_key_algorithm( &attributes, policy_alg );
1394 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001395
Gilles Peskine049c7532019-05-15 20:22:09 +02001396 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001397 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001398
Janos Follathba3fab92019-06-11 14:50:16 +01001399 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1400
1401 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1402 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001403 {
Janos Follathba3fab92019-06-11 14:50:16 +01001404 PSA_ASSERT( psa_key_derivation_input_bytes(
1405 &operation,
1406 PSA_KEY_DERIVATION_INPUT_SEED,
1407 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001408 }
Janos Follathba3fab92019-06-11 14:50:16 +01001409
1410 status = psa_key_derivation_input_key( &operation,
1411 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001412 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001413
Gilles Peskineea0fb492018-07-12 17:17:20 +02001414 if( policy_alg == exercise_alg &&
1415 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001416 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001417 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001418 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001419
1420exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001421 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001422 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001423 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001424}
1425/* END_CASE */
1426
1427/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001428void agreement_key_policy( int policy_usage,
1429 int policy_alg,
1430 int key_type_arg,
1431 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001432 int exercise_alg,
1433 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001434{
Ronald Cron5425a212020-08-04 14:58:35 +02001435 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001436 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001437 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001438 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001439 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001440 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001441
Gilles Peskine8817f612018-12-18 00:18:46 +01001442 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001443
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001444 psa_set_key_usage_flags( &attributes, policy_usage );
1445 psa_set_key_algorithm( &attributes, policy_alg );
1446 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001447
Gilles Peskine049c7532019-05-15 20:22:09 +02001448 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001449 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001450
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001451 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001452 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001453
Steven Cooremance48e852020-10-05 16:02:45 +02001454 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001455
1456exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001457 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001458 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001459 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001460}
1461/* END_CASE */
1462
1463/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001464void key_policy_alg2( int key_type_arg, data_t *key_data,
1465 int usage_arg, int alg_arg, int alg2_arg )
1466{
Ronald Cron5425a212020-08-04 14:58:35 +02001467 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001468 psa_key_type_t key_type = key_type_arg;
1469 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1470 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1471 psa_key_usage_t usage = usage_arg;
1472 psa_algorithm_t alg = alg_arg;
1473 psa_algorithm_t alg2 = alg2_arg;
1474
1475 PSA_ASSERT( psa_crypto_init( ) );
1476
1477 psa_set_key_usage_flags( &attributes, usage );
1478 psa_set_key_algorithm( &attributes, alg );
1479 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1480 psa_set_key_type( &attributes, key_type );
1481 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001482 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001483
Ronald Cron5425a212020-08-04 14:58:35 +02001484 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001485 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1486 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1487 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1488
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001489 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001490 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001491 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001492 goto exit;
1493
1494exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001495 /*
1496 * Key attributes may have been returned by psa_get_key_attributes()
1497 * thus reset them as required.
1498 */
1499 psa_reset_key_attributes( &got_attributes );
1500
Ronald Cron5425a212020-08-04 14:58:35 +02001501 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001502 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001503}
1504/* END_CASE */
1505
1506/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001507void raw_agreement_key_policy( int policy_usage,
1508 int policy_alg,
1509 int key_type_arg,
1510 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001511 int exercise_alg,
1512 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001513{
Ronald Cron5425a212020-08-04 14:58:35 +02001514 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001515 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001516 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001517 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001518 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001519 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001520
1521 PSA_ASSERT( psa_crypto_init( ) );
1522
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001523 psa_set_key_usage_flags( &attributes, policy_usage );
1524 psa_set_key_algorithm( &attributes, policy_alg );
1525 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001526
Gilles Peskine049c7532019-05-15 20:22:09 +02001527 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001528 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001529
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001530 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001531
Steven Cooremance48e852020-10-05 16:02:45 +02001532 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001533
1534exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001535 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001536 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001537 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001538}
1539/* END_CASE */
1540
1541/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001542void copy_success( int source_usage_arg,
1543 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001544 int type_arg, data_t *material,
1545 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001546 int target_usage_arg,
1547 int target_alg_arg, int target_alg2_arg,
1548 int expected_usage_arg,
1549 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001550{
Gilles Peskineca25db92019-04-19 11:43:08 +02001551 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1552 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001553 psa_key_usage_t expected_usage = expected_usage_arg;
1554 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001555 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001556 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1557 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001558 uint8_t *export_buffer = NULL;
1559
Gilles Peskine57ab7212019-01-28 13:03:09 +01001560 PSA_ASSERT( psa_crypto_init( ) );
1561
Gilles Peskineca25db92019-04-19 11:43:08 +02001562 /* Prepare the source key. */
1563 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1564 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001565 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001566 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001567 PSA_ASSERT( psa_import_key( &source_attributes,
1568 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001569 &source_key ) );
1570 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001571
Gilles Peskineca25db92019-04-19 11:43:08 +02001572 /* Prepare the target attributes. */
1573 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001574 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001575 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001576 /* Set volatile lifetime to reset the key identifier to 0. */
1577 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1578 }
1579
Gilles Peskineca25db92019-04-19 11:43:08 +02001580 if( target_usage_arg != -1 )
1581 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1582 if( target_alg_arg != -1 )
1583 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001584 if( target_alg2_arg != -1 )
1585 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001586
1587 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001588 PSA_ASSERT( psa_copy_key( source_key,
1589 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001590
1591 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001592 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001593
1594 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001595 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001596 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1597 psa_get_key_type( &target_attributes ) );
1598 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1599 psa_get_key_bits( &target_attributes ) );
1600 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1601 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001602 TEST_EQUAL( expected_alg2,
1603 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001604 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1605 {
1606 size_t length;
1607 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001608 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001609 material->len, &length ) );
1610 ASSERT_COMPARE( material->x, material->len,
1611 export_buffer, length );
1612 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001613
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001614 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001615 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001616 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001617 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001618
Ronald Cron5425a212020-08-04 14:58:35 +02001619 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001620
1621exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001622 /*
1623 * Source and target key attributes may have been returned by
1624 * psa_get_key_attributes() thus reset them as required.
1625 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001626 psa_reset_key_attributes( &source_attributes );
1627 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001628
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001629 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001630 mbedtls_free( export_buffer );
1631}
1632/* END_CASE */
1633
1634/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001635void copy_fail( int source_usage_arg,
1636 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001637 int type_arg, data_t *material,
1638 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001639 int target_usage_arg,
1640 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001641 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001642 int expected_status_arg )
1643{
1644 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1645 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001646 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1647 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001648 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001649
1650 PSA_ASSERT( psa_crypto_init( ) );
1651
1652 /* Prepare the source key. */
1653 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1654 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001655 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001656 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001657 PSA_ASSERT( psa_import_key( &source_attributes,
1658 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001659 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001660
1661 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001662 psa_set_key_id( &target_attributes, key_id );
1663 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001664 psa_set_key_type( &target_attributes, target_type_arg );
1665 psa_set_key_bits( &target_attributes, target_bits_arg );
1666 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1667 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001668 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001669
1670 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001671 TEST_EQUAL( psa_copy_key( source_key,
1672 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001673 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001674
Ronald Cron5425a212020-08-04 14:58:35 +02001675 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001676
Gilles Peskine4a644642019-05-03 17:14:08 +02001677exit:
1678 psa_reset_key_attributes( &source_attributes );
1679 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001680 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001681}
1682/* END_CASE */
1683
1684/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001685void hash_operation_init( )
1686{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001687 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001688 /* Test each valid way of initializing the object, except for `= {0}`, as
1689 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1690 * though it's OK by the C standard. We could test for this, but we'd need
1691 * to supress the Clang warning for the test. */
1692 psa_hash_operation_t func = psa_hash_operation_init( );
1693 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1694 psa_hash_operation_t zero;
1695
1696 memset( &zero, 0, sizeof( zero ) );
1697
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001698 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001699 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1700 PSA_ERROR_BAD_STATE );
1701 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1702 PSA_ERROR_BAD_STATE );
1703 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1704 PSA_ERROR_BAD_STATE );
1705
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001706 /* A default hash operation should be abortable without error. */
1707 PSA_ASSERT( psa_hash_abort( &func ) );
1708 PSA_ASSERT( psa_hash_abort( &init ) );
1709 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001710}
1711/* END_CASE */
1712
1713/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001714void hash_setup( int alg_arg,
1715 int expected_status_arg )
1716{
1717 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001718 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001719 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001720 psa_status_t status;
1721
Gilles Peskine8817f612018-12-18 00:18:46 +01001722 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001723
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001724 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001725 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001726
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001727 /* Whether setup succeeded or failed, abort must succeed. */
1728 PSA_ASSERT( psa_hash_abort( &operation ) );
1729
1730 /* If setup failed, reproduce the failure, so as to
1731 * test the resulting state of the operation object. */
1732 if( status != PSA_SUCCESS )
1733 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1734
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001735 /* Now the operation object should be reusable. */
1736#if defined(KNOWN_SUPPORTED_HASH_ALG)
1737 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1738 PSA_ASSERT( psa_hash_abort( &operation ) );
1739#endif
1740
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001741exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001742 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001743}
1744/* END_CASE */
1745
1746/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001747void hash_compute_fail( int alg_arg, data_t *input,
1748 int output_size_arg, int expected_status_arg )
1749{
1750 psa_algorithm_t alg = alg_arg;
1751 uint8_t *output = NULL;
1752 size_t output_size = output_size_arg;
1753 size_t output_length = INVALID_EXPORT_LENGTH;
1754 psa_status_t expected_status = expected_status_arg;
1755 psa_status_t status;
1756
1757 ASSERT_ALLOC( output, output_size );
1758
1759 PSA_ASSERT( psa_crypto_init( ) );
1760
1761 status = psa_hash_compute( alg, input->x, input->len,
1762 output, output_size, &output_length );
1763 TEST_EQUAL( status, expected_status );
1764 TEST_ASSERT( output_length <= output_size );
1765
1766exit:
1767 mbedtls_free( output );
1768 PSA_DONE( );
1769}
1770/* END_CASE */
1771
1772/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001773void hash_compare_fail( int alg_arg, data_t *input,
1774 data_t *reference_hash,
1775 int expected_status_arg )
1776{
1777 psa_algorithm_t alg = alg_arg;
1778 psa_status_t expected_status = expected_status_arg;
1779 psa_status_t status;
1780
1781 PSA_ASSERT( psa_crypto_init( ) );
1782
1783 status = psa_hash_compare( alg, input->x, input->len,
1784 reference_hash->x, reference_hash->len );
1785 TEST_EQUAL( status, expected_status );
1786
1787exit:
1788 PSA_DONE( );
1789}
1790/* END_CASE */
1791
1792/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001793void hash_compute_compare( int alg_arg, data_t *input,
1794 data_t *expected_output )
1795{
1796 psa_algorithm_t alg = alg_arg;
1797 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1798 size_t output_length = INVALID_EXPORT_LENGTH;
1799 size_t i;
1800
1801 PSA_ASSERT( psa_crypto_init( ) );
1802
1803 /* Compute with tight buffer */
1804 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001805 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001806 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001807 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001808 ASSERT_COMPARE( output, output_length,
1809 expected_output->x, expected_output->len );
1810
1811 /* Compute with larger buffer */
1812 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1813 output, sizeof( output ),
1814 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001815 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001816 ASSERT_COMPARE( output, output_length,
1817 expected_output->x, expected_output->len );
1818
1819 /* Compare with correct hash */
1820 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1821 output, output_length ) );
1822
1823 /* Compare with trailing garbage */
1824 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1825 output, output_length + 1 ),
1826 PSA_ERROR_INVALID_SIGNATURE );
1827
1828 /* Compare with truncated hash */
1829 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1830 output, output_length - 1 ),
1831 PSA_ERROR_INVALID_SIGNATURE );
1832
1833 /* Compare with corrupted value */
1834 for( i = 0; i < output_length; i++ )
1835 {
Chris Jones9634bb12021-01-20 15:56:42 +00001836 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001837 output[i] ^= 1;
1838 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1839 output, output_length ),
1840 PSA_ERROR_INVALID_SIGNATURE );
1841 output[i] ^= 1;
1842 }
1843
1844exit:
1845 PSA_DONE( );
1846}
1847/* END_CASE */
1848
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001849/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001850void hash_bad_order( )
1851{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001852 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001853 unsigned char input[] = "";
1854 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001855 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001856 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1857 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1858 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001859 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001860 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001861 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001862
Gilles Peskine8817f612018-12-18 00:18:46 +01001863 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001864
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001865 /* Call setup twice in a row. */
1866 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1867 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1868 PSA_ERROR_BAD_STATE );
1869 PSA_ASSERT( psa_hash_abort( &operation ) );
1870
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001871 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001872 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001873 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001874 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001875
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001876 /* Call update after finish. */
1877 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1878 PSA_ASSERT( psa_hash_finish( &operation,
1879 hash, sizeof( hash ), &hash_len ) );
1880 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001881 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001882 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001883
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001884 /* Call verify without calling setup beforehand. */
1885 TEST_EQUAL( psa_hash_verify( &operation,
1886 valid_hash, sizeof( valid_hash ) ),
1887 PSA_ERROR_BAD_STATE );
1888 PSA_ASSERT( psa_hash_abort( &operation ) );
1889
1890 /* Call verify after finish. */
1891 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1892 PSA_ASSERT( psa_hash_finish( &operation,
1893 hash, sizeof( hash ), &hash_len ) );
1894 TEST_EQUAL( psa_hash_verify( &operation,
1895 valid_hash, sizeof( valid_hash ) ),
1896 PSA_ERROR_BAD_STATE );
1897 PSA_ASSERT( psa_hash_abort( &operation ) );
1898
1899 /* Call verify twice in a row. */
1900 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1901 PSA_ASSERT( psa_hash_verify( &operation,
1902 valid_hash, sizeof( valid_hash ) ) );
1903 TEST_EQUAL( psa_hash_verify( &operation,
1904 valid_hash, sizeof( valid_hash ) ),
1905 PSA_ERROR_BAD_STATE );
1906 PSA_ASSERT( psa_hash_abort( &operation ) );
1907
1908 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001909 TEST_EQUAL( psa_hash_finish( &operation,
1910 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001911 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001912 PSA_ASSERT( psa_hash_abort( &operation ) );
1913
1914 /* Call finish twice in a row. */
1915 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1916 PSA_ASSERT( psa_hash_finish( &operation,
1917 hash, sizeof( hash ), &hash_len ) );
1918 TEST_EQUAL( psa_hash_finish( &operation,
1919 hash, sizeof( hash ), &hash_len ),
1920 PSA_ERROR_BAD_STATE );
1921 PSA_ASSERT( psa_hash_abort( &operation ) );
1922
1923 /* Call finish after calling verify. */
1924 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1925 PSA_ASSERT( psa_hash_verify( &operation,
1926 valid_hash, sizeof( valid_hash ) ) );
1927 TEST_EQUAL( psa_hash_finish( &operation,
1928 hash, sizeof( hash ), &hash_len ),
1929 PSA_ERROR_BAD_STATE );
1930 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001931
1932exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001933 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001934}
1935/* END_CASE */
1936
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001937/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001938void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001939{
1940 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001941 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1942 * appended to it */
1943 unsigned char hash[] = {
1944 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1945 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1946 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001947 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001948 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001949
Gilles Peskine8817f612018-12-18 00:18:46 +01001950 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001951
itayzafrir27e69452018-11-01 14:26:34 +02001952 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001953 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001954 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001955 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001956
itayzafrir27e69452018-11-01 14:26:34 +02001957 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001958 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001959 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001960 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001961
itayzafrir27e69452018-11-01 14:26:34 +02001962 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001963 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001964 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001965 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001966
itayzafrirec93d302018-10-18 18:01:10 +03001967exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001968 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001969}
1970/* END_CASE */
1971
Ronald Cronee414c72021-03-18 18:50:08 +01001972/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001973void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001974{
1975 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001976 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001977 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001978 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001979 size_t hash_len;
1980
Gilles Peskine8817f612018-12-18 00:18:46 +01001981 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001982
itayzafrir58028322018-10-25 10:22:01 +03001983 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001984 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001985 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001986 hash, expected_size - 1, &hash_len ),
1987 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001988
1989exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001990 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001991}
1992/* END_CASE */
1993
Ronald Cronee414c72021-03-18 18:50:08 +01001994/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001995void hash_clone_source_state( )
1996{
1997 psa_algorithm_t alg = PSA_ALG_SHA_256;
1998 unsigned char hash[PSA_HASH_MAX_SIZE];
1999 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2000 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2001 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2002 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2003 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2004 size_t hash_len;
2005
2006 PSA_ASSERT( psa_crypto_init( ) );
2007 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2008
2009 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2010 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2011 PSA_ASSERT( psa_hash_finish( &op_finished,
2012 hash, sizeof( hash ), &hash_len ) );
2013 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2014 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2015
2016 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2017 PSA_ERROR_BAD_STATE );
2018
2019 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2020 PSA_ASSERT( psa_hash_finish( &op_init,
2021 hash, sizeof( hash ), &hash_len ) );
2022 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2023 PSA_ASSERT( psa_hash_finish( &op_finished,
2024 hash, sizeof( hash ), &hash_len ) );
2025 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2026 PSA_ASSERT( psa_hash_finish( &op_aborted,
2027 hash, sizeof( hash ), &hash_len ) );
2028
2029exit:
2030 psa_hash_abort( &op_source );
2031 psa_hash_abort( &op_init );
2032 psa_hash_abort( &op_setup );
2033 psa_hash_abort( &op_finished );
2034 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002035 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002036}
2037/* END_CASE */
2038
Ronald Cronee414c72021-03-18 18:50:08 +01002039/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002040void hash_clone_target_state( )
2041{
2042 psa_algorithm_t alg = PSA_ALG_SHA_256;
2043 unsigned char hash[PSA_HASH_MAX_SIZE];
2044 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2045 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2046 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2047 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2048 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2049 size_t hash_len;
2050
2051 PSA_ASSERT( psa_crypto_init( ) );
2052
2053 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2054 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2055 PSA_ASSERT( psa_hash_finish( &op_finished,
2056 hash, sizeof( hash ), &hash_len ) );
2057 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2058 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2059
2060 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2061 PSA_ASSERT( psa_hash_finish( &op_target,
2062 hash, sizeof( hash ), &hash_len ) );
2063
2064 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2065 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2066 PSA_ERROR_BAD_STATE );
2067 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2068 PSA_ERROR_BAD_STATE );
2069
2070exit:
2071 psa_hash_abort( &op_target );
2072 psa_hash_abort( &op_init );
2073 psa_hash_abort( &op_setup );
2074 psa_hash_abort( &op_finished );
2075 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002076 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002077}
2078/* END_CASE */
2079
itayzafrir58028322018-10-25 10:22:01 +03002080/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002081void mac_operation_init( )
2082{
Jaeden Amero252ef282019-02-15 14:05:35 +00002083 const uint8_t input[1] = { 0 };
2084
Jaeden Amero769ce272019-01-04 11:48:03 +00002085 /* Test each valid way of initializing the object, except for `= {0}`, as
2086 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2087 * though it's OK by the C standard. We could test for this, but we'd need
2088 * to supress the Clang warning for the test. */
2089 psa_mac_operation_t func = psa_mac_operation_init( );
2090 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2091 psa_mac_operation_t zero;
2092
2093 memset( &zero, 0, sizeof( zero ) );
2094
Jaeden Amero252ef282019-02-15 14:05:35 +00002095 /* A freshly-initialized MAC operation should not be usable. */
2096 TEST_EQUAL( psa_mac_update( &func,
2097 input, sizeof( input ) ),
2098 PSA_ERROR_BAD_STATE );
2099 TEST_EQUAL( psa_mac_update( &init,
2100 input, sizeof( input ) ),
2101 PSA_ERROR_BAD_STATE );
2102 TEST_EQUAL( psa_mac_update( &zero,
2103 input, sizeof( input ) ),
2104 PSA_ERROR_BAD_STATE );
2105
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002106 /* A default MAC operation should be abortable without error. */
2107 PSA_ASSERT( psa_mac_abort( &func ) );
2108 PSA_ASSERT( psa_mac_abort( &init ) );
2109 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002110}
2111/* END_CASE */
2112
2113/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002114void mac_setup( int key_type_arg,
2115 data_t *key,
2116 int alg_arg,
2117 int expected_status_arg )
2118{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002119 psa_key_type_t key_type = key_type_arg;
2120 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002121 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002122 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002123 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2124#if defined(KNOWN_SUPPORTED_MAC_ALG)
2125 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2126#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002127
Gilles Peskine8817f612018-12-18 00:18:46 +01002128 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002129
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002130 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2131 &operation, &status ) )
2132 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002133 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002134
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002135 /* The operation object should be reusable. */
2136#if defined(KNOWN_SUPPORTED_MAC_ALG)
2137 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2138 smoke_test_key_data,
2139 sizeof( smoke_test_key_data ),
2140 KNOWN_SUPPORTED_MAC_ALG,
2141 &operation, &status ) )
2142 goto exit;
2143 TEST_EQUAL( status, PSA_SUCCESS );
2144#endif
2145
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002146exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002147 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002148}
2149/* END_CASE */
2150
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002151/* 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 +00002152void mac_bad_order( )
2153{
Ronald Cron5425a212020-08-04 14:58:35 +02002154 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002155 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2156 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002157 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002158 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2159 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2160 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002161 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002162 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2163 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2164 size_t sign_mac_length = 0;
2165 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2166 const uint8_t verify_mac[] = {
2167 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2168 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2169 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2170
2171 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002172 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002173 psa_set_key_algorithm( &attributes, alg );
2174 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002175
Ronald Cron5425a212020-08-04 14:58:35 +02002176 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2177 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002178
Jaeden Amero252ef282019-02-15 14:05:35 +00002179 /* Call update without calling setup beforehand. */
2180 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2181 PSA_ERROR_BAD_STATE );
2182 PSA_ASSERT( psa_mac_abort( &operation ) );
2183
2184 /* Call sign finish without calling setup beforehand. */
2185 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2186 &sign_mac_length),
2187 PSA_ERROR_BAD_STATE );
2188 PSA_ASSERT( psa_mac_abort( &operation ) );
2189
2190 /* Call verify finish without calling setup beforehand. */
2191 TEST_EQUAL( psa_mac_verify_finish( &operation,
2192 verify_mac, sizeof( verify_mac ) ),
2193 PSA_ERROR_BAD_STATE );
2194 PSA_ASSERT( psa_mac_abort( &operation ) );
2195
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002196 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002197 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2198 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002199 PSA_ERROR_BAD_STATE );
2200 PSA_ASSERT( psa_mac_abort( &operation ) );
2201
Jaeden Amero252ef282019-02-15 14:05:35 +00002202 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002203 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002204 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2205 PSA_ASSERT( psa_mac_sign_finish( &operation,
2206 sign_mac, sizeof( sign_mac ),
2207 &sign_mac_length ) );
2208 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2209 PSA_ERROR_BAD_STATE );
2210 PSA_ASSERT( psa_mac_abort( &operation ) );
2211
2212 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002213 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002214 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2215 PSA_ASSERT( psa_mac_verify_finish( &operation,
2216 verify_mac, sizeof( verify_mac ) ) );
2217 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2218 PSA_ERROR_BAD_STATE );
2219 PSA_ASSERT( psa_mac_abort( &operation ) );
2220
2221 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002222 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002223 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2224 PSA_ASSERT( psa_mac_sign_finish( &operation,
2225 sign_mac, sizeof( sign_mac ),
2226 &sign_mac_length ) );
2227 TEST_EQUAL( psa_mac_sign_finish( &operation,
2228 sign_mac, sizeof( sign_mac ),
2229 &sign_mac_length ),
2230 PSA_ERROR_BAD_STATE );
2231 PSA_ASSERT( psa_mac_abort( &operation ) );
2232
2233 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002234 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002235 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2236 PSA_ASSERT( psa_mac_verify_finish( &operation,
2237 verify_mac, sizeof( verify_mac ) ) );
2238 TEST_EQUAL( psa_mac_verify_finish( &operation,
2239 verify_mac, sizeof( verify_mac ) ),
2240 PSA_ERROR_BAD_STATE );
2241 PSA_ASSERT( psa_mac_abort( &operation ) );
2242
2243 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002244 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002245 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2246 TEST_EQUAL( psa_mac_verify_finish( &operation,
2247 verify_mac, sizeof( verify_mac ) ),
2248 PSA_ERROR_BAD_STATE );
2249 PSA_ASSERT( psa_mac_abort( &operation ) );
2250
2251 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002252 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002253 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2254 TEST_EQUAL( psa_mac_sign_finish( &operation,
2255 sign_mac, sizeof( sign_mac ),
2256 &sign_mac_length ),
2257 PSA_ERROR_BAD_STATE );
2258 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002259
Ronald Cron5425a212020-08-04 14:58:35 +02002260 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002261
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002262exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002263 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002264}
2265/* END_CASE */
2266
2267/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002268void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002269 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002270 int alg_arg,
2271 data_t *input,
2272 data_t *expected_mac )
2273{
Ronald Cron5425a212020-08-04 14:58:35 +02002274 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002275 psa_key_type_t key_type = key_type_arg;
2276 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002277 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002278 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002279 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002280 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002281 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002282 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002283 const size_t output_sizes_to_test[] = {
2284 0,
2285 1,
2286 expected_mac->len - 1,
2287 expected_mac->len,
2288 expected_mac->len + 1,
2289 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002290
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002291 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002292 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002293 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002294
Gilles Peskine8817f612018-12-18 00:18:46 +01002295 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002296
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002297 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002298 psa_set_key_algorithm( &attributes, alg );
2299 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002300
Ronald Cron5425a212020-08-04 14:58:35 +02002301 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2302 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002303
Gilles Peskine8b356b52020-08-25 23:44:59 +02002304 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2305 {
2306 const size_t output_size = output_sizes_to_test[i];
2307 psa_status_t expected_status =
2308 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2309 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002310
Chris Jones9634bb12021-01-20 15:56:42 +00002311 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002312 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002313
Gilles Peskine8b356b52020-08-25 23:44:59 +02002314 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002315 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002316 PSA_ASSERT( psa_mac_update( &operation,
2317 input->x, input->len ) );
2318 TEST_EQUAL( psa_mac_sign_finish( &operation,
2319 actual_mac, output_size,
2320 &mac_length ),
2321 expected_status );
2322 PSA_ASSERT( psa_mac_abort( &operation ) );
2323
2324 if( expected_status == PSA_SUCCESS )
2325 {
2326 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2327 actual_mac, mac_length );
2328 }
2329 mbedtls_free( actual_mac );
2330 actual_mac = NULL;
2331 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002332
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002333exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002334 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002335 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002336 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002337 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002338}
2339/* END_CASE */
2340
2341/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002342void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002343 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002344 int alg_arg,
2345 data_t *input,
2346 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002347{
Ronald Cron5425a212020-08-04 14:58:35 +02002348 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002349 psa_key_type_t key_type = key_type_arg;
2350 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002351 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002353 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002354
Gilles Peskine69c12672018-06-28 00:07:19 +02002355 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2356
Gilles Peskine8817f612018-12-18 00:18:46 +01002357 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002358
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002359 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002360 psa_set_key_algorithm( &attributes, alg );
2361 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002362
Ronald Cron5425a212020-08-04 14:58:35 +02002363 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2364 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002365
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002366 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002367 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002368 PSA_ASSERT( psa_mac_update( &operation,
2369 input->x, input->len ) );
2370 PSA_ASSERT( psa_mac_verify_finish( &operation,
2371 expected_mac->x,
2372 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002373
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002374 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002375 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002376 PSA_ASSERT( psa_mac_update( &operation,
2377 input->x, input->len ) );
2378 TEST_EQUAL( psa_mac_verify_finish( &operation,
2379 expected_mac->x,
2380 expected_mac->len - 1 ),
2381 PSA_ERROR_INVALID_SIGNATURE );
2382
2383 /* Test a MAC that's too long. */
2384 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2385 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002386 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002387 PSA_ASSERT( psa_mac_update( &operation,
2388 input->x, input->len ) );
2389 TEST_EQUAL( psa_mac_verify_finish( &operation,
2390 perturbed_mac,
2391 expected_mac->len + 1 ),
2392 PSA_ERROR_INVALID_SIGNATURE );
2393
2394 /* Test changing one byte. */
2395 for( size_t i = 0; i < expected_mac->len; i++ )
2396 {
Chris Jones9634bb12021-01-20 15:56:42 +00002397 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002398 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002399 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002400 PSA_ASSERT( psa_mac_update( &operation,
2401 input->x, input->len ) );
2402 TEST_EQUAL( psa_mac_verify_finish( &operation,
2403 perturbed_mac,
2404 expected_mac->len ),
2405 PSA_ERROR_INVALID_SIGNATURE );
2406 perturbed_mac[i] ^= 1;
2407 }
2408
Gilles Peskine8c9def32018-02-08 10:02:12 +01002409exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002410 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002411 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002412 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002413 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002414}
2415/* END_CASE */
2416
2417/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002418void cipher_operation_init( )
2419{
Jaeden Ameroab439972019-02-15 14:12:05 +00002420 const uint8_t input[1] = { 0 };
2421 unsigned char output[1] = { 0 };
2422 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002423 /* Test each valid way of initializing the object, except for `= {0}`, as
2424 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2425 * though it's OK by the C standard. We could test for this, but we'd need
2426 * to supress the Clang warning for the test. */
2427 psa_cipher_operation_t func = psa_cipher_operation_init( );
2428 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2429 psa_cipher_operation_t zero;
2430
2431 memset( &zero, 0, sizeof( zero ) );
2432
Jaeden Ameroab439972019-02-15 14:12:05 +00002433 /* A freshly-initialized cipher operation should not be usable. */
2434 TEST_EQUAL( psa_cipher_update( &func,
2435 input, sizeof( input ),
2436 output, sizeof( output ),
2437 &output_length ),
2438 PSA_ERROR_BAD_STATE );
2439 TEST_EQUAL( psa_cipher_update( &init,
2440 input, sizeof( input ),
2441 output, sizeof( output ),
2442 &output_length ),
2443 PSA_ERROR_BAD_STATE );
2444 TEST_EQUAL( psa_cipher_update( &zero,
2445 input, sizeof( input ),
2446 output, sizeof( output ),
2447 &output_length ),
2448 PSA_ERROR_BAD_STATE );
2449
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002450 /* A default cipher operation should be abortable without error. */
2451 PSA_ASSERT( psa_cipher_abort( &func ) );
2452 PSA_ASSERT( psa_cipher_abort( &init ) );
2453 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002454}
2455/* END_CASE */
2456
2457/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002458void cipher_setup( int key_type_arg,
2459 data_t *key,
2460 int alg_arg,
2461 int expected_status_arg )
2462{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002463 psa_key_type_t key_type = key_type_arg;
2464 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002465 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002466 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002467 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002468#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002469 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2470#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002471
Gilles Peskine8817f612018-12-18 00:18:46 +01002472 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002473
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002474 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2475 &operation, &status ) )
2476 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002477 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002478
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002479 /* The operation object should be reusable. */
2480#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2481 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2482 smoke_test_key_data,
2483 sizeof( smoke_test_key_data ),
2484 KNOWN_SUPPORTED_CIPHER_ALG,
2485 &operation, &status ) )
2486 goto exit;
2487 TEST_EQUAL( status, PSA_SUCCESS );
2488#endif
2489
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002490exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002491 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002492 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002493}
2494/* END_CASE */
2495
Ronald Cronee414c72021-03-18 18:50:08 +01002496/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002497void cipher_bad_order( )
2498{
Ronald Cron5425a212020-08-04 14:58:35 +02002499 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002500 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2501 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002502 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002503 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002504 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002505 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002506 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2507 0xaa, 0xaa, 0xaa, 0xaa };
2508 const uint8_t text[] = {
2509 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2510 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002511 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002512 size_t length = 0;
2513
2514 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002515 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2516 psa_set_key_algorithm( &attributes, alg );
2517 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002518 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2519 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002520
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002521 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002522 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2523 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002524 PSA_ERROR_BAD_STATE );
2525 PSA_ASSERT( psa_cipher_abort( &operation ) );
2526
2527 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002528 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2529 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002530 PSA_ERROR_BAD_STATE );
2531 PSA_ASSERT( psa_cipher_abort( &operation ) );
2532
Jaeden Ameroab439972019-02-15 14:12:05 +00002533 /* Generate an IV without calling setup beforehand. */
2534 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2535 buffer, sizeof( buffer ),
2536 &length ),
2537 PSA_ERROR_BAD_STATE );
2538 PSA_ASSERT( psa_cipher_abort( &operation ) );
2539
2540 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002541 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002542 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2543 buffer, sizeof( buffer ),
2544 &length ) );
2545 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2546 buffer, sizeof( buffer ),
2547 &length ),
2548 PSA_ERROR_BAD_STATE );
2549 PSA_ASSERT( psa_cipher_abort( &operation ) );
2550
2551 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002552 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002553 PSA_ASSERT( psa_cipher_set_iv( &operation,
2554 iv, sizeof( iv ) ) );
2555 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2556 buffer, sizeof( buffer ),
2557 &length ),
2558 PSA_ERROR_BAD_STATE );
2559 PSA_ASSERT( psa_cipher_abort( &operation ) );
2560
2561 /* Set an IV without calling setup beforehand. */
2562 TEST_EQUAL( psa_cipher_set_iv( &operation,
2563 iv, sizeof( iv ) ),
2564 PSA_ERROR_BAD_STATE );
2565 PSA_ASSERT( psa_cipher_abort( &operation ) );
2566
2567 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002568 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002569 PSA_ASSERT( psa_cipher_set_iv( &operation,
2570 iv, sizeof( iv ) ) );
2571 TEST_EQUAL( psa_cipher_set_iv( &operation,
2572 iv, sizeof( iv ) ),
2573 PSA_ERROR_BAD_STATE );
2574 PSA_ASSERT( psa_cipher_abort( &operation ) );
2575
2576 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002577 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002578 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2579 buffer, sizeof( buffer ),
2580 &length ) );
2581 TEST_EQUAL( psa_cipher_set_iv( &operation,
2582 iv, sizeof( iv ) ),
2583 PSA_ERROR_BAD_STATE );
2584 PSA_ASSERT( psa_cipher_abort( &operation ) );
2585
2586 /* Call update without calling setup beforehand. */
2587 TEST_EQUAL( psa_cipher_update( &operation,
2588 text, sizeof( text ),
2589 buffer, sizeof( buffer ),
2590 &length ),
2591 PSA_ERROR_BAD_STATE );
2592 PSA_ASSERT( psa_cipher_abort( &operation ) );
2593
2594 /* Call update without an IV where an IV is required. */
2595 TEST_EQUAL( psa_cipher_update( &operation,
2596 text, sizeof( text ),
2597 buffer, sizeof( buffer ),
2598 &length ),
2599 PSA_ERROR_BAD_STATE );
2600 PSA_ASSERT( psa_cipher_abort( &operation ) );
2601
2602 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002603 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002604 PSA_ASSERT( psa_cipher_set_iv( &operation,
2605 iv, sizeof( iv ) ) );
2606 PSA_ASSERT( psa_cipher_finish( &operation,
2607 buffer, sizeof( buffer ), &length ) );
2608 TEST_EQUAL( psa_cipher_update( &operation,
2609 text, sizeof( text ),
2610 buffer, sizeof( buffer ),
2611 &length ),
2612 PSA_ERROR_BAD_STATE );
2613 PSA_ASSERT( psa_cipher_abort( &operation ) );
2614
2615 /* Call finish without calling setup beforehand. */
2616 TEST_EQUAL( psa_cipher_finish( &operation,
2617 buffer, sizeof( buffer ), &length ),
2618 PSA_ERROR_BAD_STATE );
2619 PSA_ASSERT( psa_cipher_abort( &operation ) );
2620
2621 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002622 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002623 /* Not calling update means we are encrypting an empty buffer, which is OK
2624 * for cipher modes with padding. */
2625 TEST_EQUAL( psa_cipher_finish( &operation,
2626 buffer, sizeof( buffer ), &length ),
2627 PSA_ERROR_BAD_STATE );
2628 PSA_ASSERT( psa_cipher_abort( &operation ) );
2629
2630 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002631 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002632 PSA_ASSERT( psa_cipher_set_iv( &operation,
2633 iv, sizeof( iv ) ) );
2634 PSA_ASSERT( psa_cipher_finish( &operation,
2635 buffer, sizeof( buffer ), &length ) );
2636 TEST_EQUAL( psa_cipher_finish( &operation,
2637 buffer, sizeof( buffer ), &length ),
2638 PSA_ERROR_BAD_STATE );
2639 PSA_ASSERT( psa_cipher_abort( &operation ) );
2640
Ronald Cron5425a212020-08-04 14:58:35 +02002641 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002642
Jaeden Ameroab439972019-02-15 14:12:05 +00002643exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002644 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002645 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002646}
2647/* END_CASE */
2648
2649/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002650void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002651 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002652 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002653 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002654{
Ronald Cron5425a212020-08-04 14:58:35 +02002655 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002656 psa_status_t status;
2657 psa_key_type_t key_type = key_type_arg;
2658 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002659 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002660 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002661 size_t output_buffer_size = 0;
2662 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002663 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002664 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002665 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002666
Gilles Peskine8817f612018-12-18 00:18:46 +01002667 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002668
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002669 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2670 psa_set_key_algorithm( &attributes, alg );
2671 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002672
Ronald Cron5425a212020-08-04 14:58:35 +02002673 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2674 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002675
Ronald Cron5425a212020-08-04 14:58:35 +02002676 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002677
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002678 if( iv->len > 0 )
2679 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002680 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002681 }
2682
gabor-mezei-armceface22021-01-21 12:26:17 +01002683 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2684 TEST_ASSERT( output_buffer_size <=
2685 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002686 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002687
Gilles Peskine8817f612018-12-18 00:18:46 +01002688 PSA_ASSERT( psa_cipher_update( &operation,
2689 input->x, input->len,
2690 output, output_buffer_size,
2691 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002692 TEST_ASSERT( function_output_length <=
2693 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2694 TEST_ASSERT( function_output_length <=
2695 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002696 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002697
Gilles Peskine50e586b2018-06-08 14:28:46 +02002698 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002699 ( output_buffer_size == 0 ? NULL :
2700 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002701 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002702 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002703 TEST_ASSERT( function_output_length <=
2704 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2705 TEST_ASSERT( function_output_length <=
2706 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002707 total_output_length += function_output_length;
2708
Gilles Peskinefe11b722018-12-18 00:24:04 +01002709 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002710 if( expected_status == PSA_SUCCESS )
2711 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002712 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002713 ASSERT_COMPARE( expected_output->x, expected_output->len,
2714 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002715 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002716
Gilles Peskine50e586b2018-06-08 14:28:46 +02002717exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002718 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002719 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002720 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002721 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002722}
2723/* END_CASE */
2724
2725/* BEGIN_CASE */
2726void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002727 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002728 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002729 int first_part_size_arg,
2730 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002731 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002732{
Ronald Cron5425a212020-08-04 14:58:35 +02002733 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002734 psa_key_type_t key_type = key_type_arg;
2735 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002736 size_t first_part_size = first_part_size_arg;
2737 size_t output1_length = output1_length_arg;
2738 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002739 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002740 size_t output_buffer_size = 0;
2741 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002742 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002743 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002744 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002745
Gilles Peskine8817f612018-12-18 00:18:46 +01002746 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002747
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002748 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2749 psa_set_key_algorithm( &attributes, alg );
2750 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002751
Ronald Cron5425a212020-08-04 14:58:35 +02002752 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2753 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002754
Ronald Cron5425a212020-08-04 14:58:35 +02002755 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002756
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002757 if( iv->len > 0 )
2758 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002759 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002760 }
2761
gabor-mezei-armceface22021-01-21 12:26:17 +01002762 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2763 TEST_ASSERT( output_buffer_size <=
2764 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002765 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002766
Gilles Peskinee0866522019-02-19 19:44:00 +01002767 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002768 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2769 output, output_buffer_size,
2770 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002771 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002772 TEST_ASSERT( function_output_length <=
2773 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2774 TEST_ASSERT( function_output_length <=
2775 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002776 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002777
Gilles Peskine8817f612018-12-18 00:18:46 +01002778 PSA_ASSERT( psa_cipher_update( &operation,
2779 input->x + first_part_size,
2780 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002781 ( output_buffer_size == 0 ? NULL :
2782 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002783 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002784 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002785 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002786 TEST_ASSERT( function_output_length <=
2787 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2788 alg,
2789 input->len - first_part_size ) );
2790 TEST_ASSERT( function_output_length <=
2791 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002792 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002793
Gilles Peskine8817f612018-12-18 00:18:46 +01002794 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002795 ( output_buffer_size == 0 ? NULL :
2796 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002797 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002798 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002799 TEST_ASSERT( function_output_length <=
2800 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2801 TEST_ASSERT( function_output_length <=
2802 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002803 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002804 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002805
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002806 ASSERT_COMPARE( expected_output->x, expected_output->len,
2807 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002808
2809exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002810 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002811 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002812 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002813 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002814}
2815/* END_CASE */
2816
2817/* BEGIN_CASE */
2818void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002819 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002820 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002821 int first_part_size_arg,
2822 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002823 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002824{
Ronald Cron5425a212020-08-04 14:58:35 +02002825 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002826 psa_key_type_t key_type = key_type_arg;
2827 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002828 size_t first_part_size = first_part_size_arg;
2829 size_t output1_length = output1_length_arg;
2830 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002831 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002832 size_t output_buffer_size = 0;
2833 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002834 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002835 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002836 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002837
Gilles Peskine8817f612018-12-18 00:18:46 +01002838 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002839
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002840 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2841 psa_set_key_algorithm( &attributes, alg );
2842 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002843
Ronald Cron5425a212020-08-04 14:58:35 +02002844 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2845 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002846
Ronald Cron5425a212020-08-04 14:58:35 +02002847 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002848
Steven Cooreman177deba2020-09-07 17:14:14 +02002849 if( iv->len > 0 )
2850 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002851 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002852 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002853
gabor-mezei-armceface22021-01-21 12:26:17 +01002854 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2855 TEST_ASSERT( output_buffer_size <=
2856 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002857 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002858
Gilles Peskinee0866522019-02-19 19:44:00 +01002859 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002860 PSA_ASSERT( psa_cipher_update( &operation,
2861 input->x, first_part_size,
2862 output, output_buffer_size,
2863 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002864 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002865 TEST_ASSERT( function_output_length <=
2866 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2867 TEST_ASSERT( function_output_length <=
2868 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002869 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002870
Gilles Peskine8817f612018-12-18 00:18:46 +01002871 PSA_ASSERT( psa_cipher_update( &operation,
2872 input->x + first_part_size,
2873 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002874 ( output_buffer_size == 0 ? NULL :
2875 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002876 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002877 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002878 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002879 TEST_ASSERT( function_output_length <=
2880 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2881 alg,
2882 input->len - first_part_size ) );
2883 TEST_ASSERT( function_output_length <=
2884 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002885 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002886
Gilles Peskine8817f612018-12-18 00:18:46 +01002887 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002888 ( output_buffer_size == 0 ? NULL :
2889 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002890 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002891 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002892 TEST_ASSERT( function_output_length <=
2893 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2894 TEST_ASSERT( function_output_length <=
2895 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002896 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002897 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002898
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002899 ASSERT_COMPARE( expected_output->x, expected_output->len,
2900 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002901
2902exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002903 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002904 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002905 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002906 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002907}
2908/* END_CASE */
2909
Gilles Peskine50e586b2018-06-08 14:28:46 +02002910/* BEGIN_CASE */
2911void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002912 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002913 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002914 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002915{
Ronald Cron5425a212020-08-04 14:58:35 +02002916 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002917 psa_status_t status;
2918 psa_key_type_t key_type = key_type_arg;
2919 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002920 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002921 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002922 size_t output_buffer_size = 0;
2923 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002924 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002925 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002926 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002927
Gilles Peskine8817f612018-12-18 00:18:46 +01002928 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002929
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002930 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2931 psa_set_key_algorithm( &attributes, alg );
2932 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002933
Ronald Cron5425a212020-08-04 14:58:35 +02002934 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2935 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002936
Ronald Cron5425a212020-08-04 14:58:35 +02002937 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002938
Steven Cooreman177deba2020-09-07 17:14:14 +02002939 if( iv->len > 0 )
2940 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002941 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002942 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002943
gabor-mezei-armceface22021-01-21 12:26:17 +01002944 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2945 TEST_ASSERT( output_buffer_size <=
2946 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002947 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002948
Gilles Peskine8817f612018-12-18 00:18:46 +01002949 PSA_ASSERT( psa_cipher_update( &operation,
2950 input->x, input->len,
2951 output, output_buffer_size,
2952 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002953 TEST_ASSERT( function_output_length <=
2954 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2955 TEST_ASSERT( function_output_length <=
2956 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002957 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002958
Gilles Peskine50e586b2018-06-08 14:28:46 +02002959 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002960 ( output_buffer_size == 0 ? NULL :
2961 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002962 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002963 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002964 TEST_ASSERT( function_output_length <=
2965 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2966 TEST_ASSERT( function_output_length <=
2967 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002968 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002969 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002970
2971 if( expected_status == PSA_SUCCESS )
2972 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002973 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002974 ASSERT_COMPARE( expected_output->x, expected_output->len,
2975 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002976 }
2977
Gilles Peskine50e586b2018-06-08 14:28:46 +02002978exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002979 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002980 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002981 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002982 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002983}
2984/* END_CASE */
2985
Gilles Peskine50e586b2018-06-08 14:28:46 +02002986/* BEGIN_CASE */
2987void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002988 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002989 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002990{
Ronald Cron5425a212020-08-04 14:58:35 +02002991 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002992 psa_key_type_t key_type = key_type_arg;
2993 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002994 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002995 size_t iv_size = 16;
2996 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002997 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002998 size_t output1_size = 0;
2999 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003000 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003001 size_t output2_size = 0;
3002 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003003 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003004 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3005 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003006 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003007
Gilles Peskine8817f612018-12-18 00:18:46 +01003008 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003009
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003010 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3011 psa_set_key_algorithm( &attributes, alg );
3012 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003013
Ronald Cron5425a212020-08-04 14:58:35 +02003014 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3015 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003016
Ronald Cron5425a212020-08-04 14:58:35 +02003017 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3018 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003019
Steven Cooreman177deba2020-09-07 17:14:14 +02003020 if( alg != PSA_ALG_ECB_NO_PADDING )
3021 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003022 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3023 iv, iv_size,
3024 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003025 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003026 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3027 TEST_ASSERT( output1_size <=
3028 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003029 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003030
Gilles Peskine8817f612018-12-18 00:18:46 +01003031 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3032 output1, output1_size,
3033 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003034 TEST_ASSERT( output1_length <=
3035 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3036 TEST_ASSERT( output1_length <=
3037 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3038
Gilles Peskine8817f612018-12-18 00:18:46 +01003039 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003040 output1 + output1_length,
3041 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003042 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003043 TEST_ASSERT( function_output_length <=
3044 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3045 TEST_ASSERT( function_output_length <=
3046 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003047
Gilles Peskine048b7f02018-06-08 14:20:49 +02003048 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003049
Gilles Peskine8817f612018-12-18 00:18:46 +01003050 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003051
3052 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003053 TEST_ASSERT( output2_size <=
3054 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3055 TEST_ASSERT( output2_size <=
3056 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003057 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003058
Steven Cooreman177deba2020-09-07 17:14:14 +02003059 if( iv_length > 0 )
3060 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003061 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3062 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003063 }
3064
Gilles Peskine8817f612018-12-18 00:18:46 +01003065 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3066 output2, output2_size,
3067 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003068 TEST_ASSERT( output2_length <=
3069 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
3070 TEST_ASSERT( output2_length <=
3071 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
3072
Gilles Peskine048b7f02018-06-08 14:20:49 +02003073 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003074 PSA_ASSERT( psa_cipher_finish( &operation2,
3075 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003076 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003077 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003078 TEST_ASSERT( function_output_length <=
3079 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3080 TEST_ASSERT( function_output_length <=
3081 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03003082
Gilles Peskine048b7f02018-06-08 14:20:49 +02003083 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003084
Gilles Peskine8817f612018-12-18 00:18:46 +01003085 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003086
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003087 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003088
3089exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003090 psa_cipher_abort( &operation1 );
3091 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003092 mbedtls_free( output1 );
3093 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003094 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003095 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003096}
3097/* END_CASE */
3098
3099/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003100void cipher_verify_output_multipart( int alg_arg,
3101 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003102 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003103 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003104 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003105{
Ronald Cron5425a212020-08-04 14:58:35 +02003106 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003107 psa_key_type_t key_type = key_type_arg;
3108 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003109 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003110 unsigned char iv[16] = {0};
3111 size_t iv_size = 16;
3112 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003113 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003114 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003115 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003116 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003117 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003118 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003119 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003120 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3121 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003122 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003123
Gilles Peskine8817f612018-12-18 00:18:46 +01003124 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003125
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003126 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3127 psa_set_key_algorithm( &attributes, alg );
3128 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003129
Ronald Cron5425a212020-08-04 14:58:35 +02003130 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3131 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003132
Ronald Cron5425a212020-08-04 14:58:35 +02003133 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3134 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003135
Steven Cooreman177deba2020-09-07 17:14:14 +02003136 if( alg != PSA_ALG_ECB_NO_PADDING )
3137 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003138 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3139 iv, iv_size,
3140 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003141 }
3142
gabor-mezei-armceface22021-01-21 12:26:17 +01003143 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3144 TEST_ASSERT( output1_buffer_size <=
3145 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003146 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003147
Gilles Peskinee0866522019-02-19 19:44:00 +01003148 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003149
Gilles Peskine8817f612018-12-18 00:18:46 +01003150 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3151 output1, output1_buffer_size,
3152 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003153 TEST_ASSERT( function_output_length <=
3154 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3155 TEST_ASSERT( function_output_length <=
3156 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003157 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003158
Gilles Peskine8817f612018-12-18 00:18:46 +01003159 PSA_ASSERT( psa_cipher_update( &operation1,
3160 input->x + first_part_size,
3161 input->len - first_part_size,
3162 output1, output1_buffer_size,
3163 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003164 TEST_ASSERT( function_output_length <=
3165 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3166 alg,
3167 input->len - first_part_size ) );
3168 TEST_ASSERT( function_output_length <=
3169 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003170 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003171
Gilles Peskine8817f612018-12-18 00:18:46 +01003172 PSA_ASSERT( psa_cipher_finish( &operation1,
3173 output1 + output1_length,
3174 output1_buffer_size - output1_length,
3175 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003176 TEST_ASSERT( function_output_length <=
3177 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3178 TEST_ASSERT( function_output_length <=
3179 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003180 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003181
Gilles Peskine8817f612018-12-18 00:18:46 +01003182 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003183
Gilles Peskine048b7f02018-06-08 14:20:49 +02003184 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003185 TEST_ASSERT( output2_buffer_size <=
3186 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3187 TEST_ASSERT( output2_buffer_size <=
3188 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003189 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003190
Steven Cooreman177deba2020-09-07 17:14:14 +02003191 if( iv_length > 0 )
3192 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003193 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3194 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003195 }
Moran Pekerded84402018-06-06 16:36:50 +03003196
Gilles Peskine8817f612018-12-18 00:18:46 +01003197 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3198 output2, output2_buffer_size,
3199 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003200 TEST_ASSERT( function_output_length <=
3201 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3202 TEST_ASSERT( function_output_length <=
3203 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003204 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003205
Gilles Peskine8817f612018-12-18 00:18:46 +01003206 PSA_ASSERT( psa_cipher_update( &operation2,
3207 output1 + first_part_size,
3208 output1_length - first_part_size,
3209 output2, output2_buffer_size,
3210 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003211 TEST_ASSERT( function_output_length <=
3212 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3213 alg,
3214 output1_length - first_part_size ) );
3215 TEST_ASSERT( function_output_length <=
3216 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003217 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003218
Gilles Peskine8817f612018-12-18 00:18:46 +01003219 PSA_ASSERT( psa_cipher_finish( &operation2,
3220 output2 + output2_length,
3221 output2_buffer_size - output2_length,
3222 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003223 TEST_ASSERT( function_output_length <=
3224 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3225 TEST_ASSERT( function_output_length <=
3226 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003227 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003228
Gilles Peskine8817f612018-12-18 00:18:46 +01003229 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003230
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003231 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003232
3233exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003234 psa_cipher_abort( &operation1 );
3235 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003236 mbedtls_free( output1 );
3237 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003238 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003239 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003240}
3241/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003242
Gilles Peskine20035e32018-02-03 22:44:14 +01003243/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003244void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003245 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003246 data_t *nonce,
3247 data_t *additional_data,
3248 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003249 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003250{
Ronald Cron5425a212020-08-04 14:58:35 +02003251 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003252 psa_key_type_t key_type = key_type_arg;
3253 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003254 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003255 unsigned char *output_data = NULL;
3256 size_t output_size = 0;
3257 size_t output_length = 0;
3258 unsigned char *output_data2 = NULL;
3259 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003260 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003261 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003262 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003263
Gilles Peskine8817f612018-12-18 00:18:46 +01003264 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003265
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003266 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3267 psa_set_key_algorithm( &attributes, alg );
3268 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003269
Gilles Peskine049c7532019-05-15 20:22:09 +02003270 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003271 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003272 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3273 key_bits = psa_get_key_bits( &attributes );
3274
3275 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3276 alg );
3277 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3278 * should be exact. */
3279 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3280 expected_result != PSA_ERROR_NOT_SUPPORTED )
3281 {
3282 TEST_EQUAL( output_size,
3283 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3284 TEST_ASSERT( output_size <=
3285 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3286 }
3287 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003288
Steven Cooremanf49478b2021-02-15 15:19:25 +01003289 status = psa_aead_encrypt( key, alg,
3290 nonce->x, nonce->len,
3291 additional_data->x,
3292 additional_data->len,
3293 input_data->x, input_data->len,
3294 output_data, output_size,
3295 &output_length );
3296
3297 /* If the operation is not supported, just skip and not fail in case the
3298 * encryption involves a common limitation of cryptography hardwares and
3299 * an alternative implementation. */
3300 if( status == PSA_ERROR_NOT_SUPPORTED )
3301 {
3302 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3303 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3304 }
3305
3306 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003307
3308 if( PSA_SUCCESS == expected_result )
3309 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003310 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003311
Gilles Peskine003a4a92019-05-14 16:09:40 +02003312 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3313 * should be exact. */
3314 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003315 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003316
gabor-mezei-armceface22021-01-21 12:26:17 +01003317 TEST_ASSERT( input_data->len <=
3318 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3319
Ronald Cron5425a212020-08-04 14:58:35 +02003320 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003321 nonce->x, nonce->len,
3322 additional_data->x,
3323 additional_data->len,
3324 output_data, output_length,
3325 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003326 &output_length2 ),
3327 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003328
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003329 ASSERT_COMPARE( input_data->x, input_data->len,
3330 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003331 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003332
Gilles Peskinea1cac842018-06-11 19:33:02 +02003333exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003334 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003335 mbedtls_free( output_data );
3336 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003337 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003338}
3339/* END_CASE */
3340
3341/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003342void aead_encrypt( int key_type_arg, data_t *key_data,
3343 int alg_arg,
3344 data_t *nonce,
3345 data_t *additional_data,
3346 data_t *input_data,
3347 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003348{
Ronald Cron5425a212020-08-04 14:58:35 +02003349 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003350 psa_key_type_t key_type = key_type_arg;
3351 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003352 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003353 unsigned char *output_data = NULL;
3354 size_t output_size = 0;
3355 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003356 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003357 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003358
Gilles Peskine8817f612018-12-18 00:18:46 +01003359 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003360
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003361 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3362 psa_set_key_algorithm( &attributes, alg );
3363 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003364
Gilles Peskine049c7532019-05-15 20:22:09 +02003365 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003366 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003367 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3368 key_bits = psa_get_key_bits( &attributes );
3369
3370 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3371 alg );
3372 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3373 * should be exact. */
3374 TEST_EQUAL( output_size,
3375 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3376 TEST_ASSERT( output_size <=
3377 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3378 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003379
Steven Cooremand588ea12021-01-11 19:36:04 +01003380 status = psa_aead_encrypt( key, alg,
3381 nonce->x, nonce->len,
3382 additional_data->x, additional_data->len,
3383 input_data->x, input_data->len,
3384 output_data, output_size,
3385 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003386
Ronald Cron28a45ed2021-02-09 20:35:42 +01003387 /* If the operation is not supported, just skip and not fail in case the
3388 * encryption involves a common limitation of cryptography hardwares and
3389 * an alternative implementation. */
3390 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003391 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003392 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3393 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003394 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003395
3396 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003397 ASSERT_COMPARE( expected_result->x, expected_result->len,
3398 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003399
Gilles Peskinea1cac842018-06-11 19:33:02 +02003400exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003401 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003402 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003403 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003404}
3405/* END_CASE */
3406
3407/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003408void aead_decrypt( int key_type_arg, data_t *key_data,
3409 int alg_arg,
3410 data_t *nonce,
3411 data_t *additional_data,
3412 data_t *input_data,
3413 data_t *expected_data,
3414 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003415{
Ronald Cron5425a212020-08-04 14:58:35 +02003416 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003417 psa_key_type_t key_type = key_type_arg;
3418 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003419 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003420 unsigned char *output_data = NULL;
3421 size_t output_size = 0;
3422 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003423 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003424 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003425 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003426
Gilles Peskine8817f612018-12-18 00:18:46 +01003427 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003428
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003429 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3430 psa_set_key_algorithm( &attributes, alg );
3431 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003432
Gilles Peskine049c7532019-05-15 20:22:09 +02003433 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003434 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003435 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3436 key_bits = psa_get_key_bits( &attributes );
3437
3438 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3439 alg );
3440 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3441 expected_result != PSA_ERROR_NOT_SUPPORTED )
3442 {
3443 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3444 * should be exact. */
3445 TEST_EQUAL( output_size,
3446 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3447 TEST_ASSERT( output_size <=
3448 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3449 }
3450 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003451
Steven Cooremand588ea12021-01-11 19:36:04 +01003452 status = psa_aead_decrypt( key, alg,
3453 nonce->x, nonce->len,
3454 additional_data->x,
3455 additional_data->len,
3456 input_data->x, input_data->len,
3457 output_data, output_size,
3458 &output_length );
3459
Ronald Cron28a45ed2021-02-09 20:35:42 +01003460 /* If the operation is not supported, just skip and not fail in case the
3461 * decryption involves a common limitation of cryptography hardwares and
3462 * an alternative implementation. */
3463 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003464 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003465 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3466 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003467 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003468
3469 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003470
Gilles Peskine2d277862018-06-18 15:41:12 +02003471 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003472 ASSERT_COMPARE( expected_data->x, expected_data->len,
3473 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003474
Gilles Peskinea1cac842018-06-11 19:33:02 +02003475exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003476 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003477 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003478 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003479}
3480/* END_CASE */
3481
3482/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003483void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3484 int alg_arg,
3485 data_t *nonce,
3486 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003487 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003488 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003489 int do_test_data_chunked,
3490 int do_set_lengths,
3491 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003492{
Paul Elliottd3f82412021-06-16 16:52:21 +01003493 size_t ad_part_len = 0;
3494 size_t data_part_len = 0;
Paul Elliott33746aa2021-09-15 16:40:40 +01003495 setlengths_method set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003496
Paul Elliotte64deda2021-09-09 14:07:23 +01003497 /* Ensure that either one part of the test or the other is done, i.e this
3498 * test does something. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003499 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3500
3501 /* Temporary whilst we have algorithms that cannot support chunking */
3502 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003503 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003504 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3505 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003506 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003507 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003508
Paul Elliott33746aa2021-09-15 16:40:40 +01003509 if( do_set_lengths )
3510 {
3511 if( ad_part_len & 0x01 )
3512 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3513 else
3514 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3515 }
3516
Paul Elliott329d5382021-07-22 17:10:45 +01003517 /* Split ad into length(ad_part_len) parts. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003518 if( !aead_multipart_internal_func( key_type_arg, key_data,
3519 alg_arg, nonce,
3520 additional_data,
3521 ad_part_len,
3522 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003523 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003524 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003525 1, 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003526 break;
3527
3528 /* length(0) part, length(ad_part_len) part, length(0) part... */
3529 mbedtls_test_set_step( 1000 + ad_part_len );
3530
3531 if( !aead_multipart_internal_func( key_type_arg, key_data,
3532 alg_arg, nonce,
3533 additional_data,
3534 ad_part_len,
3535 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003536 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003537 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003538 1, 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003539 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003540 }
3541 }
Paul Elliottd3f82412021-06-16 16:52:21 +01003542
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003543 /* Temporary whilst we have algorithms that cannot support chunking */
3544 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003545 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003546 for( data_part_len = 1; data_part_len <= input_data->len;
3547 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003548 {
Paul Elliott329d5382021-07-22 17:10:45 +01003549 /* Split data into length(data_part_len) parts. */
3550 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003551
Paul Elliott33746aa2021-09-15 16:40:40 +01003552 if( do_set_lengths )
3553 {
3554 if( data_part_len & 0x01 )
3555 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3556 else
3557 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3558 }
3559
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003560 if( !aead_multipart_internal_func( key_type_arg, key_data,
3561 alg_arg, nonce,
3562 additional_data, -1,
3563 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003564 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003565 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003566 1, 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003567 break;
3568
3569 /* length(0) part, length(data_part_len) part, length(0) part... */
3570 mbedtls_test_set_step( 3000 + data_part_len );
3571
3572 if( !aead_multipart_internal_func( key_type_arg, key_data,
3573 alg_arg, nonce,
3574 additional_data, -1,
3575 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003576 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003577 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003578 1, 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003579 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003580 }
3581 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003582
Paul Elliott0023e0a2021-04-27 10:06:22 +01003583
Paul Elliott8fc45162021-06-23 16:06:01 +01003584 /* Goto is required to silence warnings about unused labels, as we
3585 * don't actually do any test assertions in this function. */
3586 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003587}
3588/* END_CASE */
3589
3590/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003591void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3592 int alg_arg,
3593 data_t *nonce,
3594 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003595 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003596 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003597 int do_test_data_chunked,
3598 int do_set_lengths,
3599 data_t *expected_output,
3600 int expect_valid_signature )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003601{
Paul Elliottd3f82412021-06-16 16:52:21 +01003602 size_t ad_part_len = 0;
3603 size_t data_part_len = 0;
Paul Elliott33746aa2021-09-15 16:40:40 +01003604 setlengths_method set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003605
Paul Elliotte64deda2021-09-09 14:07:23 +01003606 /* Ensure that either one part of the test or the other is done, i.e this
3607 * test does something. */
3608 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3609
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003610 /* Temporary whilst we have algorithms that cannot support chunking */
3611 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003612 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003613 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3614 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003615 {
Paul Elliott329d5382021-07-22 17:10:45 +01003616 /* Split ad into length(ad_part_len) parts. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003617 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003618
Paul Elliott33746aa2021-09-15 16:40:40 +01003619 if( do_set_lengths )
3620 {
3621 if( ad_part_len & 0x01 )
3622 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3623 else
3624 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3625 }
3626
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003627 if( !aead_multipart_internal_func( key_type_arg, key_data,
3628 alg_arg, nonce,
3629 additional_data,
3630 ad_part_len,
3631 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003632 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003633 expected_output,
3634 expect_valid_signature,
Paul Elliott33746aa2021-09-15 16:40:40 +01003635 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003636 break;
3637
3638 /* length(0) part, length(ad_part_len) part, length(0) part... */
3639 mbedtls_test_set_step( 1000 + ad_part_len );
3640
3641 if( !aead_multipart_internal_func( key_type_arg, key_data,
3642 alg_arg, nonce,
3643 additional_data,
3644 ad_part_len,
3645 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003646 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003647 expected_output,
3648 expect_valid_signature,
Paul Elliott33746aa2021-09-15 16:40:40 +01003649 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003650 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003651 }
3652 }
3653
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003654 /* Temporary whilst we have algorithms that cannot support chunking */
3655 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003656 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003657 for( data_part_len = 1; data_part_len <= input_data->len;
3658 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003659 {
Paul Elliott329d5382021-07-22 17:10:45 +01003660 /* Split data into length(data_part_len) parts. */
3661 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003662
Paul Elliott33746aa2021-09-15 16:40:40 +01003663 if( do_set_lengths )
3664 {
3665 if( data_part_len & 0x01 )
3666 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3667 else
3668 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3669 }
3670
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003671 if( !aead_multipart_internal_func( key_type_arg, key_data,
3672 alg_arg, nonce,
3673 additional_data, -1,
3674 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003675 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003676 expected_output,
3677 expect_valid_signature,
Paul Elliott33746aa2021-09-15 16:40:40 +01003678 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003679 break;
3680
3681 /* length(0) part, length(data_part_len) part, length(0) part... */
3682 mbedtls_test_set_step( 3000 + data_part_len );
3683
3684 if( !aead_multipart_internal_func( key_type_arg, key_data,
3685 alg_arg, nonce,
3686 additional_data, -1,
3687 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003688 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003689 expected_output,
3690 expect_valid_signature,
Paul Elliott33746aa2021-09-15 16:40:40 +01003691 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003692 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003693 }
3694 }
3695
Paul Elliott8fc45162021-06-23 16:06:01 +01003696 /* Goto is required to silence warnings about unused labels, as we
3697 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003698 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003699}
3700/* END_CASE */
3701
3702/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003703void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
3704 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01003705 int nonce_length,
3706 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003707 data_t *additional_data,
3708 data_t *input_data,
3709 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003710{
3711
3712 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3713 psa_key_type_t key_type = key_type_arg;
3714 psa_algorithm_t alg = alg_arg;
3715 psa_aead_operation_t operation;
3716 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3717 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3718 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01003719 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01003720 size_t actual_nonce_length = 0;
3721 size_t expected_nonce_length = expected_nonce_length_arg;
3722 unsigned char *output = NULL;
3723 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003724 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01003725 size_t ciphertext_size = 0;
3726 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003727 size_t tag_length = 0;
3728 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003729
3730 PSA_ASSERT( psa_crypto_init( ) );
3731
3732 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
3733 psa_set_key_algorithm( & attributes, alg );
3734 psa_set_key_type( & attributes, key_type );
3735
3736 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3737 &key ) );
3738
3739 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3740
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003741 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3742
Paul Elliottf1277632021-08-24 18:11:37 +01003743 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003744
Paul Elliottf1277632021-08-24 18:11:37 +01003745 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003746
Paul Elliottf1277632021-08-24 18:11:37 +01003747 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003748
Paul Elliottf1277632021-08-24 18:11:37 +01003749 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003750
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003751 operation = psa_aead_operation_init( );
3752
3753 status = psa_aead_encrypt_setup( &operation, key, alg );
3754
3755 /* If the operation is not supported, just skip and not fail in case the
3756 * encryption involves a common limitation of cryptography hardwares and
3757 * an alternative implementation. */
3758 if( status == PSA_ERROR_NOT_SUPPORTED )
3759 {
3760 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01003761 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003762 }
3763
3764 PSA_ASSERT( status );
3765
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003766 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01003767 nonce_length,
3768 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003769
Paul Elliott693bf312021-07-23 17:40:41 +01003770 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003771
Paul Elliottf1277632021-08-24 18:11:37 +01003772 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01003773
Paul Elliottf1277632021-08-24 18:11:37 +01003774 TEST_ASSERT( actual_nonce_length < PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01003775
Paul Elliott693bf312021-07-23 17:40:41 +01003776 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003777 {
3778
3779 /* Ensure we can still complete operation. */
3780
3781 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3782 additional_data->len ) );
3783
3784 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01003785 output, output_size,
3786 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003787
Paul Elliottf1277632021-08-24 18:11:37 +01003788 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3789 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003790 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3791 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003792
3793exit:
3794 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01003795 mbedtls_free( output );
3796 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003797 psa_aead_abort( &operation );
3798 PSA_DONE( );
3799}
3800/* END_CASE */
3801
3802/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01003803void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
3804 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01003805 int nonce_length_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01003806 data_t *additional_data,
3807 data_t *input_data,
3808 int expected_status_arg )
3809{
3810
3811 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3812 psa_key_type_t key_type = key_type_arg;
3813 psa_algorithm_t alg = alg_arg;
3814 psa_aead_operation_t operation;
3815 uint8_t *nonce_buffer = NULL;
3816 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3817 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3818 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003819 unsigned char *output = NULL;
3820 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01003821 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01003822 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003823 size_t ciphertext_size = 0;
3824 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003825 size_t tag_length = 0;
3826 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01003827 size_t index = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003828
3829 PSA_ASSERT( psa_crypto_init( ) );
3830
3831 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3832 psa_set_key_algorithm( &attributes, alg );
3833 psa_set_key_type( &attributes, key_type );
3834
3835 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3836 &key ) );
3837
3838 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3839
3840 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3841
Paul Elliott6f0e7202021-08-25 12:57:18 +01003842 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003843
Paul Elliott6f0e7202021-08-25 12:57:18 +01003844 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01003845
Paul Elliott6f0e7202021-08-25 12:57:18 +01003846 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01003847
Paul Elliott6f0e7202021-08-25 12:57:18 +01003848 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003849
3850 operation = psa_aead_operation_init( );
3851
3852 status = psa_aead_encrypt_setup( &operation, key, alg );
3853
3854 /* If the operation is not supported, just skip and not fail in case the
3855 * encryption involves a common limitation of cryptography hardwares and
3856 * an alternative implementation. */
3857 if( status == PSA_ERROR_NOT_SUPPORTED )
3858 {
3859 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003860 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01003861 }
3862
3863 PSA_ASSERT( status );
3864
Paul Elliott4023ffd2021-09-10 16:21:22 +01003865 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
3866 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01003867 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01003868 /* Arbitrary size buffer, to test zero length valid buffer. */
3869 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003870 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01003871 }
3872 else
3873 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003874 /* If length is zero, then this will return NULL. */
3875 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003876 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01003877
Paul Elliott4023ffd2021-09-10 16:21:22 +01003878 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01003879 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003880 for( index = 0; index < nonce_length - 1; ++index )
3881 {
3882 nonce_buffer[index] = 'a' + index;
3883 }
Paul Elliott66696b52021-08-16 18:42:41 +01003884 }
Paul Elliott863864a2021-07-23 17:28:31 +01003885 }
3886
Paul Elliott6f0e7202021-08-25 12:57:18 +01003887 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01003888
Paul Elliott693bf312021-07-23 17:40:41 +01003889 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01003890
3891 if( expected_status == PSA_SUCCESS )
3892 {
3893 /* Ensure we can still complete operation. */
3894
3895 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3896 additional_data->len ) );
3897
3898 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01003899 output, output_size,
3900 &ciphertext_length ) );
Paul Elliott863864a2021-07-23 17:28:31 +01003901
Paul Elliott6f0e7202021-08-25 12:57:18 +01003902 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3903 &ciphertext_length, tag_buffer,
Paul Elliott863864a2021-07-23 17:28:31 +01003904 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3905 }
3906
3907exit:
3908 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01003909 mbedtls_free( output );
3910 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01003911 mbedtls_free( nonce_buffer );
3912 psa_aead_abort( &operation );
3913 PSA_DONE( );
3914}
3915/* END_CASE */
3916
3917/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01003918void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
3919 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003920 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01003921 data_t *nonce,
3922 data_t *additional_data,
3923 data_t *input_data,
3924 int expected_status_arg )
3925{
3926
3927 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3928 psa_key_type_t key_type = key_type_arg;
3929 psa_algorithm_t alg = alg_arg;
3930 psa_aead_operation_t operation;
3931 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3932 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3933 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01003934 unsigned char *output = NULL;
3935 unsigned char *ciphertext = NULL;
3936 size_t output_size = output_size_arg;
3937 size_t ciphertext_size = 0;
3938 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01003939 size_t tag_length = 0;
3940 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
3941
3942 PSA_ASSERT( psa_crypto_init( ) );
3943
3944 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3945 psa_set_key_algorithm( &attributes, alg );
3946 psa_set_key_type( &attributes, key_type );
3947
3948 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3949 &key ) );
3950
3951 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3952
Paul Elliottc6d11d02021-09-01 12:04:23 +01003953 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003954
Paul Elliottc6d11d02021-09-01 12:04:23 +01003955 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01003956
Paul Elliottc6d11d02021-09-01 12:04:23 +01003957 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003958
3959 operation = psa_aead_operation_init( );
3960
3961 status = psa_aead_encrypt_setup( &operation, key, alg );
3962
3963 /* If the operation is not supported, just skip and not fail in case the
3964 * encryption involves a common limitation of cryptography hardwares and
3965 * an alternative implementation. */
3966 if( status == PSA_ERROR_NOT_SUPPORTED )
3967 {
3968 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3969 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3970 }
3971
3972 PSA_ASSERT( status );
3973
3974 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3975
3976 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3977 additional_data->len ) );
3978
3979 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003980 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01003981
3982 TEST_EQUAL( status, expected_status );
3983
3984 if( expected_status == PSA_SUCCESS )
3985 {
3986 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01003987 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3988 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01003989 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3990 }
3991
3992exit:
3993 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01003994 mbedtls_free( output );
3995 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01003996 psa_aead_abort( &operation );
3997 PSA_DONE( );
3998}
3999/* END_CASE */
4000
Paul Elliott91b021e2021-07-23 18:52:31 +01004001/* BEGIN_CASE */
4002void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
4003 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004004 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01004005 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01004006 data_t *nonce,
4007 data_t *additional_data,
4008 data_t *input_data,
4009 int expected_status_arg )
4010{
4011
4012 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4013 psa_key_type_t key_type = key_type_arg;
4014 psa_algorithm_t alg = alg_arg;
4015 psa_aead_operation_t operation;
4016 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4017 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4018 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004019 unsigned char *ciphertext = NULL;
4020 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01004021 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004022 size_t ciphertext_size = 0;
4023 size_t ciphertext_length = 0;
4024 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004025 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004026 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004027
4028 PSA_ASSERT( psa_crypto_init( ) );
4029
4030 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4031 psa_set_key_algorithm( &attributes, alg );
4032 psa_set_key_type( &attributes, key_type );
4033
4034 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4035 &key ) );
4036
4037 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4038
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004039 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004040
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004041 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004042
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004043 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004044
Paul Elliott719c1322021-09-13 18:27:22 +01004045 ASSERT_ALLOC( tag_buffer, tag_size );
4046
Paul Elliott91b021e2021-07-23 18:52:31 +01004047 operation = psa_aead_operation_init( );
4048
4049 status = psa_aead_encrypt_setup( &operation, key, alg );
4050
4051 /* If the operation is not supported, just skip and not fail in case the
4052 * encryption involves a common limitation of cryptography hardwares and
4053 * an alternative implementation. */
4054 if( status == PSA_ERROR_NOT_SUPPORTED )
4055 {
4056 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4057 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4058 }
4059
4060 PSA_ASSERT( status );
4061
4062 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4063
4064 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4065 additional_data->len ) );
4066
4067 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004068 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004069
4070 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004071 status = psa_aead_finish( &operation, finish_ciphertext,
4072 finish_ciphertext_size,
4073 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004074 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004075
4076 TEST_EQUAL( status, expected_status );
4077
4078exit:
4079 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004080 mbedtls_free( ciphertext );
4081 mbedtls_free( finish_ciphertext );
Paul Elliott91b021e2021-07-23 18:52:31 +01004082 psa_aead_abort( &operation );
4083 PSA_DONE( );
4084}
4085/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004086
4087/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004088void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4089 int alg_arg,
4090 data_t *nonce,
4091 data_t *additional_data,
4092 data_t *input_data )
4093{
4094 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4095 psa_key_type_t key_type = key_type_arg;
4096 psa_algorithm_t alg = alg_arg;
4097 psa_aead_operation_t operation;
4098 unsigned char *output_data = NULL;
4099 unsigned char *final_data = NULL;
4100 size_t output_size = 0;
4101 size_t finish_output_size = 0;
4102 size_t output_length = 0;
4103 size_t key_bits = 0;
4104 size_t tag_length = 0;
4105 size_t tag_size = 0;
4106 size_t nonce_length = 0;
4107 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4108 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4109 size_t output_part_length = 0;
4110 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4111
4112 PSA_ASSERT( psa_crypto_init( ) );
4113
4114 psa_set_key_usage_flags( & attributes,
4115 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4116 psa_set_key_algorithm( & attributes, alg );
4117 psa_set_key_type( & attributes, key_type );
4118
4119 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4120 &key ) );
4121
4122 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4123 key_bits = psa_get_key_bits( &attributes );
4124
4125 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4126
4127 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4128
4129 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4130
4131 ASSERT_ALLOC( output_data, output_size );
4132
4133 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4134
4135 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4136
4137 ASSERT_ALLOC( final_data, finish_output_size );
4138
4139 /* Test all operations error without calling setup first. */
4140
4141 operation = psa_aead_operation_init( );
4142
4143 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4144 PSA_ERROR_BAD_STATE );
4145
4146 psa_aead_abort( &operation );
4147
4148 operation = psa_aead_operation_init( );
4149
4150 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4151 PSA_AEAD_NONCE_MAX_SIZE,
4152 &nonce_length ),
4153 PSA_ERROR_BAD_STATE );
4154
4155 psa_aead_abort( &operation );
4156
Paul Elliott481be342021-07-16 17:38:47 +01004157 /* ------------------------------------------------------- */
4158
Paul Elliottc23a9a02021-06-21 18:32:46 +01004159 operation = psa_aead_operation_init( );
4160
4161 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4162 input_data->len ),
4163 PSA_ERROR_BAD_STATE );
4164
4165 psa_aead_abort( &operation );
4166
Paul Elliott481be342021-07-16 17:38:47 +01004167 /* ------------------------------------------------------- */
4168
Paul Elliottc23a9a02021-06-21 18:32:46 +01004169 operation = psa_aead_operation_init( );
4170
4171 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4172 additional_data->len ),
4173 PSA_ERROR_BAD_STATE );
4174
4175 psa_aead_abort( &operation );
4176
Paul Elliott481be342021-07-16 17:38:47 +01004177 /* ------------------------------------------------------- */
4178
Paul Elliottc23a9a02021-06-21 18:32:46 +01004179 operation = psa_aead_operation_init( );
4180
4181 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4182 input_data->len, output_data,
4183 output_size, &output_length ),
4184 PSA_ERROR_BAD_STATE );
4185
4186 psa_aead_abort( &operation );
4187
Paul Elliott481be342021-07-16 17:38:47 +01004188 /* ------------------------------------------------------- */
4189
Paul Elliottc23a9a02021-06-21 18:32:46 +01004190 operation = psa_aead_operation_init( );
4191
4192 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4193 finish_output_size,
4194 &output_part_length,
4195 tag_buffer, tag_length,
4196 &tag_size ),
4197 PSA_ERROR_BAD_STATE );
4198
4199 psa_aead_abort( &operation );
4200
Paul Elliott481be342021-07-16 17:38:47 +01004201 /* ------------------------------------------------------- */
4202
Paul Elliottc23a9a02021-06-21 18:32:46 +01004203 operation = psa_aead_operation_init( );
4204
4205 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4206 finish_output_size,
4207 &output_part_length,
4208 tag_buffer,
4209 tag_length ),
4210 PSA_ERROR_BAD_STATE );
4211
4212 psa_aead_abort( &operation );
4213
4214 /* Test for double setups. */
4215
4216 operation = psa_aead_operation_init( );
4217
4218 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4219
4220 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4221 PSA_ERROR_BAD_STATE );
4222
4223 psa_aead_abort( &operation );
4224
Paul Elliott481be342021-07-16 17:38:47 +01004225 /* ------------------------------------------------------- */
4226
Paul Elliottc23a9a02021-06-21 18:32:46 +01004227 operation = psa_aead_operation_init( );
4228
4229 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4230
4231 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4232 PSA_ERROR_BAD_STATE );
4233
4234 psa_aead_abort( &operation );
4235
Paul Elliott374a2be2021-07-16 17:53:40 +01004236 /* ------------------------------------------------------- */
4237
4238 operation = psa_aead_operation_init( );
4239
4240 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4241
4242 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4243 PSA_ERROR_BAD_STATE );
4244
4245 psa_aead_abort( &operation );
4246
4247 /* ------------------------------------------------------- */
4248
4249 operation = psa_aead_operation_init( );
4250
4251 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4252
4253 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4254 PSA_ERROR_BAD_STATE );
4255
4256 psa_aead_abort( &operation );
4257
Paul Elliottc23a9a02021-06-21 18:32:46 +01004258 /* Test for not setting a nonce. */
4259
4260 operation = psa_aead_operation_init( );
4261
4262 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4263
4264 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4265 additional_data->len ),
4266 PSA_ERROR_BAD_STATE );
4267
4268 psa_aead_abort( &operation );
4269
Paul Elliott7f628422021-09-01 12:08:29 +01004270 /* ------------------------------------------------------- */
4271
4272 operation = psa_aead_operation_init( );
4273
4274 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4275
4276 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4277 input_data->len, output_data,
4278 output_size, &output_length ),
4279 PSA_ERROR_BAD_STATE );
4280
4281 psa_aead_abort( &operation );
4282
Paul Elliottc23a9a02021-06-21 18:32:46 +01004283 /* Test for double setting nonce. */
4284
4285 operation = psa_aead_operation_init( );
4286
4287 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4288
4289 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4290
4291 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4292 PSA_ERROR_BAD_STATE );
4293
4294 psa_aead_abort( &operation );
4295
Paul Elliott374a2be2021-07-16 17:53:40 +01004296 /* Test for double generating nonce. */
4297
4298 operation = psa_aead_operation_init( );
4299
4300 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4301
4302 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4303 PSA_AEAD_NONCE_MAX_SIZE,
4304 &nonce_length ) );
4305
4306 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4307 PSA_AEAD_NONCE_MAX_SIZE,
4308 &nonce_length ),
4309 PSA_ERROR_BAD_STATE );
4310
4311
4312 psa_aead_abort( &operation );
4313
4314 /* Test for generate nonce then set and vice versa */
4315
4316 operation = psa_aead_operation_init( );
4317
4318 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4319
4320 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4321 PSA_AEAD_NONCE_MAX_SIZE,
4322 &nonce_length ) );
4323
4324 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4325 PSA_ERROR_BAD_STATE );
4326
4327 psa_aead_abort( &operation );
4328
4329 /* ------------------------------------------------------- */
4330
4331 operation = psa_aead_operation_init( );
4332
4333 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4334
4335 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4336
4337 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4338 PSA_AEAD_NONCE_MAX_SIZE,
4339 &nonce_length ),
4340 PSA_ERROR_BAD_STATE );
4341
4342 psa_aead_abort( &operation );
4343
Paul Elliott7220cae2021-06-22 17:25:57 +01004344 /* Test for generating nonce in decrypt setup. */
4345
4346 operation = psa_aead_operation_init( );
4347
4348 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4349
4350 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4351 PSA_AEAD_NONCE_MAX_SIZE,
4352 &nonce_length ),
4353 PSA_ERROR_BAD_STATE );
4354
4355 psa_aead_abort( &operation );
4356
Paul Elliottc23a9a02021-06-21 18:32:46 +01004357 /* Test for setting lengths twice. */
4358
4359 operation = psa_aead_operation_init( );
4360
4361 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4362
4363 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4364
4365 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4366 input_data->len ) );
4367
4368 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4369 input_data->len ),
4370 PSA_ERROR_BAD_STATE );
4371
4372 psa_aead_abort( &operation );
4373
4374 /* Test for setting lengths after already starting data. */
4375
4376 operation = psa_aead_operation_init( );
4377
4378 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4379
4380 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4381
4382 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4383 input_data->len, output_data,
4384 output_size, &output_length ) );
4385
4386 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4387 input_data->len ),
4388 PSA_ERROR_BAD_STATE );
4389
4390 psa_aead_abort( &operation );
4391
Paul Elliott243080c2021-07-21 19:01:17 +01004392 /* Test for not sending any additional data or data after setting non zero
4393 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004394
4395 operation = psa_aead_operation_init( );
4396
4397 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4398
4399 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4400
4401 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4402 input_data->len ) );
4403
4404 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4405 finish_output_size,
4406 &output_part_length,
4407 tag_buffer, tag_length,
4408 &tag_size ),
4409 PSA_ERROR_INVALID_ARGUMENT );
4410
4411 psa_aead_abort( &operation );
4412
Paul Elliott243080c2021-07-21 19:01:17 +01004413 /* Test for not sending any additional data or data after setting non-zero
4414 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004415
4416 operation = psa_aead_operation_init( );
4417
4418 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4419
4420 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4421
4422 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4423 input_data->len ) );
4424
4425 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4426 finish_output_size,
4427 &output_part_length,
4428 tag_buffer,
4429 tag_length ),
4430 PSA_ERROR_INVALID_ARGUMENT );
4431
4432 psa_aead_abort( &operation );
4433
Paul Elliott243080c2021-07-21 19:01:17 +01004434 /* Test for not sending any additional data after setting a non-zero length
4435 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004436
4437 operation = psa_aead_operation_init( );
4438
4439 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4440
4441 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4442
4443 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4444 input_data->len ) );
4445
4446 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4447 input_data->len, output_data,
4448 output_size, &output_length ),
4449 PSA_ERROR_INVALID_ARGUMENT );
4450
4451 psa_aead_abort( &operation );
4452
Paul Elliottb0450fe2021-09-01 15:06:26 +01004453 /* Test for sending too much additional data after setting lengths. */
4454
4455 operation = psa_aead_operation_init( );
4456
4457 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4458
4459 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4460
4461 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4462
4463
4464 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4465 additional_data->len ),
4466 PSA_ERROR_INVALID_ARGUMENT );
4467
4468 psa_aead_abort( &operation );
4469
4470 /* Test for sending too much data after setting lengths. */
4471
4472 operation = psa_aead_operation_init( );
4473
4474 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4475
4476 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4477
4478 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4479
4480 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4481 input_data->len, output_data,
4482 output_size, &output_length ),
4483 PSA_ERROR_INVALID_ARGUMENT );
4484
4485 psa_aead_abort( &operation );
4486
Paul Elliottc23a9a02021-06-21 18:32:46 +01004487 /* Test sending additional data after data. */
4488
4489 operation = psa_aead_operation_init( );
4490
4491 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4492
4493 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4494
4495 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4496 input_data->len, output_data,
4497 output_size, &output_length ) );
4498
4499 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4500 additional_data->len ),
4501 PSA_ERROR_BAD_STATE );
4502
4503 psa_aead_abort( &operation );
4504
Paul Elliott534d0b42021-06-22 19:15:20 +01004505 /* Test calling finish on decryption. */
4506
4507 operation = psa_aead_operation_init( );
4508
4509 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4510
4511 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4512
4513 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4514 finish_output_size,
4515 &output_part_length,
4516 tag_buffer, tag_length,
4517 &tag_size ),
4518 PSA_ERROR_BAD_STATE );
4519
4520 psa_aead_abort( &operation );
4521
4522 /* Test calling verify on encryption. */
4523
4524 operation = psa_aead_operation_init( );
4525
4526 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4527
4528 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4529
4530 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4531 finish_output_size,
4532 &output_part_length,
4533 tag_buffer,
4534 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01004535 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01004536
4537 psa_aead_abort( &operation );
4538
4539
Paul Elliottc23a9a02021-06-21 18:32:46 +01004540exit:
4541 psa_destroy_key( key );
4542 psa_aead_abort( &operation );
4543 mbedtls_free( output_data );
4544 mbedtls_free( final_data );
4545 PSA_DONE( );
4546}
4547/* END_CASE */
4548
4549/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004550void signature_size( int type_arg,
4551 int bits,
4552 int alg_arg,
4553 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004554{
4555 psa_key_type_t type = type_arg;
4556 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004557 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004558
Gilles Peskinefe11b722018-12-18 00:24:04 +01004559 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004560
Gilles Peskinee59236f2018-01-27 23:32:46 +01004561exit:
4562 ;
4563}
4564/* END_CASE */
4565
4566/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004567void sign_hash_deterministic( int key_type_arg, data_t *key_data,
4568 int alg_arg, data_t *input_data,
4569 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004570{
Ronald Cron5425a212020-08-04 14:58:35 +02004571 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004572 psa_key_type_t key_type = key_type_arg;
4573 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004574 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004575 unsigned char *signature = NULL;
4576 size_t signature_size;
4577 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004578 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004579
Gilles Peskine8817f612018-12-18 00:18:46 +01004580 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004581
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004582 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004583 psa_set_key_algorithm( &attributes, alg );
4584 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004585
Gilles Peskine049c7532019-05-15 20:22:09 +02004586 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004587 &key ) );
4588 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004589 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004590
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004591 /* Allocate a buffer which has the size advertized by the
4592 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004593 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004594 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004595 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004596 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004597 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004598
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004599 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004600 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004601 input_data->x, input_data->len,
4602 signature, signature_size,
4603 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004604 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004605 ASSERT_COMPARE( output_data->x, output_data->len,
4606 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004607
4608exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004609 /*
4610 * Key attributes may have been returned by psa_get_key_attributes()
4611 * thus reset them as required.
4612 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004613 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004614
Ronald Cron5425a212020-08-04 14:58:35 +02004615 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004616 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004617 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004618}
4619/* END_CASE */
4620
4621/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004622void sign_hash_fail( int key_type_arg, data_t *key_data,
4623 int alg_arg, data_t *input_data,
4624 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004625{
Ronald Cron5425a212020-08-04 14:58:35 +02004626 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004627 psa_key_type_t key_type = key_type_arg;
4628 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004629 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004630 psa_status_t actual_status;
4631 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004632 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004633 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004634 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004635
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004636 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004637
Gilles Peskine8817f612018-12-18 00:18:46 +01004638 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004639
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004640 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004641 psa_set_key_algorithm( &attributes, alg );
4642 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004643
Gilles Peskine049c7532019-05-15 20:22:09 +02004644 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004645 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004646
Ronald Cron5425a212020-08-04 14:58:35 +02004647 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004648 input_data->x, input_data->len,
4649 signature, signature_size,
4650 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004651 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004652 /* The value of *signature_length is unspecified on error, but
4653 * whatever it is, it should be less than signature_size, so that
4654 * if the caller tries to read *signature_length bytes without
4655 * checking the error code then they don't overflow a buffer. */
4656 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004657
4658exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004659 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004660 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004661 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004662 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004663}
4664/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004665
4666/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004667void sign_verify_hash( int key_type_arg, data_t *key_data,
4668 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02004669{
Ronald Cron5425a212020-08-04 14:58:35 +02004670 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004671 psa_key_type_t key_type = key_type_arg;
4672 psa_algorithm_t alg = alg_arg;
4673 size_t key_bits;
4674 unsigned char *signature = NULL;
4675 size_t signature_size;
4676 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004677 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004678
Gilles Peskine8817f612018-12-18 00:18:46 +01004679 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004680
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004681 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004682 psa_set_key_algorithm( &attributes, alg );
4683 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004684
Gilles Peskine049c7532019-05-15 20:22:09 +02004685 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004686 &key ) );
4687 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004688 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004689
4690 /* Allocate a buffer which has the size advertized by the
4691 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004692 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004693 key_bits, alg );
4694 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004695 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004696 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004697
4698 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004699 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004700 input_data->x, input_data->len,
4701 signature, signature_size,
4702 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004703 /* Check that the signature length looks sensible. */
4704 TEST_ASSERT( signature_length <= signature_size );
4705 TEST_ASSERT( signature_length > 0 );
4706
4707 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004708 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004709 input_data->x, input_data->len,
4710 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004711
4712 if( input_data->len != 0 )
4713 {
4714 /* Flip a bit in the input and verify that the signature is now
4715 * detected as invalid. Flip a bit at the beginning, not at the end,
4716 * because ECDSA may ignore the last few bits of the input. */
4717 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004718 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004719 input_data->x, input_data->len,
4720 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004721 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004722 }
4723
4724exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004725 /*
4726 * Key attributes may have been returned by psa_get_key_attributes()
4727 * thus reset them as required.
4728 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004729 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004730
Ronald Cron5425a212020-08-04 14:58:35 +02004731 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004732 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004733 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004734}
4735/* END_CASE */
4736
4737/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004738void verify_hash( int key_type_arg, data_t *key_data,
4739 int alg_arg, data_t *hash_data,
4740 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004741{
Ronald Cron5425a212020-08-04 14:58:35 +02004742 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004743 psa_key_type_t key_type = key_type_arg;
4744 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004745 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004746
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004747 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004748
Gilles Peskine8817f612018-12-18 00:18:46 +01004749 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004750
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004751 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004752 psa_set_key_algorithm( &attributes, alg );
4753 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004754
Gilles Peskine049c7532019-05-15 20:22:09 +02004755 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004756 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004757
Ronald Cron5425a212020-08-04 14:58:35 +02004758 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004759 hash_data->x, hash_data->len,
4760 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004761
itayzafrir5c753392018-05-08 11:18:38 +03004762exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004763 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004764 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004765 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004766}
4767/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004768
4769/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004770void verify_hash_fail( int key_type_arg, data_t *key_data,
4771 int alg_arg, data_t *hash_data,
4772 data_t *signature_data,
4773 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004774{
Ronald Cron5425a212020-08-04 14:58:35 +02004775 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004776 psa_key_type_t key_type = key_type_arg;
4777 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004778 psa_status_t actual_status;
4779 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004780 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004781
Gilles Peskine8817f612018-12-18 00:18:46 +01004782 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004783
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004784 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004785 psa_set_key_algorithm( &attributes, alg );
4786 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004787
Gilles Peskine049c7532019-05-15 20:22:09 +02004788 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004789 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004790
Ronald Cron5425a212020-08-04 14:58:35 +02004791 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004792 hash_data->x, hash_data->len,
4793 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004794 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004795
4796exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004797 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004798 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004799 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004800}
4801/* END_CASE */
4802
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004803/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02004804void sign_message_deterministic( int key_type_arg,
4805 data_t *key_data,
4806 int alg_arg,
4807 data_t *input_data,
4808 data_t *output_data )
4809{
4810 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4811 psa_key_type_t key_type = key_type_arg;
4812 psa_algorithm_t alg = alg_arg;
4813 size_t key_bits;
4814 unsigned char *signature = NULL;
4815 size_t signature_size;
4816 size_t signature_length = 0xdeadbeef;
4817 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4818
4819 PSA_ASSERT( psa_crypto_init( ) );
4820
4821 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4822 psa_set_key_algorithm( &attributes, alg );
4823 psa_set_key_type( &attributes, key_type );
4824
4825 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4826 &key ) );
4827 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4828 key_bits = psa_get_key_bits( &attributes );
4829
4830 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4831 TEST_ASSERT( signature_size != 0 );
4832 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4833 ASSERT_ALLOC( signature, signature_size );
4834
4835 PSA_ASSERT( psa_sign_message( key, alg,
4836 input_data->x, input_data->len,
4837 signature, signature_size,
4838 &signature_length ) );
4839
4840 ASSERT_COMPARE( output_data->x, output_data->len,
4841 signature, signature_length );
4842
4843exit:
4844 psa_reset_key_attributes( &attributes );
4845
4846 psa_destroy_key( key );
4847 mbedtls_free( signature );
4848 PSA_DONE( );
4849
4850}
4851/* END_CASE */
4852
4853/* BEGIN_CASE */
4854void sign_message_fail( int key_type_arg,
4855 data_t *key_data,
4856 int alg_arg,
4857 data_t *input_data,
4858 int signature_size_arg,
4859 int expected_status_arg )
4860{
4861 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4862 psa_key_type_t key_type = key_type_arg;
4863 psa_algorithm_t alg = alg_arg;
4864 size_t signature_size = signature_size_arg;
4865 psa_status_t actual_status;
4866 psa_status_t expected_status = expected_status_arg;
4867 unsigned char *signature = NULL;
4868 size_t signature_length = 0xdeadbeef;
4869 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4870
4871 ASSERT_ALLOC( signature, signature_size );
4872
4873 PSA_ASSERT( psa_crypto_init( ) );
4874
4875 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4876 psa_set_key_algorithm( &attributes, alg );
4877 psa_set_key_type( &attributes, key_type );
4878
4879 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4880 &key ) );
4881
4882 actual_status = psa_sign_message( key, alg,
4883 input_data->x, input_data->len,
4884 signature, signature_size,
4885 &signature_length );
4886 TEST_EQUAL( actual_status, expected_status );
4887 /* The value of *signature_length is unspecified on error, but
4888 * whatever it is, it should be less than signature_size, so that
4889 * if the caller tries to read *signature_length bytes without
4890 * checking the error code then they don't overflow a buffer. */
4891 TEST_ASSERT( signature_length <= signature_size );
4892
4893exit:
4894 psa_reset_key_attributes( &attributes );
4895 psa_destroy_key( key );
4896 mbedtls_free( signature );
4897 PSA_DONE( );
4898}
4899/* END_CASE */
4900
4901/* BEGIN_CASE */
4902void sign_verify_message( int key_type_arg,
4903 data_t *key_data,
4904 int alg_arg,
4905 data_t *input_data )
4906{
4907 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4908 psa_key_type_t key_type = key_type_arg;
4909 psa_algorithm_t alg = alg_arg;
4910 size_t key_bits;
4911 unsigned char *signature = NULL;
4912 size_t signature_size;
4913 size_t signature_length = 0xdeadbeef;
4914 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4915
4916 PSA_ASSERT( psa_crypto_init( ) );
4917
4918 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
4919 PSA_KEY_USAGE_VERIFY_MESSAGE );
4920 psa_set_key_algorithm( &attributes, alg );
4921 psa_set_key_type( &attributes, key_type );
4922
4923 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4924 &key ) );
4925 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4926 key_bits = psa_get_key_bits( &attributes );
4927
4928 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4929 TEST_ASSERT( signature_size != 0 );
4930 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4931 ASSERT_ALLOC( signature, signature_size );
4932
4933 PSA_ASSERT( psa_sign_message( key, alg,
4934 input_data->x, input_data->len,
4935 signature, signature_size,
4936 &signature_length ) );
4937 TEST_ASSERT( signature_length <= signature_size );
4938 TEST_ASSERT( signature_length > 0 );
4939
4940 PSA_ASSERT( psa_verify_message( key, alg,
4941 input_data->x, input_data->len,
4942 signature, signature_length ) );
4943
4944 if( input_data->len != 0 )
4945 {
4946 /* Flip a bit in the input and verify that the signature is now
4947 * detected as invalid. Flip a bit at the beginning, not at the end,
4948 * because ECDSA may ignore the last few bits of the input. */
4949 input_data->x[0] ^= 1;
4950 TEST_EQUAL( psa_verify_message( key, alg,
4951 input_data->x, input_data->len,
4952 signature, signature_length ),
4953 PSA_ERROR_INVALID_SIGNATURE );
4954 }
4955
4956exit:
4957 psa_reset_key_attributes( &attributes );
4958
4959 psa_destroy_key( key );
4960 mbedtls_free( signature );
4961 PSA_DONE( );
4962}
4963/* END_CASE */
4964
4965/* BEGIN_CASE */
4966void verify_message( int key_type_arg,
4967 data_t *key_data,
4968 int alg_arg,
4969 data_t *input_data,
4970 data_t *signature_data )
4971{
4972 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4973 psa_key_type_t key_type = key_type_arg;
4974 psa_algorithm_t alg = alg_arg;
4975 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4976
4977 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
4978
4979 PSA_ASSERT( psa_crypto_init( ) );
4980
4981 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4982 psa_set_key_algorithm( &attributes, alg );
4983 psa_set_key_type( &attributes, key_type );
4984
4985 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4986 &key ) );
4987
4988 PSA_ASSERT( psa_verify_message( key, alg,
4989 input_data->x, input_data->len,
4990 signature_data->x, signature_data->len ) );
4991
4992exit:
4993 psa_reset_key_attributes( &attributes );
4994 psa_destroy_key( key );
4995 PSA_DONE( );
4996}
4997/* END_CASE */
4998
4999/* BEGIN_CASE */
5000void verify_message_fail( int key_type_arg,
5001 data_t *key_data,
5002 int alg_arg,
5003 data_t *hash_data,
5004 data_t *signature_data,
5005 int expected_status_arg )
5006{
5007 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5008 psa_key_type_t key_type = key_type_arg;
5009 psa_algorithm_t alg = alg_arg;
5010 psa_status_t actual_status;
5011 psa_status_t expected_status = expected_status_arg;
5012 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5013
5014 PSA_ASSERT( psa_crypto_init( ) );
5015
5016 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5017 psa_set_key_algorithm( &attributes, alg );
5018 psa_set_key_type( &attributes, key_type );
5019
5020 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5021 &key ) );
5022
5023 actual_status = psa_verify_message( key, alg,
5024 hash_data->x, hash_data->len,
5025 signature_data->x,
5026 signature_data->len );
5027 TEST_EQUAL( actual_status, expected_status );
5028
5029exit:
5030 psa_reset_key_attributes( &attributes );
5031 psa_destroy_key( key );
5032 PSA_DONE( );
5033}
5034/* END_CASE */
5035
5036/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02005037void asymmetric_encrypt( int key_type_arg,
5038 data_t *key_data,
5039 int alg_arg,
5040 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02005041 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02005042 int expected_output_length_arg,
5043 int expected_status_arg )
5044{
Ronald Cron5425a212020-08-04 14:58:35 +02005045 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005046 psa_key_type_t key_type = key_type_arg;
5047 psa_algorithm_t alg = alg_arg;
5048 size_t expected_output_length = expected_output_length_arg;
5049 size_t key_bits;
5050 unsigned char *output = NULL;
5051 size_t output_size;
5052 size_t output_length = ~0;
5053 psa_status_t actual_status;
5054 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005055 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005056
Gilles Peskine8817f612018-12-18 00:18:46 +01005057 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005058
Gilles Peskine656896e2018-06-29 19:12:28 +02005059 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005060 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5061 psa_set_key_algorithm( &attributes, alg );
5062 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005063 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005064 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02005065
5066 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02005067 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005068 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005069
Gilles Peskine656896e2018-06-29 19:12:28 +02005070 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005071 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005072 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02005073
5074 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02005075 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02005076 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005077 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02005078 output, output_size,
5079 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005080 TEST_EQUAL( actual_status, expected_status );
5081 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02005082
Gilles Peskine68428122018-06-30 18:42:41 +02005083 /* If the label is empty, the test framework puts a non-null pointer
5084 * in label->x. Test that a null pointer works as well. */
5085 if( label->len == 0 )
5086 {
5087 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005088 if( output_size != 0 )
5089 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005090 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005091 input_data->x, input_data->len,
5092 NULL, label->len,
5093 output, output_size,
5094 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005095 TEST_EQUAL( actual_status, expected_status );
5096 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005097 }
5098
Gilles Peskine656896e2018-06-29 19:12:28 +02005099exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005100 /*
5101 * Key attributes may have been returned by psa_get_key_attributes()
5102 * thus reset them as required.
5103 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005104 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005105
Ronald Cron5425a212020-08-04 14:58:35 +02005106 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02005107 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005108 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02005109}
5110/* END_CASE */
5111
5112/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005113void asymmetric_encrypt_decrypt( int key_type_arg,
5114 data_t *key_data,
5115 int alg_arg,
5116 data_t *input_data,
5117 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005118{
Ronald Cron5425a212020-08-04 14:58:35 +02005119 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005120 psa_key_type_t key_type = key_type_arg;
5121 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005122 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005123 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005124 size_t output_size;
5125 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005126 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005127 size_t output2_size;
5128 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005129 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005130
Gilles Peskine8817f612018-12-18 00:18:46 +01005131 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005132
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005133 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5134 psa_set_key_algorithm( &attributes, alg );
5135 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005136
Gilles Peskine049c7532019-05-15 20:22:09 +02005137 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005138 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005139
5140 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02005141 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005142 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005143
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005144 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005145 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005146 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01005147
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005148 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01005149 TEST_ASSERT( output2_size <=
5150 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
5151 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005152 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005153
Gilles Peskineeebd7382018-06-08 18:11:54 +02005154 /* We test encryption by checking that encrypt-then-decrypt gives back
5155 * the original plaintext because of the non-optional random
5156 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02005157 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005158 input_data->x, input_data->len,
5159 label->x, label->len,
5160 output, output_size,
5161 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005162 /* We don't know what ciphertext length to expect, but check that
5163 * it looks sensible. */
5164 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005165
Ronald Cron5425a212020-08-04 14:58:35 +02005166 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005167 output, output_length,
5168 label->x, label->len,
5169 output2, output2_size,
5170 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005171 ASSERT_COMPARE( input_data->x, input_data->len,
5172 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005173
5174exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005175 /*
5176 * Key attributes may have been returned by psa_get_key_attributes()
5177 * thus reset them as required.
5178 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005179 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005180
Ronald Cron5425a212020-08-04 14:58:35 +02005181 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005182 mbedtls_free( output );
5183 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005184 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005185}
5186/* END_CASE */
5187
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005188/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005189void asymmetric_decrypt( int key_type_arg,
5190 data_t *key_data,
5191 int alg_arg,
5192 data_t *input_data,
5193 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02005194 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005195{
Ronald Cron5425a212020-08-04 14:58:35 +02005196 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005197 psa_key_type_t key_type = key_type_arg;
5198 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01005199 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005200 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03005201 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005202 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005203 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005204
Gilles Peskine8817f612018-12-18 00:18:46 +01005205 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005206
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005207 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5208 psa_set_key_algorithm( &attributes, alg );
5209 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005210
Gilles Peskine049c7532019-05-15 20:22:09 +02005211 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005212 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005213
gabor-mezei-armceface22021-01-21 12:26:17 +01005214 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5215 key_bits = psa_get_key_bits( &attributes );
5216
5217 /* Determine the maximum ciphertext length */
5218 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
5219 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
5220 ASSERT_ALLOC( output, output_size );
5221
Ronald Cron5425a212020-08-04 14:58:35 +02005222 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005223 input_data->x, input_data->len,
5224 label->x, label->len,
5225 output,
5226 output_size,
5227 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005228 ASSERT_COMPARE( expected_data->x, expected_data->len,
5229 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005230
Gilles Peskine68428122018-06-30 18:42:41 +02005231 /* If the label is empty, the test framework puts a non-null pointer
5232 * in label->x. Test that a null pointer works as well. */
5233 if( label->len == 0 )
5234 {
5235 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005236 if( output_size != 0 )
5237 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005238 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005239 input_data->x, input_data->len,
5240 NULL, label->len,
5241 output,
5242 output_size,
5243 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005244 ASSERT_COMPARE( expected_data->x, expected_data->len,
5245 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005246 }
5247
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005248exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005249 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005250 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005251 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005252 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005253}
5254/* END_CASE */
5255
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005256/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005257void asymmetric_decrypt_fail( int key_type_arg,
5258 data_t *key_data,
5259 int alg_arg,
5260 data_t *input_data,
5261 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00005262 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02005263 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005264{
Ronald Cron5425a212020-08-04 14:58:35 +02005265 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005266 psa_key_type_t key_type = key_type_arg;
5267 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005268 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00005269 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005270 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005271 psa_status_t actual_status;
5272 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005273 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005274
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005275 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005276
Gilles Peskine8817f612018-12-18 00:18:46 +01005277 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005278
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005279 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5280 psa_set_key_algorithm( &attributes, alg );
5281 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005282
Gilles Peskine049c7532019-05-15 20:22:09 +02005283 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005284 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005285
Ronald Cron5425a212020-08-04 14:58:35 +02005286 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005287 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005288 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005289 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02005290 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005291 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005292 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005293
Gilles Peskine68428122018-06-30 18:42:41 +02005294 /* If the label is empty, the test framework puts a non-null pointer
5295 * in label->x. Test that a null pointer works as well. */
5296 if( label->len == 0 )
5297 {
5298 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005299 if( output_size != 0 )
5300 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005301 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005302 input_data->x, input_data->len,
5303 NULL, label->len,
5304 output, output_size,
5305 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005306 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005307 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02005308 }
5309
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005310exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005311 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005312 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02005313 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005314 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005315}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005316/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02005317
5318/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005319void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00005320{
5321 /* Test each valid way of initializing the object, except for `= {0}`, as
5322 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
5323 * though it's OK by the C standard. We could test for this, but we'd need
5324 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005325 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005326 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
5327 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
5328 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00005329
5330 memset( &zero, 0, sizeof( zero ) );
5331
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005332 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005333 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005334 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005335 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005336 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005337 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005338 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005339
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005340 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005341 PSA_ASSERT( psa_key_derivation_abort(&func) );
5342 PSA_ASSERT( psa_key_derivation_abort(&init) );
5343 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00005344}
5345/* END_CASE */
5346
Janos Follath16de4a42019-06-13 16:32:24 +01005347/* BEGIN_CASE */
5348void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005349{
Gilles Peskineea0fb492018-07-12 17:17:20 +02005350 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005351 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005352 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005353
Gilles Peskine8817f612018-12-18 00:18:46 +01005354 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005355
Janos Follath16de4a42019-06-13 16:32:24 +01005356 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005357 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005358
5359exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005360 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005361 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005362}
5363/* END_CASE */
5364
Janos Follathaf3c2a02019-06-12 12:34:34 +01005365/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01005366void derive_set_capacity( int alg_arg, int capacity_arg,
5367 int expected_status_arg )
5368{
5369 psa_algorithm_t alg = alg_arg;
5370 size_t capacity = capacity_arg;
5371 psa_status_t expected_status = expected_status_arg;
5372 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5373
5374 PSA_ASSERT( psa_crypto_init( ) );
5375
5376 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5377
5378 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
5379 expected_status );
5380
5381exit:
5382 psa_key_derivation_abort( &operation );
5383 PSA_DONE( );
5384}
5385/* END_CASE */
5386
5387/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01005388void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02005389 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005390 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02005391 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005392 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02005393 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005394 int expected_status_arg3,
5395 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005396{
5397 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02005398 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
5399 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01005400 psa_status_t expected_statuses[] = {expected_status_arg1,
5401 expected_status_arg2,
5402 expected_status_arg3};
5403 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005404 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5405 MBEDTLS_SVC_KEY_ID_INIT,
5406 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01005407 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5408 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5409 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005410 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005411 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005412 psa_status_t expected_output_status = expected_output_status_arg;
5413 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01005414
5415 PSA_ASSERT( psa_crypto_init( ) );
5416
5417 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5418 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005419
5420 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5421
5422 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
5423 {
Gilles Peskineb8965192019-09-24 16:21:10 +02005424 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005425 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02005426 psa_set_key_type( &attributes, key_types[i] );
5427 PSA_ASSERT( psa_import_key( &attributes,
5428 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005429 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005430 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
5431 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
5432 {
5433 // When taking a private key as secret input, use key agreement
5434 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005435 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
5436 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005437 expected_statuses[i] );
5438 }
5439 else
5440 {
5441 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02005442 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005443 expected_statuses[i] );
5444 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02005445 }
5446 else
5447 {
5448 TEST_EQUAL( psa_key_derivation_input_bytes(
5449 &operation, steps[i],
5450 inputs[i]->x, inputs[i]->len ),
5451 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005452 }
5453 }
5454
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005455 if( output_key_type != PSA_KEY_TYPE_NONE )
5456 {
5457 psa_reset_key_attributes( &attributes );
5458 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
5459 psa_set_key_bits( &attributes, 8 );
5460 actual_output_status =
5461 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005462 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005463 }
5464 else
5465 {
5466 uint8_t buffer[1];
5467 actual_output_status =
5468 psa_key_derivation_output_bytes( &operation,
5469 buffer, sizeof( buffer ) );
5470 }
5471 TEST_EQUAL( actual_output_status, expected_output_status );
5472
Janos Follathaf3c2a02019-06-12 12:34:34 +01005473exit:
5474 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005475 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5476 psa_destroy_key( keys[i] );
5477 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005478 PSA_DONE( );
5479}
5480/* END_CASE */
5481
Janos Follathd958bb72019-07-03 15:02:16 +01005482/* BEGIN_CASE */
5483void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005484{
Janos Follathd958bb72019-07-03 15:02:16 +01005485 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005486 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02005487 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005488 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01005489 unsigned char input1[] = "Input 1";
5490 size_t input1_length = sizeof( input1 );
5491 unsigned char input2[] = "Input 2";
5492 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005493 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02005494 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02005495 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5496 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5497 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005498 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005499
Gilles Peskine8817f612018-12-18 00:18:46 +01005500 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005501
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005502 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5503 psa_set_key_algorithm( &attributes, alg );
5504 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005505
Gilles Peskine73676cb2019-05-15 20:15:10 +02005506 PSA_ASSERT( psa_import_key( &attributes,
5507 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02005508 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005509
5510 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005511 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5512 input1, input1_length,
5513 input2, input2_length,
5514 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01005515 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005516
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005517 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01005518 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005519 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005520
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005521 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005522
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005523 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02005524 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005525
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005526exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005527 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005528 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005529 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005530}
5531/* END_CASE */
5532
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005533/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005534void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005535{
5536 uint8_t output_buffer[16];
5537 size_t buffer_size = 16;
5538 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005539 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005540
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005541 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5542 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005543 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005544
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005545 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005546 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005547
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005548 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005549
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005550 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5551 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005552 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005553
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005554 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005555 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005556
5557exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005558 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005559}
5560/* END_CASE */
5561
5562/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005563void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02005564 int step1_arg, data_t *input1,
5565 int step2_arg, data_t *input2,
5566 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005567 int requested_capacity_arg,
5568 data_t *expected_output1,
5569 data_t *expected_output2 )
5570{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005571 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005572 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5573 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005574 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5575 MBEDTLS_SVC_KEY_ID_INIT,
5576 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005577 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005578 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005579 uint8_t *expected_outputs[2] =
5580 {expected_output1->x, expected_output2->x};
5581 size_t output_sizes[2] =
5582 {expected_output1->len, expected_output2->len};
5583 size_t output_buffer_size = 0;
5584 uint8_t *output_buffer = NULL;
5585 size_t expected_capacity;
5586 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005587 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005588 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005589 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005590
5591 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5592 {
5593 if( output_sizes[i] > output_buffer_size )
5594 output_buffer_size = output_sizes[i];
5595 if( output_sizes[i] == 0 )
5596 expected_outputs[i] = NULL;
5597 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005598 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005599 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005600
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005601 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5602 psa_set_key_algorithm( &attributes, alg );
5603 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005604
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005605 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005606 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5607 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5608 requested_capacity ) );
5609 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005610 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005611 switch( steps[i] )
5612 {
5613 case 0:
5614 break;
5615 case PSA_KEY_DERIVATION_INPUT_SECRET:
5616 PSA_ASSERT( psa_import_key( &attributes,
5617 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005618 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01005619
5620 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5621 {
5622 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
5623 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
5624 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
5625 }
5626
Gilles Peskine1468da72019-05-29 17:35:49 +02005627 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005628 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005629 break;
5630 default:
5631 PSA_ASSERT( psa_key_derivation_input_bytes(
5632 &operation, steps[i],
5633 inputs[i]->x, inputs[i]->len ) );
5634 break;
5635 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005636 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005637
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005638 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005639 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005640 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005641 expected_capacity = requested_capacity;
5642
5643 /* Expansion phase. */
5644 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5645 {
5646 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005647 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005648 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005649 if( expected_capacity == 0 && output_sizes[i] == 0 )
5650 {
5651 /* Reading 0 bytes when 0 bytes are available can go either way. */
5652 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005653 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005654 continue;
5655 }
5656 else if( expected_capacity == 0 ||
5657 output_sizes[i] > expected_capacity )
5658 {
5659 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005660 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005661 expected_capacity = 0;
5662 continue;
5663 }
5664 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005665 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005666 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005667 ASSERT_COMPARE( output_buffer, output_sizes[i],
5668 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005669 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005670 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005671 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005672 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005673 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005674 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005675 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005676
5677exit:
5678 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005679 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005680 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5681 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005682 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005683}
5684/* END_CASE */
5685
5686/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005687void derive_full( int alg_arg,
5688 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005689 data_t *input1,
5690 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005691 int requested_capacity_arg )
5692{
Ronald Cron5425a212020-08-04 14:58:35 +02005693 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005694 psa_algorithm_t alg = alg_arg;
5695 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005696 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005697 unsigned char output_buffer[16];
5698 size_t expected_capacity = requested_capacity;
5699 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005700 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005701
Gilles Peskine8817f612018-12-18 00:18:46 +01005702 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005703
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005704 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5705 psa_set_key_algorithm( &attributes, alg );
5706 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005707
Gilles Peskine049c7532019-05-15 20:22:09 +02005708 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005709 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005710
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005711 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5712 input1->x, input1->len,
5713 input2->x, input2->len,
5714 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01005715 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005716
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005717 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005718 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005719 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005720
5721 /* Expansion phase. */
5722 while( current_capacity > 0 )
5723 {
5724 size_t read_size = sizeof( output_buffer );
5725 if( read_size > current_capacity )
5726 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005727 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005728 output_buffer,
5729 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005730 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005731 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005732 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005733 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005734 }
5735
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005736 /* Check that the operation refuses to go over capacity. */
5737 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005738 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005739
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005740 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005741
5742exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005743 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005744 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005745 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005746}
5747/* END_CASE */
5748
Janos Follathe60c9052019-07-03 13:51:30 +01005749/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005750void derive_key_exercise( int alg_arg,
5751 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005752 data_t *input1,
5753 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005754 int derived_type_arg,
5755 int derived_bits_arg,
5756 int derived_usage_arg,
5757 int derived_alg_arg )
5758{
Ronald Cron5425a212020-08-04 14:58:35 +02005759 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5760 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005761 psa_algorithm_t alg = alg_arg;
5762 psa_key_type_t derived_type = derived_type_arg;
5763 size_t derived_bits = derived_bits_arg;
5764 psa_key_usage_t derived_usage = derived_usage_arg;
5765 psa_algorithm_t derived_alg = derived_alg_arg;
5766 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005767 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005768 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005769 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005770
Gilles Peskine8817f612018-12-18 00:18:46 +01005771 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005772
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005773 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5774 psa_set_key_algorithm( &attributes, alg );
5775 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005776 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005777 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005778
5779 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005780 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5781 input1->x, input1->len,
5782 input2->x, input2->len,
5783 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01005784 goto exit;
5785
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005786 psa_set_key_usage_flags( &attributes, derived_usage );
5787 psa_set_key_algorithm( &attributes, derived_alg );
5788 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005789 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005790 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005791 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005792
5793 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005794 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005795 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5796 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005797
5798 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005799 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005800 goto exit;
5801
5802exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005803 /*
5804 * Key attributes may have been returned by psa_get_key_attributes()
5805 * thus reset them as required.
5806 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005807 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005808
5809 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005810 psa_destroy_key( base_key );
5811 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005812 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005813}
5814/* END_CASE */
5815
Janos Follath42fd8882019-07-03 14:17:09 +01005816/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005817void derive_key_export( int alg_arg,
5818 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005819 data_t *input1,
5820 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005821 int bytes1_arg,
5822 int bytes2_arg )
5823{
Ronald Cron5425a212020-08-04 14:58:35 +02005824 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5825 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005826 psa_algorithm_t alg = alg_arg;
5827 size_t bytes1 = bytes1_arg;
5828 size_t bytes2 = bytes2_arg;
5829 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005830 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005831 uint8_t *output_buffer = NULL;
5832 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005833 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5834 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005835 size_t length;
5836
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005837 ASSERT_ALLOC( output_buffer, capacity );
5838 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005839 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005840
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005841 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5842 psa_set_key_algorithm( &base_attributes, alg );
5843 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005844 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005845 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005846
5847 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005848 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5849 input1->x, input1->len,
5850 input2->x, input2->len,
5851 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005852 goto exit;
5853
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005854 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005855 output_buffer,
5856 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005857 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005858
5859 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005860 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5861 input1->x, input1->len,
5862 input2->x, input2->len,
5863 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005864 goto exit;
5865
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005866 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5867 psa_set_key_algorithm( &derived_attributes, 0 );
5868 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005869 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005870 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005871 &derived_key ) );
5872 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005873 export_buffer, bytes1,
5874 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005875 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005876 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005877 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005878 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005879 &derived_key ) );
5880 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005881 export_buffer + bytes1, bytes2,
5882 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005883 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005884
5885 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005886 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5887 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005888
5889exit:
5890 mbedtls_free( output_buffer );
5891 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005892 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005893 psa_destroy_key( base_key );
5894 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005895 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005896}
5897/* END_CASE */
5898
5899/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005900void derive_key( int alg_arg,
5901 data_t *key_data, data_t *input1, data_t *input2,
5902 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005903 int expected_status_arg,
5904 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02005905{
Ronald Cron5425a212020-08-04 14:58:35 +02005906 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5907 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005908 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005909 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005910 size_t bits = bits_arg;
5911 psa_status_t expected_status = expected_status_arg;
5912 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5913 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5914 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5915
5916 PSA_ASSERT( psa_crypto_init( ) );
5917
5918 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5919 psa_set_key_algorithm( &base_attributes, alg );
5920 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5921 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005922 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005923
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005924 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5925 input1->x, input1->len,
5926 input2->x, input2->len,
5927 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02005928 goto exit;
5929
5930 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5931 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005932 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005933 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01005934
5935 psa_status_t status =
5936 psa_key_derivation_output_key( &derived_attributes,
5937 &operation,
5938 &derived_key );
5939 if( is_large_output > 0 )
5940 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5941 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02005942
5943exit:
5944 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005945 psa_destroy_key( base_key );
5946 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005947 PSA_DONE( );
5948}
5949/* END_CASE */
5950
5951/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005952void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005953 int our_key_type_arg, int our_key_alg_arg,
5954 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005955 int expected_status_arg )
5956{
Ronald Cron5425a212020-08-04 14:58:35 +02005957 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005958 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005959 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005960 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005961 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005962 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005963 psa_status_t expected_status = expected_status_arg;
5964 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005965
Gilles Peskine8817f612018-12-18 00:18:46 +01005966 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005967
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005968 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005969 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005970 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005971 PSA_ASSERT( psa_import_key( &attributes,
5972 our_key_data->x, our_key_data->len,
5973 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005974
Gilles Peskine77f40d82019-04-11 21:27:06 +02005975 /* The tests currently include inputs that should fail at either step.
5976 * Test cases that fail at the setup step should be changed to call
5977 * key_derivation_setup instead, and this function should be renamed
5978 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005979 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005980 if( status == PSA_SUCCESS )
5981 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005982 TEST_EQUAL( psa_key_derivation_key_agreement(
5983 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5984 our_key,
5985 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005986 expected_status );
5987 }
5988 else
5989 {
5990 TEST_ASSERT( status == expected_status );
5991 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005992
5993exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005994 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005995 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005996 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005997}
5998/* END_CASE */
5999
6000/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02006001void raw_key_agreement( int alg_arg,
6002 int our_key_type_arg, data_t *our_key_data,
6003 data_t *peer_key_data,
6004 data_t *expected_output )
6005{
Ronald Cron5425a212020-08-04 14:58:35 +02006006 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006007 psa_algorithm_t alg = alg_arg;
6008 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006009 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006010 unsigned char *output = NULL;
6011 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01006012 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006013
6014 ASSERT_ALLOC( output, expected_output->len );
6015 PSA_ASSERT( psa_crypto_init( ) );
6016
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006017 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6018 psa_set_key_algorithm( &attributes, alg );
6019 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006020 PSA_ASSERT( psa_import_key( &attributes,
6021 our_key_data->x, our_key_data->len,
6022 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006023
gabor-mezei-armceface22021-01-21 12:26:17 +01006024 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
6025 key_bits = psa_get_key_bits( &attributes );
6026
Gilles Peskinebe697d82019-05-16 18:00:41 +02006027 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
6028 peer_key_data->x, peer_key_data->len,
6029 output, expected_output->len,
6030 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006031 ASSERT_COMPARE( output, output_length,
6032 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01006033 TEST_ASSERT( output_length <=
6034 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
6035 TEST_ASSERT( output_length <=
6036 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006037
6038exit:
6039 mbedtls_free( output );
6040 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006041 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006042}
6043/* END_CASE */
6044
6045/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02006046void key_agreement_capacity( int alg_arg,
6047 int our_key_type_arg, data_t *our_key_data,
6048 data_t *peer_key_data,
6049 int expected_capacity_arg )
6050{
Ronald Cron5425a212020-08-04 14:58:35 +02006051 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006052 psa_algorithm_t alg = alg_arg;
6053 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006054 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006055 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006056 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02006057 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02006058
Gilles Peskine8817f612018-12-18 00:18:46 +01006059 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006060
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006061 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6062 psa_set_key_algorithm( &attributes, alg );
6063 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006064 PSA_ASSERT( psa_import_key( &attributes,
6065 our_key_data->x, our_key_data->len,
6066 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006067
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006068 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006069 PSA_ASSERT( psa_key_derivation_key_agreement(
6070 &operation,
6071 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6072 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006073 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6074 {
6075 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006076 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006077 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006078 NULL, 0 ) );
6079 }
Gilles Peskine59685592018-09-18 12:11:34 +02006080
Gilles Peskinebf491972018-10-25 22:36:12 +02006081 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006082 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006083 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006084 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02006085
Gilles Peskinebf491972018-10-25 22:36:12 +02006086 /* Test the actual capacity by reading the output. */
6087 while( actual_capacity > sizeof( output ) )
6088 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006089 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006090 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02006091 actual_capacity -= sizeof( output );
6092 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006093 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006094 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006095 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006096 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02006097
Gilles Peskine59685592018-09-18 12:11:34 +02006098exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006099 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006100 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006101 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006102}
6103/* END_CASE */
6104
6105/* BEGIN_CASE */
6106void key_agreement_output( int alg_arg,
6107 int our_key_type_arg, data_t *our_key_data,
6108 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006109 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02006110{
Ronald Cron5425a212020-08-04 14:58:35 +02006111 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006112 psa_algorithm_t alg = alg_arg;
6113 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006114 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006115 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006116 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02006117
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006118 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
6119 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006120
Gilles Peskine8817f612018-12-18 00:18:46 +01006121 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006122
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006123 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6124 psa_set_key_algorithm( &attributes, alg );
6125 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006126 PSA_ASSERT( psa_import_key( &attributes,
6127 our_key_data->x, our_key_data->len,
6128 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006129
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006130 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006131 PSA_ASSERT( psa_key_derivation_key_agreement(
6132 &operation,
6133 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6134 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006135 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6136 {
6137 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006138 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006139 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006140 NULL, 0 ) );
6141 }
Gilles Peskine59685592018-09-18 12:11:34 +02006142
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006143 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006144 actual_output,
6145 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006146 ASSERT_COMPARE( actual_output, expected_output1->len,
6147 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006148 if( expected_output2->len != 0 )
6149 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006150 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006151 actual_output,
6152 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006153 ASSERT_COMPARE( actual_output, expected_output2->len,
6154 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006155 }
Gilles Peskine59685592018-09-18 12:11:34 +02006156
6157exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006158 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006159 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006160 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006161 mbedtls_free( actual_output );
6162}
6163/* END_CASE */
6164
6165/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02006166void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02006167{
Gilles Peskinea50d7392018-06-21 10:22:13 +02006168 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006169 unsigned char *output = NULL;
6170 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02006171 size_t i;
6172 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02006173
Simon Butcher49f8e312020-03-03 15:51:50 +00006174 TEST_ASSERT( bytes_arg >= 0 );
6175
Gilles Peskine91892022021-02-08 19:50:26 +01006176 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006177 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02006178
Gilles Peskine8817f612018-12-18 00:18:46 +01006179 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02006180
Gilles Peskinea50d7392018-06-21 10:22:13 +02006181 /* Run several times, to ensure that every output byte will be
6182 * nonzero at least once with overwhelming probability
6183 * (2^(-8*number_of_runs)). */
6184 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02006185 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006186 if( bytes != 0 )
6187 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01006188 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006189
Gilles Peskinea50d7392018-06-21 10:22:13 +02006190 for( i = 0; i < bytes; i++ )
6191 {
6192 if( output[i] != 0 )
6193 ++changed[i];
6194 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006195 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02006196
6197 /* Check that every byte was changed to nonzero at least once. This
6198 * validates that psa_generate_random is overwriting every byte of
6199 * the output buffer. */
6200 for( i = 0; i < bytes; i++ )
6201 {
6202 TEST_ASSERT( changed[i] != 0 );
6203 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006204
6205exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006206 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006207 mbedtls_free( output );
6208 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02006209}
6210/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02006211
6212/* BEGIN_CASE */
6213void generate_key( int type_arg,
6214 int bits_arg,
6215 int usage_arg,
6216 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006217 int expected_status_arg,
6218 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006219{
Ronald Cron5425a212020-08-04 14:58:35 +02006220 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006221 psa_key_type_t type = type_arg;
6222 psa_key_usage_t usage = usage_arg;
6223 size_t bits = bits_arg;
6224 psa_algorithm_t alg = alg_arg;
6225 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006226 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006227 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006228
Gilles Peskine8817f612018-12-18 00:18:46 +01006229 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006230
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006231 psa_set_key_usage_flags( &attributes, usage );
6232 psa_set_key_algorithm( &attributes, alg );
6233 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006234 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006235
6236 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01006237 psa_status_t status = psa_generate_key( &attributes, &key );
6238
6239 if( is_large_key > 0 )
6240 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6241 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006242 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006243 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006244
6245 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006246 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006247 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
6248 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006249
Gilles Peskine818ca122018-06-20 18:16:48 +02006250 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006251 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02006252 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006253
6254exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006255 /*
6256 * Key attributes may have been returned by psa_get_key_attributes()
6257 * thus reset them as required.
6258 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006259 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006260
Ronald Cron5425a212020-08-04 14:58:35 +02006261 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006262 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006263}
6264/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03006265
Ronald Cronee414c72021-03-18 18:50:08 +01006266/* 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 +02006267void generate_key_rsa( int bits_arg,
6268 data_t *e_arg,
6269 int expected_status_arg )
6270{
Ronald Cron5425a212020-08-04 14:58:35 +02006271 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006272 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006273 size_t bits = bits_arg;
6274 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
6275 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
6276 psa_status_t expected_status = expected_status_arg;
6277 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6278 uint8_t *exported = NULL;
6279 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006280 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006281 size_t exported_length = SIZE_MAX;
6282 uint8_t *e_read_buffer = NULL;
6283 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02006284 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006285 size_t e_read_length = SIZE_MAX;
6286
6287 if( e_arg->len == 0 ||
6288 ( e_arg->len == 3 &&
6289 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
6290 {
6291 is_default_public_exponent = 1;
6292 e_read_size = 0;
6293 }
6294 ASSERT_ALLOC( e_read_buffer, e_read_size );
6295 ASSERT_ALLOC( exported, exported_size );
6296
6297 PSA_ASSERT( psa_crypto_init( ) );
6298
6299 psa_set_key_usage_flags( &attributes, usage );
6300 psa_set_key_algorithm( &attributes, alg );
6301 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
6302 e_arg->x, e_arg->len ) );
6303 psa_set_key_bits( &attributes, bits );
6304
6305 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006306 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006307 if( expected_status != PSA_SUCCESS )
6308 goto exit;
6309
6310 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006311 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006312 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6313 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6314 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
6315 e_read_buffer, e_read_size,
6316 &e_read_length ) );
6317 if( is_default_public_exponent )
6318 TEST_EQUAL( e_read_length, 0 );
6319 else
6320 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
6321
6322 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006323 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02006324 goto exit;
6325
6326 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02006327 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006328 exported, exported_size,
6329 &exported_length ) );
6330 {
6331 uint8_t *p = exported;
6332 uint8_t *end = exported + exported_length;
6333 size_t len;
6334 /* RSAPublicKey ::= SEQUENCE {
6335 * modulus INTEGER, -- n
6336 * publicExponent INTEGER } -- e
6337 */
6338 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006339 MBEDTLS_ASN1_SEQUENCE |
6340 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01006341 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006342 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
6343 MBEDTLS_ASN1_INTEGER ) );
6344 if( len >= 1 && p[0] == 0 )
6345 {
6346 ++p;
6347 --len;
6348 }
6349 if( e_arg->len == 0 )
6350 {
6351 TEST_EQUAL( len, 3 );
6352 TEST_EQUAL( p[0], 1 );
6353 TEST_EQUAL( p[1], 0 );
6354 TEST_EQUAL( p[2], 1 );
6355 }
6356 else
6357 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
6358 }
6359
6360exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006361 /*
6362 * Key attributes may have been returned by psa_get_key_attributes() or
6363 * set by psa_set_key_domain_parameters() thus reset them as required.
6364 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006365 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006366
Ronald Cron5425a212020-08-04 14:58:35 +02006367 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006368 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006369 mbedtls_free( e_read_buffer );
6370 mbedtls_free( exported );
6371}
6372/* END_CASE */
6373
Darryl Greend49a4992018-06-18 17:27:26 +01006374/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006375void persistent_key_load_key_from_storage( data_t *data,
6376 int type_arg, int bits_arg,
6377 int usage_flags_arg, int alg_arg,
6378 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01006379{
Ronald Cron71016a92020-08-28 19:01:50 +02006380 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006381 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02006382 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6383 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006384 psa_key_type_t type = type_arg;
6385 size_t bits = bits_arg;
6386 psa_key_usage_t usage_flags = usage_flags_arg;
6387 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006388 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01006389 unsigned char *first_export = NULL;
6390 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006391 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006392 size_t first_exported_length;
6393 size_t second_exported_length;
6394
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006395 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6396 {
6397 ASSERT_ALLOC( first_export, export_size );
6398 ASSERT_ALLOC( second_export, export_size );
6399 }
Darryl Greend49a4992018-06-18 17:27:26 +01006400
Gilles Peskine8817f612018-12-18 00:18:46 +01006401 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006402
Gilles Peskinec87af662019-05-15 16:12:22 +02006403 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006404 psa_set_key_usage_flags( &attributes, usage_flags );
6405 psa_set_key_algorithm( &attributes, alg );
6406 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006407 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006408
Darryl Green0c6575a2018-11-07 16:05:30 +00006409 switch( generation_method )
6410 {
6411 case IMPORT_KEY:
6412 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02006413 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006414 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006415 break;
Darryl Greend49a4992018-06-18 17:27:26 +01006416
Darryl Green0c6575a2018-11-07 16:05:30 +00006417 case GENERATE_KEY:
6418 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006419 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006420 break;
6421
6422 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01006423#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006424 {
6425 /* Create base key */
6426 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
6427 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6428 psa_set_key_usage_flags( &base_attributes,
6429 PSA_KEY_USAGE_DERIVE );
6430 psa_set_key_algorithm( &base_attributes, derive_alg );
6431 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006432 PSA_ASSERT( psa_import_key( &base_attributes,
6433 data->x, data->len,
6434 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006435 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006436 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006437 PSA_ASSERT( psa_key_derivation_input_key(
6438 &operation,
6439 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006440 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006441 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006442 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006443 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
6444 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006445 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006446 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006447 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02006448 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006449 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01006450#else
6451 TEST_ASSUME( ! "KDF not supported in this configuration" );
6452#endif
6453 break;
6454
6455 default:
6456 TEST_ASSERT( ! "generation_method not implemented in test" );
6457 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00006458 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006459 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01006460
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006461 /* Export the key if permitted by the key policy. */
6462 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6463 {
Ronald Cron5425a212020-08-04 14:58:35 +02006464 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006465 first_export, export_size,
6466 &first_exported_length ) );
6467 if( generation_method == IMPORT_KEY )
6468 ASSERT_COMPARE( data->x, data->len,
6469 first_export, first_exported_length );
6470 }
Darryl Greend49a4992018-06-18 17:27:26 +01006471
6472 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02006473 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006474 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01006475 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006476
Darryl Greend49a4992018-06-18 17:27:26 +01006477 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02006478 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02006479 TEST_ASSERT( mbedtls_svc_key_id_equal(
6480 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006481 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
6482 PSA_KEY_LIFETIME_PERSISTENT );
6483 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6484 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6485 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
6486 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01006487
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006488 /* Export the key again if permitted by the key policy. */
6489 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00006490 {
Ronald Cron5425a212020-08-04 14:58:35 +02006491 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006492 second_export, export_size,
6493 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006494 ASSERT_COMPARE( first_export, first_exported_length,
6495 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006496 }
6497
6498 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006499 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00006500 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01006501
6502exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006503 /*
6504 * Key attributes may have been returned by psa_get_key_attributes()
6505 * thus reset them as required.
6506 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006507 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006508
Darryl Greend49a4992018-06-18 17:27:26 +01006509 mbedtls_free( first_export );
6510 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006511 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006512 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02006513 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006514 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01006515}
6516/* END_CASE */