blob: f25872d168dcb8bc3805f3cd387a9333a5ae9601 [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,
Paul Elliott329d5382021-07-22 17:10:45 +0100312 int is_encrypt,
Paul Elliott33746aa2021-09-15 16:40:40 +0100313 int do_zero_parts )
Paul Elliottd3f82412021-06-16 16:52:21 +0100314{
315 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
316 psa_key_type_t key_type = key_type_arg;
317 psa_algorithm_t alg = alg_arg;
318 psa_aead_operation_t operation;
319 unsigned char *output_data = NULL;
320 unsigned char *part_data = NULL;
321 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100322 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100323 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100324 size_t output_size = 0;
325 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100326 size_t output_length = 0;
327 size_t key_bits = 0;
328 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100329 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100330 size_t part_length = 0;
331 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100332 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100333 size_t ad_part_len = 0;
334 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100335 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100336 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
337 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
338
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100339 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100340 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100341
Paul Elliottd3f82412021-06-16 16:52:21 +0100342 PSA_ASSERT( psa_crypto_init( ) );
343
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100344 if( is_encrypt )
345 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
346 else
347 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
348
Paul Elliottd3f82412021-06-16 16:52:21 +0100349 psa_set_key_algorithm( &attributes, alg );
350 psa_set_key_type( &attributes, key_type );
351
352 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
353 &key ) );
354
355 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
356 key_bits = psa_get_key_bits( &attributes );
357
358 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
359
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100360 if( is_encrypt )
361 {
362 /* Tag gets written at end of buffer. */
363 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
364 ( input_data->len +
365 tag_length ) );
366 data_true_size = input_data->len;
367 }
368 else
369 {
370 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
371 ( input_data->len -
372 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100373
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100374 /* Do not want to attempt to decrypt tag. */
375 data_true_size = input_data->len - tag_length;
376 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100377
378 ASSERT_ALLOC( output_data, output_size );
379
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100380 if( is_encrypt )
381 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100382 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
383 TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100384 }
385 else
386 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100387 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
388 TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100389 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100390
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100391 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100392
393 operation = psa_aead_operation_init( );
394
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100395
396 if( is_encrypt )
397 status = psa_aead_encrypt_setup( &operation, key, alg );
398 else
399 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100400
401 /* If the operation is not supported, just skip and not fail in case the
402 * encryption involves a common limitation of cryptography hardwares and
403 * an alternative implementation. */
404 if( status == PSA_ERROR_NOT_SUPPORTED )
405 {
406 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
407 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
408 }
409
410 PSA_ASSERT( status );
411
Paul Elliott33746aa2021-09-15 16:40:40 +0100412 if( set_lengths_method == DO_NOT_SET_LENGTHS )
413 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
414 else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
Paul Elliottd3f82412021-06-16 16:52:21 +0100415 {
Paul Elliott33746aa2021-09-15 16:40:40 +0100416 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
417 data_true_size ) );
Paul Elliottebf91632021-07-22 17:54:42 +0100418 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
419 }
Paul Elliott33746aa2021-09-15 16:40:40 +0100420 else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
Paul Elliottebf91632021-07-22 17:54:42 +0100421 {
422 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
423
Paul Elliott33746aa2021-09-15 16:40:40 +0100424 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
425 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100426 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100427
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100428 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100429 {
430 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100431 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100432
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100433 for( part_offset = 0, part_count = 0;
434 part_offset < additional_data->len;
435 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100436 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100437 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100438 {
Paul Elliott329d5382021-07-22 17:10:45 +0100439 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100440 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100441 else if( additional_data->len - part_offset < ad_part_len )
442 {
443 part_length = additional_data->len - part_offset;
444 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100445 else
446 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100447 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100448 }
449
450 PSA_ASSERT( psa_aead_update_ad( &operation,
451 additional_data->x + part_offset,
452 part_length ) );
453
Paul Elliottd3f82412021-06-16 16:52:21 +0100454 }
455 }
456 else
457 {
458 /* Pass additional data in one go. */
459 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
460 additional_data->len ) );
461 }
462
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100463 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100464 {
465 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100466 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100467 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100468 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100469
470 ASSERT_ALLOC( part_data, part_data_size );
471
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100472 for( part_offset = 0, part_count = 0;
473 part_offset < data_true_size;
474 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100475 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100476 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100477 {
Paul Elliott329d5382021-07-22 17:10:45 +0100478 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100479 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100480 else if( ( data_true_size - part_offset ) < data_part_len )
481 {
482 part_length = ( data_true_size - part_offset );
483 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100484 else
485 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100486 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100487 }
488
489 PSA_ASSERT( psa_aead_update( &operation,
490 ( input_data->x + part_offset ),
491 part_length, part_data,
492 part_data_size,
493 &output_part_length ) );
494
495 if( output_data && output_part_length )
496 {
497 memcpy( ( output_data + part_offset ), part_data,
498 output_part_length );
499 }
500
Paul Elliottd3f82412021-06-16 16:52:21 +0100501 output_length += output_part_length;
502 }
503 }
504 else
505 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100506 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100507 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100508 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100509 output_size, &output_length ) );
510 }
511
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100512 if( is_encrypt )
513 PSA_ASSERT( psa_aead_finish( &operation, final_data,
514 final_output_size,
515 &output_part_length,
516 tag_buffer, tag_length,
517 &tag_size ) );
518 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100519 {
Paul Elliott9961a662021-09-17 19:19:02 +0100520 PSA_ASSERT( psa_aead_verify( &operation, final_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100521 final_output_size,
522 &output_part_length,
523 ( input_data->x + data_true_size ),
Paul Elliott9961a662021-09-17 19:19:02 +0100524 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100525 }
526
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100527 if( output_data && output_part_length )
528 memcpy( ( output_data + output_length ), final_data,
529 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100530
531 output_length += output_part_length;
532
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100533
534 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
535 * should be exact.*/
536 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100537 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100538 TEST_EQUAL( tag_length, tag_size );
539
540 if( output_data && tag_length )
541 memcpy( ( output_data + output_length ), tag_buffer,
542 tag_length );
543
544 output_length += tag_length;
545
546 TEST_EQUAL( output_length,
547 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
548 input_data->len ) );
549 TEST_ASSERT( output_length <=
550 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
551 }
552 else
553 {
554 TEST_EQUAL( output_length,
555 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
556 input_data->len ) );
557 TEST_ASSERT( output_length <=
558 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100559 }
560
Paul Elliottd3f82412021-06-16 16:52:21 +0100561
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100562 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100563 output_data, output_length );
564
Paul Elliottd3f82412021-06-16 16:52:21 +0100565
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100566 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100567
568exit:
569 psa_destroy_key( key );
570 psa_aead_abort( &operation );
571 mbedtls_free( output_data );
572 mbedtls_free( part_data );
573 mbedtls_free( final_data );
574 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100575
576 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100577}
578
Gilles Peskinee59236f2018-01-27 23:32:46 +0100579/* END_HEADER */
580
581/* BEGIN_DEPENDENCIES
582 * depends_on:MBEDTLS_PSA_CRYPTO_C
583 * END_DEPENDENCIES
584 */
585
586/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200587void static_checks( )
588{
589 size_t max_truncated_mac_size =
590 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
591
592 /* Check that the length for a truncated MAC always fits in the algorithm
593 * encoding. The shifted mask is the maximum truncated value. The
594 * untruncated algorithm may be one byte larger. */
595 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
596}
597/* END_CASE */
598
599/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200600void import_with_policy( int type_arg,
601 int usage_arg, int alg_arg,
602 int expected_status_arg )
603{
604 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
605 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200606 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200607 psa_key_type_t type = type_arg;
608 psa_key_usage_t usage = usage_arg;
609 psa_algorithm_t alg = alg_arg;
610 psa_status_t expected_status = expected_status_arg;
611 const uint8_t key_material[16] = {0};
612 psa_status_t status;
613
614 PSA_ASSERT( psa_crypto_init( ) );
615
616 psa_set_key_type( &attributes, type );
617 psa_set_key_usage_flags( &attributes, usage );
618 psa_set_key_algorithm( &attributes, alg );
619
620 status = psa_import_key( &attributes,
621 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200622 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200623 TEST_EQUAL( status, expected_status );
624 if( status != PSA_SUCCESS )
625 goto exit;
626
Ronald Cron5425a212020-08-04 14:58:35 +0200627 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200628 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
629 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
630 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200631 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200632
Ronald Cron5425a212020-08-04 14:58:35 +0200633 PSA_ASSERT( psa_destroy_key( key ) );
634 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200635
636exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100637 /*
638 * Key attributes may have been returned by psa_get_key_attributes()
639 * thus reset them as required.
640 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200641 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100642
643 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200644 PSA_DONE( );
645}
646/* END_CASE */
647
648/* BEGIN_CASE */
649void import_with_data( data_t *data, int type_arg,
650 int attr_bits_arg,
651 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200652{
653 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
654 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200655 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200656 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200657 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200658 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100659 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100660
Gilles Peskine8817f612018-12-18 00:18:46 +0100661 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100662
Gilles Peskine4747d192019-04-17 15:05:45 +0200663 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200664 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200665
Ronald Cron5425a212020-08-04 14:58:35 +0200666 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100667 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200668 if( status != PSA_SUCCESS )
669 goto exit;
670
Ronald Cron5425a212020-08-04 14:58:35 +0200671 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200672 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200673 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200674 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200675 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200676
Ronald Cron5425a212020-08-04 14:58:35 +0200677 PSA_ASSERT( psa_destroy_key( key ) );
678 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100679
680exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100681 /*
682 * Key attributes may have been returned by psa_get_key_attributes()
683 * thus reset them as required.
684 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200685 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100686
687 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200688 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100689}
690/* END_CASE */
691
692/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200693void import_large_key( int type_arg, int byte_size_arg,
694 int expected_status_arg )
695{
696 psa_key_type_t type = type_arg;
697 size_t byte_size = byte_size_arg;
698 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
699 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200700 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200701 psa_status_t status;
702 uint8_t *buffer = NULL;
703 size_t buffer_size = byte_size + 1;
704 size_t n;
705
Steven Cooreman69967ce2021-01-18 18:01:08 +0100706 /* Skip the test case if the target running the test cannot
707 * accomodate large keys due to heap size constraints */
708 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200709 memset( buffer, 'K', byte_size );
710
711 PSA_ASSERT( psa_crypto_init( ) );
712
713 /* Try importing the key */
714 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
715 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200716 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100717 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200718 TEST_EQUAL( status, expected_status );
719
720 if( status == PSA_SUCCESS )
721 {
Ronald Cron5425a212020-08-04 14:58:35 +0200722 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200723 TEST_EQUAL( psa_get_key_type( &attributes ), type );
724 TEST_EQUAL( psa_get_key_bits( &attributes ),
725 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200726 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200727 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200728 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200729 for( n = 0; n < byte_size; n++ )
730 TEST_EQUAL( buffer[n], 'K' );
731 for( n = byte_size; n < buffer_size; n++ )
732 TEST_EQUAL( buffer[n], 0 );
733 }
734
735exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100736 /*
737 * Key attributes may have been returned by psa_get_key_attributes()
738 * thus reset them as required.
739 */
740 psa_reset_key_attributes( &attributes );
741
Ronald Cron5425a212020-08-04 14:58:35 +0200742 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200743 PSA_DONE( );
744 mbedtls_free( buffer );
745}
746/* END_CASE */
747
748/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200749void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
750{
Ronald Cron5425a212020-08-04 14:58:35 +0200751 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200752 size_t bits = bits_arg;
753 psa_status_t expected_status = expected_status_arg;
754 psa_status_t status;
755 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200756 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200757 size_t buffer_size = /* Slight overapproximations */
758 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200759 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200760 unsigned char *p;
761 int ret;
762 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200763 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200764
Gilles Peskine8817f612018-12-18 00:18:46 +0100765 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200766 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200767
768 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
769 bits, keypair ) ) >= 0 );
770 length = ret;
771
772 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200773 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200774 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100775 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200776
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200777 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200778 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200779
780exit:
781 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200782 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200783}
784/* END_CASE */
785
786/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300787void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300788 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200789 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100790 int expected_bits,
791 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200792 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100793 int canonical_input )
794{
Ronald Cron5425a212020-08-04 14:58:35 +0200795 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100796 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200797 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200798 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100799 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100800 unsigned char *exported = NULL;
801 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100802 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100803 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100804 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200805 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200806 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100807
Moran Pekercb088e72018-07-17 17:36:59 +0300808 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200809 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100810 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200811 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100812 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100813
Gilles Peskine4747d192019-04-17 15:05:45 +0200814 psa_set_key_usage_flags( &attributes, usage_arg );
815 psa_set_key_algorithm( &attributes, alg );
816 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700817
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100818 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200819 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100820
821 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200822 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200823 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
824 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200825 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100826
827 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200828 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100829 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100830
831 /* The exported length must be set by psa_export_key() to a value between 0
832 * and export_size. On errors, the exported length must be 0. */
833 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
834 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
835 TEST_ASSERT( exported_length <= export_size );
836
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200837 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200838 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100839 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200840 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100841 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100842 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200843 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100844
Gilles Peskineea38a922021-02-13 00:05:16 +0100845 /* Run sanity checks on the exported key. For non-canonical inputs,
846 * this validates the canonical representations. For canonical inputs,
847 * this doesn't directly validate the implementation, but it still helps
848 * by cross-validating the test data with the sanity check code. */
849 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200850 goto exit;
851
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100852 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200853 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100854 else
855 {
Ronald Cron5425a212020-08-04 14:58:35 +0200856 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200857 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200858 &key2 ) );
859 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100860 reexported,
861 export_size,
862 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200863 ASSERT_COMPARE( exported, exported_length,
864 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200865 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100866 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100867 TEST_ASSERT( exported_length <=
868 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
869 psa_get_key_bits( &got_attributes ) ) );
870 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100871
872destroy:
873 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200874 PSA_ASSERT( psa_destroy_key( key ) );
875 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100876
877exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100878 /*
879 * Key attributes may have been returned by psa_get_key_attributes()
880 * thus reset them as required.
881 */
882 psa_reset_key_attributes( &got_attributes );
883
itayzafrir3e02b3b2018-06-12 17:06:52 +0300884 mbedtls_free( exported );
885 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200886 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100887}
888/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100889
Moran Pekerf709f4a2018-06-06 17:26:04 +0300890/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300891void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200892 int type_arg,
893 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100894 int export_size_delta,
895 int expected_export_status_arg,
896 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300897{
Ronald Cron5425a212020-08-04 14:58:35 +0200898 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300899 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200900 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200901 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300902 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300903 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100904 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100905 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200906 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300907
Gilles Peskine8817f612018-12-18 00:18:46 +0100908 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300909
Gilles Peskine4747d192019-04-17 15:05:45 +0200910 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
911 psa_set_key_algorithm( &attributes, alg );
912 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300913
914 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200915 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300916
Gilles Peskine49c25912018-10-29 15:15:31 +0100917 /* Export the public key */
918 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200919 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200920 exported, export_size,
921 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100922 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100923 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100924 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200925 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100926 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200927 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200928 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100929 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100930 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100931 TEST_ASSERT( expected_public_key->len <=
932 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
933 TEST_ASSERT( expected_public_key->len <=
934 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100935 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
936 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100937 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300938
939exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100940 /*
941 * Key attributes may have been returned by psa_get_key_attributes()
942 * thus reset them as required.
943 */
944 psa_reset_key_attributes( &attributes );
945
itayzafrir3e02b3b2018-06-12 17:06:52 +0300946 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200947 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200948 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300949}
950/* END_CASE */
951
Gilles Peskine20035e32018-02-03 22:44:14 +0100952/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200953void import_and_exercise_key( data_t *data,
954 int type_arg,
955 int bits_arg,
956 int alg_arg )
957{
Ronald Cron5425a212020-08-04 14:58:35 +0200958 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200959 psa_key_type_t type = type_arg;
960 size_t bits = bits_arg;
961 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100962 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200963 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200964 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200965
Gilles Peskine8817f612018-12-18 00:18:46 +0100966 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200967
Gilles Peskine4747d192019-04-17 15:05:45 +0200968 psa_set_key_usage_flags( &attributes, usage );
969 psa_set_key_algorithm( &attributes, alg );
970 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200971
972 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200973 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200974
975 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200976 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200977 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
978 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200979
980 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100981 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200982 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200983
Ronald Cron5425a212020-08-04 14:58:35 +0200984 PSA_ASSERT( psa_destroy_key( key ) );
985 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200986
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200987exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100988 /*
989 * Key attributes may have been returned by psa_get_key_attributes()
990 * thus reset them as required.
991 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200992 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100993
994 psa_reset_key_attributes( &attributes );
995 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200996 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200997}
998/* END_CASE */
999
1000/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001001void effective_key_attributes( int type_arg, int expected_type_arg,
1002 int bits_arg, int expected_bits_arg,
1003 int usage_arg, int expected_usage_arg,
1004 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001005{
Ronald Cron5425a212020-08-04 14:58:35 +02001006 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001007 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001008 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001009 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001010 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001011 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001012 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001013 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001014 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001015 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001016
Gilles Peskine8817f612018-12-18 00:18:46 +01001017 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001018
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001019 psa_set_key_usage_flags( &attributes, usage );
1020 psa_set_key_algorithm( &attributes, alg );
1021 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001022 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001023
Ronald Cron5425a212020-08-04 14:58:35 +02001024 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001025 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001026
Ronald Cron5425a212020-08-04 14:58:35 +02001027 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001028 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1029 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1030 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1031 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001032
1033exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001034 /*
1035 * Key attributes may have been returned by psa_get_key_attributes()
1036 * thus reset them as required.
1037 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001038 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001039
1040 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001041 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001042}
1043/* END_CASE */
1044
1045/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001046void check_key_policy( int type_arg, int bits_arg,
1047 int usage_arg, int alg_arg )
1048{
1049 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1050 usage_arg, usage_arg, alg_arg, alg_arg );
1051 goto exit;
1052}
1053/* END_CASE */
1054
1055/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001056void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001057{
1058 /* Test each valid way of initializing the object, except for `= {0}`, as
1059 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1060 * though it's OK by the C standard. We could test for this, but we'd need
1061 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001062 psa_key_attributes_t func = psa_key_attributes_init( );
1063 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1064 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001065
1066 memset( &zero, 0, sizeof( zero ) );
1067
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001068 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1069 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1070 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001071
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001072 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1073 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1074 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1075
1076 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1077 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1078 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1079
1080 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1081 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1082 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1083
1084 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1085 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1086 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001087}
1088/* END_CASE */
1089
1090/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001091void mac_key_policy( int policy_usage,
1092 int policy_alg,
1093 int key_type,
1094 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001095 int exercise_alg,
1096 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001097{
Ronald Cron5425a212020-08-04 14:58:35 +02001098 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001099 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001100 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001101 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001102 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001103 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001104
Gilles Peskine8817f612018-12-18 00:18:46 +01001105 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001106
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001107 psa_set_key_usage_flags( &attributes, policy_usage );
1108 psa_set_key_algorithm( &attributes, policy_alg );
1109 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001110
Gilles Peskine049c7532019-05-15 20:22:09 +02001111 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001112 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001113
Ronald Cron5425a212020-08-04 14:58:35 +02001114 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001115 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001116 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001117 else
1118 TEST_EQUAL( status, expected_status );
1119
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001120 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001121
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001122 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001123 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001124 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001125 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001126 else
1127 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001128
1129exit:
1130 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001131 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001132 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001133}
1134/* END_CASE */
1135
1136/* BEGIN_CASE */
1137void cipher_key_policy( int policy_usage,
1138 int policy_alg,
1139 int key_type,
1140 data_t *key_data,
1141 int exercise_alg )
1142{
Ronald Cron5425a212020-08-04 14:58:35 +02001143 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001144 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001145 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001146 psa_status_t status;
1147
Gilles Peskine8817f612018-12-18 00:18:46 +01001148 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001149
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001150 psa_set_key_usage_flags( &attributes, policy_usage );
1151 psa_set_key_algorithm( &attributes, policy_alg );
1152 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001153
Gilles Peskine049c7532019-05-15 20:22:09 +02001154 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001155 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001156
Ronald Cron5425a212020-08-04 14:58:35 +02001157 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001158 if( policy_alg == exercise_alg &&
1159 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001160 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001161 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001162 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001163 psa_cipher_abort( &operation );
1164
Ronald Cron5425a212020-08-04 14:58:35 +02001165 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001166 if( policy_alg == exercise_alg &&
1167 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001168 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001169 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001170 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001171
1172exit:
1173 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001174 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001175 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001176}
1177/* END_CASE */
1178
1179/* BEGIN_CASE */
1180void aead_key_policy( int policy_usage,
1181 int policy_alg,
1182 int key_type,
1183 data_t *key_data,
1184 int nonce_length_arg,
1185 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001186 int exercise_alg,
1187 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001188{
Ronald Cron5425a212020-08-04 14:58:35 +02001189 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001190 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001191 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001192 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001193 unsigned char nonce[16] = {0};
1194 size_t nonce_length = nonce_length_arg;
1195 unsigned char tag[16];
1196 size_t tag_length = tag_length_arg;
1197 size_t output_length;
1198
1199 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1200 TEST_ASSERT( tag_length <= sizeof( tag ) );
1201
Gilles Peskine8817f612018-12-18 00:18:46 +01001202 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001203
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001204 psa_set_key_usage_flags( &attributes, policy_usage );
1205 psa_set_key_algorithm( &attributes, policy_alg );
1206 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001207
Gilles Peskine049c7532019-05-15 20:22:09 +02001208 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001209 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001210
Ronald Cron5425a212020-08-04 14:58:35 +02001211 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001212 nonce, nonce_length,
1213 NULL, 0,
1214 NULL, 0,
1215 tag, tag_length,
1216 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001217 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1218 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001219 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001220 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001221
1222 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001223 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001224 nonce, nonce_length,
1225 NULL, 0,
1226 tag, tag_length,
1227 NULL, 0,
1228 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001229 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1230 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1231 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001232 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001233 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001234 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001235
1236exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001237 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001238 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001239}
1240/* END_CASE */
1241
1242/* BEGIN_CASE */
1243void asymmetric_encryption_key_policy( int policy_usage,
1244 int policy_alg,
1245 int key_type,
1246 data_t *key_data,
1247 int exercise_alg )
1248{
Ronald Cron5425a212020-08-04 14:58:35 +02001249 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001250 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001251 psa_status_t status;
1252 size_t key_bits;
1253 size_t buffer_length;
1254 unsigned char *buffer = NULL;
1255 size_t output_length;
1256
Gilles Peskine8817f612018-12-18 00:18:46 +01001257 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001258
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001259 psa_set_key_usage_flags( &attributes, policy_usage );
1260 psa_set_key_algorithm( &attributes, policy_alg );
1261 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001262
Gilles Peskine049c7532019-05-15 20:22:09 +02001263 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001264 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001265
Ronald Cron5425a212020-08-04 14:58:35 +02001266 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001267 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001268 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1269 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001270 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001271
Ronald Cron5425a212020-08-04 14:58:35 +02001272 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001273 NULL, 0,
1274 NULL, 0,
1275 buffer, buffer_length,
1276 &output_length );
1277 if( policy_alg == exercise_alg &&
1278 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001279 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001280 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001281 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001282
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001283 if( buffer_length != 0 )
1284 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001285 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001286 buffer, buffer_length,
1287 NULL, 0,
1288 buffer, buffer_length,
1289 &output_length );
1290 if( policy_alg == exercise_alg &&
1291 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001292 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001293 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001294 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001295
1296exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001297 /*
1298 * Key attributes may have been returned by psa_get_key_attributes()
1299 * thus reset them as required.
1300 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001301 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001302
1303 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001304 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001305 mbedtls_free( buffer );
1306}
1307/* END_CASE */
1308
1309/* BEGIN_CASE */
1310void asymmetric_signature_key_policy( int policy_usage,
1311 int policy_alg,
1312 int key_type,
1313 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001314 int exercise_alg,
1315 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001316{
Ronald Cron5425a212020-08-04 14:58:35 +02001317 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001318 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001319 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001320 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1321 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1322 * compatible with the policy and `payload_length_arg` is supposed to be
1323 * a valid input length to sign. If `payload_length_arg <= 0`,
1324 * `exercise_alg` is supposed to be forbidden by the policy. */
1325 int compatible_alg = payload_length_arg > 0;
1326 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001327 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001328 size_t signature_length;
1329
Gilles Peskine8817f612018-12-18 00:18:46 +01001330 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001331
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001332 psa_set_key_usage_flags( &attributes, policy_usage );
1333 psa_set_key_algorithm( &attributes, policy_alg );
1334 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001335
Gilles Peskine049c7532019-05-15 20:22:09 +02001336 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001337 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001338
Ronald Cron5425a212020-08-04 14:58:35 +02001339 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001340 payload, payload_length,
1341 signature, sizeof( signature ),
1342 &signature_length );
1343 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001344 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001345 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001346 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001347
1348 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001349 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001350 payload, payload_length,
1351 signature, sizeof( signature ) );
1352 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001353 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001354 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001355 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001356
1357exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001358 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001359 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001360}
1361/* END_CASE */
1362
Janos Follathba3fab92019-06-11 14:50:16 +01001363/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001364void derive_key_policy( int policy_usage,
1365 int policy_alg,
1366 int key_type,
1367 data_t *key_data,
1368 int exercise_alg )
1369{
Ronald Cron5425a212020-08-04 14:58:35 +02001370 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001371 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001372 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001373 psa_status_t status;
1374
Gilles Peskine8817f612018-12-18 00:18:46 +01001375 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001376
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001377 psa_set_key_usage_flags( &attributes, policy_usage );
1378 psa_set_key_algorithm( &attributes, policy_alg );
1379 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001380
Gilles Peskine049c7532019-05-15 20:22:09 +02001381 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001382 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001383
Janos Follathba3fab92019-06-11 14:50:16 +01001384 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1385
1386 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1387 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001388 {
Janos Follathba3fab92019-06-11 14:50:16 +01001389 PSA_ASSERT( psa_key_derivation_input_bytes(
1390 &operation,
1391 PSA_KEY_DERIVATION_INPUT_SEED,
1392 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001393 }
Janos Follathba3fab92019-06-11 14:50:16 +01001394
1395 status = psa_key_derivation_input_key( &operation,
1396 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001397 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001398
Gilles Peskineea0fb492018-07-12 17:17:20 +02001399 if( policy_alg == exercise_alg &&
1400 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001401 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001402 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001403 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001404
1405exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001406 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001407 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001408 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001409}
1410/* END_CASE */
1411
1412/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001413void agreement_key_policy( int policy_usage,
1414 int policy_alg,
1415 int key_type_arg,
1416 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001417 int exercise_alg,
1418 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001419{
Ronald Cron5425a212020-08-04 14:58:35 +02001420 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001421 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001422 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001423 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001424 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001425 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001426
Gilles Peskine8817f612018-12-18 00:18:46 +01001427 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001428
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001429 psa_set_key_usage_flags( &attributes, policy_usage );
1430 psa_set_key_algorithm( &attributes, policy_alg );
1431 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001432
Gilles Peskine049c7532019-05-15 20:22:09 +02001433 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001434 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001435
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001436 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001437 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001438
Steven Cooremance48e852020-10-05 16:02:45 +02001439 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001440
1441exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001442 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001443 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001444 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001445}
1446/* END_CASE */
1447
1448/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001449void key_policy_alg2( int key_type_arg, data_t *key_data,
1450 int usage_arg, int alg_arg, int alg2_arg )
1451{
Ronald Cron5425a212020-08-04 14:58:35 +02001452 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001453 psa_key_type_t key_type = key_type_arg;
1454 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1455 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1456 psa_key_usage_t usage = usage_arg;
1457 psa_algorithm_t alg = alg_arg;
1458 psa_algorithm_t alg2 = alg2_arg;
1459
1460 PSA_ASSERT( psa_crypto_init( ) );
1461
1462 psa_set_key_usage_flags( &attributes, usage );
1463 psa_set_key_algorithm( &attributes, alg );
1464 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1465 psa_set_key_type( &attributes, key_type );
1466 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001467 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001468
Ronald Cron5425a212020-08-04 14:58:35 +02001469 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001470 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1471 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1472 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1473
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001474 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001475 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001476 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001477 goto exit;
1478
1479exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001480 /*
1481 * Key attributes may have been returned by psa_get_key_attributes()
1482 * thus reset them as required.
1483 */
1484 psa_reset_key_attributes( &got_attributes );
1485
Ronald Cron5425a212020-08-04 14:58:35 +02001486 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001487 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001488}
1489/* END_CASE */
1490
1491/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001492void raw_agreement_key_policy( int policy_usage,
1493 int policy_alg,
1494 int key_type_arg,
1495 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001496 int exercise_alg,
1497 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001498{
Ronald Cron5425a212020-08-04 14:58:35 +02001499 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001500 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001501 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001502 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001503 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001504 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001505
1506 PSA_ASSERT( psa_crypto_init( ) );
1507
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001508 psa_set_key_usage_flags( &attributes, policy_usage );
1509 psa_set_key_algorithm( &attributes, policy_alg );
1510 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001511
Gilles Peskine049c7532019-05-15 20:22:09 +02001512 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001513 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001514
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001515 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001516
Steven Cooremance48e852020-10-05 16:02:45 +02001517 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001518
1519exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001520 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001521 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001522 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001523}
1524/* END_CASE */
1525
1526/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001527void copy_success( int source_usage_arg,
1528 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001529 int type_arg, data_t *material,
1530 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001531 int target_usage_arg,
1532 int target_alg_arg, int target_alg2_arg,
1533 int expected_usage_arg,
1534 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001535{
Gilles Peskineca25db92019-04-19 11:43:08 +02001536 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1537 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001538 psa_key_usage_t expected_usage = expected_usage_arg;
1539 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001540 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001541 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1542 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001543 uint8_t *export_buffer = NULL;
1544
Gilles Peskine57ab7212019-01-28 13:03:09 +01001545 PSA_ASSERT( psa_crypto_init( ) );
1546
Gilles Peskineca25db92019-04-19 11:43:08 +02001547 /* Prepare the source key. */
1548 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1549 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001550 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001551 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001552 PSA_ASSERT( psa_import_key( &source_attributes,
1553 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001554 &source_key ) );
1555 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001556
Gilles Peskineca25db92019-04-19 11:43:08 +02001557 /* Prepare the target attributes. */
1558 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001559 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001560 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001561 /* Set volatile lifetime to reset the key identifier to 0. */
1562 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1563 }
1564
Gilles Peskineca25db92019-04-19 11:43:08 +02001565 if( target_usage_arg != -1 )
1566 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1567 if( target_alg_arg != -1 )
1568 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001569 if( target_alg2_arg != -1 )
1570 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001571
1572 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001573 PSA_ASSERT( psa_copy_key( source_key,
1574 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001575
1576 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001577 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001578
1579 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001580 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001581 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1582 psa_get_key_type( &target_attributes ) );
1583 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1584 psa_get_key_bits( &target_attributes ) );
1585 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1586 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001587 TEST_EQUAL( expected_alg2,
1588 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001589 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1590 {
1591 size_t length;
1592 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001593 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001594 material->len, &length ) );
1595 ASSERT_COMPARE( material->x, material->len,
1596 export_buffer, length );
1597 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001598
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001599 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001600 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001601 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001602 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001603
Ronald Cron5425a212020-08-04 14:58:35 +02001604 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001605
1606exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001607 /*
1608 * Source and target key attributes may have been returned by
1609 * psa_get_key_attributes() thus reset them as required.
1610 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001611 psa_reset_key_attributes( &source_attributes );
1612 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001613
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001614 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001615 mbedtls_free( export_buffer );
1616}
1617/* END_CASE */
1618
1619/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001620void copy_fail( int source_usage_arg,
1621 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001622 int type_arg, data_t *material,
1623 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001624 int target_usage_arg,
1625 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001626 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001627 int expected_status_arg )
1628{
1629 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1630 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001631 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1632 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001633 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001634
1635 PSA_ASSERT( psa_crypto_init( ) );
1636
1637 /* Prepare the source key. */
1638 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1639 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001640 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001641 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001642 PSA_ASSERT( psa_import_key( &source_attributes,
1643 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001644 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001645
1646 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001647 psa_set_key_id( &target_attributes, key_id );
1648 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001649 psa_set_key_type( &target_attributes, target_type_arg );
1650 psa_set_key_bits( &target_attributes, target_bits_arg );
1651 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1652 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001653 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001654
1655 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001656 TEST_EQUAL( psa_copy_key( source_key,
1657 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001658 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001659
Ronald Cron5425a212020-08-04 14:58:35 +02001660 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001661
Gilles Peskine4a644642019-05-03 17:14:08 +02001662exit:
1663 psa_reset_key_attributes( &source_attributes );
1664 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001665 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001666}
1667/* END_CASE */
1668
1669/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001670void hash_operation_init( )
1671{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001672 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001673 /* Test each valid way of initializing the object, except for `= {0}`, as
1674 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1675 * though it's OK by the C standard. We could test for this, but we'd need
1676 * to supress the Clang warning for the test. */
1677 psa_hash_operation_t func = psa_hash_operation_init( );
1678 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1679 psa_hash_operation_t zero;
1680
1681 memset( &zero, 0, sizeof( zero ) );
1682
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001683 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001684 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1685 PSA_ERROR_BAD_STATE );
1686 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1687 PSA_ERROR_BAD_STATE );
1688 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1689 PSA_ERROR_BAD_STATE );
1690
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001691 /* A default hash operation should be abortable without error. */
1692 PSA_ASSERT( psa_hash_abort( &func ) );
1693 PSA_ASSERT( psa_hash_abort( &init ) );
1694 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001695}
1696/* END_CASE */
1697
1698/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001699void hash_setup( int alg_arg,
1700 int expected_status_arg )
1701{
1702 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001703 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001704 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001705 psa_status_t status;
1706
Gilles Peskine8817f612018-12-18 00:18:46 +01001707 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001708
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001709 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001710 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001711
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001712 /* Whether setup succeeded or failed, abort must succeed. */
1713 PSA_ASSERT( psa_hash_abort( &operation ) );
1714
1715 /* If setup failed, reproduce the failure, so as to
1716 * test the resulting state of the operation object. */
1717 if( status != PSA_SUCCESS )
1718 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1719
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001720 /* Now the operation object should be reusable. */
1721#if defined(KNOWN_SUPPORTED_HASH_ALG)
1722 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1723 PSA_ASSERT( psa_hash_abort( &operation ) );
1724#endif
1725
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001726exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001727 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001728}
1729/* END_CASE */
1730
1731/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001732void hash_compute_fail( int alg_arg, data_t *input,
1733 int output_size_arg, int expected_status_arg )
1734{
1735 psa_algorithm_t alg = alg_arg;
1736 uint8_t *output = NULL;
1737 size_t output_size = output_size_arg;
1738 size_t output_length = INVALID_EXPORT_LENGTH;
1739 psa_status_t expected_status = expected_status_arg;
1740 psa_status_t status;
1741
1742 ASSERT_ALLOC( output, output_size );
1743
1744 PSA_ASSERT( psa_crypto_init( ) );
1745
1746 status = psa_hash_compute( alg, input->x, input->len,
1747 output, output_size, &output_length );
1748 TEST_EQUAL( status, expected_status );
1749 TEST_ASSERT( output_length <= output_size );
1750
1751exit:
1752 mbedtls_free( output );
1753 PSA_DONE( );
1754}
1755/* END_CASE */
1756
1757/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001758void hash_compare_fail( int alg_arg, data_t *input,
1759 data_t *reference_hash,
1760 int expected_status_arg )
1761{
1762 psa_algorithm_t alg = alg_arg;
1763 psa_status_t expected_status = expected_status_arg;
1764 psa_status_t status;
1765
1766 PSA_ASSERT( psa_crypto_init( ) );
1767
1768 status = psa_hash_compare( alg, input->x, input->len,
1769 reference_hash->x, reference_hash->len );
1770 TEST_EQUAL( status, expected_status );
1771
1772exit:
1773 PSA_DONE( );
1774}
1775/* END_CASE */
1776
1777/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001778void hash_compute_compare( int alg_arg, data_t *input,
1779 data_t *expected_output )
1780{
1781 psa_algorithm_t alg = alg_arg;
1782 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1783 size_t output_length = INVALID_EXPORT_LENGTH;
1784 size_t i;
1785
1786 PSA_ASSERT( psa_crypto_init( ) );
1787
1788 /* Compute with tight buffer */
1789 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001790 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001791 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001792 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001793 ASSERT_COMPARE( output, output_length,
1794 expected_output->x, expected_output->len );
1795
1796 /* Compute with larger buffer */
1797 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1798 output, sizeof( output ),
1799 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001800 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001801 ASSERT_COMPARE( output, output_length,
1802 expected_output->x, expected_output->len );
1803
1804 /* Compare with correct hash */
1805 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1806 output, output_length ) );
1807
1808 /* Compare with trailing garbage */
1809 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1810 output, output_length + 1 ),
1811 PSA_ERROR_INVALID_SIGNATURE );
1812
1813 /* Compare with truncated hash */
1814 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1815 output, output_length - 1 ),
1816 PSA_ERROR_INVALID_SIGNATURE );
1817
1818 /* Compare with corrupted value */
1819 for( i = 0; i < output_length; i++ )
1820 {
Chris Jones9634bb12021-01-20 15:56:42 +00001821 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001822 output[i] ^= 1;
1823 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1824 output, output_length ),
1825 PSA_ERROR_INVALID_SIGNATURE );
1826 output[i] ^= 1;
1827 }
1828
1829exit:
1830 PSA_DONE( );
1831}
1832/* END_CASE */
1833
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001834/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001835void hash_bad_order( )
1836{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001837 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001838 unsigned char input[] = "";
1839 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001840 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001841 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1842 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1843 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001844 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001845 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001846 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001847
Gilles Peskine8817f612018-12-18 00:18:46 +01001848 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001849
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001850 /* Call setup twice in a row. */
1851 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1852 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1853 PSA_ERROR_BAD_STATE );
1854 PSA_ASSERT( psa_hash_abort( &operation ) );
1855
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001856 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001857 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001858 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001859 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001860
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001861 /* Call update after finish. */
1862 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1863 PSA_ASSERT( psa_hash_finish( &operation,
1864 hash, sizeof( hash ), &hash_len ) );
1865 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001866 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001867 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001868
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001869 /* Call verify without calling setup beforehand. */
1870 TEST_EQUAL( psa_hash_verify( &operation,
1871 valid_hash, sizeof( valid_hash ) ),
1872 PSA_ERROR_BAD_STATE );
1873 PSA_ASSERT( psa_hash_abort( &operation ) );
1874
1875 /* Call verify after finish. */
1876 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1877 PSA_ASSERT( psa_hash_finish( &operation,
1878 hash, sizeof( hash ), &hash_len ) );
1879 TEST_EQUAL( psa_hash_verify( &operation,
1880 valid_hash, sizeof( valid_hash ) ),
1881 PSA_ERROR_BAD_STATE );
1882 PSA_ASSERT( psa_hash_abort( &operation ) );
1883
1884 /* Call verify twice in a row. */
1885 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1886 PSA_ASSERT( psa_hash_verify( &operation,
1887 valid_hash, sizeof( valid_hash ) ) );
1888 TEST_EQUAL( psa_hash_verify( &operation,
1889 valid_hash, sizeof( valid_hash ) ),
1890 PSA_ERROR_BAD_STATE );
1891 PSA_ASSERT( psa_hash_abort( &operation ) );
1892
1893 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001894 TEST_EQUAL( psa_hash_finish( &operation,
1895 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001896 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001897 PSA_ASSERT( psa_hash_abort( &operation ) );
1898
1899 /* Call finish twice in a row. */
1900 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1901 PSA_ASSERT( psa_hash_finish( &operation,
1902 hash, sizeof( hash ), &hash_len ) );
1903 TEST_EQUAL( psa_hash_finish( &operation,
1904 hash, sizeof( hash ), &hash_len ),
1905 PSA_ERROR_BAD_STATE );
1906 PSA_ASSERT( psa_hash_abort( &operation ) );
1907
1908 /* Call finish after calling verify. */
1909 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1910 PSA_ASSERT( psa_hash_verify( &operation,
1911 valid_hash, sizeof( valid_hash ) ) );
1912 TEST_EQUAL( psa_hash_finish( &operation,
1913 hash, sizeof( hash ), &hash_len ),
1914 PSA_ERROR_BAD_STATE );
1915 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001916
1917exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001918 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001919}
1920/* END_CASE */
1921
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001922/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001923void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001924{
1925 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001926 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1927 * appended to it */
1928 unsigned char hash[] = {
1929 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1930 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1931 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001932 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001933 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001934
Gilles Peskine8817f612018-12-18 00:18:46 +01001935 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001936
itayzafrir27e69452018-11-01 14:26:34 +02001937 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001938 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001939 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001940 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001941
itayzafrir27e69452018-11-01 14:26:34 +02001942 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001943 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001944 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001945 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001946
itayzafrir27e69452018-11-01 14:26:34 +02001947 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001948 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001949 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001950 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001951
itayzafrirec93d302018-10-18 18:01:10 +03001952exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001953 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001954}
1955/* END_CASE */
1956
Ronald Cronee414c72021-03-18 18:50:08 +01001957/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001958void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001959{
1960 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001961 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001962 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001963 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001964 size_t hash_len;
1965
Gilles Peskine8817f612018-12-18 00:18:46 +01001966 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001967
itayzafrir58028322018-10-25 10:22:01 +03001968 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001969 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001970 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001971 hash, expected_size - 1, &hash_len ),
1972 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001973
1974exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001975 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001976}
1977/* END_CASE */
1978
Ronald Cronee414c72021-03-18 18:50:08 +01001979/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001980void hash_clone_source_state( )
1981{
1982 psa_algorithm_t alg = PSA_ALG_SHA_256;
1983 unsigned char hash[PSA_HASH_MAX_SIZE];
1984 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1985 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1986 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1987 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1988 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1989 size_t hash_len;
1990
1991 PSA_ASSERT( psa_crypto_init( ) );
1992 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1993
1994 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1995 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1996 PSA_ASSERT( psa_hash_finish( &op_finished,
1997 hash, sizeof( hash ), &hash_len ) );
1998 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1999 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2000
2001 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2002 PSA_ERROR_BAD_STATE );
2003
2004 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2005 PSA_ASSERT( psa_hash_finish( &op_init,
2006 hash, sizeof( hash ), &hash_len ) );
2007 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2008 PSA_ASSERT( psa_hash_finish( &op_finished,
2009 hash, sizeof( hash ), &hash_len ) );
2010 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2011 PSA_ASSERT( psa_hash_finish( &op_aborted,
2012 hash, sizeof( hash ), &hash_len ) );
2013
2014exit:
2015 psa_hash_abort( &op_source );
2016 psa_hash_abort( &op_init );
2017 psa_hash_abort( &op_setup );
2018 psa_hash_abort( &op_finished );
2019 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002020 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002021}
2022/* END_CASE */
2023
Ronald Cronee414c72021-03-18 18:50:08 +01002024/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002025void hash_clone_target_state( )
2026{
2027 psa_algorithm_t alg = PSA_ALG_SHA_256;
2028 unsigned char hash[PSA_HASH_MAX_SIZE];
2029 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2030 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2031 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2032 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2033 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2034 size_t hash_len;
2035
2036 PSA_ASSERT( psa_crypto_init( ) );
2037
2038 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2039 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2040 PSA_ASSERT( psa_hash_finish( &op_finished,
2041 hash, sizeof( hash ), &hash_len ) );
2042 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2043 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2044
2045 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2046 PSA_ASSERT( psa_hash_finish( &op_target,
2047 hash, sizeof( hash ), &hash_len ) );
2048
2049 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2050 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2051 PSA_ERROR_BAD_STATE );
2052 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2053 PSA_ERROR_BAD_STATE );
2054
2055exit:
2056 psa_hash_abort( &op_target );
2057 psa_hash_abort( &op_init );
2058 psa_hash_abort( &op_setup );
2059 psa_hash_abort( &op_finished );
2060 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002061 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002062}
2063/* END_CASE */
2064
itayzafrir58028322018-10-25 10:22:01 +03002065/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002066void mac_operation_init( )
2067{
Jaeden Amero252ef282019-02-15 14:05:35 +00002068 const uint8_t input[1] = { 0 };
2069
Jaeden Amero769ce272019-01-04 11:48:03 +00002070 /* Test each valid way of initializing the object, except for `= {0}`, as
2071 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2072 * though it's OK by the C standard. We could test for this, but we'd need
2073 * to supress the Clang warning for the test. */
2074 psa_mac_operation_t func = psa_mac_operation_init( );
2075 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2076 psa_mac_operation_t zero;
2077
2078 memset( &zero, 0, sizeof( zero ) );
2079
Jaeden Amero252ef282019-02-15 14:05:35 +00002080 /* A freshly-initialized MAC operation should not be usable. */
2081 TEST_EQUAL( psa_mac_update( &func,
2082 input, sizeof( input ) ),
2083 PSA_ERROR_BAD_STATE );
2084 TEST_EQUAL( psa_mac_update( &init,
2085 input, sizeof( input ) ),
2086 PSA_ERROR_BAD_STATE );
2087 TEST_EQUAL( psa_mac_update( &zero,
2088 input, sizeof( input ) ),
2089 PSA_ERROR_BAD_STATE );
2090
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002091 /* A default MAC operation should be abortable without error. */
2092 PSA_ASSERT( psa_mac_abort( &func ) );
2093 PSA_ASSERT( psa_mac_abort( &init ) );
2094 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002095}
2096/* END_CASE */
2097
2098/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002099void mac_setup( int key_type_arg,
2100 data_t *key,
2101 int alg_arg,
2102 int expected_status_arg )
2103{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002104 psa_key_type_t key_type = key_type_arg;
2105 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002106 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002107 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002108 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2109#if defined(KNOWN_SUPPORTED_MAC_ALG)
2110 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2111#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002112
Gilles Peskine8817f612018-12-18 00:18:46 +01002113 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002114
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002115 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2116 &operation, &status ) )
2117 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002118 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002119
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002120 /* The operation object should be reusable. */
2121#if defined(KNOWN_SUPPORTED_MAC_ALG)
2122 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2123 smoke_test_key_data,
2124 sizeof( smoke_test_key_data ),
2125 KNOWN_SUPPORTED_MAC_ALG,
2126 &operation, &status ) )
2127 goto exit;
2128 TEST_EQUAL( status, PSA_SUCCESS );
2129#endif
2130
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002131exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002132 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002133}
2134/* END_CASE */
2135
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002136/* 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 +00002137void mac_bad_order( )
2138{
Ronald Cron5425a212020-08-04 14:58:35 +02002139 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002140 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2141 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002142 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002143 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2144 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2145 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002146 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002147 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2148 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2149 size_t sign_mac_length = 0;
2150 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2151 const uint8_t verify_mac[] = {
2152 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2153 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2154 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2155
2156 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002157 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002158 psa_set_key_algorithm( &attributes, alg );
2159 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002160
Ronald Cron5425a212020-08-04 14:58:35 +02002161 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2162 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002163
Jaeden Amero252ef282019-02-15 14:05:35 +00002164 /* Call update without calling setup beforehand. */
2165 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2166 PSA_ERROR_BAD_STATE );
2167 PSA_ASSERT( psa_mac_abort( &operation ) );
2168
2169 /* Call sign finish without calling setup beforehand. */
2170 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2171 &sign_mac_length),
2172 PSA_ERROR_BAD_STATE );
2173 PSA_ASSERT( psa_mac_abort( &operation ) );
2174
2175 /* Call verify finish without calling setup beforehand. */
2176 TEST_EQUAL( psa_mac_verify_finish( &operation,
2177 verify_mac, sizeof( verify_mac ) ),
2178 PSA_ERROR_BAD_STATE );
2179 PSA_ASSERT( psa_mac_abort( &operation ) );
2180
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002181 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002182 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2183 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002184 PSA_ERROR_BAD_STATE );
2185 PSA_ASSERT( psa_mac_abort( &operation ) );
2186
Jaeden Amero252ef282019-02-15 14:05:35 +00002187 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002188 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002189 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2190 PSA_ASSERT( psa_mac_sign_finish( &operation,
2191 sign_mac, sizeof( sign_mac ),
2192 &sign_mac_length ) );
2193 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2194 PSA_ERROR_BAD_STATE );
2195 PSA_ASSERT( psa_mac_abort( &operation ) );
2196
2197 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002198 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002199 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2200 PSA_ASSERT( psa_mac_verify_finish( &operation,
2201 verify_mac, sizeof( verify_mac ) ) );
2202 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2203 PSA_ERROR_BAD_STATE );
2204 PSA_ASSERT( psa_mac_abort( &operation ) );
2205
2206 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002207 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002208 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2209 PSA_ASSERT( psa_mac_sign_finish( &operation,
2210 sign_mac, sizeof( sign_mac ),
2211 &sign_mac_length ) );
2212 TEST_EQUAL( psa_mac_sign_finish( &operation,
2213 sign_mac, sizeof( sign_mac ),
2214 &sign_mac_length ),
2215 PSA_ERROR_BAD_STATE );
2216 PSA_ASSERT( psa_mac_abort( &operation ) );
2217
2218 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002219 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002220 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2221 PSA_ASSERT( psa_mac_verify_finish( &operation,
2222 verify_mac, sizeof( verify_mac ) ) );
2223 TEST_EQUAL( psa_mac_verify_finish( &operation,
2224 verify_mac, sizeof( verify_mac ) ),
2225 PSA_ERROR_BAD_STATE );
2226 PSA_ASSERT( psa_mac_abort( &operation ) );
2227
2228 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002229 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002230 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2231 TEST_EQUAL( psa_mac_verify_finish( &operation,
2232 verify_mac, sizeof( verify_mac ) ),
2233 PSA_ERROR_BAD_STATE );
2234 PSA_ASSERT( psa_mac_abort( &operation ) );
2235
2236 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002237 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002238 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2239 TEST_EQUAL( psa_mac_sign_finish( &operation,
2240 sign_mac, sizeof( sign_mac ),
2241 &sign_mac_length ),
2242 PSA_ERROR_BAD_STATE );
2243 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002244
Ronald Cron5425a212020-08-04 14:58:35 +02002245 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002246
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002247exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002248 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002249}
2250/* END_CASE */
2251
2252/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002253void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002254 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002255 int alg_arg,
2256 data_t *input,
2257 data_t *expected_mac )
2258{
Ronald Cron5425a212020-08-04 14:58:35 +02002259 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002260 psa_key_type_t key_type = key_type_arg;
2261 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002262 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002263 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002264 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002265 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002266 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002267 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002268 const size_t output_sizes_to_test[] = {
2269 0,
2270 1,
2271 expected_mac->len - 1,
2272 expected_mac->len,
2273 expected_mac->len + 1,
2274 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002275
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002276 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002277 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002278 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002279
Gilles Peskine8817f612018-12-18 00:18:46 +01002280 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002281
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002282 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002283 psa_set_key_algorithm( &attributes, alg );
2284 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002285
Ronald Cron5425a212020-08-04 14:58:35 +02002286 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2287 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002288
Gilles Peskine8b356b52020-08-25 23:44:59 +02002289 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2290 {
2291 const size_t output_size = output_sizes_to_test[i];
2292 psa_status_t expected_status =
2293 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2294 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002295
Chris Jones9634bb12021-01-20 15:56:42 +00002296 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002297 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002298
Gilles Peskine8b356b52020-08-25 23:44:59 +02002299 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002300 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002301 PSA_ASSERT( psa_mac_update( &operation,
2302 input->x, input->len ) );
2303 TEST_EQUAL( psa_mac_sign_finish( &operation,
2304 actual_mac, output_size,
2305 &mac_length ),
2306 expected_status );
2307 PSA_ASSERT( psa_mac_abort( &operation ) );
2308
2309 if( expected_status == PSA_SUCCESS )
2310 {
2311 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2312 actual_mac, mac_length );
2313 }
2314 mbedtls_free( actual_mac );
2315 actual_mac = NULL;
2316 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002317
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002318exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002319 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002320 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002321 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002322 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002323}
2324/* END_CASE */
2325
2326/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002327void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002328 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002329 int alg_arg,
2330 data_t *input,
2331 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002332{
Ronald Cron5425a212020-08-04 14:58:35 +02002333 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002334 psa_key_type_t key_type = key_type_arg;
2335 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002336 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002337 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002338 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002339
Gilles Peskine69c12672018-06-28 00:07:19 +02002340 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2341
Gilles Peskine8817f612018-12-18 00:18:46 +01002342 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002343
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002344 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002345 psa_set_key_algorithm( &attributes, alg );
2346 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002347
Ronald Cron5425a212020-08-04 14:58:35 +02002348 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2349 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002350
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002351 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002352 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002353 PSA_ASSERT( psa_mac_update( &operation,
2354 input->x, input->len ) );
2355 PSA_ASSERT( psa_mac_verify_finish( &operation,
2356 expected_mac->x,
2357 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002358
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002359 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002360 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002361 PSA_ASSERT( psa_mac_update( &operation,
2362 input->x, input->len ) );
2363 TEST_EQUAL( psa_mac_verify_finish( &operation,
2364 expected_mac->x,
2365 expected_mac->len - 1 ),
2366 PSA_ERROR_INVALID_SIGNATURE );
2367
2368 /* Test a MAC that's too long. */
2369 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2370 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002371 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002372 PSA_ASSERT( psa_mac_update( &operation,
2373 input->x, input->len ) );
2374 TEST_EQUAL( psa_mac_verify_finish( &operation,
2375 perturbed_mac,
2376 expected_mac->len + 1 ),
2377 PSA_ERROR_INVALID_SIGNATURE );
2378
2379 /* Test changing one byte. */
2380 for( size_t i = 0; i < expected_mac->len; i++ )
2381 {
Chris Jones9634bb12021-01-20 15:56:42 +00002382 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002383 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002384 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002385 PSA_ASSERT( psa_mac_update( &operation,
2386 input->x, input->len ) );
2387 TEST_EQUAL( psa_mac_verify_finish( &operation,
2388 perturbed_mac,
2389 expected_mac->len ),
2390 PSA_ERROR_INVALID_SIGNATURE );
2391 perturbed_mac[i] ^= 1;
2392 }
2393
Gilles Peskine8c9def32018-02-08 10:02:12 +01002394exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002395 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002396 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002397 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002398 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002399}
2400/* END_CASE */
2401
2402/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002403void cipher_operation_init( )
2404{
Jaeden Ameroab439972019-02-15 14:12:05 +00002405 const uint8_t input[1] = { 0 };
2406 unsigned char output[1] = { 0 };
2407 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002408 /* Test each valid way of initializing the object, except for `= {0}`, as
2409 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2410 * though it's OK by the C standard. We could test for this, but we'd need
2411 * to supress the Clang warning for the test. */
2412 psa_cipher_operation_t func = psa_cipher_operation_init( );
2413 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2414 psa_cipher_operation_t zero;
2415
2416 memset( &zero, 0, sizeof( zero ) );
2417
Jaeden Ameroab439972019-02-15 14:12:05 +00002418 /* A freshly-initialized cipher operation should not be usable. */
2419 TEST_EQUAL( psa_cipher_update( &func,
2420 input, sizeof( input ),
2421 output, sizeof( output ),
2422 &output_length ),
2423 PSA_ERROR_BAD_STATE );
2424 TEST_EQUAL( psa_cipher_update( &init,
2425 input, sizeof( input ),
2426 output, sizeof( output ),
2427 &output_length ),
2428 PSA_ERROR_BAD_STATE );
2429 TEST_EQUAL( psa_cipher_update( &zero,
2430 input, sizeof( input ),
2431 output, sizeof( output ),
2432 &output_length ),
2433 PSA_ERROR_BAD_STATE );
2434
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002435 /* A default cipher operation should be abortable without error. */
2436 PSA_ASSERT( psa_cipher_abort( &func ) );
2437 PSA_ASSERT( psa_cipher_abort( &init ) );
2438 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002439}
2440/* END_CASE */
2441
2442/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002443void cipher_setup( int key_type_arg,
2444 data_t *key,
2445 int alg_arg,
2446 int expected_status_arg )
2447{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002448 psa_key_type_t key_type = key_type_arg;
2449 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002450 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002451 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002452 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002453#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002454 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2455#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002456
Gilles Peskine8817f612018-12-18 00:18:46 +01002457 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002458
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002459 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2460 &operation, &status ) )
2461 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002462 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002463
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002464 /* The operation object should be reusable. */
2465#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2466 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2467 smoke_test_key_data,
2468 sizeof( smoke_test_key_data ),
2469 KNOWN_SUPPORTED_CIPHER_ALG,
2470 &operation, &status ) )
2471 goto exit;
2472 TEST_EQUAL( status, PSA_SUCCESS );
2473#endif
2474
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002475exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002476 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002477 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002478}
2479/* END_CASE */
2480
Ronald Cronee414c72021-03-18 18:50:08 +01002481/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002482void cipher_bad_order( )
2483{
Ronald Cron5425a212020-08-04 14:58:35 +02002484 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002485 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2486 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002487 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002488 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002489 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002490 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002491 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2492 0xaa, 0xaa, 0xaa, 0xaa };
2493 const uint8_t text[] = {
2494 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2495 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002496 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002497 size_t length = 0;
2498
2499 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002500 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2501 psa_set_key_algorithm( &attributes, alg );
2502 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002503 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2504 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002505
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002506 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002507 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2508 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002509 PSA_ERROR_BAD_STATE );
2510 PSA_ASSERT( psa_cipher_abort( &operation ) );
2511
2512 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002513 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2514 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002515 PSA_ERROR_BAD_STATE );
2516 PSA_ASSERT( psa_cipher_abort( &operation ) );
2517
Jaeden Ameroab439972019-02-15 14:12:05 +00002518 /* Generate an IV without calling setup beforehand. */
2519 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2520 buffer, sizeof( buffer ),
2521 &length ),
2522 PSA_ERROR_BAD_STATE );
2523 PSA_ASSERT( psa_cipher_abort( &operation ) );
2524
2525 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002526 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002527 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2528 buffer, sizeof( buffer ),
2529 &length ) );
2530 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2531 buffer, sizeof( buffer ),
2532 &length ),
2533 PSA_ERROR_BAD_STATE );
2534 PSA_ASSERT( psa_cipher_abort( &operation ) );
2535
2536 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002537 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002538 PSA_ASSERT( psa_cipher_set_iv( &operation,
2539 iv, sizeof( iv ) ) );
2540 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2541 buffer, sizeof( buffer ),
2542 &length ),
2543 PSA_ERROR_BAD_STATE );
2544 PSA_ASSERT( psa_cipher_abort( &operation ) );
2545
2546 /* Set an IV without calling setup beforehand. */
2547 TEST_EQUAL( psa_cipher_set_iv( &operation,
2548 iv, sizeof( iv ) ),
2549 PSA_ERROR_BAD_STATE );
2550 PSA_ASSERT( psa_cipher_abort( &operation ) );
2551
2552 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002553 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002554 PSA_ASSERT( psa_cipher_set_iv( &operation,
2555 iv, sizeof( iv ) ) );
2556 TEST_EQUAL( psa_cipher_set_iv( &operation,
2557 iv, sizeof( iv ) ),
2558 PSA_ERROR_BAD_STATE );
2559 PSA_ASSERT( psa_cipher_abort( &operation ) );
2560
2561 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002562 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002563 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2564 buffer, sizeof( buffer ),
2565 &length ) );
2566 TEST_EQUAL( psa_cipher_set_iv( &operation,
2567 iv, sizeof( iv ) ),
2568 PSA_ERROR_BAD_STATE );
2569 PSA_ASSERT( psa_cipher_abort( &operation ) );
2570
2571 /* Call update without calling setup beforehand. */
2572 TEST_EQUAL( psa_cipher_update( &operation,
2573 text, sizeof( text ),
2574 buffer, sizeof( buffer ),
2575 &length ),
2576 PSA_ERROR_BAD_STATE );
2577 PSA_ASSERT( psa_cipher_abort( &operation ) );
2578
2579 /* Call update without an IV where an IV is required. */
2580 TEST_EQUAL( psa_cipher_update( &operation,
2581 text, sizeof( text ),
2582 buffer, sizeof( buffer ),
2583 &length ),
2584 PSA_ERROR_BAD_STATE );
2585 PSA_ASSERT( psa_cipher_abort( &operation ) );
2586
2587 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002588 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002589 PSA_ASSERT( psa_cipher_set_iv( &operation,
2590 iv, sizeof( iv ) ) );
2591 PSA_ASSERT( psa_cipher_finish( &operation,
2592 buffer, sizeof( buffer ), &length ) );
2593 TEST_EQUAL( psa_cipher_update( &operation,
2594 text, sizeof( text ),
2595 buffer, sizeof( buffer ),
2596 &length ),
2597 PSA_ERROR_BAD_STATE );
2598 PSA_ASSERT( psa_cipher_abort( &operation ) );
2599
2600 /* Call finish without calling setup beforehand. */
2601 TEST_EQUAL( psa_cipher_finish( &operation,
2602 buffer, sizeof( buffer ), &length ),
2603 PSA_ERROR_BAD_STATE );
2604 PSA_ASSERT( psa_cipher_abort( &operation ) );
2605
2606 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002607 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002608 /* Not calling update means we are encrypting an empty buffer, which is OK
2609 * for cipher modes with padding. */
2610 TEST_EQUAL( psa_cipher_finish( &operation,
2611 buffer, sizeof( buffer ), &length ),
2612 PSA_ERROR_BAD_STATE );
2613 PSA_ASSERT( psa_cipher_abort( &operation ) );
2614
2615 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002616 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002617 PSA_ASSERT( psa_cipher_set_iv( &operation,
2618 iv, sizeof( iv ) ) );
2619 PSA_ASSERT( psa_cipher_finish( &operation,
2620 buffer, sizeof( buffer ), &length ) );
2621 TEST_EQUAL( psa_cipher_finish( &operation,
2622 buffer, sizeof( buffer ), &length ),
2623 PSA_ERROR_BAD_STATE );
2624 PSA_ASSERT( psa_cipher_abort( &operation ) );
2625
Ronald Cron5425a212020-08-04 14:58:35 +02002626 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002627
Jaeden Ameroab439972019-02-15 14:12:05 +00002628exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002629 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002630 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002631}
2632/* END_CASE */
2633
2634/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002635void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002636 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002637 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002638 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002639{
Ronald Cron5425a212020-08-04 14:58:35 +02002640 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002641 psa_status_t status;
2642 psa_key_type_t key_type = key_type_arg;
2643 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002644 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002645 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002646 size_t output_buffer_size = 0;
2647 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002648 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002649 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002650 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002651
Gilles Peskine8817f612018-12-18 00:18:46 +01002652 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002653
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002654 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2655 psa_set_key_algorithm( &attributes, alg );
2656 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002657
Ronald Cron5425a212020-08-04 14:58:35 +02002658 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2659 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002660
Ronald Cron5425a212020-08-04 14:58:35 +02002661 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002662
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002663 if( iv->len > 0 )
2664 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002665 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002666 }
2667
gabor-mezei-armceface22021-01-21 12:26:17 +01002668 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2669 TEST_ASSERT( output_buffer_size <=
2670 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002671 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002672
Gilles Peskine8817f612018-12-18 00:18:46 +01002673 PSA_ASSERT( psa_cipher_update( &operation,
2674 input->x, input->len,
2675 output, output_buffer_size,
2676 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002677 TEST_ASSERT( function_output_length <=
2678 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2679 TEST_ASSERT( function_output_length <=
2680 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002681 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002682
Gilles Peskine50e586b2018-06-08 14:28:46 +02002683 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002684 ( output_buffer_size == 0 ? NULL :
2685 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002686 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002687 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002688 TEST_ASSERT( function_output_length <=
2689 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2690 TEST_ASSERT( function_output_length <=
2691 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002692 total_output_length += function_output_length;
2693
Gilles Peskinefe11b722018-12-18 00:24:04 +01002694 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002695 if( expected_status == PSA_SUCCESS )
2696 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002697 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002698 ASSERT_COMPARE( expected_output->x, expected_output->len,
2699 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002700 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002701
Gilles Peskine50e586b2018-06-08 14:28:46 +02002702exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002703 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002704 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002705 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002706 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002707}
2708/* END_CASE */
2709
2710/* BEGIN_CASE */
2711void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002712 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002713 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002714 int first_part_size_arg,
2715 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002716 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002717{
Ronald Cron5425a212020-08-04 14:58:35 +02002718 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002719 psa_key_type_t key_type = key_type_arg;
2720 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002721 size_t first_part_size = first_part_size_arg;
2722 size_t output1_length = output1_length_arg;
2723 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002724 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002725 size_t output_buffer_size = 0;
2726 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002727 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002728 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002729 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002730
Gilles Peskine8817f612018-12-18 00:18:46 +01002731 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002732
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002733 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2734 psa_set_key_algorithm( &attributes, alg );
2735 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002736
Ronald Cron5425a212020-08-04 14:58:35 +02002737 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2738 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002739
Ronald Cron5425a212020-08-04 14:58:35 +02002740 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002741
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002742 if( iv->len > 0 )
2743 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002744 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002745 }
2746
gabor-mezei-armceface22021-01-21 12:26:17 +01002747 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2748 TEST_ASSERT( output_buffer_size <=
2749 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002750 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002751
Gilles Peskinee0866522019-02-19 19:44:00 +01002752 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002753 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2754 output, output_buffer_size,
2755 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002756 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002757 TEST_ASSERT( function_output_length <=
2758 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2759 TEST_ASSERT( function_output_length <=
2760 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002761 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002762
Gilles Peskine8817f612018-12-18 00:18:46 +01002763 PSA_ASSERT( psa_cipher_update( &operation,
2764 input->x + first_part_size,
2765 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002766 ( output_buffer_size == 0 ? NULL :
2767 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002768 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002769 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002770 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002771 TEST_ASSERT( function_output_length <=
2772 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2773 alg,
2774 input->len - first_part_size ) );
2775 TEST_ASSERT( function_output_length <=
2776 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002777 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002778
Gilles Peskine8817f612018-12-18 00:18:46 +01002779 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002780 ( output_buffer_size == 0 ? NULL :
2781 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002782 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002783 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002784 TEST_ASSERT( function_output_length <=
2785 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2786 TEST_ASSERT( function_output_length <=
2787 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002788 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002789 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002790
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002791 ASSERT_COMPARE( expected_output->x, expected_output->len,
2792 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002793
2794exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002795 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002796 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002797 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002798 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002799}
2800/* END_CASE */
2801
2802/* BEGIN_CASE */
2803void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002804 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002805 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002806 int first_part_size_arg,
2807 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002808 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002809{
Ronald Cron5425a212020-08-04 14:58:35 +02002810 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002811 psa_key_type_t key_type = key_type_arg;
2812 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002813 size_t first_part_size = first_part_size_arg;
2814 size_t output1_length = output1_length_arg;
2815 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002816 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002817 size_t output_buffer_size = 0;
2818 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002819 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002820 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002821 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002822
Gilles Peskine8817f612018-12-18 00:18:46 +01002823 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002824
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002825 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2826 psa_set_key_algorithm( &attributes, alg );
2827 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002828
Ronald Cron5425a212020-08-04 14:58:35 +02002829 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2830 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002831
Ronald Cron5425a212020-08-04 14:58:35 +02002832 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002833
Steven Cooreman177deba2020-09-07 17:14:14 +02002834 if( iv->len > 0 )
2835 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002836 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002837 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002838
gabor-mezei-armceface22021-01-21 12:26:17 +01002839 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2840 TEST_ASSERT( output_buffer_size <=
2841 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002842 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002843
Gilles Peskinee0866522019-02-19 19:44:00 +01002844 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002845 PSA_ASSERT( psa_cipher_update( &operation,
2846 input->x, first_part_size,
2847 output, output_buffer_size,
2848 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002849 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002850 TEST_ASSERT( function_output_length <=
2851 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2852 TEST_ASSERT( function_output_length <=
2853 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002854 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002855
Gilles Peskine8817f612018-12-18 00:18:46 +01002856 PSA_ASSERT( psa_cipher_update( &operation,
2857 input->x + first_part_size,
2858 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002859 ( output_buffer_size == 0 ? NULL :
2860 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002861 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002862 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002863 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002864 TEST_ASSERT( function_output_length <=
2865 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2866 alg,
2867 input->len - first_part_size ) );
2868 TEST_ASSERT( function_output_length <=
2869 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002870 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002871
Gilles Peskine8817f612018-12-18 00:18:46 +01002872 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002873 ( output_buffer_size == 0 ? NULL :
2874 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002875 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002876 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002877 TEST_ASSERT( function_output_length <=
2878 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2879 TEST_ASSERT( function_output_length <=
2880 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002881 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002882 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002883
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002884 ASSERT_COMPARE( expected_output->x, expected_output->len,
2885 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002886
2887exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002888 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002889 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002890 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002891 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002892}
2893/* END_CASE */
2894
Gilles Peskine50e586b2018-06-08 14:28:46 +02002895/* BEGIN_CASE */
2896void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002897 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002898 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002899 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002900{
Ronald Cron5425a212020-08-04 14:58:35 +02002901 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002902 psa_status_t status;
2903 psa_key_type_t key_type = key_type_arg;
2904 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002905 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002906 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002907 size_t output_buffer_size = 0;
2908 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002909 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002910 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002911 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002912
Gilles Peskine8817f612018-12-18 00:18:46 +01002913 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002914
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002915 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2916 psa_set_key_algorithm( &attributes, alg );
2917 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002918
Ronald Cron5425a212020-08-04 14:58:35 +02002919 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2920 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002921
Ronald Cron5425a212020-08-04 14:58:35 +02002922 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002923
Steven Cooreman177deba2020-09-07 17:14:14 +02002924 if( iv->len > 0 )
2925 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002926 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002927 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002928
gabor-mezei-armceface22021-01-21 12:26:17 +01002929 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2930 TEST_ASSERT( output_buffer_size <=
2931 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002932 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002933
Gilles Peskine8817f612018-12-18 00:18:46 +01002934 PSA_ASSERT( psa_cipher_update( &operation,
2935 input->x, input->len,
2936 output, output_buffer_size,
2937 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002938 TEST_ASSERT( function_output_length <=
2939 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2940 TEST_ASSERT( function_output_length <=
2941 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002942 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002943
Gilles Peskine50e586b2018-06-08 14:28:46 +02002944 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002945 ( output_buffer_size == 0 ? NULL :
2946 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002947 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002948 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002949 TEST_ASSERT( function_output_length <=
2950 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2951 TEST_ASSERT( function_output_length <=
2952 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002953 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002954 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002955
2956 if( expected_status == PSA_SUCCESS )
2957 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002958 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002959 ASSERT_COMPARE( expected_output->x, expected_output->len,
2960 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002961 }
2962
Gilles Peskine50e586b2018-06-08 14:28:46 +02002963exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002964 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002965 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002966 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002967 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002968}
2969/* END_CASE */
2970
Gilles Peskine50e586b2018-06-08 14:28:46 +02002971/* BEGIN_CASE */
2972void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002973 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002974 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002975{
Ronald Cron5425a212020-08-04 14:58:35 +02002976 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002977 psa_key_type_t key_type = key_type_arg;
2978 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002979 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002980 size_t iv_size = 16;
2981 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002982 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002983 size_t output1_size = 0;
2984 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002985 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002986 size_t output2_size = 0;
2987 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002988 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002989 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2990 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002991 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002992
Gilles Peskine8817f612018-12-18 00:18:46 +01002993 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002994
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002995 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2996 psa_set_key_algorithm( &attributes, alg );
2997 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002998
Ronald Cron5425a212020-08-04 14:58:35 +02002999 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3000 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003001
Ronald Cron5425a212020-08-04 14:58:35 +02003002 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3003 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003004
Steven Cooreman177deba2020-09-07 17:14:14 +02003005 if( alg != PSA_ALG_ECB_NO_PADDING )
3006 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003007 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3008 iv, iv_size,
3009 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003010 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003011 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3012 TEST_ASSERT( output1_size <=
3013 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003014 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003015
Gilles Peskine8817f612018-12-18 00:18:46 +01003016 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3017 output1, output1_size,
3018 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003019 TEST_ASSERT( output1_length <=
3020 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3021 TEST_ASSERT( output1_length <=
3022 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3023
Gilles Peskine8817f612018-12-18 00:18:46 +01003024 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003025 output1 + output1_length,
3026 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003027 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003028 TEST_ASSERT( function_output_length <=
3029 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3030 TEST_ASSERT( function_output_length <=
3031 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003032
Gilles Peskine048b7f02018-06-08 14:20:49 +02003033 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003034
Gilles Peskine8817f612018-12-18 00:18:46 +01003035 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003036
3037 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003038 TEST_ASSERT( output2_size <=
3039 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3040 TEST_ASSERT( output2_size <=
3041 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003042 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003043
Steven Cooreman177deba2020-09-07 17:14:14 +02003044 if( iv_length > 0 )
3045 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003046 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3047 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003048 }
3049
Gilles Peskine8817f612018-12-18 00:18:46 +01003050 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3051 output2, output2_size,
3052 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003053 TEST_ASSERT( output2_length <=
3054 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
3055 TEST_ASSERT( output2_length <=
3056 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
3057
Gilles Peskine048b7f02018-06-08 14:20:49 +02003058 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003059 PSA_ASSERT( psa_cipher_finish( &operation2,
3060 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003061 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003062 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003063 TEST_ASSERT( function_output_length <=
3064 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3065 TEST_ASSERT( function_output_length <=
3066 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03003067
Gilles Peskine048b7f02018-06-08 14:20:49 +02003068 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003069
Gilles Peskine8817f612018-12-18 00:18:46 +01003070 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003071
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003072 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003073
3074exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003075 psa_cipher_abort( &operation1 );
3076 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003077 mbedtls_free( output1 );
3078 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003079 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003080 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003081}
3082/* END_CASE */
3083
3084/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003085void cipher_verify_output_multipart( int alg_arg,
3086 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003087 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003088 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003089 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003090{
Ronald Cron5425a212020-08-04 14:58:35 +02003091 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003092 psa_key_type_t key_type = key_type_arg;
3093 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003094 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003095 unsigned char iv[16] = {0};
3096 size_t iv_size = 16;
3097 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003098 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003099 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003100 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003101 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003102 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003103 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003104 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003105 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3106 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003107 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003108
Gilles Peskine8817f612018-12-18 00:18:46 +01003109 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003110
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003111 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3112 psa_set_key_algorithm( &attributes, alg );
3113 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003114
Ronald Cron5425a212020-08-04 14:58:35 +02003115 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3116 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003117
Ronald Cron5425a212020-08-04 14:58:35 +02003118 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3119 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003120
Steven Cooreman177deba2020-09-07 17:14:14 +02003121 if( alg != PSA_ALG_ECB_NO_PADDING )
3122 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003123 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3124 iv, iv_size,
3125 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003126 }
3127
gabor-mezei-armceface22021-01-21 12:26:17 +01003128 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3129 TEST_ASSERT( output1_buffer_size <=
3130 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003131 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003132
Gilles Peskinee0866522019-02-19 19:44:00 +01003133 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003134
Gilles Peskine8817f612018-12-18 00:18:46 +01003135 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3136 output1, output1_buffer_size,
3137 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003138 TEST_ASSERT( function_output_length <=
3139 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3140 TEST_ASSERT( function_output_length <=
3141 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003142 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003143
Gilles Peskine8817f612018-12-18 00:18:46 +01003144 PSA_ASSERT( psa_cipher_update( &operation1,
3145 input->x + first_part_size,
3146 input->len - first_part_size,
3147 output1, output1_buffer_size,
3148 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003149 TEST_ASSERT( function_output_length <=
3150 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3151 alg,
3152 input->len - first_part_size ) );
3153 TEST_ASSERT( function_output_length <=
3154 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003155 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003156
Gilles Peskine8817f612018-12-18 00:18:46 +01003157 PSA_ASSERT( psa_cipher_finish( &operation1,
3158 output1 + output1_length,
3159 output1_buffer_size - output1_length,
3160 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003161 TEST_ASSERT( function_output_length <=
3162 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3163 TEST_ASSERT( function_output_length <=
3164 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003165 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003166
Gilles Peskine8817f612018-12-18 00:18:46 +01003167 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003168
Gilles Peskine048b7f02018-06-08 14:20:49 +02003169 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003170 TEST_ASSERT( output2_buffer_size <=
3171 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3172 TEST_ASSERT( output2_buffer_size <=
3173 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003174 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003175
Steven Cooreman177deba2020-09-07 17:14:14 +02003176 if( iv_length > 0 )
3177 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003178 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3179 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003180 }
Moran Pekerded84402018-06-06 16:36:50 +03003181
Gilles Peskine8817f612018-12-18 00:18:46 +01003182 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3183 output2, output2_buffer_size,
3184 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003185 TEST_ASSERT( function_output_length <=
3186 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3187 TEST_ASSERT( function_output_length <=
3188 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003189 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003190
Gilles Peskine8817f612018-12-18 00:18:46 +01003191 PSA_ASSERT( psa_cipher_update( &operation2,
3192 output1 + first_part_size,
3193 output1_length - first_part_size,
3194 output2, output2_buffer_size,
3195 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003196 TEST_ASSERT( function_output_length <=
3197 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3198 alg,
3199 output1_length - first_part_size ) );
3200 TEST_ASSERT( function_output_length <=
3201 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003202 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003203
Gilles Peskine8817f612018-12-18 00:18:46 +01003204 PSA_ASSERT( psa_cipher_finish( &operation2,
3205 output2 + output2_length,
3206 output2_buffer_size - output2_length,
3207 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003208 TEST_ASSERT( function_output_length <=
3209 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3210 TEST_ASSERT( function_output_length <=
3211 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003212 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003213
Gilles Peskine8817f612018-12-18 00:18:46 +01003214 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003215
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003216 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003217
3218exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003219 psa_cipher_abort( &operation1 );
3220 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003221 mbedtls_free( output1 );
3222 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003223 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003224 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003225}
3226/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003227
Gilles Peskine20035e32018-02-03 22:44:14 +01003228/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003229void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003230 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003231 data_t *nonce,
3232 data_t *additional_data,
3233 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003234 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003235{
Ronald Cron5425a212020-08-04 14:58:35 +02003236 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003237 psa_key_type_t key_type = key_type_arg;
3238 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003239 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003240 unsigned char *output_data = NULL;
3241 size_t output_size = 0;
3242 size_t output_length = 0;
3243 unsigned char *output_data2 = NULL;
3244 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003245 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003246 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003247 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003248
Gilles Peskine8817f612018-12-18 00:18:46 +01003249 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003250
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003251 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3252 psa_set_key_algorithm( &attributes, alg );
3253 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003254
Gilles Peskine049c7532019-05-15 20:22:09 +02003255 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003256 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003257 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3258 key_bits = psa_get_key_bits( &attributes );
3259
3260 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3261 alg );
3262 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3263 * should be exact. */
3264 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3265 expected_result != PSA_ERROR_NOT_SUPPORTED )
3266 {
3267 TEST_EQUAL( output_size,
3268 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3269 TEST_ASSERT( output_size <=
3270 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3271 }
3272 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003273
Steven Cooremanf49478b2021-02-15 15:19:25 +01003274 status = psa_aead_encrypt( key, alg,
3275 nonce->x, nonce->len,
3276 additional_data->x,
3277 additional_data->len,
3278 input_data->x, input_data->len,
3279 output_data, output_size,
3280 &output_length );
3281
3282 /* If the operation is not supported, just skip and not fail in case the
3283 * encryption involves a common limitation of cryptography hardwares and
3284 * an alternative implementation. */
3285 if( status == PSA_ERROR_NOT_SUPPORTED )
3286 {
3287 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3288 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3289 }
3290
3291 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003292
3293 if( PSA_SUCCESS == expected_result )
3294 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003295 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003296
Gilles Peskine003a4a92019-05-14 16:09:40 +02003297 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3298 * should be exact. */
3299 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003300 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003301
gabor-mezei-armceface22021-01-21 12:26:17 +01003302 TEST_ASSERT( input_data->len <=
3303 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3304
Ronald Cron5425a212020-08-04 14:58:35 +02003305 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003306 nonce->x, nonce->len,
3307 additional_data->x,
3308 additional_data->len,
3309 output_data, output_length,
3310 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003311 &output_length2 ),
3312 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003313
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003314 ASSERT_COMPARE( input_data->x, input_data->len,
3315 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003316 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003317
Gilles Peskinea1cac842018-06-11 19:33:02 +02003318exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003319 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003320 mbedtls_free( output_data );
3321 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003322 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003323}
3324/* END_CASE */
3325
3326/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003327void aead_encrypt( int key_type_arg, data_t *key_data,
3328 int alg_arg,
3329 data_t *nonce,
3330 data_t *additional_data,
3331 data_t *input_data,
3332 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003333{
Ronald Cron5425a212020-08-04 14:58:35 +02003334 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003335 psa_key_type_t key_type = key_type_arg;
3336 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003337 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003338 unsigned char *output_data = NULL;
3339 size_t output_size = 0;
3340 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003341 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003342 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003343
Gilles Peskine8817f612018-12-18 00:18:46 +01003344 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003345
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003346 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3347 psa_set_key_algorithm( &attributes, alg );
3348 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003349
Gilles Peskine049c7532019-05-15 20:22:09 +02003350 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003351 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003352 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3353 key_bits = psa_get_key_bits( &attributes );
3354
3355 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3356 alg );
3357 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3358 * should be exact. */
3359 TEST_EQUAL( output_size,
3360 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3361 TEST_ASSERT( output_size <=
3362 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3363 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003364
Steven Cooremand588ea12021-01-11 19:36:04 +01003365 status = psa_aead_encrypt( key, alg,
3366 nonce->x, nonce->len,
3367 additional_data->x, additional_data->len,
3368 input_data->x, input_data->len,
3369 output_data, output_size,
3370 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003371
Ronald Cron28a45ed2021-02-09 20:35:42 +01003372 /* If the operation is not supported, just skip and not fail in case the
3373 * encryption involves a common limitation of cryptography hardwares and
3374 * an alternative implementation. */
3375 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003376 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003377 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3378 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003379 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003380
3381 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003382 ASSERT_COMPARE( expected_result->x, expected_result->len,
3383 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003384
Gilles Peskinea1cac842018-06-11 19:33:02 +02003385exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003386 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003387 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003388 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003389}
3390/* END_CASE */
3391
3392/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003393void aead_decrypt( int key_type_arg, data_t *key_data,
3394 int alg_arg,
3395 data_t *nonce,
3396 data_t *additional_data,
3397 data_t *input_data,
3398 data_t *expected_data,
3399 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003400{
Ronald Cron5425a212020-08-04 14:58:35 +02003401 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003402 psa_key_type_t key_type = key_type_arg;
3403 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003404 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003405 unsigned char *output_data = NULL;
3406 size_t output_size = 0;
3407 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003408 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003409 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003410 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003411
Gilles Peskine8817f612018-12-18 00:18:46 +01003412 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003413
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003414 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3415 psa_set_key_algorithm( &attributes, alg );
3416 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003417
Gilles Peskine049c7532019-05-15 20:22:09 +02003418 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003419 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003420 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3421 key_bits = psa_get_key_bits( &attributes );
3422
3423 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3424 alg );
3425 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3426 expected_result != PSA_ERROR_NOT_SUPPORTED )
3427 {
3428 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3429 * should be exact. */
3430 TEST_EQUAL( output_size,
3431 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3432 TEST_ASSERT( output_size <=
3433 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3434 }
3435 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003436
Steven Cooremand588ea12021-01-11 19:36:04 +01003437 status = psa_aead_decrypt( key, alg,
3438 nonce->x, nonce->len,
3439 additional_data->x,
3440 additional_data->len,
3441 input_data->x, input_data->len,
3442 output_data, output_size,
3443 &output_length );
3444
Ronald Cron28a45ed2021-02-09 20:35:42 +01003445 /* If the operation is not supported, just skip and not fail in case the
3446 * decryption involves a common limitation of cryptography hardwares and
3447 * an alternative implementation. */
3448 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003449 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003450 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3451 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003452 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003453
3454 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003455
Gilles Peskine2d277862018-06-18 15:41:12 +02003456 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003457 ASSERT_COMPARE( expected_data->x, expected_data->len,
3458 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003459
Gilles Peskinea1cac842018-06-11 19:33:02 +02003460exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003461 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003462 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003463 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003464}
3465/* END_CASE */
3466
3467/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003468void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3469 int alg_arg,
3470 data_t *nonce,
3471 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003472 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003473 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003474 int do_test_data_chunked,
3475 int do_set_lengths,
3476 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003477{
Paul Elliottd3f82412021-06-16 16:52:21 +01003478 size_t ad_part_len = 0;
3479 size_t data_part_len = 0;
Paul Elliott33746aa2021-09-15 16:40:40 +01003480 setlengths_method set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003481
Paul Elliotte64deda2021-09-09 14:07:23 +01003482 /* Ensure that either one part of the test or the other is done, i.e this
3483 * test does something. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003484 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3485
3486 /* Temporary whilst we have algorithms that cannot support chunking */
3487 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003488 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003489 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3490 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003491 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003492 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003493
Paul Elliott33746aa2021-09-15 16:40:40 +01003494 if( do_set_lengths )
3495 {
3496 if( ad_part_len & 0x01 )
3497 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3498 else
3499 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3500 }
3501
Paul Elliott329d5382021-07-22 17:10:45 +01003502 /* Split ad into length(ad_part_len) parts. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003503 if( !aead_multipart_internal_func( key_type_arg, key_data,
3504 alg_arg, nonce,
3505 additional_data,
3506 ad_part_len,
3507 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003508 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003509 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003510 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003511 break;
3512
3513 /* length(0) part, length(ad_part_len) part, length(0) part... */
3514 mbedtls_test_set_step( 1000 + ad_part_len );
3515
3516 if( !aead_multipart_internal_func( key_type_arg, key_data,
3517 alg_arg, nonce,
3518 additional_data,
3519 ad_part_len,
3520 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003521 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003522 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003523 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003524 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003525 }
3526 }
Paul Elliottd3f82412021-06-16 16:52:21 +01003527
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003528 /* Temporary whilst we have algorithms that cannot support chunking */
3529 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003530 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003531 for( data_part_len = 1; data_part_len <= input_data->len;
3532 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003533 {
Paul Elliott329d5382021-07-22 17:10:45 +01003534 /* Split data into length(data_part_len) parts. */
3535 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003536
Paul Elliott33746aa2021-09-15 16:40:40 +01003537 if( do_set_lengths )
3538 {
3539 if( data_part_len & 0x01 )
3540 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3541 else
3542 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3543 }
3544
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003545 if( !aead_multipart_internal_func( key_type_arg, key_data,
3546 alg_arg, nonce,
3547 additional_data, -1,
3548 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003549 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003550 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003551 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003552 break;
3553
3554 /* length(0) part, length(data_part_len) part, length(0) part... */
3555 mbedtls_test_set_step( 3000 + data_part_len );
3556
3557 if( !aead_multipart_internal_func( key_type_arg, key_data,
3558 alg_arg, nonce,
3559 additional_data, -1,
3560 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003561 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003562 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003563 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003564 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003565 }
3566 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003567
Paul Elliott0023e0a2021-04-27 10:06:22 +01003568
Paul Elliott8fc45162021-06-23 16:06:01 +01003569 /* Goto is required to silence warnings about unused labels, as we
3570 * don't actually do any test assertions in this function. */
3571 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003572}
3573/* END_CASE */
3574
3575/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003576void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3577 int alg_arg,
3578 data_t *nonce,
3579 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003580 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003581 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003582 int do_test_data_chunked,
3583 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01003584 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003585{
Paul Elliottd3f82412021-06-16 16:52:21 +01003586 size_t ad_part_len = 0;
3587 size_t data_part_len = 0;
Paul Elliott33746aa2021-09-15 16:40:40 +01003588 setlengths_method set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003589
Paul Elliotte64deda2021-09-09 14:07:23 +01003590 /* Ensure that either one part of the test or the other is done, i.e this
3591 * test does something. */
3592 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3593
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003594 /* Temporary whilst we have algorithms that cannot support chunking */
3595 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003596 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003597 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3598 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003599 {
Paul Elliott329d5382021-07-22 17:10:45 +01003600 /* Split ad into length(ad_part_len) parts. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003601 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003602
Paul Elliott33746aa2021-09-15 16:40:40 +01003603 if( do_set_lengths )
3604 {
3605 if( ad_part_len & 0x01 )
3606 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3607 else
3608 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3609 }
3610
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003611 if( !aead_multipart_internal_func( key_type_arg, key_data,
3612 alg_arg, nonce,
3613 additional_data,
3614 ad_part_len,
3615 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003616 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003617 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003618 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003619 break;
3620
3621 /* length(0) part, length(ad_part_len) part, length(0) part... */
3622 mbedtls_test_set_step( 1000 + ad_part_len );
3623
3624 if( !aead_multipart_internal_func( key_type_arg, key_data,
3625 alg_arg, nonce,
3626 additional_data,
3627 ad_part_len,
3628 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003629 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003630 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003631 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003632 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003633 }
3634 }
3635
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003636 /* Temporary whilst we have algorithms that cannot support chunking */
3637 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003638 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003639 for( data_part_len = 1; data_part_len <= input_data->len;
3640 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003641 {
Paul Elliott329d5382021-07-22 17:10:45 +01003642 /* Split data into length(data_part_len) parts. */
3643 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003644
Paul Elliott33746aa2021-09-15 16:40:40 +01003645 if( do_set_lengths )
3646 {
3647 if( data_part_len & 0x01 )
3648 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3649 else
3650 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3651 }
3652
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003653 if( !aead_multipart_internal_func( key_type_arg, key_data,
3654 alg_arg, nonce,
3655 additional_data, -1,
3656 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003657 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003658 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003659 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003660 break;
3661
3662 /* length(0) part, length(data_part_len) part, length(0) part... */
3663 mbedtls_test_set_step( 3000 + data_part_len );
3664
3665 if( !aead_multipart_internal_func( key_type_arg, key_data,
3666 alg_arg, nonce,
3667 additional_data, -1,
3668 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003669 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003670 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003671 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003672 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003673 }
3674 }
3675
Paul Elliott8fc45162021-06-23 16:06:01 +01003676 /* Goto is required to silence warnings about unused labels, as we
3677 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003678 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003679}
3680/* END_CASE */
3681
3682/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003683void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
3684 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01003685 int nonce_length,
3686 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003687 data_t *additional_data,
3688 data_t *input_data,
3689 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003690{
3691
3692 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3693 psa_key_type_t key_type = key_type_arg;
3694 psa_algorithm_t alg = alg_arg;
3695 psa_aead_operation_t operation;
3696 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3697 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3698 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01003699 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01003700 size_t actual_nonce_length = 0;
3701 size_t expected_nonce_length = expected_nonce_length_arg;
3702 unsigned char *output = NULL;
3703 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003704 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01003705 size_t ciphertext_size = 0;
3706 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003707 size_t tag_length = 0;
3708 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003709
3710 PSA_ASSERT( psa_crypto_init( ) );
3711
3712 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
3713 psa_set_key_algorithm( & attributes, alg );
3714 psa_set_key_type( & attributes, key_type );
3715
3716 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3717 &key ) );
3718
3719 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3720
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003721 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3722
Paul Elliottf1277632021-08-24 18:11:37 +01003723 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003724
Paul Elliottf1277632021-08-24 18:11:37 +01003725 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003726
Paul Elliottf1277632021-08-24 18:11:37 +01003727 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003728
Paul Elliottf1277632021-08-24 18:11:37 +01003729 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003730
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003731 operation = psa_aead_operation_init( );
3732
3733 status = psa_aead_encrypt_setup( &operation, key, alg );
3734
3735 /* If the operation is not supported, just skip and not fail in case the
3736 * encryption involves a common limitation of cryptography hardwares and
3737 * an alternative implementation. */
3738 if( status == PSA_ERROR_NOT_SUPPORTED )
3739 {
3740 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01003741 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003742 }
3743
3744 PSA_ASSERT( status );
3745
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003746 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01003747 nonce_length,
3748 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003749
Paul Elliott693bf312021-07-23 17:40:41 +01003750 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003751
Paul Elliottf1277632021-08-24 18:11:37 +01003752 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01003753
Paul Elliottf1277632021-08-24 18:11:37 +01003754 TEST_ASSERT( actual_nonce_length < PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01003755
Paul Elliott693bf312021-07-23 17:40:41 +01003756 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003757 {
3758
3759 /* Ensure we can still complete operation. */
3760
3761 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3762 additional_data->len ) );
3763
3764 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01003765 output, output_size,
3766 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003767
Paul Elliottf1277632021-08-24 18:11:37 +01003768 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3769 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003770 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3771 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003772
3773exit:
3774 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01003775 mbedtls_free( output );
3776 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003777 psa_aead_abort( &operation );
3778 PSA_DONE( );
3779}
3780/* END_CASE */
3781
3782/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01003783void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
3784 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01003785 int nonce_length_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01003786 data_t *additional_data,
3787 data_t *input_data,
3788 int expected_status_arg )
3789{
3790
3791 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3792 psa_key_type_t key_type = key_type_arg;
3793 psa_algorithm_t alg = alg_arg;
3794 psa_aead_operation_t operation;
3795 uint8_t *nonce_buffer = NULL;
3796 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3797 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3798 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003799 unsigned char *output = NULL;
3800 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01003801 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01003802 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003803 size_t ciphertext_size = 0;
3804 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003805 size_t tag_length = 0;
3806 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01003807 size_t index = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003808
3809 PSA_ASSERT( psa_crypto_init( ) );
3810
3811 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3812 psa_set_key_algorithm( &attributes, alg );
3813 psa_set_key_type( &attributes, key_type );
3814
3815 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3816 &key ) );
3817
3818 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3819
3820 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3821
Paul Elliott6f0e7202021-08-25 12:57:18 +01003822 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003823
Paul Elliott6f0e7202021-08-25 12:57:18 +01003824 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01003825
Paul Elliott6f0e7202021-08-25 12:57:18 +01003826 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01003827
Paul Elliott6f0e7202021-08-25 12:57:18 +01003828 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003829
3830 operation = psa_aead_operation_init( );
3831
3832 status = psa_aead_encrypt_setup( &operation, key, alg );
3833
3834 /* If the operation is not supported, just skip and not fail in case the
3835 * encryption involves a common limitation of cryptography hardwares and
3836 * an alternative implementation. */
3837 if( status == PSA_ERROR_NOT_SUPPORTED )
3838 {
3839 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003840 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01003841 }
3842
3843 PSA_ASSERT( status );
3844
Paul Elliott4023ffd2021-09-10 16:21:22 +01003845 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
3846 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01003847 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01003848 /* Arbitrary size buffer, to test zero length valid buffer. */
3849 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003850 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01003851 }
3852 else
3853 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003854 /* If length is zero, then this will return NULL. */
3855 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003856 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01003857
Paul Elliott4023ffd2021-09-10 16:21:22 +01003858 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01003859 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003860 for( index = 0; index < nonce_length - 1; ++index )
3861 {
3862 nonce_buffer[index] = 'a' + index;
3863 }
Paul Elliott66696b52021-08-16 18:42:41 +01003864 }
Paul Elliott863864a2021-07-23 17:28:31 +01003865 }
3866
Paul Elliott6f0e7202021-08-25 12:57:18 +01003867 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01003868
Paul Elliott693bf312021-07-23 17:40:41 +01003869 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01003870
3871 if( expected_status == PSA_SUCCESS )
3872 {
3873 /* Ensure we can still complete operation. */
3874
3875 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3876 additional_data->len ) );
3877
3878 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01003879 output, output_size,
3880 &ciphertext_length ) );
Paul Elliott863864a2021-07-23 17:28:31 +01003881
Paul Elliott6f0e7202021-08-25 12:57:18 +01003882 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3883 &ciphertext_length, tag_buffer,
Paul Elliott863864a2021-07-23 17:28:31 +01003884 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3885 }
3886
3887exit:
3888 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01003889 mbedtls_free( output );
3890 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01003891 mbedtls_free( nonce_buffer );
3892 psa_aead_abort( &operation );
3893 PSA_DONE( );
3894}
3895/* END_CASE */
3896
3897/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01003898void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
3899 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003900 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01003901 data_t *nonce,
3902 data_t *additional_data,
3903 data_t *input_data,
3904 int expected_status_arg )
3905{
3906
3907 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3908 psa_key_type_t key_type = key_type_arg;
3909 psa_algorithm_t alg = alg_arg;
3910 psa_aead_operation_t operation;
3911 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3912 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3913 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01003914 unsigned char *output = NULL;
3915 unsigned char *ciphertext = NULL;
3916 size_t output_size = output_size_arg;
3917 size_t ciphertext_size = 0;
3918 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01003919 size_t tag_length = 0;
3920 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
3921
3922 PSA_ASSERT( psa_crypto_init( ) );
3923
3924 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3925 psa_set_key_algorithm( &attributes, alg );
3926 psa_set_key_type( &attributes, key_type );
3927
3928 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3929 &key ) );
3930
3931 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3932
Paul Elliottc6d11d02021-09-01 12:04:23 +01003933 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003934
Paul Elliottc6d11d02021-09-01 12:04:23 +01003935 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01003936
Paul Elliottc6d11d02021-09-01 12:04:23 +01003937 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003938
3939 operation = psa_aead_operation_init( );
3940
3941 status = psa_aead_encrypt_setup( &operation, key, alg );
3942
3943 /* If the operation is not supported, just skip and not fail in case the
3944 * encryption involves a common limitation of cryptography hardwares and
3945 * an alternative implementation. */
3946 if( status == PSA_ERROR_NOT_SUPPORTED )
3947 {
3948 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3949 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3950 }
3951
3952 PSA_ASSERT( status );
3953
3954 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3955
3956 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3957 additional_data->len ) );
3958
3959 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003960 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01003961
3962 TEST_EQUAL( status, expected_status );
3963
3964 if( expected_status == PSA_SUCCESS )
3965 {
3966 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01003967 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3968 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01003969 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3970 }
3971
3972exit:
3973 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01003974 mbedtls_free( output );
3975 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01003976 psa_aead_abort( &operation );
3977 PSA_DONE( );
3978}
3979/* END_CASE */
3980
Paul Elliott91b021e2021-07-23 18:52:31 +01003981/* BEGIN_CASE */
3982void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
3983 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01003984 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01003985 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01003986 data_t *nonce,
3987 data_t *additional_data,
3988 data_t *input_data,
3989 int expected_status_arg )
3990{
3991
3992 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3993 psa_key_type_t key_type = key_type_arg;
3994 psa_algorithm_t alg = alg_arg;
3995 psa_aead_operation_t operation;
3996 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3997 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3998 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01003999 unsigned char *ciphertext = NULL;
4000 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01004001 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004002 size_t ciphertext_size = 0;
4003 size_t ciphertext_length = 0;
4004 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004005 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004006 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004007
4008 PSA_ASSERT( psa_crypto_init( ) );
4009
4010 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4011 psa_set_key_algorithm( &attributes, alg );
4012 psa_set_key_type( &attributes, key_type );
4013
4014 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4015 &key ) );
4016
4017 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4018
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004019 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004020
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004021 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004022
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004023 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004024
Paul Elliott719c1322021-09-13 18:27:22 +01004025 ASSERT_ALLOC( tag_buffer, tag_size );
4026
Paul Elliott91b021e2021-07-23 18:52:31 +01004027 operation = psa_aead_operation_init( );
4028
4029 status = psa_aead_encrypt_setup( &operation, key, alg );
4030
4031 /* If the operation is not supported, just skip and not fail in case the
4032 * encryption involves a common limitation of cryptography hardwares and
4033 * an alternative implementation. */
4034 if( status == PSA_ERROR_NOT_SUPPORTED )
4035 {
4036 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4037 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4038 }
4039
4040 PSA_ASSERT( status );
4041
4042 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4043
4044 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4045 additional_data->len ) );
4046
4047 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004048 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004049
4050 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004051 status = psa_aead_finish( &operation, finish_ciphertext,
4052 finish_ciphertext_size,
4053 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004054 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004055
4056 TEST_EQUAL( status, expected_status );
4057
4058exit:
4059 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004060 mbedtls_free( ciphertext );
4061 mbedtls_free( finish_ciphertext );
Paul Elliott91b021e2021-07-23 18:52:31 +01004062 psa_aead_abort( &operation );
4063 PSA_DONE( );
4064}
4065/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004066
4067/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01004068void aead_multipart_verify( int key_type_arg, data_t *key_data,
4069 int alg_arg,
4070 data_t *nonce,
4071 data_t *additional_data,
4072 data_t *input_data,
4073 data_t *tag,
4074 int expected_status_arg )
4075{
4076 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4077 psa_key_type_t key_type = key_type_arg;
4078 psa_algorithm_t alg = alg_arg;
4079 psa_aead_operation_t operation;
4080 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4081 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4082 psa_status_t expected_status = expected_status_arg;
4083 unsigned char *plaintext = NULL;
4084 unsigned char *finish_plaintext = NULL;
4085 size_t plaintext_size = 0;
4086 size_t plaintext_length = 0;
4087 size_t verify_plaintext_size = 0;
4088
4089 PSA_ASSERT( psa_crypto_init( ) );
4090
4091 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4092 psa_set_key_algorithm( &attributes, alg );
4093 psa_set_key_type( &attributes, key_type );
4094
4095 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4096 &key ) );
4097
4098 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4099
4100 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
4101 input_data->len );
4102
4103 ASSERT_ALLOC( plaintext, plaintext_size );
4104
4105 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
4106
4107 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
4108
4109 operation = psa_aead_operation_init( );
4110
4111 status = psa_aead_decrypt_setup( &operation, key, alg );
4112
4113 /* If the operation is not supported, just skip and not fail in case the
4114 * encryption involves a common limitation of cryptography hardwares and
4115 * an alternative implementation. */
4116 if( status == PSA_ERROR_NOT_SUPPORTED )
4117 {
4118 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4119 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4120 }
4121
4122 PSA_ASSERT( status );
4123
4124 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4125
4126 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4127 additional_data->len ) );
4128
4129 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4130 input_data->len,
4131 plaintext, plaintext_size,
4132 &plaintext_length ) );
4133
4134 status = psa_aead_verify( &operation, finish_plaintext,
4135 verify_plaintext_size,
4136 &plaintext_length,
4137 tag->x, tag->len );
4138
4139 TEST_EQUAL( status, expected_status );
4140
4141exit:
4142 psa_destroy_key( key );
4143 mbedtls_free( plaintext );
4144 mbedtls_free( finish_plaintext );
4145 psa_aead_abort( &operation );
4146 PSA_DONE( );
4147}
4148/* END_CASE */
4149
4150
4151/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004152void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4153 int alg_arg,
4154 data_t *nonce,
4155 data_t *additional_data,
4156 data_t *input_data )
4157{
4158 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4159 psa_key_type_t key_type = key_type_arg;
4160 psa_algorithm_t alg = alg_arg;
4161 psa_aead_operation_t operation;
4162 unsigned char *output_data = NULL;
4163 unsigned char *final_data = NULL;
4164 size_t output_size = 0;
4165 size_t finish_output_size = 0;
4166 size_t output_length = 0;
4167 size_t key_bits = 0;
4168 size_t tag_length = 0;
4169 size_t tag_size = 0;
4170 size_t nonce_length = 0;
4171 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4172 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4173 size_t output_part_length = 0;
4174 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4175
4176 PSA_ASSERT( psa_crypto_init( ) );
4177
4178 psa_set_key_usage_flags( & attributes,
4179 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4180 psa_set_key_algorithm( & attributes, alg );
4181 psa_set_key_type( & attributes, key_type );
4182
4183 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4184 &key ) );
4185
4186 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4187 key_bits = psa_get_key_bits( &attributes );
4188
4189 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4190
4191 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4192
4193 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4194
4195 ASSERT_ALLOC( output_data, output_size );
4196
4197 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4198
4199 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4200
4201 ASSERT_ALLOC( final_data, finish_output_size );
4202
4203 /* Test all operations error without calling setup first. */
4204
4205 operation = psa_aead_operation_init( );
4206
4207 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4208 PSA_ERROR_BAD_STATE );
4209
4210 psa_aead_abort( &operation );
4211
4212 operation = psa_aead_operation_init( );
4213
4214 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4215 PSA_AEAD_NONCE_MAX_SIZE,
4216 &nonce_length ),
4217 PSA_ERROR_BAD_STATE );
4218
4219 psa_aead_abort( &operation );
4220
Paul Elliott481be342021-07-16 17:38:47 +01004221 /* ------------------------------------------------------- */
4222
Paul Elliottc23a9a02021-06-21 18:32:46 +01004223 operation = psa_aead_operation_init( );
4224
4225 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4226 input_data->len ),
4227 PSA_ERROR_BAD_STATE );
4228
4229 psa_aead_abort( &operation );
4230
Paul Elliott481be342021-07-16 17:38:47 +01004231 /* ------------------------------------------------------- */
4232
Paul Elliottc23a9a02021-06-21 18:32:46 +01004233 operation = psa_aead_operation_init( );
4234
4235 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4236 additional_data->len ),
4237 PSA_ERROR_BAD_STATE );
4238
4239 psa_aead_abort( &operation );
4240
Paul Elliott481be342021-07-16 17:38:47 +01004241 /* ------------------------------------------------------- */
4242
Paul Elliottc23a9a02021-06-21 18:32:46 +01004243 operation = psa_aead_operation_init( );
4244
4245 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4246 input_data->len, output_data,
4247 output_size, &output_length ),
4248 PSA_ERROR_BAD_STATE );
4249
4250 psa_aead_abort( &operation );
4251
Paul Elliott481be342021-07-16 17:38:47 +01004252 /* ------------------------------------------------------- */
4253
Paul Elliottc23a9a02021-06-21 18:32:46 +01004254 operation = psa_aead_operation_init( );
4255
4256 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4257 finish_output_size,
4258 &output_part_length,
4259 tag_buffer, tag_length,
4260 &tag_size ),
4261 PSA_ERROR_BAD_STATE );
4262
4263 psa_aead_abort( &operation );
4264
Paul Elliott481be342021-07-16 17:38:47 +01004265 /* ------------------------------------------------------- */
4266
Paul Elliottc23a9a02021-06-21 18:32:46 +01004267 operation = psa_aead_operation_init( );
4268
4269 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4270 finish_output_size,
4271 &output_part_length,
4272 tag_buffer,
4273 tag_length ),
4274 PSA_ERROR_BAD_STATE );
4275
4276 psa_aead_abort( &operation );
4277
4278 /* Test for double setups. */
4279
4280 operation = psa_aead_operation_init( );
4281
4282 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4283
4284 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4285 PSA_ERROR_BAD_STATE );
4286
4287 psa_aead_abort( &operation );
4288
Paul Elliott481be342021-07-16 17:38:47 +01004289 /* ------------------------------------------------------- */
4290
Paul Elliottc23a9a02021-06-21 18:32:46 +01004291 operation = psa_aead_operation_init( );
4292
4293 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4294
4295 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4296 PSA_ERROR_BAD_STATE );
4297
4298 psa_aead_abort( &operation );
4299
Paul Elliott374a2be2021-07-16 17:53:40 +01004300 /* ------------------------------------------------------- */
4301
4302 operation = psa_aead_operation_init( );
4303
4304 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4305
4306 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4307 PSA_ERROR_BAD_STATE );
4308
4309 psa_aead_abort( &operation );
4310
4311 /* ------------------------------------------------------- */
4312
4313 operation = psa_aead_operation_init( );
4314
4315 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4316
4317 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4318 PSA_ERROR_BAD_STATE );
4319
4320 psa_aead_abort( &operation );
4321
Paul Elliottc23a9a02021-06-21 18:32:46 +01004322 /* Test for not setting a nonce. */
4323
4324 operation = psa_aead_operation_init( );
4325
4326 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4327
4328 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4329 additional_data->len ),
4330 PSA_ERROR_BAD_STATE );
4331
4332 psa_aead_abort( &operation );
4333
Paul Elliott7f628422021-09-01 12:08:29 +01004334 /* ------------------------------------------------------- */
4335
4336 operation = psa_aead_operation_init( );
4337
4338 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4339
4340 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4341 input_data->len, output_data,
4342 output_size, &output_length ),
4343 PSA_ERROR_BAD_STATE );
4344
4345 psa_aead_abort( &operation );
4346
Paul Elliottc23a9a02021-06-21 18:32:46 +01004347 /* Test for double setting nonce. */
4348
4349 operation = psa_aead_operation_init( );
4350
4351 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4352
4353 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4354
4355 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4356 PSA_ERROR_BAD_STATE );
4357
4358 psa_aead_abort( &operation );
4359
Paul Elliott374a2be2021-07-16 17:53:40 +01004360 /* Test for double generating nonce. */
4361
4362 operation = psa_aead_operation_init( );
4363
4364 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4365
4366 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4367 PSA_AEAD_NONCE_MAX_SIZE,
4368 &nonce_length ) );
4369
4370 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4371 PSA_AEAD_NONCE_MAX_SIZE,
4372 &nonce_length ),
4373 PSA_ERROR_BAD_STATE );
4374
4375
4376 psa_aead_abort( &operation );
4377
4378 /* Test for generate nonce then set and vice versa */
4379
4380 operation = psa_aead_operation_init( );
4381
4382 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4383
4384 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4385 PSA_AEAD_NONCE_MAX_SIZE,
4386 &nonce_length ) );
4387
4388 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4389 PSA_ERROR_BAD_STATE );
4390
4391 psa_aead_abort( &operation );
4392
4393 /* ------------------------------------------------------- */
4394
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 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4402 PSA_AEAD_NONCE_MAX_SIZE,
4403 &nonce_length ),
4404 PSA_ERROR_BAD_STATE );
4405
4406 psa_aead_abort( &operation );
4407
Paul Elliott7220cae2021-06-22 17:25:57 +01004408 /* Test for generating nonce in decrypt setup. */
4409
4410 operation = psa_aead_operation_init( );
4411
4412 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4413
4414 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4415 PSA_AEAD_NONCE_MAX_SIZE,
4416 &nonce_length ),
4417 PSA_ERROR_BAD_STATE );
4418
4419 psa_aead_abort( &operation );
4420
Paul Elliottc23a9a02021-06-21 18:32:46 +01004421 /* Test for setting lengths twice. */
4422
4423 operation = psa_aead_operation_init( );
4424
4425 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4426
4427 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4428
4429 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4430 input_data->len ) );
4431
4432 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4433 input_data->len ),
4434 PSA_ERROR_BAD_STATE );
4435
4436 psa_aead_abort( &operation );
4437
4438 /* Test for setting lengths after already starting data. */
4439
4440 operation = psa_aead_operation_init( );
4441
4442 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4443
4444 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4445
4446 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4447 input_data->len, output_data,
4448 output_size, &output_length ) );
4449
4450 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4451 input_data->len ),
4452 PSA_ERROR_BAD_STATE );
4453
4454 psa_aead_abort( &operation );
4455
Paul Elliott243080c2021-07-21 19:01:17 +01004456 /* Test for not sending any additional data or data after setting non zero
4457 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004458
4459 operation = psa_aead_operation_init( );
4460
4461 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4462
4463 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4464
4465 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4466 input_data->len ) );
4467
4468 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4469 finish_output_size,
4470 &output_part_length,
4471 tag_buffer, tag_length,
4472 &tag_size ),
4473 PSA_ERROR_INVALID_ARGUMENT );
4474
4475 psa_aead_abort( &operation );
4476
Paul Elliott243080c2021-07-21 19:01:17 +01004477 /* Test for not sending any additional data or data after setting non-zero
4478 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004479
4480 operation = psa_aead_operation_init( );
4481
4482 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4483
4484 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4485
4486 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4487 input_data->len ) );
4488
4489 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4490 finish_output_size,
4491 &output_part_length,
4492 tag_buffer,
4493 tag_length ),
4494 PSA_ERROR_INVALID_ARGUMENT );
4495
4496 psa_aead_abort( &operation );
4497
Paul Elliott243080c2021-07-21 19:01:17 +01004498 /* Test for not sending any additional data after setting a non-zero length
4499 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004500
4501 operation = psa_aead_operation_init( );
4502
4503 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4504
4505 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4506
4507 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4508 input_data->len ) );
4509
4510 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4511 input_data->len, output_data,
4512 output_size, &output_length ),
4513 PSA_ERROR_INVALID_ARGUMENT );
4514
4515 psa_aead_abort( &operation );
4516
Paul Elliottb0450fe2021-09-01 15:06:26 +01004517 /* Test for sending too much additional data after setting lengths. */
4518
4519 operation = psa_aead_operation_init( );
4520
4521 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4522
4523 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4524
4525 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4526
4527
4528 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4529 additional_data->len ),
4530 PSA_ERROR_INVALID_ARGUMENT );
4531
4532 psa_aead_abort( &operation );
4533
Paul Elliottfd0c154c2021-09-17 18:03:52 +01004534 operation = psa_aead_operation_init( );
4535
4536 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4537
4538 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4539
4540 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4541 input_data->len ) );
4542
4543 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4544 additional_data->len ) );
4545
4546 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4547 1 ),
4548 PSA_ERROR_INVALID_ARGUMENT );
4549
4550 psa_aead_abort( &operation );
4551
Paul Elliottb0450fe2021-09-01 15:06:26 +01004552 /* Test for sending too much data after setting lengths. */
4553
4554 operation = psa_aead_operation_init( );
4555
4556 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4557
4558 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4559
4560 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4561
4562 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4563 input_data->len, output_data,
4564 output_size, &output_length ),
4565 PSA_ERROR_INVALID_ARGUMENT );
4566
4567 psa_aead_abort( &operation );
4568
Paul Elliottfd0c154c2021-09-17 18:03:52 +01004569 operation = psa_aead_operation_init( );
4570
4571 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4572
4573 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4574
4575 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4576 input_data->len ) );
4577
4578 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4579 additional_data->len ) );
4580
4581 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4582 input_data->len, output_data,
4583 output_size, &output_length ) );
4584
4585 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4586 1, output_data,
4587 output_size, &output_length ),
4588 PSA_ERROR_INVALID_ARGUMENT );
4589
4590 psa_aead_abort( &operation );
4591
Paul Elliottc23a9a02021-06-21 18:32:46 +01004592 /* Test sending additional data after data. */
4593
4594 operation = psa_aead_operation_init( );
4595
4596 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4597
4598 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4599
4600 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4601 input_data->len, output_data,
4602 output_size, &output_length ) );
4603
4604 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4605 additional_data->len ),
4606 PSA_ERROR_BAD_STATE );
4607
4608 psa_aead_abort( &operation );
4609
Paul Elliott534d0b42021-06-22 19:15:20 +01004610 /* Test calling finish on decryption. */
4611
4612 operation = psa_aead_operation_init( );
4613
4614 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4615
4616 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4617
4618 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4619 finish_output_size,
4620 &output_part_length,
4621 tag_buffer, tag_length,
4622 &tag_size ),
4623 PSA_ERROR_BAD_STATE );
4624
4625 psa_aead_abort( &operation );
4626
4627 /* Test calling verify on encryption. */
4628
4629 operation = psa_aead_operation_init( );
4630
4631 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4632
4633 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4634
4635 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4636 finish_output_size,
4637 &output_part_length,
4638 tag_buffer,
4639 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01004640 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01004641
4642 psa_aead_abort( &operation );
4643
4644
Paul Elliottc23a9a02021-06-21 18:32:46 +01004645exit:
4646 psa_destroy_key( key );
4647 psa_aead_abort( &operation );
4648 mbedtls_free( output_data );
4649 mbedtls_free( final_data );
4650 PSA_DONE( );
4651}
4652/* END_CASE */
4653
4654/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004655void signature_size( int type_arg,
4656 int bits,
4657 int alg_arg,
4658 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004659{
4660 psa_key_type_t type = type_arg;
4661 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004662 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004663
Gilles Peskinefe11b722018-12-18 00:24:04 +01004664 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004665
Gilles Peskinee59236f2018-01-27 23:32:46 +01004666exit:
4667 ;
4668}
4669/* END_CASE */
4670
4671/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004672void sign_hash_deterministic( int key_type_arg, data_t *key_data,
4673 int alg_arg, data_t *input_data,
4674 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004675{
Ronald Cron5425a212020-08-04 14:58:35 +02004676 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004677 psa_key_type_t key_type = key_type_arg;
4678 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004679 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004680 unsigned char *signature = NULL;
4681 size_t signature_size;
4682 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004683 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004684
Gilles Peskine8817f612018-12-18 00:18:46 +01004685 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004686
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004687 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004688 psa_set_key_algorithm( &attributes, alg );
4689 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004690
Gilles Peskine049c7532019-05-15 20:22:09 +02004691 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004692 &key ) );
4693 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004694 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004695
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004696 /* Allocate a buffer which has the size advertized by the
4697 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004698 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004699 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004700 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004701 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004702 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004703
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004704 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004705 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004706 input_data->x, input_data->len,
4707 signature, signature_size,
4708 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004709 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004710 ASSERT_COMPARE( output_data->x, output_data->len,
4711 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004712
4713exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004714 /*
4715 * Key attributes may have been returned by psa_get_key_attributes()
4716 * thus reset them as required.
4717 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004718 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004719
Ronald Cron5425a212020-08-04 14:58:35 +02004720 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004721 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004722 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004723}
4724/* END_CASE */
4725
4726/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004727void sign_hash_fail( int key_type_arg, data_t *key_data,
4728 int alg_arg, data_t *input_data,
4729 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004730{
Ronald Cron5425a212020-08-04 14:58:35 +02004731 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004732 psa_key_type_t key_type = key_type_arg;
4733 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004734 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004735 psa_status_t actual_status;
4736 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004737 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004738 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004739 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004740
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004741 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004742
Gilles Peskine8817f612018-12-18 00:18:46 +01004743 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004744
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004745 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004746 psa_set_key_algorithm( &attributes, alg );
4747 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004748
Gilles Peskine049c7532019-05-15 20:22:09 +02004749 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004750 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004751
Ronald Cron5425a212020-08-04 14:58:35 +02004752 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004753 input_data->x, input_data->len,
4754 signature, signature_size,
4755 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004756 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004757 /* The value of *signature_length is unspecified on error, but
4758 * whatever it is, it should be less than signature_size, so that
4759 * if the caller tries to read *signature_length bytes without
4760 * checking the error code then they don't overflow a buffer. */
4761 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004762
4763exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004764 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004765 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004766 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004767 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004768}
4769/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004770
4771/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004772void sign_verify_hash( int key_type_arg, data_t *key_data,
4773 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02004774{
Ronald Cron5425a212020-08-04 14:58:35 +02004775 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004776 psa_key_type_t key_type = key_type_arg;
4777 psa_algorithm_t alg = alg_arg;
4778 size_t key_bits;
4779 unsigned char *signature = NULL;
4780 size_t signature_size;
4781 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004782 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004783
Gilles Peskine8817f612018-12-18 00:18:46 +01004784 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004785
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004786 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004787 psa_set_key_algorithm( &attributes, alg );
4788 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004789
Gilles Peskine049c7532019-05-15 20:22:09 +02004790 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004791 &key ) );
4792 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004793 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004794
4795 /* Allocate a buffer which has the size advertized by the
4796 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004797 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004798 key_bits, alg );
4799 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004800 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004801 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004802
4803 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004804 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004805 input_data->x, input_data->len,
4806 signature, signature_size,
4807 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004808 /* Check that the signature length looks sensible. */
4809 TEST_ASSERT( signature_length <= signature_size );
4810 TEST_ASSERT( signature_length > 0 );
4811
4812 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004813 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004814 input_data->x, input_data->len,
4815 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004816
4817 if( input_data->len != 0 )
4818 {
4819 /* Flip a bit in the input and verify that the signature is now
4820 * detected as invalid. Flip a bit at the beginning, not at the end,
4821 * because ECDSA may ignore the last few bits of the input. */
4822 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004823 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004824 input_data->x, input_data->len,
4825 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004826 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004827 }
4828
4829exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004830 /*
4831 * Key attributes may have been returned by psa_get_key_attributes()
4832 * thus reset them as required.
4833 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004834 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004835
Ronald Cron5425a212020-08-04 14:58:35 +02004836 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004837 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004838 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004839}
4840/* END_CASE */
4841
4842/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004843void verify_hash( int key_type_arg, data_t *key_data,
4844 int alg_arg, data_t *hash_data,
4845 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004846{
Ronald Cron5425a212020-08-04 14:58:35 +02004847 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004848 psa_key_type_t key_type = key_type_arg;
4849 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004850 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004851
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004852 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004853
Gilles Peskine8817f612018-12-18 00:18:46 +01004854 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004855
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004856 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004857 psa_set_key_algorithm( &attributes, alg );
4858 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004859
Gilles Peskine049c7532019-05-15 20:22:09 +02004860 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004861 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004862
Ronald Cron5425a212020-08-04 14:58:35 +02004863 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004864 hash_data->x, hash_data->len,
4865 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004866
itayzafrir5c753392018-05-08 11:18:38 +03004867exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004868 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004869 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004870 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004871}
4872/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004873
4874/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004875void verify_hash_fail( int key_type_arg, data_t *key_data,
4876 int alg_arg, data_t *hash_data,
4877 data_t *signature_data,
4878 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004879{
Ronald Cron5425a212020-08-04 14:58:35 +02004880 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004881 psa_key_type_t key_type = key_type_arg;
4882 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004883 psa_status_t actual_status;
4884 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004885 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004886
Gilles Peskine8817f612018-12-18 00:18:46 +01004887 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004888
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004889 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004890 psa_set_key_algorithm( &attributes, alg );
4891 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004892
Gilles Peskine049c7532019-05-15 20:22:09 +02004893 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004894 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004895
Ronald Cron5425a212020-08-04 14:58:35 +02004896 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004897 hash_data->x, hash_data->len,
4898 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004899 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004900
4901exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004902 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004903 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004904 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004905}
4906/* END_CASE */
4907
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004908/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02004909void sign_message_deterministic( int key_type_arg,
4910 data_t *key_data,
4911 int alg_arg,
4912 data_t *input_data,
4913 data_t *output_data )
4914{
4915 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4916 psa_key_type_t key_type = key_type_arg;
4917 psa_algorithm_t alg = alg_arg;
4918 size_t key_bits;
4919 unsigned char *signature = NULL;
4920 size_t signature_size;
4921 size_t signature_length = 0xdeadbeef;
4922 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4923
4924 PSA_ASSERT( psa_crypto_init( ) );
4925
4926 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4927 psa_set_key_algorithm( &attributes, alg );
4928 psa_set_key_type( &attributes, key_type );
4929
4930 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4931 &key ) );
4932 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4933 key_bits = psa_get_key_bits( &attributes );
4934
4935 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4936 TEST_ASSERT( signature_size != 0 );
4937 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4938 ASSERT_ALLOC( signature, signature_size );
4939
4940 PSA_ASSERT( psa_sign_message( key, alg,
4941 input_data->x, input_data->len,
4942 signature, signature_size,
4943 &signature_length ) );
4944
4945 ASSERT_COMPARE( output_data->x, output_data->len,
4946 signature, signature_length );
4947
4948exit:
4949 psa_reset_key_attributes( &attributes );
4950
4951 psa_destroy_key( key );
4952 mbedtls_free( signature );
4953 PSA_DONE( );
4954
4955}
4956/* END_CASE */
4957
4958/* BEGIN_CASE */
4959void sign_message_fail( int key_type_arg,
4960 data_t *key_data,
4961 int alg_arg,
4962 data_t *input_data,
4963 int signature_size_arg,
4964 int expected_status_arg )
4965{
4966 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4967 psa_key_type_t key_type = key_type_arg;
4968 psa_algorithm_t alg = alg_arg;
4969 size_t signature_size = signature_size_arg;
4970 psa_status_t actual_status;
4971 psa_status_t expected_status = expected_status_arg;
4972 unsigned char *signature = NULL;
4973 size_t signature_length = 0xdeadbeef;
4974 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4975
4976 ASSERT_ALLOC( signature, signature_size );
4977
4978 PSA_ASSERT( psa_crypto_init( ) );
4979
4980 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4981 psa_set_key_algorithm( &attributes, alg );
4982 psa_set_key_type( &attributes, key_type );
4983
4984 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4985 &key ) );
4986
4987 actual_status = psa_sign_message( key, alg,
4988 input_data->x, input_data->len,
4989 signature, signature_size,
4990 &signature_length );
4991 TEST_EQUAL( actual_status, expected_status );
4992 /* The value of *signature_length is unspecified on error, but
4993 * whatever it is, it should be less than signature_size, so that
4994 * if the caller tries to read *signature_length bytes without
4995 * checking the error code then they don't overflow a buffer. */
4996 TEST_ASSERT( signature_length <= signature_size );
4997
4998exit:
4999 psa_reset_key_attributes( &attributes );
5000 psa_destroy_key( key );
5001 mbedtls_free( signature );
5002 PSA_DONE( );
5003}
5004/* END_CASE */
5005
5006/* BEGIN_CASE */
5007void sign_verify_message( int key_type_arg,
5008 data_t *key_data,
5009 int alg_arg,
5010 data_t *input_data )
5011{
5012 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5013 psa_key_type_t key_type = key_type_arg;
5014 psa_algorithm_t alg = alg_arg;
5015 size_t key_bits;
5016 unsigned char *signature = NULL;
5017 size_t signature_size;
5018 size_t signature_length = 0xdeadbeef;
5019 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5020
5021 PSA_ASSERT( psa_crypto_init( ) );
5022
5023 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
5024 PSA_KEY_USAGE_VERIFY_MESSAGE );
5025 psa_set_key_algorithm( &attributes, alg );
5026 psa_set_key_type( &attributes, key_type );
5027
5028 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5029 &key ) );
5030 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5031 key_bits = psa_get_key_bits( &attributes );
5032
5033 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5034 TEST_ASSERT( signature_size != 0 );
5035 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5036 ASSERT_ALLOC( signature, signature_size );
5037
5038 PSA_ASSERT( psa_sign_message( key, alg,
5039 input_data->x, input_data->len,
5040 signature, signature_size,
5041 &signature_length ) );
5042 TEST_ASSERT( signature_length <= signature_size );
5043 TEST_ASSERT( signature_length > 0 );
5044
5045 PSA_ASSERT( psa_verify_message( key, alg,
5046 input_data->x, input_data->len,
5047 signature, signature_length ) );
5048
5049 if( input_data->len != 0 )
5050 {
5051 /* Flip a bit in the input and verify that the signature is now
5052 * detected as invalid. Flip a bit at the beginning, not at the end,
5053 * because ECDSA may ignore the last few bits of the input. */
5054 input_data->x[0] ^= 1;
5055 TEST_EQUAL( psa_verify_message( key, alg,
5056 input_data->x, input_data->len,
5057 signature, signature_length ),
5058 PSA_ERROR_INVALID_SIGNATURE );
5059 }
5060
5061exit:
5062 psa_reset_key_attributes( &attributes );
5063
5064 psa_destroy_key( key );
5065 mbedtls_free( signature );
5066 PSA_DONE( );
5067}
5068/* END_CASE */
5069
5070/* BEGIN_CASE */
5071void verify_message( int key_type_arg,
5072 data_t *key_data,
5073 int alg_arg,
5074 data_t *input_data,
5075 data_t *signature_data )
5076{
5077 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5078 psa_key_type_t key_type = key_type_arg;
5079 psa_algorithm_t alg = alg_arg;
5080 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5081
5082 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
5083
5084 PSA_ASSERT( psa_crypto_init( ) );
5085
5086 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5087 psa_set_key_algorithm( &attributes, alg );
5088 psa_set_key_type( &attributes, key_type );
5089
5090 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5091 &key ) );
5092
5093 PSA_ASSERT( psa_verify_message( key, alg,
5094 input_data->x, input_data->len,
5095 signature_data->x, signature_data->len ) );
5096
5097exit:
5098 psa_reset_key_attributes( &attributes );
5099 psa_destroy_key( key );
5100 PSA_DONE( );
5101}
5102/* END_CASE */
5103
5104/* BEGIN_CASE */
5105void verify_message_fail( int key_type_arg,
5106 data_t *key_data,
5107 int alg_arg,
5108 data_t *hash_data,
5109 data_t *signature_data,
5110 int expected_status_arg )
5111{
5112 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5113 psa_key_type_t key_type = key_type_arg;
5114 psa_algorithm_t alg = alg_arg;
5115 psa_status_t actual_status;
5116 psa_status_t expected_status = expected_status_arg;
5117 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5118
5119 PSA_ASSERT( psa_crypto_init( ) );
5120
5121 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5122 psa_set_key_algorithm( &attributes, alg );
5123 psa_set_key_type( &attributes, key_type );
5124
5125 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5126 &key ) );
5127
5128 actual_status = psa_verify_message( key, alg,
5129 hash_data->x, hash_data->len,
5130 signature_data->x,
5131 signature_data->len );
5132 TEST_EQUAL( actual_status, expected_status );
5133
5134exit:
5135 psa_reset_key_attributes( &attributes );
5136 psa_destroy_key( key );
5137 PSA_DONE( );
5138}
5139/* END_CASE */
5140
5141/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02005142void asymmetric_encrypt( int key_type_arg,
5143 data_t *key_data,
5144 int alg_arg,
5145 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02005146 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02005147 int expected_output_length_arg,
5148 int expected_status_arg )
5149{
Ronald Cron5425a212020-08-04 14:58:35 +02005150 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005151 psa_key_type_t key_type = key_type_arg;
5152 psa_algorithm_t alg = alg_arg;
5153 size_t expected_output_length = expected_output_length_arg;
5154 size_t key_bits;
5155 unsigned char *output = NULL;
5156 size_t output_size;
5157 size_t output_length = ~0;
5158 psa_status_t actual_status;
5159 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005160 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005161
Gilles Peskine8817f612018-12-18 00:18:46 +01005162 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005163
Gilles Peskine656896e2018-06-29 19:12:28 +02005164 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005165 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5166 psa_set_key_algorithm( &attributes, alg );
5167 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005168 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005169 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02005170
5171 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02005172 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005173 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005174
Gilles Peskine656896e2018-06-29 19:12:28 +02005175 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005176 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005177 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02005178
5179 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02005180 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02005181 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005182 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02005183 output, output_size,
5184 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005185 TEST_EQUAL( actual_status, expected_status );
5186 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02005187
Gilles Peskine68428122018-06-30 18:42:41 +02005188 /* If the label is empty, the test framework puts a non-null pointer
5189 * in label->x. Test that a null pointer works as well. */
5190 if( label->len == 0 )
5191 {
5192 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005193 if( output_size != 0 )
5194 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005195 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005196 input_data->x, input_data->len,
5197 NULL, label->len,
5198 output, output_size,
5199 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005200 TEST_EQUAL( actual_status, expected_status );
5201 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005202 }
5203
Gilles Peskine656896e2018-06-29 19:12:28 +02005204exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005205 /*
5206 * Key attributes may have been returned by psa_get_key_attributes()
5207 * thus reset them as required.
5208 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005209 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005210
Ronald Cron5425a212020-08-04 14:58:35 +02005211 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02005212 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005213 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02005214}
5215/* END_CASE */
5216
5217/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005218void asymmetric_encrypt_decrypt( int key_type_arg,
5219 data_t *key_data,
5220 int alg_arg,
5221 data_t *input_data,
5222 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005223{
Ronald Cron5425a212020-08-04 14:58:35 +02005224 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005225 psa_key_type_t key_type = key_type_arg;
5226 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005227 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005228 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005229 size_t output_size;
5230 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005231 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005232 size_t output2_size;
5233 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005234 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005235
Gilles Peskine8817f612018-12-18 00:18:46 +01005236 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005237
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005238 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5239 psa_set_key_algorithm( &attributes, alg );
5240 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005241
Gilles Peskine049c7532019-05-15 20:22:09 +02005242 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005243 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005244
5245 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02005246 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005247 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005248
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005249 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005250 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005251 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01005252
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005253 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01005254 TEST_ASSERT( output2_size <=
5255 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
5256 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005257 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005258
Gilles Peskineeebd7382018-06-08 18:11:54 +02005259 /* We test encryption by checking that encrypt-then-decrypt gives back
5260 * the original plaintext because of the non-optional random
5261 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02005262 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005263 input_data->x, input_data->len,
5264 label->x, label->len,
5265 output, output_size,
5266 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005267 /* We don't know what ciphertext length to expect, but check that
5268 * it looks sensible. */
5269 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005270
Ronald Cron5425a212020-08-04 14:58:35 +02005271 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005272 output, output_length,
5273 label->x, label->len,
5274 output2, output2_size,
5275 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005276 ASSERT_COMPARE( input_data->x, input_data->len,
5277 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005278
5279exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005280 /*
5281 * Key attributes may have been returned by psa_get_key_attributes()
5282 * thus reset them as required.
5283 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005284 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005285
Ronald Cron5425a212020-08-04 14:58:35 +02005286 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005287 mbedtls_free( output );
5288 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005289 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005290}
5291/* END_CASE */
5292
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005293/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005294void asymmetric_decrypt( int key_type_arg,
5295 data_t *key_data,
5296 int alg_arg,
5297 data_t *input_data,
5298 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02005299 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005300{
Ronald Cron5425a212020-08-04 14:58:35 +02005301 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005302 psa_key_type_t key_type = key_type_arg;
5303 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01005304 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005305 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03005306 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005307 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005308 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005309
Gilles Peskine8817f612018-12-18 00:18:46 +01005310 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005311
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005312 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5313 psa_set_key_algorithm( &attributes, alg );
5314 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005315
Gilles Peskine049c7532019-05-15 20:22:09 +02005316 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005317 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005318
gabor-mezei-armceface22021-01-21 12:26:17 +01005319 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5320 key_bits = psa_get_key_bits( &attributes );
5321
5322 /* Determine the maximum ciphertext length */
5323 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
5324 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
5325 ASSERT_ALLOC( output, output_size );
5326
Ronald Cron5425a212020-08-04 14:58:35 +02005327 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005328 input_data->x, input_data->len,
5329 label->x, label->len,
5330 output,
5331 output_size,
5332 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005333 ASSERT_COMPARE( expected_data->x, expected_data->len,
5334 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005335
Gilles Peskine68428122018-06-30 18:42:41 +02005336 /* If the label is empty, the test framework puts a non-null pointer
5337 * in label->x. Test that a null pointer works as well. */
5338 if( label->len == 0 )
5339 {
5340 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005341 if( output_size != 0 )
5342 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005343 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005344 input_data->x, input_data->len,
5345 NULL, label->len,
5346 output,
5347 output_size,
5348 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005349 ASSERT_COMPARE( expected_data->x, expected_data->len,
5350 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005351 }
5352
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005353exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005354 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005355 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005356 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005357 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005358}
5359/* END_CASE */
5360
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005361/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005362void asymmetric_decrypt_fail( int key_type_arg,
5363 data_t *key_data,
5364 int alg_arg,
5365 data_t *input_data,
5366 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00005367 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02005368 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005369{
Ronald Cron5425a212020-08-04 14:58:35 +02005370 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005371 psa_key_type_t key_type = key_type_arg;
5372 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005373 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00005374 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005375 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005376 psa_status_t actual_status;
5377 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005378 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005379
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005380 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005381
Gilles Peskine8817f612018-12-18 00:18:46 +01005382 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005383
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005384 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5385 psa_set_key_algorithm( &attributes, alg );
5386 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005387
Gilles Peskine049c7532019-05-15 20:22:09 +02005388 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005389 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005390
Ronald Cron5425a212020-08-04 14:58:35 +02005391 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005392 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005393 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005394 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02005395 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005396 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005397 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005398
Gilles Peskine68428122018-06-30 18:42:41 +02005399 /* If the label is empty, the test framework puts a non-null pointer
5400 * in label->x. Test that a null pointer works as well. */
5401 if( label->len == 0 )
5402 {
5403 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005404 if( output_size != 0 )
5405 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005406 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005407 input_data->x, input_data->len,
5408 NULL, label->len,
5409 output, output_size,
5410 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005411 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005412 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02005413 }
5414
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005415exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005416 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005417 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02005418 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005419 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005420}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005421/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02005422
5423/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005424void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00005425{
5426 /* Test each valid way of initializing the object, except for `= {0}`, as
5427 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
5428 * though it's OK by the C standard. We could test for this, but we'd need
5429 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005430 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005431 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
5432 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
5433 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00005434
5435 memset( &zero, 0, sizeof( zero ) );
5436
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005437 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005438 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005439 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005440 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005441 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005442 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005443 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005444
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005445 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005446 PSA_ASSERT( psa_key_derivation_abort(&func) );
5447 PSA_ASSERT( psa_key_derivation_abort(&init) );
5448 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00005449}
5450/* END_CASE */
5451
Janos Follath16de4a42019-06-13 16:32:24 +01005452/* BEGIN_CASE */
5453void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005454{
Gilles Peskineea0fb492018-07-12 17:17:20 +02005455 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005456 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005457 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005458
Gilles Peskine8817f612018-12-18 00:18:46 +01005459 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005460
Janos Follath16de4a42019-06-13 16:32:24 +01005461 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005462 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005463
5464exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005465 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005466 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005467}
5468/* END_CASE */
5469
Janos Follathaf3c2a02019-06-12 12:34:34 +01005470/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01005471void derive_set_capacity( int alg_arg, int capacity_arg,
5472 int expected_status_arg )
5473{
5474 psa_algorithm_t alg = alg_arg;
5475 size_t capacity = capacity_arg;
5476 psa_status_t expected_status = expected_status_arg;
5477 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5478
5479 PSA_ASSERT( psa_crypto_init( ) );
5480
5481 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5482
5483 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
5484 expected_status );
5485
5486exit:
5487 psa_key_derivation_abort( &operation );
5488 PSA_DONE( );
5489}
5490/* END_CASE */
5491
5492/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01005493void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02005494 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005495 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02005496 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005497 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02005498 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005499 int expected_status_arg3,
5500 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005501{
5502 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02005503 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
5504 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01005505 psa_status_t expected_statuses[] = {expected_status_arg1,
5506 expected_status_arg2,
5507 expected_status_arg3};
5508 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005509 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5510 MBEDTLS_SVC_KEY_ID_INIT,
5511 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01005512 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5513 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5514 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005515 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005516 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005517 psa_status_t expected_output_status = expected_output_status_arg;
5518 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01005519
5520 PSA_ASSERT( psa_crypto_init( ) );
5521
5522 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5523 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005524
5525 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5526
5527 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
5528 {
Gilles Peskineb8965192019-09-24 16:21:10 +02005529 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005530 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02005531 psa_set_key_type( &attributes, key_types[i] );
5532 PSA_ASSERT( psa_import_key( &attributes,
5533 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005534 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005535 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
5536 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
5537 {
5538 // When taking a private key as secret input, use key agreement
5539 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005540 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
5541 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005542 expected_statuses[i] );
5543 }
5544 else
5545 {
5546 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02005547 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005548 expected_statuses[i] );
5549 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02005550 }
5551 else
5552 {
5553 TEST_EQUAL( psa_key_derivation_input_bytes(
5554 &operation, steps[i],
5555 inputs[i]->x, inputs[i]->len ),
5556 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005557 }
5558 }
5559
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005560 if( output_key_type != PSA_KEY_TYPE_NONE )
5561 {
5562 psa_reset_key_attributes( &attributes );
5563 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
5564 psa_set_key_bits( &attributes, 8 );
5565 actual_output_status =
5566 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005567 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005568 }
5569 else
5570 {
5571 uint8_t buffer[1];
5572 actual_output_status =
5573 psa_key_derivation_output_bytes( &operation,
5574 buffer, sizeof( buffer ) );
5575 }
5576 TEST_EQUAL( actual_output_status, expected_output_status );
5577
Janos Follathaf3c2a02019-06-12 12:34:34 +01005578exit:
5579 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005580 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5581 psa_destroy_key( keys[i] );
5582 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005583 PSA_DONE( );
5584}
5585/* END_CASE */
5586
Janos Follathd958bb72019-07-03 15:02:16 +01005587/* BEGIN_CASE */
5588void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005589{
Janos Follathd958bb72019-07-03 15:02:16 +01005590 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005591 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02005592 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005593 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01005594 unsigned char input1[] = "Input 1";
5595 size_t input1_length = sizeof( input1 );
5596 unsigned char input2[] = "Input 2";
5597 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005598 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02005599 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02005600 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5601 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5602 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005603 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005604
Gilles Peskine8817f612018-12-18 00:18:46 +01005605 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005606
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005607 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5608 psa_set_key_algorithm( &attributes, alg );
5609 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005610
Gilles Peskine73676cb2019-05-15 20:15:10 +02005611 PSA_ASSERT( psa_import_key( &attributes,
5612 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02005613 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005614
5615 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005616 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5617 input1, input1_length,
5618 input2, input2_length,
5619 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01005620 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005621
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005622 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01005623 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005624 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005625
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005626 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005627
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005628 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02005629 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005630
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005631exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005632 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005633 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005634 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005635}
5636/* END_CASE */
5637
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005638/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005639void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005640{
5641 uint8_t output_buffer[16];
5642 size_t buffer_size = 16;
5643 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005644 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005645
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005646 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5647 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005648 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005649
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005650 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005651 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005652
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005653 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005654
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005655 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5656 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005657 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005658
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005659 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005660 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005661
5662exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005663 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005664}
5665/* END_CASE */
5666
5667/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005668void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02005669 int step1_arg, data_t *input1,
5670 int step2_arg, data_t *input2,
5671 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005672 int requested_capacity_arg,
5673 data_t *expected_output1,
5674 data_t *expected_output2 )
5675{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005676 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005677 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5678 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005679 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5680 MBEDTLS_SVC_KEY_ID_INIT,
5681 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005682 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005683 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005684 uint8_t *expected_outputs[2] =
5685 {expected_output1->x, expected_output2->x};
5686 size_t output_sizes[2] =
5687 {expected_output1->len, expected_output2->len};
5688 size_t output_buffer_size = 0;
5689 uint8_t *output_buffer = NULL;
5690 size_t expected_capacity;
5691 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005692 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005693 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005694 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005695
5696 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5697 {
5698 if( output_sizes[i] > output_buffer_size )
5699 output_buffer_size = output_sizes[i];
5700 if( output_sizes[i] == 0 )
5701 expected_outputs[i] = NULL;
5702 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005703 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005704 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005705
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005706 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5707 psa_set_key_algorithm( &attributes, alg );
5708 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005709
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005710 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005711 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5712 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5713 requested_capacity ) );
5714 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005715 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005716 switch( steps[i] )
5717 {
5718 case 0:
5719 break;
5720 case PSA_KEY_DERIVATION_INPUT_SECRET:
5721 PSA_ASSERT( psa_import_key( &attributes,
5722 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005723 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01005724
5725 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5726 {
5727 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
5728 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
5729 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
5730 }
5731
Gilles Peskine1468da72019-05-29 17:35:49 +02005732 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005733 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005734 break;
5735 default:
5736 PSA_ASSERT( psa_key_derivation_input_bytes(
5737 &operation, steps[i],
5738 inputs[i]->x, inputs[i]->len ) );
5739 break;
5740 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005741 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005742
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005743 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005744 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005745 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005746 expected_capacity = requested_capacity;
5747
5748 /* Expansion phase. */
5749 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5750 {
5751 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005752 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005753 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005754 if( expected_capacity == 0 && output_sizes[i] == 0 )
5755 {
5756 /* Reading 0 bytes when 0 bytes are available can go either way. */
5757 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005758 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005759 continue;
5760 }
5761 else if( expected_capacity == 0 ||
5762 output_sizes[i] > expected_capacity )
5763 {
5764 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005765 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005766 expected_capacity = 0;
5767 continue;
5768 }
5769 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005770 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005771 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005772 ASSERT_COMPARE( output_buffer, output_sizes[i],
5773 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005774 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005775 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005776 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005777 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005778 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005779 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005780 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005781
5782exit:
5783 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005784 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005785 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5786 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005787 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005788}
5789/* END_CASE */
5790
5791/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005792void derive_full( int alg_arg,
5793 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005794 data_t *input1,
5795 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005796 int requested_capacity_arg )
5797{
Ronald Cron5425a212020-08-04 14:58:35 +02005798 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005799 psa_algorithm_t alg = alg_arg;
5800 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005801 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005802 unsigned char output_buffer[16];
5803 size_t expected_capacity = requested_capacity;
5804 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005805 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005806
Gilles Peskine8817f612018-12-18 00:18:46 +01005807 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005808
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005809 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5810 psa_set_key_algorithm( &attributes, alg );
5811 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005812
Gilles Peskine049c7532019-05-15 20:22:09 +02005813 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005814 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005815
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005816 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5817 input1->x, input1->len,
5818 input2->x, input2->len,
5819 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01005820 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005821
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005822 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005823 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005824 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005825
5826 /* Expansion phase. */
5827 while( current_capacity > 0 )
5828 {
5829 size_t read_size = sizeof( output_buffer );
5830 if( read_size > current_capacity )
5831 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005832 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005833 output_buffer,
5834 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005835 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005836 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005837 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005838 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005839 }
5840
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005841 /* Check that the operation refuses to go over capacity. */
5842 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005843 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005844
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005845 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005846
5847exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005848 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005849 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005850 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005851}
5852/* END_CASE */
5853
Janos Follathe60c9052019-07-03 13:51:30 +01005854/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005855void derive_key_exercise( int alg_arg,
5856 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005857 data_t *input1,
5858 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005859 int derived_type_arg,
5860 int derived_bits_arg,
5861 int derived_usage_arg,
5862 int derived_alg_arg )
5863{
Ronald Cron5425a212020-08-04 14:58:35 +02005864 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5865 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005866 psa_algorithm_t alg = alg_arg;
5867 psa_key_type_t derived_type = derived_type_arg;
5868 size_t derived_bits = derived_bits_arg;
5869 psa_key_usage_t derived_usage = derived_usage_arg;
5870 psa_algorithm_t derived_alg = derived_alg_arg;
5871 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005872 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005873 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005874 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005875
Gilles Peskine8817f612018-12-18 00:18:46 +01005876 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005877
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005878 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5879 psa_set_key_algorithm( &attributes, alg );
5880 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005881 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005882 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005883
5884 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005885 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5886 input1->x, input1->len,
5887 input2->x, input2->len,
5888 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01005889 goto exit;
5890
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005891 psa_set_key_usage_flags( &attributes, derived_usage );
5892 psa_set_key_algorithm( &attributes, derived_alg );
5893 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005894 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005895 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005896 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005897
5898 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005899 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005900 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5901 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005902
5903 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005904 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005905 goto exit;
5906
5907exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005908 /*
5909 * Key attributes may have been returned by psa_get_key_attributes()
5910 * thus reset them as required.
5911 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005912 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005913
5914 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005915 psa_destroy_key( base_key );
5916 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005917 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005918}
5919/* END_CASE */
5920
Janos Follath42fd8882019-07-03 14:17:09 +01005921/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005922void derive_key_export( int alg_arg,
5923 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005924 data_t *input1,
5925 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005926 int bytes1_arg,
5927 int bytes2_arg )
5928{
Ronald Cron5425a212020-08-04 14:58:35 +02005929 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5930 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005931 psa_algorithm_t alg = alg_arg;
5932 size_t bytes1 = bytes1_arg;
5933 size_t bytes2 = bytes2_arg;
5934 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005935 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005936 uint8_t *output_buffer = NULL;
5937 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005938 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5939 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005940 size_t length;
5941
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005942 ASSERT_ALLOC( output_buffer, capacity );
5943 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005944 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005945
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005946 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5947 psa_set_key_algorithm( &base_attributes, alg );
5948 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005949 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005950 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005951
5952 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005953 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5954 input1->x, input1->len,
5955 input2->x, input2->len,
5956 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005957 goto exit;
5958
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005959 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005960 output_buffer,
5961 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005962 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005963
5964 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005965 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5966 input1->x, input1->len,
5967 input2->x, input2->len,
5968 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005969 goto exit;
5970
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005971 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5972 psa_set_key_algorithm( &derived_attributes, 0 );
5973 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005974 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005975 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005976 &derived_key ) );
5977 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005978 export_buffer, bytes1,
5979 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005980 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005981 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005982 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005983 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005984 &derived_key ) );
5985 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005986 export_buffer + bytes1, bytes2,
5987 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005988 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005989
5990 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005991 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5992 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005993
5994exit:
5995 mbedtls_free( output_buffer );
5996 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005997 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005998 psa_destroy_key( base_key );
5999 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006000 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006001}
6002/* END_CASE */
6003
6004/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006005void derive_key( int alg_arg,
6006 data_t *key_data, data_t *input1, data_t *input2,
6007 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006008 int expected_status_arg,
6009 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02006010{
Ronald Cron5425a212020-08-04 14:58:35 +02006011 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6012 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02006013 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006014 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02006015 size_t bits = bits_arg;
6016 psa_status_t expected_status = expected_status_arg;
6017 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6018 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6019 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
6020
6021 PSA_ASSERT( psa_crypto_init( ) );
6022
6023 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6024 psa_set_key_algorithm( &base_attributes, alg );
6025 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
6026 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006027 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02006028
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006029 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6030 input1->x, input1->len,
6031 input2->x, input2->len,
6032 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02006033 goto exit;
6034
6035 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6036 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006037 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02006038 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01006039
6040 psa_status_t status =
6041 psa_key_derivation_output_key( &derived_attributes,
6042 &operation,
6043 &derived_key );
6044 if( is_large_output > 0 )
6045 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6046 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02006047
6048exit:
6049 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006050 psa_destroy_key( base_key );
6051 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02006052 PSA_DONE( );
6053}
6054/* END_CASE */
6055
6056/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006057void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02006058 int our_key_type_arg, int our_key_alg_arg,
6059 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006060 int expected_status_arg )
6061{
Ronald Cron5425a212020-08-04 14:58:35 +02006062 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006063 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006064 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006065 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006066 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006067 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02006068 psa_status_t expected_status = expected_status_arg;
6069 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006070
Gilles Peskine8817f612018-12-18 00:18:46 +01006071 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006072
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006073 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006074 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006075 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006076 PSA_ASSERT( psa_import_key( &attributes,
6077 our_key_data->x, our_key_data->len,
6078 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006079
Gilles Peskine77f40d82019-04-11 21:27:06 +02006080 /* The tests currently include inputs that should fail at either step.
6081 * Test cases that fail at the setup step should be changed to call
6082 * key_derivation_setup instead, and this function should be renamed
6083 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006084 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02006085 if( status == PSA_SUCCESS )
6086 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006087 TEST_EQUAL( psa_key_derivation_key_agreement(
6088 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
6089 our_key,
6090 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02006091 expected_status );
6092 }
6093 else
6094 {
6095 TEST_ASSERT( status == expected_status );
6096 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02006097
6098exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006099 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006100 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006101 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006102}
6103/* END_CASE */
6104
6105/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02006106void raw_key_agreement( int alg_arg,
6107 int our_key_type_arg, data_t *our_key_data,
6108 data_t *peer_key_data,
6109 data_t *expected_output )
6110{
Ronald Cron5425a212020-08-04 14:58:35 +02006111 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006112 psa_algorithm_t alg = alg_arg;
6113 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006114 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006115 unsigned char *output = NULL;
6116 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01006117 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006118
6119 ASSERT_ALLOC( output, expected_output->len );
6120 PSA_ASSERT( psa_crypto_init( ) );
6121
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006122 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6123 psa_set_key_algorithm( &attributes, alg );
6124 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006125 PSA_ASSERT( psa_import_key( &attributes,
6126 our_key_data->x, our_key_data->len,
6127 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006128
gabor-mezei-armceface22021-01-21 12:26:17 +01006129 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
6130 key_bits = psa_get_key_bits( &attributes );
6131
Gilles Peskinebe697d82019-05-16 18:00:41 +02006132 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
6133 peer_key_data->x, peer_key_data->len,
6134 output, expected_output->len,
6135 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006136 ASSERT_COMPARE( output, output_length,
6137 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01006138 TEST_ASSERT( output_length <=
6139 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
6140 TEST_ASSERT( output_length <=
6141 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006142
6143exit:
6144 mbedtls_free( output );
6145 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006146 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006147}
6148/* END_CASE */
6149
6150/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02006151void key_agreement_capacity( int alg_arg,
6152 int our_key_type_arg, data_t *our_key_data,
6153 data_t *peer_key_data,
6154 int expected_capacity_arg )
6155{
Ronald Cron5425a212020-08-04 14:58:35 +02006156 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006157 psa_algorithm_t alg = alg_arg;
6158 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006159 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006160 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006161 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02006162 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02006163
Gilles Peskine8817f612018-12-18 00:18:46 +01006164 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006165
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006166 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6167 psa_set_key_algorithm( &attributes, alg );
6168 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006169 PSA_ASSERT( psa_import_key( &attributes,
6170 our_key_data->x, our_key_data->len,
6171 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006172
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006173 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006174 PSA_ASSERT( psa_key_derivation_key_agreement(
6175 &operation,
6176 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6177 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006178 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6179 {
6180 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006181 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006182 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006183 NULL, 0 ) );
6184 }
Gilles Peskine59685592018-09-18 12:11:34 +02006185
Gilles Peskinebf491972018-10-25 22:36:12 +02006186 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006187 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006188 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006189 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02006190
Gilles Peskinebf491972018-10-25 22:36:12 +02006191 /* Test the actual capacity by reading the output. */
6192 while( actual_capacity > sizeof( output ) )
6193 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006194 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006195 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02006196 actual_capacity -= sizeof( output );
6197 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006198 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006199 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006200 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006201 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02006202
Gilles Peskine59685592018-09-18 12:11:34 +02006203exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006204 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006205 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006206 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006207}
6208/* END_CASE */
6209
6210/* BEGIN_CASE */
6211void key_agreement_output( int alg_arg,
6212 int our_key_type_arg, data_t *our_key_data,
6213 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006214 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02006215{
Ronald Cron5425a212020-08-04 14:58:35 +02006216 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006217 psa_algorithm_t alg = alg_arg;
6218 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006219 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006220 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006221 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02006222
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006223 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
6224 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006225
Gilles Peskine8817f612018-12-18 00:18:46 +01006226 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006227
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006228 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6229 psa_set_key_algorithm( &attributes, alg );
6230 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006231 PSA_ASSERT( psa_import_key( &attributes,
6232 our_key_data->x, our_key_data->len,
6233 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006234
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006235 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006236 PSA_ASSERT( psa_key_derivation_key_agreement(
6237 &operation,
6238 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6239 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006240 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6241 {
6242 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006243 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006244 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006245 NULL, 0 ) );
6246 }
Gilles Peskine59685592018-09-18 12:11:34 +02006247
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006248 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006249 actual_output,
6250 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006251 ASSERT_COMPARE( actual_output, expected_output1->len,
6252 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006253 if( expected_output2->len != 0 )
6254 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006255 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006256 actual_output,
6257 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006258 ASSERT_COMPARE( actual_output, expected_output2->len,
6259 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006260 }
Gilles Peskine59685592018-09-18 12:11:34 +02006261
6262exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006263 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006264 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006265 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006266 mbedtls_free( actual_output );
6267}
6268/* END_CASE */
6269
6270/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02006271void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02006272{
Gilles Peskinea50d7392018-06-21 10:22:13 +02006273 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006274 unsigned char *output = NULL;
6275 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02006276 size_t i;
6277 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02006278
Simon Butcher49f8e312020-03-03 15:51:50 +00006279 TEST_ASSERT( bytes_arg >= 0 );
6280
Gilles Peskine91892022021-02-08 19:50:26 +01006281 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006282 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02006283
Gilles Peskine8817f612018-12-18 00:18:46 +01006284 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02006285
Gilles Peskinea50d7392018-06-21 10:22:13 +02006286 /* Run several times, to ensure that every output byte will be
6287 * nonzero at least once with overwhelming probability
6288 * (2^(-8*number_of_runs)). */
6289 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02006290 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006291 if( bytes != 0 )
6292 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01006293 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006294
Gilles Peskinea50d7392018-06-21 10:22:13 +02006295 for( i = 0; i < bytes; i++ )
6296 {
6297 if( output[i] != 0 )
6298 ++changed[i];
6299 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006300 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02006301
6302 /* Check that every byte was changed to nonzero at least once. This
6303 * validates that psa_generate_random is overwriting every byte of
6304 * the output buffer. */
6305 for( i = 0; i < bytes; i++ )
6306 {
6307 TEST_ASSERT( changed[i] != 0 );
6308 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006309
6310exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006311 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006312 mbedtls_free( output );
6313 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02006314}
6315/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02006316
6317/* BEGIN_CASE */
6318void generate_key( int type_arg,
6319 int bits_arg,
6320 int usage_arg,
6321 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006322 int expected_status_arg,
6323 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006324{
Ronald Cron5425a212020-08-04 14:58:35 +02006325 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006326 psa_key_type_t type = type_arg;
6327 psa_key_usage_t usage = usage_arg;
6328 size_t bits = bits_arg;
6329 psa_algorithm_t alg = alg_arg;
6330 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006331 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006332 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006333
Gilles Peskine8817f612018-12-18 00:18:46 +01006334 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006335
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006336 psa_set_key_usage_flags( &attributes, usage );
6337 psa_set_key_algorithm( &attributes, alg );
6338 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006339 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006340
6341 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01006342 psa_status_t status = psa_generate_key( &attributes, &key );
6343
6344 if( is_large_key > 0 )
6345 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6346 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006347 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006348 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006349
6350 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006351 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006352 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
6353 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006354
Gilles Peskine818ca122018-06-20 18:16:48 +02006355 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006356 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02006357 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006358
6359exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006360 /*
6361 * Key attributes may have been returned by psa_get_key_attributes()
6362 * thus reset them as required.
6363 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006364 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006365
Ronald Cron5425a212020-08-04 14:58:35 +02006366 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006367 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006368}
6369/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03006370
Ronald Cronee414c72021-03-18 18:50:08 +01006371/* 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 +02006372void generate_key_rsa( int bits_arg,
6373 data_t *e_arg,
6374 int expected_status_arg )
6375{
Ronald Cron5425a212020-08-04 14:58:35 +02006376 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006377 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006378 size_t bits = bits_arg;
6379 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
6380 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
6381 psa_status_t expected_status = expected_status_arg;
6382 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6383 uint8_t *exported = NULL;
6384 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006385 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006386 size_t exported_length = SIZE_MAX;
6387 uint8_t *e_read_buffer = NULL;
6388 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02006389 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006390 size_t e_read_length = SIZE_MAX;
6391
6392 if( e_arg->len == 0 ||
6393 ( e_arg->len == 3 &&
6394 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
6395 {
6396 is_default_public_exponent = 1;
6397 e_read_size = 0;
6398 }
6399 ASSERT_ALLOC( e_read_buffer, e_read_size );
6400 ASSERT_ALLOC( exported, exported_size );
6401
6402 PSA_ASSERT( psa_crypto_init( ) );
6403
6404 psa_set_key_usage_flags( &attributes, usage );
6405 psa_set_key_algorithm( &attributes, alg );
6406 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
6407 e_arg->x, e_arg->len ) );
6408 psa_set_key_bits( &attributes, bits );
6409
6410 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006411 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006412 if( expected_status != PSA_SUCCESS )
6413 goto exit;
6414
6415 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006416 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006417 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6418 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6419 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
6420 e_read_buffer, e_read_size,
6421 &e_read_length ) );
6422 if( is_default_public_exponent )
6423 TEST_EQUAL( e_read_length, 0 );
6424 else
6425 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
6426
6427 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006428 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02006429 goto exit;
6430
6431 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02006432 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006433 exported, exported_size,
6434 &exported_length ) );
6435 {
6436 uint8_t *p = exported;
6437 uint8_t *end = exported + exported_length;
6438 size_t len;
6439 /* RSAPublicKey ::= SEQUENCE {
6440 * modulus INTEGER, -- n
6441 * publicExponent INTEGER } -- e
6442 */
6443 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006444 MBEDTLS_ASN1_SEQUENCE |
6445 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01006446 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006447 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
6448 MBEDTLS_ASN1_INTEGER ) );
6449 if( len >= 1 && p[0] == 0 )
6450 {
6451 ++p;
6452 --len;
6453 }
6454 if( e_arg->len == 0 )
6455 {
6456 TEST_EQUAL( len, 3 );
6457 TEST_EQUAL( p[0], 1 );
6458 TEST_EQUAL( p[1], 0 );
6459 TEST_EQUAL( p[2], 1 );
6460 }
6461 else
6462 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
6463 }
6464
6465exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006466 /*
6467 * Key attributes may have been returned by psa_get_key_attributes() or
6468 * set by psa_set_key_domain_parameters() thus reset them as required.
6469 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006470 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006471
Ronald Cron5425a212020-08-04 14:58:35 +02006472 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006473 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006474 mbedtls_free( e_read_buffer );
6475 mbedtls_free( exported );
6476}
6477/* END_CASE */
6478
Darryl Greend49a4992018-06-18 17:27:26 +01006479/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006480void persistent_key_load_key_from_storage( data_t *data,
6481 int type_arg, int bits_arg,
6482 int usage_flags_arg, int alg_arg,
6483 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01006484{
Ronald Cron71016a92020-08-28 19:01:50 +02006485 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006486 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02006487 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6488 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006489 psa_key_type_t type = type_arg;
6490 size_t bits = bits_arg;
6491 psa_key_usage_t usage_flags = usage_flags_arg;
6492 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006493 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01006494 unsigned char *first_export = NULL;
6495 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006496 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006497 size_t first_exported_length;
6498 size_t second_exported_length;
6499
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006500 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6501 {
6502 ASSERT_ALLOC( first_export, export_size );
6503 ASSERT_ALLOC( second_export, export_size );
6504 }
Darryl Greend49a4992018-06-18 17:27:26 +01006505
Gilles Peskine8817f612018-12-18 00:18:46 +01006506 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006507
Gilles Peskinec87af662019-05-15 16:12:22 +02006508 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006509 psa_set_key_usage_flags( &attributes, usage_flags );
6510 psa_set_key_algorithm( &attributes, alg );
6511 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006512 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006513
Darryl Green0c6575a2018-11-07 16:05:30 +00006514 switch( generation_method )
6515 {
6516 case IMPORT_KEY:
6517 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02006518 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006519 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006520 break;
Darryl Greend49a4992018-06-18 17:27:26 +01006521
Darryl Green0c6575a2018-11-07 16:05:30 +00006522 case GENERATE_KEY:
6523 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006524 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006525 break;
6526
6527 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01006528#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006529 {
6530 /* Create base key */
6531 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
6532 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6533 psa_set_key_usage_flags( &base_attributes,
6534 PSA_KEY_USAGE_DERIVE );
6535 psa_set_key_algorithm( &base_attributes, derive_alg );
6536 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006537 PSA_ASSERT( psa_import_key( &base_attributes,
6538 data->x, data->len,
6539 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006540 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006541 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006542 PSA_ASSERT( psa_key_derivation_input_key(
6543 &operation,
6544 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006545 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006546 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006547 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006548 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
6549 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006550 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006551 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006552 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02006553 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006554 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01006555#else
6556 TEST_ASSUME( ! "KDF not supported in this configuration" );
6557#endif
6558 break;
6559
6560 default:
6561 TEST_ASSERT( ! "generation_method not implemented in test" );
6562 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00006563 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006564 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01006565
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006566 /* Export the key if permitted by the key policy. */
6567 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6568 {
Ronald Cron5425a212020-08-04 14:58:35 +02006569 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006570 first_export, export_size,
6571 &first_exported_length ) );
6572 if( generation_method == IMPORT_KEY )
6573 ASSERT_COMPARE( data->x, data->len,
6574 first_export, first_exported_length );
6575 }
Darryl Greend49a4992018-06-18 17:27:26 +01006576
6577 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02006578 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006579 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01006580 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006581
Darryl Greend49a4992018-06-18 17:27:26 +01006582 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02006583 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02006584 TEST_ASSERT( mbedtls_svc_key_id_equal(
6585 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006586 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
6587 PSA_KEY_LIFETIME_PERSISTENT );
6588 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6589 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6590 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
6591 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01006592
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006593 /* Export the key again if permitted by the key policy. */
6594 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00006595 {
Ronald Cron5425a212020-08-04 14:58:35 +02006596 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006597 second_export, export_size,
6598 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006599 ASSERT_COMPARE( first_export, first_exported_length,
6600 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006601 }
6602
6603 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006604 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00006605 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01006606
6607exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006608 /*
6609 * Key attributes may have been returned by psa_get_key_attributes()
6610 * thus reset them as required.
6611 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006612 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006613
Darryl Greend49a4992018-06-18 17:27:26 +01006614 mbedtls_free( first_export );
6615 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006616 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006617 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02006618 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006619 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01006620}
6621/* END_CASE */