blob: 406509091a7f972dbe84ff487b7109e6e1409ec4 [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
Paul Elliottbb979e72021-09-22 12:54:42 +0100272} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100273
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100274typedef enum
275{
276 USE_NULL_TAG = 0,
277 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100278} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100279
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100280/*!
281 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100282 * \param key_type_arg Type of key passed in
283 * \param key_data The encryption / decryption key data
284 * \param alg_arg The type of algorithm used
285 * \param nonce Nonce data
286 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100287 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100288 * feed additional data in to be encrypted /
289 * decrypted. If -1, no chunking.
290 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100291 * \param data_part_len_arg If not -1, the length of chunks to feed
292 * the data in to be encrypted / decrypted. If
293 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100294 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100295 * expected here, this controls whether or not
296 * to set lengths, and in what order with
297 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100298 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100299 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100300 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100301 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100302 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100303 */
304static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
305 int alg_arg,
306 data_t *nonce,
307 data_t *additional_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100308 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100309 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100310 int data_part_len_arg,
Paul Elliottbb979e72021-09-22 12:54:42 +0100311 set_lengths_method_t set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100312 data_t *expected_output,
Paul Elliott329d5382021-07-22 17:10:45 +0100313 int is_encrypt,
Paul Elliott33746aa2021-09-15 16:40:40 +0100314 int do_zero_parts )
Paul Elliottd3f82412021-06-16 16:52:21 +0100315{
316 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
317 psa_key_type_t key_type = key_type_arg;
318 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100319 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100320 unsigned char *output_data = NULL;
321 unsigned char *part_data = NULL;
322 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100323 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100324 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100325 size_t output_size = 0;
326 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100327 size_t output_length = 0;
328 size_t key_bits = 0;
329 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100330 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100331 size_t part_length = 0;
332 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100333 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100334 size_t ad_part_len = 0;
335 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100336 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100337 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
338 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
339
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100340 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100341 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100342
Paul Elliottd3f82412021-06-16 16:52:21 +0100343 PSA_ASSERT( psa_crypto_init( ) );
344
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100345 if( is_encrypt )
346 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
347 else
348 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
349
Paul Elliottd3f82412021-06-16 16:52:21 +0100350 psa_set_key_algorithm( &attributes, alg );
351 psa_set_key_type( &attributes, key_type );
352
353 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
354 &key ) );
355
356 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
357 key_bits = psa_get_key_bits( &attributes );
358
359 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
360
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100361 if( is_encrypt )
362 {
363 /* Tag gets written at end of buffer. */
364 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
365 ( input_data->len +
366 tag_length ) );
367 data_true_size = input_data->len;
368 }
369 else
370 {
371 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
372 ( input_data->len -
373 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100374
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100375 /* Do not want to attempt to decrypt tag. */
376 data_true_size = input_data->len - tag_length;
377 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100378
379 ASSERT_ALLOC( output_data, output_size );
380
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100381 if( is_encrypt )
382 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100383 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
384 TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100385 }
386 else
387 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100388 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
389 TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100390 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100391
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100392 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100393
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100394 if( is_encrypt )
395 status = psa_aead_encrypt_setup( &operation, key, alg );
396 else
397 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100398
399 /* If the operation is not supported, just skip and not fail in case the
400 * encryption involves a common limitation of cryptography hardwares and
401 * an alternative implementation. */
402 if( status == PSA_ERROR_NOT_SUPPORTED )
403 {
404 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
405 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
406 }
407
408 PSA_ASSERT( status );
409
Paul Elliott33746aa2021-09-15 16:40:40 +0100410 if( set_lengths_method == DO_NOT_SET_LENGTHS )
411 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
412 else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
Paul Elliottd3f82412021-06-16 16:52:21 +0100413 {
Paul Elliott33746aa2021-09-15 16:40:40 +0100414 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
415 data_true_size ) );
Paul Elliottebf91632021-07-22 17:54:42 +0100416 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
417 }
Paul Elliott33746aa2021-09-15 16:40:40 +0100418 else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
Paul Elliottebf91632021-07-22 17:54:42 +0100419 {
420 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
421
Paul Elliott33746aa2021-09-15 16:40:40 +0100422 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
423 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100424 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100425
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100426 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100427 {
428 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100429 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100430
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100431 for( part_offset = 0, part_count = 0;
432 part_offset < additional_data->len;
433 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100434 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100435 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100436 {
Paul Elliott329d5382021-07-22 17:10:45 +0100437 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100438 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100439 else if( additional_data->len - part_offset < ad_part_len )
440 {
441 part_length = additional_data->len - part_offset;
442 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100443 else
444 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100445 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100446 }
447
448 PSA_ASSERT( psa_aead_update_ad( &operation,
449 additional_data->x + part_offset,
450 part_length ) );
451
Paul Elliottd3f82412021-06-16 16:52:21 +0100452 }
453 }
454 else
455 {
456 /* Pass additional data in one go. */
457 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
458 additional_data->len ) );
459 }
460
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100461 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100462 {
463 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100464 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100465 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100466 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100467
468 ASSERT_ALLOC( part_data, part_data_size );
469
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100470 for( part_offset = 0, part_count = 0;
471 part_offset < data_true_size;
472 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100473 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100474 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100475 {
Paul Elliott329d5382021-07-22 17:10:45 +0100476 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100477 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100478 else if( ( data_true_size - part_offset ) < data_part_len )
479 {
480 part_length = ( data_true_size - part_offset );
481 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100482 else
483 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100484 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100485 }
486
487 PSA_ASSERT( psa_aead_update( &operation,
488 ( input_data->x + part_offset ),
489 part_length, part_data,
490 part_data_size,
491 &output_part_length ) );
492
493 if( output_data && output_part_length )
494 {
495 memcpy( ( output_data + part_offset ), part_data,
496 output_part_length );
497 }
498
Paul Elliottd3f82412021-06-16 16:52:21 +0100499 output_length += output_part_length;
500 }
501 }
502 else
503 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100504 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100505 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100506 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100507 output_size, &output_length ) );
508 }
509
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100510 if( is_encrypt )
511 PSA_ASSERT( psa_aead_finish( &operation, final_data,
512 final_output_size,
513 &output_part_length,
514 tag_buffer, tag_length,
515 &tag_size ) );
516 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100517 {
Paul Elliott9961a662021-09-17 19:19:02 +0100518 PSA_ASSERT( psa_aead_verify( &operation, final_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100519 final_output_size,
520 &output_part_length,
521 ( input_data->x + data_true_size ),
Paul Elliott9961a662021-09-17 19:19:02 +0100522 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100523 }
524
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100525 if( output_data && output_part_length )
526 memcpy( ( output_data + output_length ), final_data,
527 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100528
529 output_length += output_part_length;
530
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100531
532 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
533 * should be exact.*/
534 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100535 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100536 TEST_EQUAL( tag_length, tag_size );
537
538 if( output_data && tag_length )
539 memcpy( ( output_data + output_length ), tag_buffer,
540 tag_length );
541
542 output_length += tag_length;
543
544 TEST_EQUAL( output_length,
545 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
546 input_data->len ) );
547 TEST_ASSERT( output_length <=
548 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
549 }
550 else
551 {
552 TEST_EQUAL( output_length,
553 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
554 input_data->len ) );
555 TEST_ASSERT( output_length <=
556 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100557 }
558
Paul Elliottd3f82412021-06-16 16:52:21 +0100559
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100560 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100561 output_data, output_length );
562
Paul Elliottd3f82412021-06-16 16:52:21 +0100563
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100564 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100565
566exit:
567 psa_destroy_key( key );
568 psa_aead_abort( &operation );
569 mbedtls_free( output_data );
570 mbedtls_free( part_data );
571 mbedtls_free( final_data );
572 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100573
574 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100575}
576
Gilles Peskinee59236f2018-01-27 23:32:46 +0100577/* END_HEADER */
578
579/* BEGIN_DEPENDENCIES
580 * depends_on:MBEDTLS_PSA_CRYPTO_C
581 * END_DEPENDENCIES
582 */
583
584/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200585void static_checks( )
586{
587 size_t max_truncated_mac_size =
588 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
589
590 /* Check that the length for a truncated MAC always fits in the algorithm
591 * encoding. The shifted mask is the maximum truncated value. The
592 * untruncated algorithm may be one byte larger. */
593 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
594}
595/* END_CASE */
596
597/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200598void import_with_policy( int type_arg,
599 int usage_arg, int alg_arg,
600 int expected_status_arg )
601{
602 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
603 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200604 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200605 psa_key_type_t type = type_arg;
606 psa_key_usage_t usage = usage_arg;
607 psa_algorithm_t alg = alg_arg;
608 psa_status_t expected_status = expected_status_arg;
609 const uint8_t key_material[16] = {0};
610 psa_status_t status;
611
612 PSA_ASSERT( psa_crypto_init( ) );
613
614 psa_set_key_type( &attributes, type );
615 psa_set_key_usage_flags( &attributes, usage );
616 psa_set_key_algorithm( &attributes, alg );
617
618 status = psa_import_key( &attributes,
619 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200620 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200621 TEST_EQUAL( status, expected_status );
622 if( status != PSA_SUCCESS )
623 goto exit;
624
Ronald Cron5425a212020-08-04 14:58:35 +0200625 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200626 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
627 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
628 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200629 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200630
Ronald Cron5425a212020-08-04 14:58:35 +0200631 PSA_ASSERT( psa_destroy_key( key ) );
632 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200633
634exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100635 /*
636 * Key attributes may have been returned by psa_get_key_attributes()
637 * thus reset them as required.
638 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200639 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100640
641 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200642 PSA_DONE( );
643}
644/* END_CASE */
645
646/* BEGIN_CASE */
647void import_with_data( data_t *data, int type_arg,
648 int attr_bits_arg,
649 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200650{
651 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
652 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200653 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200654 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200655 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200656 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100657 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100658
Gilles Peskine8817f612018-12-18 00:18:46 +0100659 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100660
Gilles Peskine4747d192019-04-17 15:05:45 +0200661 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200662 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200663
Ronald Cron5425a212020-08-04 14:58:35 +0200664 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100665 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200666 if( status != PSA_SUCCESS )
667 goto exit;
668
Ronald Cron5425a212020-08-04 14:58:35 +0200669 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200670 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200671 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200672 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200673 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200674
Ronald Cron5425a212020-08-04 14:58:35 +0200675 PSA_ASSERT( psa_destroy_key( key ) );
676 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100677
678exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100679 /*
680 * Key attributes may have been returned by psa_get_key_attributes()
681 * thus reset them as required.
682 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200683 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100684
685 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200686 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100687}
688/* END_CASE */
689
690/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200691void import_large_key( int type_arg, int byte_size_arg,
692 int expected_status_arg )
693{
694 psa_key_type_t type = type_arg;
695 size_t byte_size = byte_size_arg;
696 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
697 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200698 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200699 psa_status_t status;
700 uint8_t *buffer = NULL;
701 size_t buffer_size = byte_size + 1;
702 size_t n;
703
Steven Cooreman69967ce2021-01-18 18:01:08 +0100704 /* Skip the test case if the target running the test cannot
705 * accomodate large keys due to heap size constraints */
706 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200707 memset( buffer, 'K', byte_size );
708
709 PSA_ASSERT( psa_crypto_init( ) );
710
711 /* Try importing the key */
712 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
713 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200714 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100715 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200716 TEST_EQUAL( status, expected_status );
717
718 if( status == PSA_SUCCESS )
719 {
Ronald Cron5425a212020-08-04 14:58:35 +0200720 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200721 TEST_EQUAL( psa_get_key_type( &attributes ), type );
722 TEST_EQUAL( psa_get_key_bits( &attributes ),
723 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200724 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200725 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200726 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200727 for( n = 0; n < byte_size; n++ )
728 TEST_EQUAL( buffer[n], 'K' );
729 for( n = byte_size; n < buffer_size; n++ )
730 TEST_EQUAL( buffer[n], 0 );
731 }
732
733exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100734 /*
735 * Key attributes may have been returned by psa_get_key_attributes()
736 * thus reset them as required.
737 */
738 psa_reset_key_attributes( &attributes );
739
Ronald Cron5425a212020-08-04 14:58:35 +0200740 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200741 PSA_DONE( );
742 mbedtls_free( buffer );
743}
744/* END_CASE */
745
746/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200747void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
748{
Ronald Cron5425a212020-08-04 14:58:35 +0200749 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200750 size_t bits = bits_arg;
751 psa_status_t expected_status = expected_status_arg;
752 psa_status_t status;
753 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200754 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200755 size_t buffer_size = /* Slight overapproximations */
756 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200757 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200758 unsigned char *p;
759 int ret;
760 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200761 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200762
Gilles Peskine8817f612018-12-18 00:18:46 +0100763 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200764 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200765
766 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
767 bits, keypair ) ) >= 0 );
768 length = ret;
769
770 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200771 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200772 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100773 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200774
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200775 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200776 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200777
778exit:
779 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200780 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200781}
782/* END_CASE */
783
784/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300785void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300786 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200787 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100788 int expected_bits,
789 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200790 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100791 int canonical_input )
792{
Ronald Cron5425a212020-08-04 14:58:35 +0200793 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100794 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200795 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200796 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100797 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100798 unsigned char *exported = NULL;
799 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100800 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100801 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100802 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200803 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200804 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100805
Moran Pekercb088e72018-07-17 17:36:59 +0300806 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200807 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100808 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200809 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100810 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100811
Gilles Peskine4747d192019-04-17 15:05:45 +0200812 psa_set_key_usage_flags( &attributes, usage_arg );
813 psa_set_key_algorithm( &attributes, alg );
814 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700815
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100816 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200817 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100818
819 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200820 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200821 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
822 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200823 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100824
825 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200826 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100827 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100828
829 /* The exported length must be set by psa_export_key() to a value between 0
830 * and export_size. On errors, the exported length must be 0. */
831 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
832 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
833 TEST_ASSERT( exported_length <= export_size );
834
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200835 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200836 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100837 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200838 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100839 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100840 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200841 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100842
Gilles Peskineea38a922021-02-13 00:05:16 +0100843 /* Run sanity checks on the exported key. For non-canonical inputs,
844 * this validates the canonical representations. For canonical inputs,
845 * this doesn't directly validate the implementation, but it still helps
846 * by cross-validating the test data with the sanity check code. */
847 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200848 goto exit;
849
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100850 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200851 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100852 else
853 {
Ronald Cron5425a212020-08-04 14:58:35 +0200854 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200855 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200856 &key2 ) );
857 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100858 reexported,
859 export_size,
860 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200861 ASSERT_COMPARE( exported, exported_length,
862 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200863 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100864 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100865 TEST_ASSERT( exported_length <=
866 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
867 psa_get_key_bits( &got_attributes ) ) );
868 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100869
870destroy:
871 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200872 PSA_ASSERT( psa_destroy_key( key ) );
873 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100874
875exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100876 /*
877 * Key attributes may have been returned by psa_get_key_attributes()
878 * thus reset them as required.
879 */
880 psa_reset_key_attributes( &got_attributes );
881
itayzafrir3e02b3b2018-06-12 17:06:52 +0300882 mbedtls_free( exported );
883 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200884 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100885}
886/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100887
Moran Pekerf709f4a2018-06-06 17:26:04 +0300888/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300889void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200890 int type_arg,
891 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100892 int export_size_delta,
893 int expected_export_status_arg,
894 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300895{
Ronald Cron5425a212020-08-04 14:58:35 +0200896 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300897 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200898 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200899 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300900 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300901 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100902 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100903 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200904 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300905
Gilles Peskine8817f612018-12-18 00:18:46 +0100906 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300907
Gilles Peskine4747d192019-04-17 15:05:45 +0200908 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
909 psa_set_key_algorithm( &attributes, alg );
910 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300911
912 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200913 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300914
Gilles Peskine49c25912018-10-29 15:15:31 +0100915 /* Export the public key */
916 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200917 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200918 exported, export_size,
919 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100920 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100921 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100922 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200923 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100924 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200925 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200926 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100927 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100928 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100929 TEST_ASSERT( expected_public_key->len <=
930 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
931 TEST_ASSERT( expected_public_key->len <=
932 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100933 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
934 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100935 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300936
937exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100938 /*
939 * Key attributes may have been returned by psa_get_key_attributes()
940 * thus reset them as required.
941 */
942 psa_reset_key_attributes( &attributes );
943
itayzafrir3e02b3b2018-06-12 17:06:52 +0300944 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200945 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200946 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300947}
948/* END_CASE */
949
Gilles Peskine20035e32018-02-03 22:44:14 +0100950/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200951void import_and_exercise_key( data_t *data,
952 int type_arg,
953 int bits_arg,
954 int alg_arg )
955{
Ronald Cron5425a212020-08-04 14:58:35 +0200956 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200957 psa_key_type_t type = type_arg;
958 size_t bits = bits_arg;
959 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100960 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200961 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200962 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200963
Gilles Peskine8817f612018-12-18 00:18:46 +0100964 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200965
Gilles Peskine4747d192019-04-17 15:05:45 +0200966 psa_set_key_usage_flags( &attributes, usage );
967 psa_set_key_algorithm( &attributes, alg );
968 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200969
970 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200971 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200972
973 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200974 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200975 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
976 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200977
978 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100979 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200980 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200981
Ronald Cron5425a212020-08-04 14:58:35 +0200982 PSA_ASSERT( psa_destroy_key( key ) );
983 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200984
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200985exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100986 /*
987 * Key attributes may have been returned by psa_get_key_attributes()
988 * thus reset them as required.
989 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200990 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100991
992 psa_reset_key_attributes( &attributes );
993 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200994 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200995}
996/* END_CASE */
997
998/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100999void effective_key_attributes( int type_arg, int expected_type_arg,
1000 int bits_arg, int expected_bits_arg,
1001 int usage_arg, int expected_usage_arg,
1002 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001003{
Ronald Cron5425a212020-08-04 14:58:35 +02001004 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001005 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001006 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001007 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001008 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001009 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001010 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001011 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001012 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001013 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001014
Gilles Peskine8817f612018-12-18 00:18:46 +01001015 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001016
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001017 psa_set_key_usage_flags( &attributes, usage );
1018 psa_set_key_algorithm( &attributes, alg );
1019 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001020 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001021
Ronald Cron5425a212020-08-04 14:58:35 +02001022 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001023 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001024
Ronald Cron5425a212020-08-04 14:58:35 +02001025 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001026 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1027 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1028 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1029 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001030
1031exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001032 /*
1033 * Key attributes may have been returned by psa_get_key_attributes()
1034 * thus reset them as required.
1035 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001036 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001037
1038 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001039 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001040}
1041/* END_CASE */
1042
1043/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001044void check_key_policy( int type_arg, int bits_arg,
1045 int usage_arg, int alg_arg )
1046{
1047 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1048 usage_arg, usage_arg, alg_arg, alg_arg );
1049 goto exit;
1050}
1051/* END_CASE */
1052
1053/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001054void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001055{
1056 /* Test each valid way of initializing the object, except for `= {0}`, as
1057 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1058 * though it's OK by the C standard. We could test for this, but we'd need
1059 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001060 psa_key_attributes_t func = psa_key_attributes_init( );
1061 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1062 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001063
1064 memset( &zero, 0, sizeof( zero ) );
1065
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001066 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1067 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1068 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001069
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001070 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1071 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1072 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1073
1074 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1075 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1076 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1077
1078 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1079 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1080 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1081
1082 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1083 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1084 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001085}
1086/* END_CASE */
1087
1088/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001089void mac_key_policy( int policy_usage,
1090 int policy_alg,
1091 int key_type,
1092 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001093 int exercise_alg,
1094 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001095{
Ronald Cron5425a212020-08-04 14:58:35 +02001096 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001097 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001098 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001099 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001100 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001101 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001102
Gilles Peskine8817f612018-12-18 00:18:46 +01001103 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001104
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001105 psa_set_key_usage_flags( &attributes, policy_usage );
1106 psa_set_key_algorithm( &attributes, policy_alg );
1107 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001108
Gilles Peskine049c7532019-05-15 20:22:09 +02001109 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001110 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001111
Ronald Cron5425a212020-08-04 14:58:35 +02001112 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001113 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001114 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001115 else
1116 TEST_EQUAL( status, expected_status );
1117
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001118 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001119
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001120 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001121 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001122 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001123 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001124 else
1125 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001126
1127exit:
1128 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001129 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001130 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001131}
1132/* END_CASE */
1133
1134/* BEGIN_CASE */
1135void cipher_key_policy( int policy_usage,
1136 int policy_alg,
1137 int key_type,
1138 data_t *key_data,
1139 int exercise_alg )
1140{
Ronald Cron5425a212020-08-04 14:58:35 +02001141 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001142 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001143 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001144 psa_status_t status;
1145
Gilles Peskine8817f612018-12-18 00:18:46 +01001146 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001147
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001148 psa_set_key_usage_flags( &attributes, policy_usage );
1149 psa_set_key_algorithm( &attributes, policy_alg );
1150 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001151
Gilles Peskine049c7532019-05-15 20:22:09 +02001152 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001153 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001154
Ronald Cron5425a212020-08-04 14:58:35 +02001155 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001156 if( policy_alg == exercise_alg &&
1157 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001158 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001159 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001160 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001161 psa_cipher_abort( &operation );
1162
Ronald Cron5425a212020-08-04 14:58:35 +02001163 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001164 if( policy_alg == exercise_alg &&
1165 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001166 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001167 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001168 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001169
1170exit:
1171 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001172 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001173 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001174}
1175/* END_CASE */
1176
1177/* BEGIN_CASE */
1178void aead_key_policy( int policy_usage,
1179 int policy_alg,
1180 int key_type,
1181 data_t *key_data,
1182 int nonce_length_arg,
1183 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001184 int exercise_alg,
1185 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001186{
Ronald Cron5425a212020-08-04 14:58:35 +02001187 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001188 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001189 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001190 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001191 unsigned char nonce[16] = {0};
1192 size_t nonce_length = nonce_length_arg;
1193 unsigned char tag[16];
1194 size_t tag_length = tag_length_arg;
1195 size_t output_length;
1196
1197 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1198 TEST_ASSERT( tag_length <= sizeof( tag ) );
1199
Gilles Peskine8817f612018-12-18 00:18:46 +01001200 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001201
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001202 psa_set_key_usage_flags( &attributes, policy_usage );
1203 psa_set_key_algorithm( &attributes, policy_alg );
1204 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001205
Gilles Peskine049c7532019-05-15 20:22:09 +02001206 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001207 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001208
Ronald Cron5425a212020-08-04 14:58:35 +02001209 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001210 nonce, nonce_length,
1211 NULL, 0,
1212 NULL, 0,
1213 tag, tag_length,
1214 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001215 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1216 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001217 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001218 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001219
1220 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001221 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001222 nonce, nonce_length,
1223 NULL, 0,
1224 tag, tag_length,
1225 NULL, 0,
1226 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001227 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1228 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1229 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001230 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001231 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001232 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001233
1234exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001235 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001236 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001237}
1238/* END_CASE */
1239
1240/* BEGIN_CASE */
1241void asymmetric_encryption_key_policy( int policy_usage,
1242 int policy_alg,
1243 int key_type,
1244 data_t *key_data,
1245 int exercise_alg )
1246{
Ronald Cron5425a212020-08-04 14:58:35 +02001247 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001248 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001249 psa_status_t status;
1250 size_t key_bits;
1251 size_t buffer_length;
1252 unsigned char *buffer = NULL;
1253 size_t output_length;
1254
Gilles Peskine8817f612018-12-18 00:18:46 +01001255 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001256
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001257 psa_set_key_usage_flags( &attributes, policy_usage );
1258 psa_set_key_algorithm( &attributes, policy_alg );
1259 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001260
Gilles Peskine049c7532019-05-15 20:22:09 +02001261 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001262 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001263
Ronald Cron5425a212020-08-04 14:58:35 +02001264 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001265 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001266 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1267 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001268 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001269
Ronald Cron5425a212020-08-04 14:58:35 +02001270 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001271 NULL, 0,
1272 NULL, 0,
1273 buffer, buffer_length,
1274 &output_length );
1275 if( policy_alg == exercise_alg &&
1276 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001277 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001278 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001279 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001280
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001281 if( buffer_length != 0 )
1282 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001283 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001284 buffer, buffer_length,
1285 NULL, 0,
1286 buffer, buffer_length,
1287 &output_length );
1288 if( policy_alg == exercise_alg &&
1289 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001290 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001291 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001292 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001293
1294exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001295 /*
1296 * Key attributes may have been returned by psa_get_key_attributes()
1297 * thus reset them as required.
1298 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001299 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001300
1301 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001302 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001303 mbedtls_free( buffer );
1304}
1305/* END_CASE */
1306
1307/* BEGIN_CASE */
1308void asymmetric_signature_key_policy( int policy_usage,
1309 int policy_alg,
1310 int key_type,
1311 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001312 int exercise_alg,
1313 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001314{
Ronald Cron5425a212020-08-04 14:58:35 +02001315 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001316 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001317 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001318 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1319 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1320 * compatible with the policy and `payload_length_arg` is supposed to be
1321 * a valid input length to sign. If `payload_length_arg <= 0`,
1322 * `exercise_alg` is supposed to be forbidden by the policy. */
1323 int compatible_alg = payload_length_arg > 0;
1324 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001325 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001326 size_t signature_length;
1327
Gilles Peskine8817f612018-12-18 00:18:46 +01001328 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001329
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001330 psa_set_key_usage_flags( &attributes, policy_usage );
1331 psa_set_key_algorithm( &attributes, policy_alg );
1332 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001333
Gilles Peskine049c7532019-05-15 20:22:09 +02001334 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001335 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001336
Ronald Cron5425a212020-08-04 14:58:35 +02001337 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001338 payload, payload_length,
1339 signature, sizeof( signature ),
1340 &signature_length );
1341 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001342 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001343 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001344 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001345
1346 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001347 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001348 payload, payload_length,
1349 signature, sizeof( signature ) );
1350 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001351 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001352 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001353 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001354
1355exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001356 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001357 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001358}
1359/* END_CASE */
1360
Janos Follathba3fab92019-06-11 14:50:16 +01001361/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001362void derive_key_policy( int policy_usage,
1363 int policy_alg,
1364 int key_type,
1365 data_t *key_data,
1366 int exercise_alg )
1367{
Ronald Cron5425a212020-08-04 14:58:35 +02001368 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001369 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001370 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001371 psa_status_t status;
1372
Gilles Peskine8817f612018-12-18 00:18:46 +01001373 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001374
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001375 psa_set_key_usage_flags( &attributes, policy_usage );
1376 psa_set_key_algorithm( &attributes, policy_alg );
1377 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001378
Gilles Peskine049c7532019-05-15 20:22:09 +02001379 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001380 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001381
Janos Follathba3fab92019-06-11 14:50:16 +01001382 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1383
1384 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1385 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001386 {
Janos Follathba3fab92019-06-11 14:50:16 +01001387 PSA_ASSERT( psa_key_derivation_input_bytes(
1388 &operation,
1389 PSA_KEY_DERIVATION_INPUT_SEED,
1390 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001391 }
Janos Follathba3fab92019-06-11 14:50:16 +01001392
1393 status = psa_key_derivation_input_key( &operation,
1394 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001395 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001396
Gilles Peskineea0fb492018-07-12 17:17:20 +02001397 if( policy_alg == exercise_alg &&
1398 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001399 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001400 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001401 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001402
1403exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001404 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001405 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001406 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001407}
1408/* END_CASE */
1409
1410/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001411void agreement_key_policy( int policy_usage,
1412 int policy_alg,
1413 int key_type_arg,
1414 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001415 int exercise_alg,
1416 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001417{
Ronald Cron5425a212020-08-04 14:58:35 +02001418 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001419 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001420 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001421 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001422 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001423 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001424
Gilles Peskine8817f612018-12-18 00:18:46 +01001425 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001426
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001427 psa_set_key_usage_flags( &attributes, policy_usage );
1428 psa_set_key_algorithm( &attributes, policy_alg );
1429 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001430
Gilles Peskine049c7532019-05-15 20:22:09 +02001431 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001432 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001433
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001434 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001435 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001436
Steven Cooremance48e852020-10-05 16:02:45 +02001437 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001438
1439exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001440 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001441 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001442 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001443}
1444/* END_CASE */
1445
1446/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001447void key_policy_alg2( int key_type_arg, data_t *key_data,
1448 int usage_arg, int alg_arg, int alg2_arg )
1449{
Ronald Cron5425a212020-08-04 14:58:35 +02001450 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001451 psa_key_type_t key_type = key_type_arg;
1452 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1453 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1454 psa_key_usage_t usage = usage_arg;
1455 psa_algorithm_t alg = alg_arg;
1456 psa_algorithm_t alg2 = alg2_arg;
1457
1458 PSA_ASSERT( psa_crypto_init( ) );
1459
1460 psa_set_key_usage_flags( &attributes, usage );
1461 psa_set_key_algorithm( &attributes, alg );
1462 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1463 psa_set_key_type( &attributes, key_type );
1464 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001465 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001466
Ronald Cron5425a212020-08-04 14:58:35 +02001467 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001468 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1469 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1470 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1471
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001472 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001473 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001474 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001475 goto exit;
1476
1477exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001478 /*
1479 * Key attributes may have been returned by psa_get_key_attributes()
1480 * thus reset them as required.
1481 */
1482 psa_reset_key_attributes( &got_attributes );
1483
Ronald Cron5425a212020-08-04 14:58:35 +02001484 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001485 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001486}
1487/* END_CASE */
1488
1489/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001490void raw_agreement_key_policy( int policy_usage,
1491 int policy_alg,
1492 int key_type_arg,
1493 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001494 int exercise_alg,
1495 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001496{
Ronald Cron5425a212020-08-04 14:58:35 +02001497 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001498 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001499 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001500 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001501 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001502 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001503
1504 PSA_ASSERT( psa_crypto_init( ) );
1505
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001506 psa_set_key_usage_flags( &attributes, policy_usage );
1507 psa_set_key_algorithm( &attributes, policy_alg );
1508 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001509
Gilles Peskine049c7532019-05-15 20:22:09 +02001510 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001511 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001512
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001513 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001514
Steven Cooremance48e852020-10-05 16:02:45 +02001515 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001516
1517exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001518 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001519 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001520 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001521}
1522/* END_CASE */
1523
1524/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001525void copy_success( int source_usage_arg,
1526 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001527 int type_arg, data_t *material,
1528 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001529 int target_usage_arg,
1530 int target_alg_arg, int target_alg2_arg,
1531 int expected_usage_arg,
1532 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001533{
Gilles Peskineca25db92019-04-19 11:43:08 +02001534 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1535 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001536 psa_key_usage_t expected_usage = expected_usage_arg;
1537 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001538 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001539 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1540 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001541 uint8_t *export_buffer = NULL;
1542
Gilles Peskine57ab7212019-01-28 13:03:09 +01001543 PSA_ASSERT( psa_crypto_init( ) );
1544
Gilles Peskineca25db92019-04-19 11:43:08 +02001545 /* Prepare the source key. */
1546 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1547 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001548 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001549 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001550 PSA_ASSERT( psa_import_key( &source_attributes,
1551 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001552 &source_key ) );
1553 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001554
Gilles Peskineca25db92019-04-19 11:43:08 +02001555 /* Prepare the target attributes. */
1556 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001557 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001558 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001559 /* Set volatile lifetime to reset the key identifier to 0. */
1560 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1561 }
1562
Gilles Peskineca25db92019-04-19 11:43:08 +02001563 if( target_usage_arg != -1 )
1564 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1565 if( target_alg_arg != -1 )
1566 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001567 if( target_alg2_arg != -1 )
1568 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001569
1570 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001571 PSA_ASSERT( psa_copy_key( source_key,
1572 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001573
1574 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001575 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001576
1577 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001578 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001579 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1580 psa_get_key_type( &target_attributes ) );
1581 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1582 psa_get_key_bits( &target_attributes ) );
1583 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1584 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001585 TEST_EQUAL( expected_alg2,
1586 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001587 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1588 {
1589 size_t length;
1590 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001591 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001592 material->len, &length ) );
1593 ASSERT_COMPARE( material->x, material->len,
1594 export_buffer, length );
1595 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001596
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001597 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001598 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001599 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001600 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001601
Ronald Cron5425a212020-08-04 14:58:35 +02001602 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001603
1604exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001605 /*
1606 * Source and target key attributes may have been returned by
1607 * psa_get_key_attributes() thus reset them as required.
1608 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001609 psa_reset_key_attributes( &source_attributes );
1610 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001611
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001612 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001613 mbedtls_free( export_buffer );
1614}
1615/* END_CASE */
1616
1617/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001618void copy_fail( int source_usage_arg,
1619 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001620 int type_arg, data_t *material,
1621 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001622 int target_usage_arg,
1623 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001624 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001625 int expected_status_arg )
1626{
1627 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1628 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001629 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1630 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001631 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001632
1633 PSA_ASSERT( psa_crypto_init( ) );
1634
1635 /* Prepare the source key. */
1636 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1637 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001638 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001639 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001640 PSA_ASSERT( psa_import_key( &source_attributes,
1641 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001642 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001643
1644 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001645 psa_set_key_id( &target_attributes, key_id );
1646 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001647 psa_set_key_type( &target_attributes, target_type_arg );
1648 psa_set_key_bits( &target_attributes, target_bits_arg );
1649 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1650 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001651 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001652
1653 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001654 TEST_EQUAL( psa_copy_key( source_key,
1655 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001656 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001657
Ronald Cron5425a212020-08-04 14:58:35 +02001658 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001659
Gilles Peskine4a644642019-05-03 17:14:08 +02001660exit:
1661 psa_reset_key_attributes( &source_attributes );
1662 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001663 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001664}
1665/* END_CASE */
1666
1667/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001668void hash_operation_init( )
1669{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001670 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001671 /* Test each valid way of initializing the object, except for `= {0}`, as
1672 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1673 * though it's OK by the C standard. We could test for this, but we'd need
1674 * to supress the Clang warning for the test. */
1675 psa_hash_operation_t func = psa_hash_operation_init( );
1676 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1677 psa_hash_operation_t zero;
1678
1679 memset( &zero, 0, sizeof( zero ) );
1680
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001681 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001682 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1683 PSA_ERROR_BAD_STATE );
1684 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1685 PSA_ERROR_BAD_STATE );
1686 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1687 PSA_ERROR_BAD_STATE );
1688
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001689 /* A default hash operation should be abortable without error. */
1690 PSA_ASSERT( psa_hash_abort( &func ) );
1691 PSA_ASSERT( psa_hash_abort( &init ) );
1692 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001693}
1694/* END_CASE */
1695
1696/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001697void hash_setup( int alg_arg,
1698 int expected_status_arg )
1699{
1700 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001701 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001702 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001703 psa_status_t status;
1704
Gilles Peskine8817f612018-12-18 00:18:46 +01001705 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001706
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001707 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001708 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001709
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001710 /* Whether setup succeeded or failed, abort must succeed. */
1711 PSA_ASSERT( psa_hash_abort( &operation ) );
1712
1713 /* If setup failed, reproduce the failure, so as to
1714 * test the resulting state of the operation object. */
1715 if( status != PSA_SUCCESS )
1716 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1717
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001718 /* Now the operation object should be reusable. */
1719#if defined(KNOWN_SUPPORTED_HASH_ALG)
1720 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1721 PSA_ASSERT( psa_hash_abort( &operation ) );
1722#endif
1723
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001724exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001725 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001726}
1727/* END_CASE */
1728
1729/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001730void hash_compute_fail( int alg_arg, data_t *input,
1731 int output_size_arg, int expected_status_arg )
1732{
1733 psa_algorithm_t alg = alg_arg;
1734 uint8_t *output = NULL;
1735 size_t output_size = output_size_arg;
1736 size_t output_length = INVALID_EXPORT_LENGTH;
1737 psa_status_t expected_status = expected_status_arg;
1738 psa_status_t status;
1739
1740 ASSERT_ALLOC( output, output_size );
1741
1742 PSA_ASSERT( psa_crypto_init( ) );
1743
1744 status = psa_hash_compute( alg, input->x, input->len,
1745 output, output_size, &output_length );
1746 TEST_EQUAL( status, expected_status );
1747 TEST_ASSERT( output_length <= output_size );
1748
1749exit:
1750 mbedtls_free( output );
1751 PSA_DONE( );
1752}
1753/* END_CASE */
1754
1755/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001756void hash_compare_fail( int alg_arg, data_t *input,
1757 data_t *reference_hash,
1758 int expected_status_arg )
1759{
1760 psa_algorithm_t alg = alg_arg;
1761 psa_status_t expected_status = expected_status_arg;
1762 psa_status_t status;
1763
1764 PSA_ASSERT( psa_crypto_init( ) );
1765
1766 status = psa_hash_compare( alg, input->x, input->len,
1767 reference_hash->x, reference_hash->len );
1768 TEST_EQUAL( status, expected_status );
1769
1770exit:
1771 PSA_DONE( );
1772}
1773/* END_CASE */
1774
1775/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001776void hash_compute_compare( int alg_arg, data_t *input,
1777 data_t *expected_output )
1778{
1779 psa_algorithm_t alg = alg_arg;
1780 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1781 size_t output_length = INVALID_EXPORT_LENGTH;
1782 size_t i;
1783
1784 PSA_ASSERT( psa_crypto_init( ) );
1785
1786 /* Compute with tight buffer */
1787 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001788 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001789 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001790 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001791 ASSERT_COMPARE( output, output_length,
1792 expected_output->x, expected_output->len );
1793
1794 /* Compute with larger buffer */
1795 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1796 output, sizeof( output ),
1797 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001798 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001799 ASSERT_COMPARE( output, output_length,
1800 expected_output->x, expected_output->len );
1801
1802 /* Compare with correct hash */
1803 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1804 output, output_length ) );
1805
1806 /* Compare with trailing garbage */
1807 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1808 output, output_length + 1 ),
1809 PSA_ERROR_INVALID_SIGNATURE );
1810
1811 /* Compare with truncated hash */
1812 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1813 output, output_length - 1 ),
1814 PSA_ERROR_INVALID_SIGNATURE );
1815
1816 /* Compare with corrupted value */
1817 for( i = 0; i < output_length; i++ )
1818 {
Chris Jones9634bb12021-01-20 15:56:42 +00001819 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001820 output[i] ^= 1;
1821 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1822 output, output_length ),
1823 PSA_ERROR_INVALID_SIGNATURE );
1824 output[i] ^= 1;
1825 }
1826
1827exit:
1828 PSA_DONE( );
1829}
1830/* END_CASE */
1831
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001832/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001833void hash_bad_order( )
1834{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001835 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001836 unsigned char input[] = "";
1837 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001838 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001839 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1840 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1841 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001842 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001843 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001844 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001845
Gilles Peskine8817f612018-12-18 00:18:46 +01001846 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001847
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001848 /* Call setup twice in a row. */
1849 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1850 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1851 PSA_ERROR_BAD_STATE );
1852 PSA_ASSERT( psa_hash_abort( &operation ) );
1853
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001854 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001855 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001856 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001857 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001858
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001859 /* Call update after finish. */
1860 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1861 PSA_ASSERT( psa_hash_finish( &operation,
1862 hash, sizeof( hash ), &hash_len ) );
1863 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001864 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001865 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001866
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001867 /* Call verify without calling setup beforehand. */
1868 TEST_EQUAL( psa_hash_verify( &operation,
1869 valid_hash, sizeof( valid_hash ) ),
1870 PSA_ERROR_BAD_STATE );
1871 PSA_ASSERT( psa_hash_abort( &operation ) );
1872
1873 /* Call verify after finish. */
1874 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1875 PSA_ASSERT( psa_hash_finish( &operation,
1876 hash, sizeof( hash ), &hash_len ) );
1877 TEST_EQUAL( psa_hash_verify( &operation,
1878 valid_hash, sizeof( valid_hash ) ),
1879 PSA_ERROR_BAD_STATE );
1880 PSA_ASSERT( psa_hash_abort( &operation ) );
1881
1882 /* Call verify twice in a row. */
1883 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1884 PSA_ASSERT( psa_hash_verify( &operation,
1885 valid_hash, sizeof( valid_hash ) ) );
1886 TEST_EQUAL( psa_hash_verify( &operation,
1887 valid_hash, sizeof( valid_hash ) ),
1888 PSA_ERROR_BAD_STATE );
1889 PSA_ASSERT( psa_hash_abort( &operation ) );
1890
1891 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001892 TEST_EQUAL( psa_hash_finish( &operation,
1893 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001894 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001895 PSA_ASSERT( psa_hash_abort( &operation ) );
1896
1897 /* Call finish twice in a row. */
1898 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1899 PSA_ASSERT( psa_hash_finish( &operation,
1900 hash, sizeof( hash ), &hash_len ) );
1901 TEST_EQUAL( psa_hash_finish( &operation,
1902 hash, sizeof( hash ), &hash_len ),
1903 PSA_ERROR_BAD_STATE );
1904 PSA_ASSERT( psa_hash_abort( &operation ) );
1905
1906 /* Call finish after calling verify. */
1907 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1908 PSA_ASSERT( psa_hash_verify( &operation,
1909 valid_hash, sizeof( valid_hash ) ) );
1910 TEST_EQUAL( psa_hash_finish( &operation,
1911 hash, sizeof( hash ), &hash_len ),
1912 PSA_ERROR_BAD_STATE );
1913 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001914
1915exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001916 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001917}
1918/* END_CASE */
1919
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001920/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001921void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001922{
1923 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001924 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1925 * appended to it */
1926 unsigned char hash[] = {
1927 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1928 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1929 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001930 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001931 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001932
Gilles Peskine8817f612018-12-18 00:18:46 +01001933 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001934
itayzafrir27e69452018-11-01 14:26:34 +02001935 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001936 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001937 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001938 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001939
itayzafrir27e69452018-11-01 14:26:34 +02001940 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001941 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001942 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001943 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001944
itayzafrir27e69452018-11-01 14:26:34 +02001945 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001946 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001947 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001948 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001949
itayzafrirec93d302018-10-18 18:01:10 +03001950exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001951 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001952}
1953/* END_CASE */
1954
Ronald Cronee414c72021-03-18 18:50:08 +01001955/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001956void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001957{
1958 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001959 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001960 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001961 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001962 size_t hash_len;
1963
Gilles Peskine8817f612018-12-18 00:18:46 +01001964 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001965
itayzafrir58028322018-10-25 10:22:01 +03001966 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001967 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001968 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001969 hash, expected_size - 1, &hash_len ),
1970 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001971
1972exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001973 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001974}
1975/* END_CASE */
1976
Ronald Cronee414c72021-03-18 18:50:08 +01001977/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001978void hash_clone_source_state( )
1979{
1980 psa_algorithm_t alg = PSA_ALG_SHA_256;
1981 unsigned char hash[PSA_HASH_MAX_SIZE];
1982 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1983 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1984 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1985 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1986 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1987 size_t hash_len;
1988
1989 PSA_ASSERT( psa_crypto_init( ) );
1990 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1991
1992 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1993 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1994 PSA_ASSERT( psa_hash_finish( &op_finished,
1995 hash, sizeof( hash ), &hash_len ) );
1996 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1997 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1998
1999 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2000 PSA_ERROR_BAD_STATE );
2001
2002 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2003 PSA_ASSERT( psa_hash_finish( &op_init,
2004 hash, sizeof( hash ), &hash_len ) );
2005 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2006 PSA_ASSERT( psa_hash_finish( &op_finished,
2007 hash, sizeof( hash ), &hash_len ) );
2008 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2009 PSA_ASSERT( psa_hash_finish( &op_aborted,
2010 hash, sizeof( hash ), &hash_len ) );
2011
2012exit:
2013 psa_hash_abort( &op_source );
2014 psa_hash_abort( &op_init );
2015 psa_hash_abort( &op_setup );
2016 psa_hash_abort( &op_finished );
2017 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002018 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002019}
2020/* END_CASE */
2021
Ronald Cronee414c72021-03-18 18:50:08 +01002022/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002023void hash_clone_target_state( )
2024{
2025 psa_algorithm_t alg = PSA_ALG_SHA_256;
2026 unsigned char hash[PSA_HASH_MAX_SIZE];
2027 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2028 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2029 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2030 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2031 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2032 size_t hash_len;
2033
2034 PSA_ASSERT( psa_crypto_init( ) );
2035
2036 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2037 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2038 PSA_ASSERT( psa_hash_finish( &op_finished,
2039 hash, sizeof( hash ), &hash_len ) );
2040 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2041 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2042
2043 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2044 PSA_ASSERT( psa_hash_finish( &op_target,
2045 hash, sizeof( hash ), &hash_len ) );
2046
2047 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2048 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2049 PSA_ERROR_BAD_STATE );
2050 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2051 PSA_ERROR_BAD_STATE );
2052
2053exit:
2054 psa_hash_abort( &op_target );
2055 psa_hash_abort( &op_init );
2056 psa_hash_abort( &op_setup );
2057 psa_hash_abort( &op_finished );
2058 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002059 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002060}
2061/* END_CASE */
2062
itayzafrir58028322018-10-25 10:22:01 +03002063/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002064void mac_operation_init( )
2065{
Jaeden Amero252ef282019-02-15 14:05:35 +00002066 const uint8_t input[1] = { 0 };
2067
Jaeden Amero769ce272019-01-04 11:48:03 +00002068 /* Test each valid way of initializing the object, except for `= {0}`, as
2069 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2070 * though it's OK by the C standard. We could test for this, but we'd need
2071 * to supress the Clang warning for the test. */
2072 psa_mac_operation_t func = psa_mac_operation_init( );
2073 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2074 psa_mac_operation_t zero;
2075
2076 memset( &zero, 0, sizeof( zero ) );
2077
Jaeden Amero252ef282019-02-15 14:05:35 +00002078 /* A freshly-initialized MAC operation should not be usable. */
2079 TEST_EQUAL( psa_mac_update( &func,
2080 input, sizeof( input ) ),
2081 PSA_ERROR_BAD_STATE );
2082 TEST_EQUAL( psa_mac_update( &init,
2083 input, sizeof( input ) ),
2084 PSA_ERROR_BAD_STATE );
2085 TEST_EQUAL( psa_mac_update( &zero,
2086 input, sizeof( input ) ),
2087 PSA_ERROR_BAD_STATE );
2088
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002089 /* A default MAC operation should be abortable without error. */
2090 PSA_ASSERT( psa_mac_abort( &func ) );
2091 PSA_ASSERT( psa_mac_abort( &init ) );
2092 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002093}
2094/* END_CASE */
2095
2096/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002097void mac_setup( int key_type_arg,
2098 data_t *key,
2099 int alg_arg,
2100 int expected_status_arg )
2101{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002102 psa_key_type_t key_type = key_type_arg;
2103 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002104 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002105 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002106 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2107#if defined(KNOWN_SUPPORTED_MAC_ALG)
2108 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2109#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002110
Gilles Peskine8817f612018-12-18 00:18:46 +01002111 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002112
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002113 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2114 &operation, &status ) )
2115 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002116 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002117
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002118 /* The operation object should be reusable. */
2119#if defined(KNOWN_SUPPORTED_MAC_ALG)
2120 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2121 smoke_test_key_data,
2122 sizeof( smoke_test_key_data ),
2123 KNOWN_SUPPORTED_MAC_ALG,
2124 &operation, &status ) )
2125 goto exit;
2126 TEST_EQUAL( status, PSA_SUCCESS );
2127#endif
2128
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002129exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002130 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002131}
2132/* END_CASE */
2133
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002134/* 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 +00002135void mac_bad_order( )
2136{
Ronald Cron5425a212020-08-04 14:58:35 +02002137 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002138 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2139 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002140 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002141 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2142 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2143 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002144 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002145 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2146 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2147 size_t sign_mac_length = 0;
2148 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2149 const uint8_t verify_mac[] = {
2150 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2151 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2152 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2153
2154 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002155 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002156 psa_set_key_algorithm( &attributes, alg );
2157 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002158
Ronald Cron5425a212020-08-04 14:58:35 +02002159 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2160 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002161
Jaeden Amero252ef282019-02-15 14:05:35 +00002162 /* Call update without calling setup beforehand. */
2163 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2164 PSA_ERROR_BAD_STATE );
2165 PSA_ASSERT( psa_mac_abort( &operation ) );
2166
2167 /* Call sign finish without calling setup beforehand. */
2168 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2169 &sign_mac_length),
2170 PSA_ERROR_BAD_STATE );
2171 PSA_ASSERT( psa_mac_abort( &operation ) );
2172
2173 /* Call verify finish without calling setup beforehand. */
2174 TEST_EQUAL( psa_mac_verify_finish( &operation,
2175 verify_mac, sizeof( verify_mac ) ),
2176 PSA_ERROR_BAD_STATE );
2177 PSA_ASSERT( psa_mac_abort( &operation ) );
2178
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002179 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002180 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2181 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002182 PSA_ERROR_BAD_STATE );
2183 PSA_ASSERT( psa_mac_abort( &operation ) );
2184
Jaeden Amero252ef282019-02-15 14:05:35 +00002185 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002186 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002187 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2188 PSA_ASSERT( psa_mac_sign_finish( &operation,
2189 sign_mac, sizeof( sign_mac ),
2190 &sign_mac_length ) );
2191 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2192 PSA_ERROR_BAD_STATE );
2193 PSA_ASSERT( psa_mac_abort( &operation ) );
2194
2195 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002196 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002197 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2198 PSA_ASSERT( psa_mac_verify_finish( &operation,
2199 verify_mac, sizeof( verify_mac ) ) );
2200 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2201 PSA_ERROR_BAD_STATE );
2202 PSA_ASSERT( psa_mac_abort( &operation ) );
2203
2204 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002205 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002206 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2207 PSA_ASSERT( psa_mac_sign_finish( &operation,
2208 sign_mac, sizeof( sign_mac ),
2209 &sign_mac_length ) );
2210 TEST_EQUAL( psa_mac_sign_finish( &operation,
2211 sign_mac, sizeof( sign_mac ),
2212 &sign_mac_length ),
2213 PSA_ERROR_BAD_STATE );
2214 PSA_ASSERT( psa_mac_abort( &operation ) );
2215
2216 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002217 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002218 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2219 PSA_ASSERT( psa_mac_verify_finish( &operation,
2220 verify_mac, sizeof( verify_mac ) ) );
2221 TEST_EQUAL( psa_mac_verify_finish( &operation,
2222 verify_mac, sizeof( verify_mac ) ),
2223 PSA_ERROR_BAD_STATE );
2224 PSA_ASSERT( psa_mac_abort( &operation ) );
2225
2226 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002227 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002228 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2229 TEST_EQUAL( psa_mac_verify_finish( &operation,
2230 verify_mac, sizeof( verify_mac ) ),
2231 PSA_ERROR_BAD_STATE );
2232 PSA_ASSERT( psa_mac_abort( &operation ) );
2233
2234 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002235 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002236 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2237 TEST_EQUAL( psa_mac_sign_finish( &operation,
2238 sign_mac, sizeof( sign_mac ),
2239 &sign_mac_length ),
2240 PSA_ERROR_BAD_STATE );
2241 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002242
Ronald Cron5425a212020-08-04 14:58:35 +02002243 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002244
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002245exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002246 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002247}
2248/* END_CASE */
2249
2250/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002251void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002252 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002253 int alg_arg,
2254 data_t *input,
2255 data_t *expected_mac )
2256{
Ronald Cron5425a212020-08-04 14:58:35 +02002257 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002258 psa_key_type_t key_type = key_type_arg;
2259 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002260 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002261 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002262 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002263 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002264 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002265 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002266 const size_t output_sizes_to_test[] = {
2267 0,
2268 1,
2269 expected_mac->len - 1,
2270 expected_mac->len,
2271 expected_mac->len + 1,
2272 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002273
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002274 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002275 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002276 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002277
Gilles Peskine8817f612018-12-18 00:18:46 +01002278 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002279
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002280 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002281 psa_set_key_algorithm( &attributes, alg );
2282 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002283
Ronald Cron5425a212020-08-04 14:58:35 +02002284 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2285 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002286
Gilles Peskine8b356b52020-08-25 23:44:59 +02002287 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2288 {
2289 const size_t output_size = output_sizes_to_test[i];
2290 psa_status_t expected_status =
2291 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2292 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002293
Chris Jones9634bb12021-01-20 15:56:42 +00002294 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002295 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002296
Gilles Peskine8b356b52020-08-25 23:44:59 +02002297 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002298 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002299 PSA_ASSERT( psa_mac_update( &operation,
2300 input->x, input->len ) );
2301 TEST_EQUAL( psa_mac_sign_finish( &operation,
2302 actual_mac, output_size,
2303 &mac_length ),
2304 expected_status );
2305 PSA_ASSERT( psa_mac_abort( &operation ) );
2306
2307 if( expected_status == PSA_SUCCESS )
2308 {
2309 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2310 actual_mac, mac_length );
2311 }
2312 mbedtls_free( actual_mac );
2313 actual_mac = NULL;
2314 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002315
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002316exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002317 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002318 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002319 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002320 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002321}
2322/* END_CASE */
2323
2324/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002325void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002326 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002327 int alg_arg,
2328 data_t *input,
2329 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002330{
Ronald Cron5425a212020-08-04 14:58:35 +02002331 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002332 psa_key_type_t key_type = key_type_arg;
2333 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002334 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002335 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002336 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002337
Gilles Peskine69c12672018-06-28 00:07:19 +02002338 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2339
Gilles Peskine8817f612018-12-18 00:18:46 +01002340 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002341
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002342 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002343 psa_set_key_algorithm( &attributes, alg );
2344 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002345
Ronald Cron5425a212020-08-04 14:58:35 +02002346 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2347 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002348
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002349 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002350 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002351 PSA_ASSERT( psa_mac_update( &operation,
2352 input->x, input->len ) );
2353 PSA_ASSERT( psa_mac_verify_finish( &operation,
2354 expected_mac->x,
2355 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002356
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002357 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002358 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002359 PSA_ASSERT( psa_mac_update( &operation,
2360 input->x, input->len ) );
2361 TEST_EQUAL( psa_mac_verify_finish( &operation,
2362 expected_mac->x,
2363 expected_mac->len - 1 ),
2364 PSA_ERROR_INVALID_SIGNATURE );
2365
2366 /* Test a MAC that's too long. */
2367 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2368 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002369 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002370 PSA_ASSERT( psa_mac_update( &operation,
2371 input->x, input->len ) );
2372 TEST_EQUAL( psa_mac_verify_finish( &operation,
2373 perturbed_mac,
2374 expected_mac->len + 1 ),
2375 PSA_ERROR_INVALID_SIGNATURE );
2376
2377 /* Test changing one byte. */
2378 for( size_t i = 0; i < expected_mac->len; i++ )
2379 {
Chris Jones9634bb12021-01-20 15:56:42 +00002380 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002381 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002382 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002383 PSA_ASSERT( psa_mac_update( &operation,
2384 input->x, input->len ) );
2385 TEST_EQUAL( psa_mac_verify_finish( &operation,
2386 perturbed_mac,
2387 expected_mac->len ),
2388 PSA_ERROR_INVALID_SIGNATURE );
2389 perturbed_mac[i] ^= 1;
2390 }
2391
Gilles Peskine8c9def32018-02-08 10:02:12 +01002392exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002393 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002394 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002395 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002396 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002397}
2398/* END_CASE */
2399
2400/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002401void cipher_operation_init( )
2402{
Jaeden Ameroab439972019-02-15 14:12:05 +00002403 const uint8_t input[1] = { 0 };
2404 unsigned char output[1] = { 0 };
2405 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002406 /* Test each valid way of initializing the object, except for `= {0}`, as
2407 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2408 * though it's OK by the C standard. We could test for this, but we'd need
2409 * to supress the Clang warning for the test. */
2410 psa_cipher_operation_t func = psa_cipher_operation_init( );
2411 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2412 psa_cipher_operation_t zero;
2413
2414 memset( &zero, 0, sizeof( zero ) );
2415
Jaeden Ameroab439972019-02-15 14:12:05 +00002416 /* A freshly-initialized cipher operation should not be usable. */
2417 TEST_EQUAL( psa_cipher_update( &func,
2418 input, sizeof( input ),
2419 output, sizeof( output ),
2420 &output_length ),
2421 PSA_ERROR_BAD_STATE );
2422 TEST_EQUAL( psa_cipher_update( &init,
2423 input, sizeof( input ),
2424 output, sizeof( output ),
2425 &output_length ),
2426 PSA_ERROR_BAD_STATE );
2427 TEST_EQUAL( psa_cipher_update( &zero,
2428 input, sizeof( input ),
2429 output, sizeof( output ),
2430 &output_length ),
2431 PSA_ERROR_BAD_STATE );
2432
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002433 /* A default cipher operation should be abortable without error. */
2434 PSA_ASSERT( psa_cipher_abort( &func ) );
2435 PSA_ASSERT( psa_cipher_abort( &init ) );
2436 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002437}
2438/* END_CASE */
2439
2440/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002441void cipher_setup( int key_type_arg,
2442 data_t *key,
2443 int alg_arg,
2444 int expected_status_arg )
2445{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002446 psa_key_type_t key_type = key_type_arg;
2447 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002448 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002449 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002450 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002451#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002452 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2453#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002454
Gilles Peskine8817f612018-12-18 00:18:46 +01002455 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002456
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002457 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2458 &operation, &status ) )
2459 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002460 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002461
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002462 /* The operation object should be reusable. */
2463#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2464 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2465 smoke_test_key_data,
2466 sizeof( smoke_test_key_data ),
2467 KNOWN_SUPPORTED_CIPHER_ALG,
2468 &operation, &status ) )
2469 goto exit;
2470 TEST_EQUAL( status, PSA_SUCCESS );
2471#endif
2472
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002473exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002474 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002475 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002476}
2477/* END_CASE */
2478
Ronald Cronee414c72021-03-18 18:50:08 +01002479/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002480void cipher_bad_order( )
2481{
Ronald Cron5425a212020-08-04 14:58:35 +02002482 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002483 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2484 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002485 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002486 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002487 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002488 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002489 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2490 0xaa, 0xaa, 0xaa, 0xaa };
2491 const uint8_t text[] = {
2492 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2493 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002494 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002495 size_t length = 0;
2496
2497 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002498 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2499 psa_set_key_algorithm( &attributes, alg );
2500 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002501 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2502 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002503
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002504 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002505 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2506 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002507 PSA_ERROR_BAD_STATE );
2508 PSA_ASSERT( psa_cipher_abort( &operation ) );
2509
2510 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002511 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2512 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002513 PSA_ERROR_BAD_STATE );
2514 PSA_ASSERT( psa_cipher_abort( &operation ) );
2515
Jaeden Ameroab439972019-02-15 14:12:05 +00002516 /* Generate an IV without calling setup beforehand. */
2517 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2518 buffer, sizeof( buffer ),
2519 &length ),
2520 PSA_ERROR_BAD_STATE );
2521 PSA_ASSERT( psa_cipher_abort( &operation ) );
2522
2523 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002524 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002525 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2526 buffer, sizeof( buffer ),
2527 &length ) );
2528 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2529 buffer, sizeof( buffer ),
2530 &length ),
2531 PSA_ERROR_BAD_STATE );
2532 PSA_ASSERT( psa_cipher_abort( &operation ) );
2533
2534 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002535 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002536 PSA_ASSERT( psa_cipher_set_iv( &operation,
2537 iv, sizeof( iv ) ) );
2538 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2539 buffer, sizeof( buffer ),
2540 &length ),
2541 PSA_ERROR_BAD_STATE );
2542 PSA_ASSERT( psa_cipher_abort( &operation ) );
2543
2544 /* Set an IV without calling setup beforehand. */
2545 TEST_EQUAL( psa_cipher_set_iv( &operation,
2546 iv, sizeof( iv ) ),
2547 PSA_ERROR_BAD_STATE );
2548 PSA_ASSERT( psa_cipher_abort( &operation ) );
2549
2550 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002551 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002552 PSA_ASSERT( psa_cipher_set_iv( &operation,
2553 iv, sizeof( iv ) ) );
2554 TEST_EQUAL( psa_cipher_set_iv( &operation,
2555 iv, sizeof( iv ) ),
2556 PSA_ERROR_BAD_STATE );
2557 PSA_ASSERT( psa_cipher_abort( &operation ) );
2558
2559 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002560 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002561 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2562 buffer, sizeof( buffer ),
2563 &length ) );
2564 TEST_EQUAL( psa_cipher_set_iv( &operation,
2565 iv, sizeof( iv ) ),
2566 PSA_ERROR_BAD_STATE );
2567 PSA_ASSERT( psa_cipher_abort( &operation ) );
2568
2569 /* Call update without calling setup beforehand. */
2570 TEST_EQUAL( psa_cipher_update( &operation,
2571 text, sizeof( text ),
2572 buffer, sizeof( buffer ),
2573 &length ),
2574 PSA_ERROR_BAD_STATE );
2575 PSA_ASSERT( psa_cipher_abort( &operation ) );
2576
2577 /* Call update without an IV where an IV is required. */
2578 TEST_EQUAL( psa_cipher_update( &operation,
2579 text, sizeof( text ),
2580 buffer, sizeof( buffer ),
2581 &length ),
2582 PSA_ERROR_BAD_STATE );
2583 PSA_ASSERT( psa_cipher_abort( &operation ) );
2584
2585 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002586 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002587 PSA_ASSERT( psa_cipher_set_iv( &operation,
2588 iv, sizeof( iv ) ) );
2589 PSA_ASSERT( psa_cipher_finish( &operation,
2590 buffer, sizeof( buffer ), &length ) );
2591 TEST_EQUAL( psa_cipher_update( &operation,
2592 text, sizeof( text ),
2593 buffer, sizeof( buffer ),
2594 &length ),
2595 PSA_ERROR_BAD_STATE );
2596 PSA_ASSERT( psa_cipher_abort( &operation ) );
2597
2598 /* Call finish without calling setup beforehand. */
2599 TEST_EQUAL( psa_cipher_finish( &operation,
2600 buffer, sizeof( buffer ), &length ),
2601 PSA_ERROR_BAD_STATE );
2602 PSA_ASSERT( psa_cipher_abort( &operation ) );
2603
2604 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002605 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002606 /* Not calling update means we are encrypting an empty buffer, which is OK
2607 * for cipher modes with padding. */
2608 TEST_EQUAL( psa_cipher_finish( &operation,
2609 buffer, sizeof( buffer ), &length ),
2610 PSA_ERROR_BAD_STATE );
2611 PSA_ASSERT( psa_cipher_abort( &operation ) );
2612
2613 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002614 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002615 PSA_ASSERT( psa_cipher_set_iv( &operation,
2616 iv, sizeof( iv ) ) );
2617 PSA_ASSERT( psa_cipher_finish( &operation,
2618 buffer, sizeof( buffer ), &length ) );
2619 TEST_EQUAL( psa_cipher_finish( &operation,
2620 buffer, sizeof( buffer ), &length ),
2621 PSA_ERROR_BAD_STATE );
2622 PSA_ASSERT( psa_cipher_abort( &operation ) );
2623
Ronald Cron5425a212020-08-04 14:58:35 +02002624 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002625
Jaeden Ameroab439972019-02-15 14:12:05 +00002626exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002627 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002628 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002629}
2630/* END_CASE */
2631
2632/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002633void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002634 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002635 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002636 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002637{
Ronald Cron5425a212020-08-04 14:58:35 +02002638 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002639 psa_status_t status;
2640 psa_key_type_t key_type = key_type_arg;
2641 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002642 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002643 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002644 size_t output_buffer_size = 0;
2645 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002646 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002647 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002648 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002649
Gilles Peskine8817f612018-12-18 00:18:46 +01002650 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002651
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002652 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2653 psa_set_key_algorithm( &attributes, alg );
2654 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002655
Ronald Cron5425a212020-08-04 14:58:35 +02002656 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2657 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002658
Ronald Cron5425a212020-08-04 14:58:35 +02002659 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002660
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002661 if( iv->len > 0 )
2662 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002663 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002664 }
2665
gabor-mezei-armceface22021-01-21 12:26:17 +01002666 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2667 TEST_ASSERT( output_buffer_size <=
2668 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002669 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002670
Gilles Peskine8817f612018-12-18 00:18:46 +01002671 PSA_ASSERT( psa_cipher_update( &operation,
2672 input->x, input->len,
2673 output, output_buffer_size,
2674 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002675 TEST_ASSERT( function_output_length <=
2676 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2677 TEST_ASSERT( function_output_length <=
2678 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002679 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002680
Gilles Peskine50e586b2018-06-08 14:28:46 +02002681 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002682 ( output_buffer_size == 0 ? NULL :
2683 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002684 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002685 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002686 TEST_ASSERT( function_output_length <=
2687 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2688 TEST_ASSERT( function_output_length <=
2689 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002690 total_output_length += function_output_length;
2691
Gilles Peskinefe11b722018-12-18 00:24:04 +01002692 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002693 if( expected_status == PSA_SUCCESS )
2694 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002695 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002696 ASSERT_COMPARE( expected_output->x, expected_output->len,
2697 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002698 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002699
Gilles Peskine50e586b2018-06-08 14:28:46 +02002700exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002701 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002702 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002703 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002704 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002705}
2706/* END_CASE */
2707
2708/* BEGIN_CASE */
2709void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002710 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002711 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002712 int first_part_size_arg,
2713 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002714 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002715{
Ronald Cron5425a212020-08-04 14:58:35 +02002716 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002717 psa_key_type_t key_type = key_type_arg;
2718 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002719 size_t first_part_size = first_part_size_arg;
2720 size_t output1_length = output1_length_arg;
2721 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002722 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002723 size_t output_buffer_size = 0;
2724 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002725 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002726 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002727 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002728
Gilles Peskine8817f612018-12-18 00:18:46 +01002729 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002730
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002731 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2732 psa_set_key_algorithm( &attributes, alg );
2733 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002734
Ronald Cron5425a212020-08-04 14:58:35 +02002735 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2736 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002737
Ronald Cron5425a212020-08-04 14:58:35 +02002738 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002739
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002740 if( iv->len > 0 )
2741 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002742 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002743 }
2744
gabor-mezei-armceface22021-01-21 12:26:17 +01002745 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2746 TEST_ASSERT( output_buffer_size <=
2747 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002748 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002749
Gilles Peskinee0866522019-02-19 19:44:00 +01002750 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002751 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2752 output, output_buffer_size,
2753 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002754 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002755 TEST_ASSERT( function_output_length <=
2756 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2757 TEST_ASSERT( function_output_length <=
2758 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002759 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002760
Gilles Peskine8817f612018-12-18 00:18:46 +01002761 PSA_ASSERT( psa_cipher_update( &operation,
2762 input->x + first_part_size,
2763 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002764 ( output_buffer_size == 0 ? NULL :
2765 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002766 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002767 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002768 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002769 TEST_ASSERT( function_output_length <=
2770 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2771 alg,
2772 input->len - first_part_size ) );
2773 TEST_ASSERT( function_output_length <=
2774 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002775 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002776
Gilles Peskine8817f612018-12-18 00:18:46 +01002777 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002778 ( output_buffer_size == 0 ? NULL :
2779 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002780 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002781 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002782 TEST_ASSERT( function_output_length <=
2783 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2784 TEST_ASSERT( function_output_length <=
2785 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002786 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002787 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002788
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002789 ASSERT_COMPARE( expected_output->x, expected_output->len,
2790 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002791
2792exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002793 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002794 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002795 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002796 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002797}
2798/* END_CASE */
2799
2800/* BEGIN_CASE */
2801void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002802 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002803 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002804 int first_part_size_arg,
2805 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002806 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002807{
Ronald Cron5425a212020-08-04 14:58:35 +02002808 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002809 psa_key_type_t key_type = key_type_arg;
2810 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002811 size_t first_part_size = first_part_size_arg;
2812 size_t output1_length = output1_length_arg;
2813 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002814 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002815 size_t output_buffer_size = 0;
2816 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002817 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002818 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002819 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002820
Gilles Peskine8817f612018-12-18 00:18:46 +01002821 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002822
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002823 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2824 psa_set_key_algorithm( &attributes, alg );
2825 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002826
Ronald Cron5425a212020-08-04 14:58:35 +02002827 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2828 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002829
Ronald Cron5425a212020-08-04 14:58:35 +02002830 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002831
Steven Cooreman177deba2020-09-07 17:14:14 +02002832 if( iv->len > 0 )
2833 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002834 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002835 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002836
gabor-mezei-armceface22021-01-21 12:26:17 +01002837 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2838 TEST_ASSERT( output_buffer_size <=
2839 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002840 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002841
Gilles Peskinee0866522019-02-19 19:44:00 +01002842 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002843 PSA_ASSERT( psa_cipher_update( &operation,
2844 input->x, first_part_size,
2845 output, output_buffer_size,
2846 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002847 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002848 TEST_ASSERT( function_output_length <=
2849 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2850 TEST_ASSERT( function_output_length <=
2851 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002852 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002853
Gilles Peskine8817f612018-12-18 00:18:46 +01002854 PSA_ASSERT( psa_cipher_update( &operation,
2855 input->x + first_part_size,
2856 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002857 ( output_buffer_size == 0 ? NULL :
2858 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002859 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002860 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002861 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002862 TEST_ASSERT( function_output_length <=
2863 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2864 alg,
2865 input->len - first_part_size ) );
2866 TEST_ASSERT( function_output_length <=
2867 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002868 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002869
Gilles Peskine8817f612018-12-18 00:18:46 +01002870 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002871 ( output_buffer_size == 0 ? NULL :
2872 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002873 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002874 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002875 TEST_ASSERT( function_output_length <=
2876 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2877 TEST_ASSERT( function_output_length <=
2878 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002879 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002880 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002881
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002882 ASSERT_COMPARE( expected_output->x, expected_output->len,
2883 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002884
2885exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002886 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002887 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002888 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002889 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002890}
2891/* END_CASE */
2892
Gilles Peskine50e586b2018-06-08 14:28:46 +02002893/* BEGIN_CASE */
2894void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002895 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002896 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002897 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002898{
Ronald Cron5425a212020-08-04 14:58:35 +02002899 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002900 psa_status_t status;
2901 psa_key_type_t key_type = key_type_arg;
2902 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002903 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002904 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002905 size_t output_buffer_size = 0;
2906 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002907 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002908 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002909 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002910
Gilles Peskine8817f612018-12-18 00:18:46 +01002911 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002912
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002913 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2914 psa_set_key_algorithm( &attributes, alg );
2915 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002916
Ronald Cron5425a212020-08-04 14:58:35 +02002917 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2918 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002919
Ronald Cron5425a212020-08-04 14:58:35 +02002920 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002921
Steven Cooreman177deba2020-09-07 17:14:14 +02002922 if( iv->len > 0 )
2923 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002924 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002925 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002926
gabor-mezei-armceface22021-01-21 12:26:17 +01002927 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2928 TEST_ASSERT( output_buffer_size <=
2929 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002930 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002931
Gilles Peskine8817f612018-12-18 00:18:46 +01002932 PSA_ASSERT( psa_cipher_update( &operation,
2933 input->x, input->len,
2934 output, output_buffer_size,
2935 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002936 TEST_ASSERT( function_output_length <=
2937 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2938 TEST_ASSERT( function_output_length <=
2939 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002940 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002941
Gilles Peskine50e586b2018-06-08 14:28:46 +02002942 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002943 ( output_buffer_size == 0 ? NULL :
2944 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002945 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002946 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002947 TEST_ASSERT( function_output_length <=
2948 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2949 TEST_ASSERT( function_output_length <=
2950 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002951 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002952 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002953
2954 if( expected_status == PSA_SUCCESS )
2955 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002956 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002957 ASSERT_COMPARE( expected_output->x, expected_output->len,
2958 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002959 }
2960
Gilles Peskine50e586b2018-06-08 14:28:46 +02002961exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002962 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002963 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002964 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002965 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002966}
2967/* END_CASE */
2968
Gilles Peskine50e586b2018-06-08 14:28:46 +02002969/* BEGIN_CASE */
2970void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002971 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002972 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002973{
Ronald Cron5425a212020-08-04 14:58:35 +02002974 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002975 psa_key_type_t key_type = key_type_arg;
2976 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002977 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002978 size_t iv_size = 16;
2979 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002980 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002981 size_t output1_size = 0;
2982 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002983 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002984 size_t output2_size = 0;
2985 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002986 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002987 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2988 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002989 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002990
Gilles Peskine8817f612018-12-18 00:18:46 +01002991 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002992
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002993 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2994 psa_set_key_algorithm( &attributes, alg );
2995 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002996
Ronald Cron5425a212020-08-04 14:58:35 +02002997 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2998 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002999
Ronald Cron5425a212020-08-04 14:58:35 +02003000 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3001 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003002
Steven Cooreman177deba2020-09-07 17:14:14 +02003003 if( alg != PSA_ALG_ECB_NO_PADDING )
3004 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003005 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3006 iv, iv_size,
3007 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003008 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003009 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3010 TEST_ASSERT( output1_size <=
3011 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003012 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003013
Gilles Peskine8817f612018-12-18 00:18:46 +01003014 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3015 output1, output1_size,
3016 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003017 TEST_ASSERT( output1_length <=
3018 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3019 TEST_ASSERT( output1_length <=
3020 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3021
Gilles Peskine8817f612018-12-18 00:18:46 +01003022 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003023 output1 + output1_length,
3024 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003025 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003026 TEST_ASSERT( function_output_length <=
3027 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3028 TEST_ASSERT( function_output_length <=
3029 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003030
Gilles Peskine048b7f02018-06-08 14:20:49 +02003031 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003032
Gilles Peskine8817f612018-12-18 00:18:46 +01003033 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003034
3035 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003036 TEST_ASSERT( output2_size <=
3037 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3038 TEST_ASSERT( output2_size <=
3039 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003040 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003041
Steven Cooreman177deba2020-09-07 17:14:14 +02003042 if( iv_length > 0 )
3043 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003044 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3045 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003046 }
3047
Gilles Peskine8817f612018-12-18 00:18:46 +01003048 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3049 output2, output2_size,
3050 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003051 TEST_ASSERT( output2_length <=
3052 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
3053 TEST_ASSERT( output2_length <=
3054 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
3055
Gilles Peskine048b7f02018-06-08 14:20:49 +02003056 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003057 PSA_ASSERT( psa_cipher_finish( &operation2,
3058 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003059 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003060 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003061 TEST_ASSERT( function_output_length <=
3062 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3063 TEST_ASSERT( function_output_length <=
3064 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03003065
Gilles Peskine048b7f02018-06-08 14:20:49 +02003066 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003067
Gilles Peskine8817f612018-12-18 00:18:46 +01003068 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003069
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003070 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003071
3072exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003073 psa_cipher_abort( &operation1 );
3074 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003075 mbedtls_free( output1 );
3076 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003077 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003078 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003079}
3080/* END_CASE */
3081
3082/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003083void cipher_verify_output_multipart( int alg_arg,
3084 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003085 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003086 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003087 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003088{
Ronald Cron5425a212020-08-04 14:58:35 +02003089 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003090 psa_key_type_t key_type = key_type_arg;
3091 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003092 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003093 unsigned char iv[16] = {0};
3094 size_t iv_size = 16;
3095 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003096 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003097 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003098 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003099 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003100 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003101 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003102 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003103 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3104 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003105 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003106
Gilles Peskine8817f612018-12-18 00:18:46 +01003107 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003108
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003109 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3110 psa_set_key_algorithm( &attributes, alg );
3111 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003112
Ronald Cron5425a212020-08-04 14:58:35 +02003113 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3114 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003115
Ronald Cron5425a212020-08-04 14:58:35 +02003116 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3117 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003118
Steven Cooreman177deba2020-09-07 17:14:14 +02003119 if( alg != PSA_ALG_ECB_NO_PADDING )
3120 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003121 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3122 iv, iv_size,
3123 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003124 }
3125
gabor-mezei-armceface22021-01-21 12:26:17 +01003126 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3127 TEST_ASSERT( output1_buffer_size <=
3128 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003129 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003130
Gilles Peskinee0866522019-02-19 19:44:00 +01003131 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003132
Gilles Peskine8817f612018-12-18 00:18:46 +01003133 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3134 output1, output1_buffer_size,
3135 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003136 TEST_ASSERT( function_output_length <=
3137 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3138 TEST_ASSERT( function_output_length <=
3139 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003140 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003141
Gilles Peskine8817f612018-12-18 00:18:46 +01003142 PSA_ASSERT( psa_cipher_update( &operation1,
3143 input->x + first_part_size,
3144 input->len - first_part_size,
3145 output1, output1_buffer_size,
3146 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003147 TEST_ASSERT( function_output_length <=
3148 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3149 alg,
3150 input->len - first_part_size ) );
3151 TEST_ASSERT( function_output_length <=
3152 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003153 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003154
Gilles Peskine8817f612018-12-18 00:18:46 +01003155 PSA_ASSERT( psa_cipher_finish( &operation1,
3156 output1 + output1_length,
3157 output1_buffer_size - output1_length,
3158 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003159 TEST_ASSERT( function_output_length <=
3160 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3161 TEST_ASSERT( function_output_length <=
3162 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003163 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003164
Gilles Peskine8817f612018-12-18 00:18:46 +01003165 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003166
Gilles Peskine048b7f02018-06-08 14:20:49 +02003167 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003168 TEST_ASSERT( output2_buffer_size <=
3169 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3170 TEST_ASSERT( output2_buffer_size <=
3171 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003172 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003173
Steven Cooreman177deba2020-09-07 17:14:14 +02003174 if( iv_length > 0 )
3175 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003176 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3177 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003178 }
Moran Pekerded84402018-06-06 16:36:50 +03003179
Gilles Peskine8817f612018-12-18 00:18:46 +01003180 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3181 output2, output2_buffer_size,
3182 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003183 TEST_ASSERT( function_output_length <=
3184 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3185 TEST_ASSERT( function_output_length <=
3186 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003187 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003188
Gilles Peskine8817f612018-12-18 00:18:46 +01003189 PSA_ASSERT( psa_cipher_update( &operation2,
3190 output1 + first_part_size,
3191 output1_length - first_part_size,
3192 output2, output2_buffer_size,
3193 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003194 TEST_ASSERT( function_output_length <=
3195 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3196 alg,
3197 output1_length - first_part_size ) );
3198 TEST_ASSERT( function_output_length <=
3199 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003200 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003201
Gilles Peskine8817f612018-12-18 00:18:46 +01003202 PSA_ASSERT( psa_cipher_finish( &operation2,
3203 output2 + output2_length,
3204 output2_buffer_size - output2_length,
3205 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003206 TEST_ASSERT( function_output_length <=
3207 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3208 TEST_ASSERT( function_output_length <=
3209 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003210 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003211
Gilles Peskine8817f612018-12-18 00:18:46 +01003212 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003213
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003214 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003215
3216exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003217 psa_cipher_abort( &operation1 );
3218 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003219 mbedtls_free( output1 );
3220 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003221 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003222 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003223}
3224/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003225
Gilles Peskine20035e32018-02-03 22:44:14 +01003226/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003227void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003228 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003229 data_t *nonce,
3230 data_t *additional_data,
3231 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003232 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003233{
Ronald Cron5425a212020-08-04 14:58:35 +02003234 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003235 psa_key_type_t key_type = key_type_arg;
3236 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003237 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003238 unsigned char *output_data = NULL;
3239 size_t output_size = 0;
3240 size_t output_length = 0;
3241 unsigned char *output_data2 = NULL;
3242 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003243 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003244 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003245 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003246
Gilles Peskine8817f612018-12-18 00:18:46 +01003247 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003248
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003249 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3250 psa_set_key_algorithm( &attributes, alg );
3251 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003252
Gilles Peskine049c7532019-05-15 20:22:09 +02003253 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003254 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003255 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3256 key_bits = psa_get_key_bits( &attributes );
3257
3258 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3259 alg );
3260 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3261 * should be exact. */
3262 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3263 expected_result != PSA_ERROR_NOT_SUPPORTED )
3264 {
3265 TEST_EQUAL( output_size,
3266 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3267 TEST_ASSERT( output_size <=
3268 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3269 }
3270 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003271
Steven Cooremanf49478b2021-02-15 15:19:25 +01003272 status = psa_aead_encrypt( key, alg,
3273 nonce->x, nonce->len,
3274 additional_data->x,
3275 additional_data->len,
3276 input_data->x, input_data->len,
3277 output_data, output_size,
3278 &output_length );
3279
3280 /* If the operation is not supported, just skip and not fail in case the
3281 * encryption involves a common limitation of cryptography hardwares and
3282 * an alternative implementation. */
3283 if( status == PSA_ERROR_NOT_SUPPORTED )
3284 {
3285 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3286 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3287 }
3288
3289 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003290
3291 if( PSA_SUCCESS == expected_result )
3292 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003293 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003294
Gilles Peskine003a4a92019-05-14 16:09:40 +02003295 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3296 * should be exact. */
3297 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003298 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003299
gabor-mezei-armceface22021-01-21 12:26:17 +01003300 TEST_ASSERT( input_data->len <=
3301 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3302
Ronald Cron5425a212020-08-04 14:58:35 +02003303 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003304 nonce->x, nonce->len,
3305 additional_data->x,
3306 additional_data->len,
3307 output_data, output_length,
3308 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003309 &output_length2 ),
3310 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003311
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003312 ASSERT_COMPARE( input_data->x, input_data->len,
3313 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003314 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003315
Gilles Peskinea1cac842018-06-11 19:33:02 +02003316exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003317 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003318 mbedtls_free( output_data );
3319 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003320 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003321}
3322/* END_CASE */
3323
3324/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003325void aead_encrypt( int key_type_arg, data_t *key_data,
3326 int alg_arg,
3327 data_t *nonce,
3328 data_t *additional_data,
3329 data_t *input_data,
3330 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003331{
Ronald Cron5425a212020-08-04 14:58:35 +02003332 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003333 psa_key_type_t key_type = key_type_arg;
3334 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003335 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003336 unsigned char *output_data = NULL;
3337 size_t output_size = 0;
3338 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003339 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003340 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003341
Gilles Peskine8817f612018-12-18 00:18:46 +01003342 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003343
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003344 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3345 psa_set_key_algorithm( &attributes, alg );
3346 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003347
Gilles Peskine049c7532019-05-15 20:22:09 +02003348 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003349 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003350 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3351 key_bits = psa_get_key_bits( &attributes );
3352
3353 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3354 alg );
3355 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3356 * should be exact. */
3357 TEST_EQUAL( output_size,
3358 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3359 TEST_ASSERT( output_size <=
3360 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3361 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003362
Steven Cooremand588ea12021-01-11 19:36:04 +01003363 status = psa_aead_encrypt( key, alg,
3364 nonce->x, nonce->len,
3365 additional_data->x, additional_data->len,
3366 input_data->x, input_data->len,
3367 output_data, output_size,
3368 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003369
Ronald Cron28a45ed2021-02-09 20:35:42 +01003370 /* If the operation is not supported, just skip and not fail in case the
3371 * encryption involves a common limitation of cryptography hardwares and
3372 * an alternative implementation. */
3373 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003374 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003375 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3376 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003377 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003378
3379 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003380 ASSERT_COMPARE( expected_result->x, expected_result->len,
3381 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003382
Gilles Peskinea1cac842018-06-11 19:33:02 +02003383exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003384 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003385 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003386 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003387}
3388/* END_CASE */
3389
3390/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003391void aead_decrypt( int key_type_arg, data_t *key_data,
3392 int alg_arg,
3393 data_t *nonce,
3394 data_t *additional_data,
3395 data_t *input_data,
3396 data_t *expected_data,
3397 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003398{
Ronald Cron5425a212020-08-04 14:58:35 +02003399 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003400 psa_key_type_t key_type = key_type_arg;
3401 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003402 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003403 unsigned char *output_data = NULL;
3404 size_t output_size = 0;
3405 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003406 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003407 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003408 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003409
Gilles Peskine8817f612018-12-18 00:18:46 +01003410 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003411
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003412 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3413 psa_set_key_algorithm( &attributes, alg );
3414 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003415
Gilles Peskine049c7532019-05-15 20:22:09 +02003416 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003417 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003418 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3419 key_bits = psa_get_key_bits( &attributes );
3420
3421 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3422 alg );
3423 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3424 expected_result != PSA_ERROR_NOT_SUPPORTED )
3425 {
3426 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3427 * should be exact. */
3428 TEST_EQUAL( output_size,
3429 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3430 TEST_ASSERT( output_size <=
3431 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3432 }
3433 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003434
Steven Cooremand588ea12021-01-11 19:36:04 +01003435 status = psa_aead_decrypt( key, alg,
3436 nonce->x, nonce->len,
3437 additional_data->x,
3438 additional_data->len,
3439 input_data->x, input_data->len,
3440 output_data, output_size,
3441 &output_length );
3442
Ronald Cron28a45ed2021-02-09 20:35:42 +01003443 /* If the operation is not supported, just skip and not fail in case the
3444 * decryption involves a common limitation of cryptography hardwares and
3445 * an alternative implementation. */
3446 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003447 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003448 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3449 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003450 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003451
3452 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003453
Gilles Peskine2d277862018-06-18 15:41:12 +02003454 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003455 ASSERT_COMPARE( expected_data->x, expected_data->len,
3456 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003457
Gilles Peskinea1cac842018-06-11 19:33:02 +02003458exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003459 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003460 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003461 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003462}
3463/* END_CASE */
3464
3465/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003466void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3467 int alg_arg,
3468 data_t *nonce,
3469 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003470 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003471 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003472 int do_test_data_chunked,
3473 int do_set_lengths,
3474 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003475{
Paul Elliottd3f82412021-06-16 16:52:21 +01003476 size_t ad_part_len = 0;
3477 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01003478 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003479
Paul Elliotte64deda2021-09-09 14:07:23 +01003480 /* Ensure that either one part of the test or the other is done, i.e this
3481 * test does something. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003482 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3483
3484 /* Temporary whilst we have algorithms that cannot support chunking */
3485 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003486 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003487 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3488 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003489 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003490 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003491
Paul Elliott33746aa2021-09-15 16:40:40 +01003492 if( do_set_lengths )
3493 {
3494 if( ad_part_len & 0x01 )
3495 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3496 else
3497 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3498 }
3499
Paul Elliott329d5382021-07-22 17:10:45 +01003500 /* Split ad into length(ad_part_len) parts. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003501 if( !aead_multipart_internal_func( key_type_arg, key_data,
3502 alg_arg, nonce,
3503 additional_data,
3504 ad_part_len,
3505 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003506 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003507 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003508 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003509 break;
3510
3511 /* length(0) part, length(ad_part_len) part, length(0) part... */
3512 mbedtls_test_set_step( 1000 + ad_part_len );
3513
3514 if( !aead_multipart_internal_func( key_type_arg, key_data,
3515 alg_arg, nonce,
3516 additional_data,
3517 ad_part_len,
3518 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003519 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003520 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003521 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003522 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003523 }
3524 }
Paul Elliottd3f82412021-06-16 16:52:21 +01003525
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003526 /* Temporary whilst we have algorithms that cannot support chunking */
3527 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003528 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003529 for( data_part_len = 1; data_part_len <= input_data->len;
3530 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003531 {
Paul Elliott329d5382021-07-22 17:10:45 +01003532 /* Split data into length(data_part_len) parts. */
3533 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003534
Paul Elliott33746aa2021-09-15 16:40:40 +01003535 if( do_set_lengths )
3536 {
3537 if( data_part_len & 0x01 )
3538 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3539 else
3540 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3541 }
3542
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003543 if( !aead_multipart_internal_func( key_type_arg, key_data,
3544 alg_arg, nonce,
3545 additional_data, -1,
3546 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003547 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003548 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003549 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003550 break;
3551
3552 /* length(0) part, length(data_part_len) part, length(0) part... */
3553 mbedtls_test_set_step( 3000 + data_part_len );
3554
3555 if( !aead_multipart_internal_func( key_type_arg, key_data,
3556 alg_arg, nonce,
3557 additional_data, -1,
3558 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003559 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003560 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003561 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003562 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003563 }
3564 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003565
Paul Elliott0023e0a2021-04-27 10:06:22 +01003566
Paul Elliott8fc45162021-06-23 16:06:01 +01003567 /* Goto is required to silence warnings about unused labels, as we
3568 * don't actually do any test assertions in this function. */
3569 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003570}
3571/* END_CASE */
3572
3573/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003574void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3575 int alg_arg,
3576 data_t *nonce,
3577 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003578 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003579 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003580 int do_test_data_chunked,
3581 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01003582 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003583{
Paul Elliottd3f82412021-06-16 16:52:21 +01003584 size_t ad_part_len = 0;
3585 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01003586 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003587
Paul Elliotte64deda2021-09-09 14:07:23 +01003588 /* Ensure that either one part of the test or the other is done, i.e this
3589 * test does something. */
3590 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3591
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003592 /* Temporary whilst we have algorithms that cannot support chunking */
3593 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003594 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003595 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3596 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003597 {
Paul Elliott329d5382021-07-22 17:10:45 +01003598 /* Split ad into length(ad_part_len) parts. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003599 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003600
Paul Elliott33746aa2021-09-15 16:40:40 +01003601 if( do_set_lengths )
3602 {
3603 if( ad_part_len & 0x01 )
3604 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3605 else
3606 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3607 }
3608
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003609 if( !aead_multipart_internal_func( key_type_arg, key_data,
3610 alg_arg, nonce,
3611 additional_data,
3612 ad_part_len,
3613 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003614 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003615 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003616 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003617 break;
3618
3619 /* length(0) part, length(ad_part_len) part, length(0) part... */
3620 mbedtls_test_set_step( 1000 + ad_part_len );
3621
3622 if( !aead_multipart_internal_func( key_type_arg, key_data,
3623 alg_arg, nonce,
3624 additional_data,
3625 ad_part_len,
3626 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003627 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003628 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003629 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003630 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003631 }
3632 }
3633
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003634 /* Temporary whilst we have algorithms that cannot support chunking */
3635 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003636 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003637 for( data_part_len = 1; data_part_len <= input_data->len;
3638 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003639 {
Paul Elliott329d5382021-07-22 17:10:45 +01003640 /* Split data into length(data_part_len) parts. */
3641 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003642
Paul Elliott33746aa2021-09-15 16:40:40 +01003643 if( do_set_lengths )
3644 {
3645 if( data_part_len & 0x01 )
3646 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3647 else
3648 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3649 }
3650
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003651 if( !aead_multipart_internal_func( key_type_arg, key_data,
3652 alg_arg, nonce,
3653 additional_data, -1,
3654 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003655 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003656 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003657 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003658 break;
3659
3660 /* length(0) part, length(data_part_len) part, length(0) part... */
3661 mbedtls_test_set_step( 3000 + data_part_len );
3662
3663 if( !aead_multipart_internal_func( key_type_arg, key_data,
3664 alg_arg, nonce,
3665 additional_data, -1,
3666 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003667 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003668 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003669 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003670 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003671 }
3672 }
3673
Paul Elliott8fc45162021-06-23 16:06:01 +01003674 /* Goto is required to silence warnings about unused labels, as we
3675 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003676 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003677}
3678/* END_CASE */
3679
3680/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003681void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
3682 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01003683 int nonce_length,
3684 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003685 data_t *additional_data,
3686 data_t *input_data,
3687 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003688{
3689
3690 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3691 psa_key_type_t key_type = key_type_arg;
3692 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01003693 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003694 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3695 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3696 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01003697 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01003698 size_t actual_nonce_length = 0;
3699 size_t expected_nonce_length = expected_nonce_length_arg;
3700 unsigned char *output = NULL;
3701 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003702 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01003703 size_t ciphertext_size = 0;
3704 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003705 size_t tag_length = 0;
3706 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003707
3708 PSA_ASSERT( psa_crypto_init( ) );
3709
3710 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
3711 psa_set_key_algorithm( & attributes, alg );
3712 psa_set_key_type( & attributes, key_type );
3713
3714 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3715 &key ) );
3716
3717 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3718
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003719 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3720
Paul Elliottf1277632021-08-24 18:11:37 +01003721 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003722
Paul Elliottf1277632021-08-24 18:11:37 +01003723 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003724
Paul Elliottf1277632021-08-24 18:11:37 +01003725 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003726
Paul Elliottf1277632021-08-24 18:11:37 +01003727 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003728
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003729 status = psa_aead_encrypt_setup( &operation, key, alg );
3730
3731 /* If the operation is not supported, just skip and not fail in case the
3732 * encryption involves a common limitation of cryptography hardwares and
3733 * an alternative implementation. */
3734 if( status == PSA_ERROR_NOT_SUPPORTED )
3735 {
3736 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01003737 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003738 }
3739
3740 PSA_ASSERT( status );
3741
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003742 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01003743 nonce_length,
3744 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003745
Paul Elliott693bf312021-07-23 17:40:41 +01003746 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003747
Paul Elliottf1277632021-08-24 18:11:37 +01003748 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01003749
Paul Elliottf1277632021-08-24 18:11:37 +01003750 TEST_ASSERT( actual_nonce_length < PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01003751
Paul Elliott693bf312021-07-23 17:40:41 +01003752 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003753 {
3754
3755 /* Ensure we can still complete operation. */
3756
3757 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3758 additional_data->len ) );
3759
3760 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01003761 output, output_size,
3762 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003763
Paul Elliottf1277632021-08-24 18:11:37 +01003764 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3765 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003766 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3767 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003768
3769exit:
3770 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01003771 mbedtls_free( output );
3772 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003773 psa_aead_abort( &operation );
3774 PSA_DONE( );
3775}
3776/* END_CASE */
3777
3778/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01003779void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
3780 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01003781 int nonce_length_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01003782 data_t *additional_data,
3783 data_t *input_data,
3784 int expected_status_arg )
3785{
3786
3787 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3788 psa_key_type_t key_type = key_type_arg;
3789 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01003790 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01003791 uint8_t *nonce_buffer = NULL;
3792 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3793 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3794 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003795 unsigned char *output = NULL;
3796 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01003797 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01003798 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003799 size_t ciphertext_size = 0;
3800 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003801 size_t tag_length = 0;
3802 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01003803 size_t index = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003804
3805 PSA_ASSERT( psa_crypto_init( ) );
3806
3807 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3808 psa_set_key_algorithm( &attributes, alg );
3809 psa_set_key_type( &attributes, key_type );
3810
3811 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3812 &key ) );
3813
3814 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3815
3816 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3817
Paul Elliott6f0e7202021-08-25 12:57:18 +01003818 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003819
Paul Elliott6f0e7202021-08-25 12:57:18 +01003820 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01003821
Paul Elliott6f0e7202021-08-25 12:57:18 +01003822 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01003823
Paul Elliott6f0e7202021-08-25 12:57:18 +01003824 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003825
Paul Elliott863864a2021-07-23 17:28:31 +01003826 status = psa_aead_encrypt_setup( &operation, key, alg );
3827
3828 /* If the operation is not supported, just skip and not fail in case the
3829 * encryption involves a common limitation of cryptography hardwares and
3830 * an alternative implementation. */
3831 if( status == PSA_ERROR_NOT_SUPPORTED )
3832 {
3833 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003834 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01003835 }
3836
3837 PSA_ASSERT( status );
3838
Paul Elliott4023ffd2021-09-10 16:21:22 +01003839 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
3840 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01003841 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01003842 /* Arbitrary size buffer, to test zero length valid buffer. */
3843 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003844 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01003845 }
3846 else
3847 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003848 /* If length is zero, then this will return NULL. */
3849 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003850 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01003851
Paul Elliott4023ffd2021-09-10 16:21:22 +01003852 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01003853 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003854 for( index = 0; index < nonce_length - 1; ++index )
3855 {
3856 nonce_buffer[index] = 'a' + index;
3857 }
Paul Elliott66696b52021-08-16 18:42:41 +01003858 }
Paul Elliott863864a2021-07-23 17:28:31 +01003859 }
3860
Paul Elliott6f0e7202021-08-25 12:57:18 +01003861 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01003862
Paul Elliott693bf312021-07-23 17:40:41 +01003863 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01003864
3865 if( expected_status == PSA_SUCCESS )
3866 {
3867 /* Ensure we can still complete operation. */
3868
3869 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3870 additional_data->len ) );
3871
3872 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01003873 output, output_size,
3874 &ciphertext_length ) );
Paul Elliott863864a2021-07-23 17:28:31 +01003875
Paul Elliott6f0e7202021-08-25 12:57:18 +01003876 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3877 &ciphertext_length, tag_buffer,
Paul Elliott863864a2021-07-23 17:28:31 +01003878 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3879 }
3880
3881exit:
3882 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01003883 mbedtls_free( output );
3884 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01003885 mbedtls_free( nonce_buffer );
3886 psa_aead_abort( &operation );
3887 PSA_DONE( );
3888}
3889/* END_CASE */
3890
3891/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01003892void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
3893 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003894 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01003895 data_t *nonce,
3896 data_t *additional_data,
3897 data_t *input_data,
3898 int expected_status_arg )
3899{
3900
3901 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3902 psa_key_type_t key_type = key_type_arg;
3903 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01003904 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01003905 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3906 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3907 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01003908 unsigned char *output = NULL;
3909 unsigned char *ciphertext = NULL;
3910 size_t output_size = output_size_arg;
3911 size_t ciphertext_size = 0;
3912 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01003913 size_t tag_length = 0;
3914 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
3915
3916 PSA_ASSERT( psa_crypto_init( ) );
3917
3918 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3919 psa_set_key_algorithm( &attributes, alg );
3920 psa_set_key_type( &attributes, key_type );
3921
3922 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3923 &key ) );
3924
3925 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3926
Paul Elliottc6d11d02021-09-01 12:04:23 +01003927 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003928
Paul Elliottc6d11d02021-09-01 12:04:23 +01003929 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01003930
Paul Elliottc6d11d02021-09-01 12:04:23 +01003931 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003932
Paul Elliott43fbda62021-07-23 18:30:59 +01003933 status = psa_aead_encrypt_setup( &operation, key, alg );
3934
3935 /* If the operation is not supported, just skip and not fail in case the
3936 * encryption involves a common limitation of cryptography hardwares and
3937 * an alternative implementation. */
3938 if( status == PSA_ERROR_NOT_SUPPORTED )
3939 {
3940 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3941 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3942 }
3943
3944 PSA_ASSERT( status );
3945
3946 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3947
3948 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3949 additional_data->len ) );
3950
3951 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003952 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01003953
3954 TEST_EQUAL( status, expected_status );
3955
3956 if( expected_status == PSA_SUCCESS )
3957 {
3958 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01003959 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3960 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01003961 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3962 }
3963
3964exit:
3965 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01003966 mbedtls_free( output );
3967 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01003968 psa_aead_abort( &operation );
3969 PSA_DONE( );
3970}
3971/* END_CASE */
3972
Paul Elliott91b021e2021-07-23 18:52:31 +01003973/* BEGIN_CASE */
3974void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
3975 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01003976 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01003977 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01003978 data_t *nonce,
3979 data_t *additional_data,
3980 data_t *input_data,
3981 int expected_status_arg )
3982{
3983
3984 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3985 psa_key_type_t key_type = key_type_arg;
3986 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01003987 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01003988 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3989 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3990 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01003991 unsigned char *ciphertext = NULL;
3992 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01003993 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01003994 size_t ciphertext_size = 0;
3995 size_t ciphertext_length = 0;
3996 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01003997 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01003998 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01003999
4000 PSA_ASSERT( psa_crypto_init( ) );
4001
4002 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4003 psa_set_key_algorithm( &attributes, alg );
4004 psa_set_key_type( &attributes, key_type );
4005
4006 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4007 &key ) );
4008
4009 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4010
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004011 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004012
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004013 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004014
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004015 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004016
Paul Elliott719c1322021-09-13 18:27:22 +01004017 ASSERT_ALLOC( tag_buffer, tag_size );
4018
Paul Elliott91b021e2021-07-23 18:52:31 +01004019 status = psa_aead_encrypt_setup( &operation, key, alg );
4020
4021 /* If the operation is not supported, just skip and not fail in case the
4022 * encryption involves a common limitation of cryptography hardwares and
4023 * an alternative implementation. */
4024 if( status == PSA_ERROR_NOT_SUPPORTED )
4025 {
4026 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4027 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4028 }
4029
4030 PSA_ASSERT( status );
4031
4032 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4033
4034 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4035 additional_data->len ) );
4036
4037 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004038 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004039
4040 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004041 status = psa_aead_finish( &operation, finish_ciphertext,
4042 finish_ciphertext_size,
4043 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004044 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004045
4046 TEST_EQUAL( status, expected_status );
4047
4048exit:
4049 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004050 mbedtls_free( ciphertext );
4051 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01004052 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01004053 psa_aead_abort( &operation );
4054 PSA_DONE( );
4055}
4056/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004057
4058/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01004059void aead_multipart_verify( int key_type_arg, data_t *key_data,
4060 int alg_arg,
4061 data_t *nonce,
4062 data_t *additional_data,
4063 data_t *input_data,
4064 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004065 int tag_usage_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01004066 int expected_status_arg )
4067{
4068 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4069 psa_key_type_t key_type = key_type_arg;
4070 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004071 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01004072 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4073 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4074 psa_status_t expected_status = expected_status_arg;
4075 unsigned char *plaintext = NULL;
4076 unsigned char *finish_plaintext = NULL;
4077 size_t plaintext_size = 0;
4078 size_t plaintext_length = 0;
4079 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004080 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004081 unsigned char *tag_buffer = NULL;
4082 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01004083
4084 PSA_ASSERT( psa_crypto_init( ) );
4085
4086 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4087 psa_set_key_algorithm( &attributes, alg );
4088 psa_set_key_type( &attributes, key_type );
4089
4090 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4091 &key ) );
4092
4093 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4094
4095 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
4096 input_data->len );
4097
4098 ASSERT_ALLOC( plaintext, plaintext_size );
4099
4100 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
4101
4102 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
4103
Paul Elliott9961a662021-09-17 19:19:02 +01004104 status = psa_aead_decrypt_setup( &operation, key, alg );
4105
4106 /* If the operation is not supported, just skip and not fail in case the
4107 * encryption involves a common limitation of cryptography hardwares and
4108 * an alternative implementation. */
4109 if( status == PSA_ERROR_NOT_SUPPORTED )
4110 {
4111 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4112 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4113 }
4114
4115 PSA_ASSERT( status );
4116
4117 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4118
4119 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4120 additional_data->len ) );
4121
4122 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4123 input_data->len,
4124 plaintext, plaintext_size,
4125 &plaintext_length ) );
4126
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004127 if( tag_usage == USE_GIVEN_TAG )
4128 {
4129 tag_buffer = tag->x;
4130 tag_size = tag->len;
4131 }
4132
Paul Elliott9961a662021-09-17 19:19:02 +01004133 status = psa_aead_verify( &operation, finish_plaintext,
4134 verify_plaintext_size,
4135 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004136 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01004137
4138 TEST_EQUAL( status, expected_status );
4139
4140exit:
4141 psa_destroy_key( key );
4142 mbedtls_free( plaintext );
4143 mbedtls_free( finish_plaintext );
4144 psa_aead_abort( &operation );
4145 PSA_DONE( );
4146}
4147/* END_CASE */
4148
Paul Elliott9961a662021-09-17 19:19:02 +01004149/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01004150void aead_multipart_setup( int key_type_arg, data_t *key_data,
4151 int alg_arg, int expected_status_arg )
4152{
4153 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4154 psa_key_type_t key_type = key_type_arg;
4155 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004156 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01004157 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4158 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4159 psa_status_t expected_status = expected_status_arg;
4160
4161 PSA_ASSERT( psa_crypto_init( ) );
4162
4163 psa_set_key_usage_flags( &attributes,
4164 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4165 psa_set_key_algorithm( &attributes, alg );
4166 psa_set_key_type( &attributes, key_type );
4167
4168 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4169 &key ) );
4170
4171 mbedtls_test_set_step( 0 );
4172
4173 status = psa_aead_encrypt_setup( &operation, key, alg );
4174
4175 TEST_EQUAL( status, expected_status );
4176
4177 psa_aead_abort( &operation );
4178
Paul Elliott5221ef62021-09-19 17:33:03 +01004179 mbedtls_test_set_step( 1 );
4180
4181 status = psa_aead_decrypt_setup( &operation, key, alg );
4182
4183 TEST_EQUAL(status, expected_status );
4184
4185exit:
4186 psa_destroy_key( key );
4187 psa_aead_abort( &operation );
4188 PSA_DONE( );
4189}
4190/* END_CASE */
4191
4192/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004193void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4194 int alg_arg,
4195 data_t *nonce,
4196 data_t *additional_data,
4197 data_t *input_data )
4198{
4199 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4200 psa_key_type_t key_type = key_type_arg;
4201 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004202 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01004203 unsigned char *output_data = NULL;
4204 unsigned char *final_data = NULL;
4205 size_t output_size = 0;
4206 size_t finish_output_size = 0;
4207 size_t output_length = 0;
4208 size_t key_bits = 0;
4209 size_t tag_length = 0;
4210 size_t tag_size = 0;
4211 size_t nonce_length = 0;
4212 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4213 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4214 size_t output_part_length = 0;
4215 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4216
4217 PSA_ASSERT( psa_crypto_init( ) );
4218
4219 psa_set_key_usage_flags( & attributes,
4220 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4221 psa_set_key_algorithm( & attributes, alg );
4222 psa_set_key_type( & attributes, key_type );
4223
4224 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4225 &key ) );
4226
4227 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4228 key_bits = psa_get_key_bits( &attributes );
4229
4230 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4231
4232 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4233
4234 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4235
4236 ASSERT_ALLOC( output_data, output_size );
4237
4238 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4239
4240 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4241
4242 ASSERT_ALLOC( final_data, finish_output_size );
4243
4244 /* Test all operations error without calling setup first. */
4245
Paul Elliottc23a9a02021-06-21 18:32:46 +01004246 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4247 PSA_ERROR_BAD_STATE );
4248
4249 psa_aead_abort( &operation );
4250
Paul Elliottc23a9a02021-06-21 18:32:46 +01004251 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4252 PSA_AEAD_NONCE_MAX_SIZE,
4253 &nonce_length ),
4254 PSA_ERROR_BAD_STATE );
4255
4256 psa_aead_abort( &operation );
4257
Paul Elliott481be342021-07-16 17:38:47 +01004258 /* ------------------------------------------------------- */
4259
Paul Elliottc23a9a02021-06-21 18:32:46 +01004260 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4261 input_data->len ),
4262 PSA_ERROR_BAD_STATE );
4263
4264 psa_aead_abort( &operation );
4265
Paul Elliott481be342021-07-16 17:38:47 +01004266 /* ------------------------------------------------------- */
4267
Paul Elliottc23a9a02021-06-21 18:32:46 +01004268 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4269 additional_data->len ),
4270 PSA_ERROR_BAD_STATE );
4271
4272 psa_aead_abort( &operation );
4273
Paul Elliott481be342021-07-16 17:38:47 +01004274 /* ------------------------------------------------------- */
4275
Paul Elliottc23a9a02021-06-21 18:32:46 +01004276 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4277 input_data->len, output_data,
4278 output_size, &output_length ),
4279 PSA_ERROR_BAD_STATE );
4280
4281 psa_aead_abort( &operation );
4282
Paul Elliott481be342021-07-16 17:38:47 +01004283 /* ------------------------------------------------------- */
4284
Paul Elliottc23a9a02021-06-21 18:32:46 +01004285 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4286 finish_output_size,
4287 &output_part_length,
4288 tag_buffer, tag_length,
4289 &tag_size ),
4290 PSA_ERROR_BAD_STATE );
4291
4292 psa_aead_abort( &operation );
4293
Paul Elliott481be342021-07-16 17:38:47 +01004294 /* ------------------------------------------------------- */
4295
Paul Elliottc23a9a02021-06-21 18:32:46 +01004296 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4297 finish_output_size,
4298 &output_part_length,
4299 tag_buffer,
4300 tag_length ),
4301 PSA_ERROR_BAD_STATE );
4302
4303 psa_aead_abort( &operation );
4304
4305 /* Test for double setups. */
4306
Paul Elliottc23a9a02021-06-21 18:32:46 +01004307 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4308
4309 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4310 PSA_ERROR_BAD_STATE );
4311
4312 psa_aead_abort( &operation );
4313
Paul Elliott481be342021-07-16 17:38:47 +01004314 /* ------------------------------------------------------- */
4315
Paul Elliottc23a9a02021-06-21 18:32:46 +01004316 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4317
4318 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4319 PSA_ERROR_BAD_STATE );
4320
4321 psa_aead_abort( &operation );
4322
Paul Elliott374a2be2021-07-16 17:53:40 +01004323 /* ------------------------------------------------------- */
4324
Paul Elliott374a2be2021-07-16 17:53:40 +01004325 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4326
4327 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4328 PSA_ERROR_BAD_STATE );
4329
4330 psa_aead_abort( &operation );
4331
4332 /* ------------------------------------------------------- */
4333
Paul Elliott374a2be2021-07-16 17:53:40 +01004334 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4335
4336 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4337 PSA_ERROR_BAD_STATE );
4338
4339 psa_aead_abort( &operation );
4340
Paul Elliottc23a9a02021-06-21 18:32:46 +01004341 /* Test for not setting a nonce. */
4342
Paul Elliottc23a9a02021-06-21 18:32:46 +01004343 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4344
4345 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4346 additional_data->len ),
4347 PSA_ERROR_BAD_STATE );
4348
4349 psa_aead_abort( &operation );
4350
Paul Elliott7f628422021-09-01 12:08:29 +01004351 /* ------------------------------------------------------- */
4352
Paul Elliott7f628422021-09-01 12:08:29 +01004353 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4354
4355 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4356 input_data->len, output_data,
4357 output_size, &output_length ),
4358 PSA_ERROR_BAD_STATE );
4359
4360 psa_aead_abort( &operation );
4361
Paul Elliottbdc2c682021-09-21 18:37:10 +01004362 /* ------------------------------------------------------- */
4363
Paul Elliottbdc2c682021-09-21 18:37:10 +01004364 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4365
4366 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4367 finish_output_size,
4368 &output_part_length,
4369 tag_buffer, tag_length,
4370 &tag_size ),
4371 PSA_ERROR_BAD_STATE );
4372
4373 psa_aead_abort( &operation );
4374
4375 /* ------------------------------------------------------- */
4376
Paul Elliottbdc2c682021-09-21 18:37:10 +01004377 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4378
4379 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4380 finish_output_size,
4381 &output_part_length,
4382 tag_buffer,
4383 tag_length ),
4384 PSA_ERROR_BAD_STATE );
4385
4386 psa_aead_abort( &operation );
4387
Paul Elliottc23a9a02021-06-21 18:32:46 +01004388 /* Test for double setting nonce. */
4389
Paul Elliottc23a9a02021-06-21 18:32:46 +01004390 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4391
4392 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4393
4394 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4395 PSA_ERROR_BAD_STATE );
4396
4397 psa_aead_abort( &operation );
4398
Paul Elliott374a2be2021-07-16 17:53:40 +01004399 /* Test for double generating nonce. */
4400
Paul Elliott374a2be2021-07-16 17:53:40 +01004401 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4402
4403 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4404 PSA_AEAD_NONCE_MAX_SIZE,
4405 &nonce_length ) );
4406
4407 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4408 PSA_AEAD_NONCE_MAX_SIZE,
4409 &nonce_length ),
4410 PSA_ERROR_BAD_STATE );
4411
4412
4413 psa_aead_abort( &operation );
4414
4415 /* Test for generate nonce then set and vice versa */
4416
Paul Elliott374a2be2021-07-16 17:53:40 +01004417 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4418
4419 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4420 PSA_AEAD_NONCE_MAX_SIZE,
4421 &nonce_length ) );
4422
4423 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4424 PSA_ERROR_BAD_STATE );
4425
4426 psa_aead_abort( &operation );
4427
4428 /* ------------------------------------------------------- */
4429
Paul Elliott374a2be2021-07-16 17:53:40 +01004430 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4431
4432 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4433
4434 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4435 PSA_AEAD_NONCE_MAX_SIZE,
4436 &nonce_length ),
4437 PSA_ERROR_BAD_STATE );
4438
4439 psa_aead_abort( &operation );
4440
Paul Elliott7220cae2021-06-22 17:25:57 +01004441 /* Test for generating nonce in decrypt setup. */
4442
Paul Elliott7220cae2021-06-22 17:25:57 +01004443 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4444
4445 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4446 PSA_AEAD_NONCE_MAX_SIZE,
4447 &nonce_length ),
4448 PSA_ERROR_BAD_STATE );
4449
4450 psa_aead_abort( &operation );
4451
Paul Elliottc23a9a02021-06-21 18:32:46 +01004452 /* Test for setting lengths twice. */
4453
Paul Elliottc23a9a02021-06-21 18:32:46 +01004454 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4455
4456 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4457
4458 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4459 input_data->len ) );
4460
4461 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4462 input_data->len ),
4463 PSA_ERROR_BAD_STATE );
4464
4465 psa_aead_abort( &operation );
4466
4467 /* Test for setting lengths after already starting data. */
4468
Paul Elliottc23a9a02021-06-21 18:32:46 +01004469 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4470
4471 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4472
Paul Elliottf94bd992021-09-19 18:15:59 +01004473 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4474 additional_data->len ) );
4475
4476 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4477 input_data->len ),
4478 PSA_ERROR_BAD_STATE );
4479
4480 psa_aead_abort( &operation );
4481
4482 /* ------------------------------------------------------- */
4483
Paul Elliottf94bd992021-09-19 18:15:59 +01004484 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4485
4486 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4487
Paul Elliottc23a9a02021-06-21 18:32:46 +01004488 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4489 input_data->len, output_data,
4490 output_size, &output_length ) );
4491
4492 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4493 input_data->len ),
4494 PSA_ERROR_BAD_STATE );
4495
4496 psa_aead_abort( &operation );
4497
Paul Elliott243080c2021-07-21 19:01:17 +01004498 /* Test for not sending any additional data or data after setting non zero
4499 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004500
Paul Elliottc23a9a02021-06-21 18:32:46 +01004501 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4502
4503 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4504
4505 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4506 input_data->len ) );
4507
4508 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4509 finish_output_size,
4510 &output_part_length,
4511 tag_buffer, tag_length,
4512 &tag_size ),
4513 PSA_ERROR_INVALID_ARGUMENT );
4514
4515 psa_aead_abort( &operation );
4516
Paul Elliott243080c2021-07-21 19:01:17 +01004517 /* Test for not sending any additional data or data after setting non-zero
4518 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004519
Paul Elliottc23a9a02021-06-21 18:32:46 +01004520 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4521
4522 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4523
4524 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4525 input_data->len ) );
4526
4527 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4528 finish_output_size,
4529 &output_part_length,
4530 tag_buffer,
4531 tag_length ),
4532 PSA_ERROR_INVALID_ARGUMENT );
4533
4534 psa_aead_abort( &operation );
4535
Paul Elliott243080c2021-07-21 19:01:17 +01004536 /* Test for not sending any additional data after setting a non-zero length
4537 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004538
Paul Elliottc23a9a02021-06-21 18:32:46 +01004539 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4540
4541 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4542
4543 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4544 input_data->len ) );
4545
4546 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4547 input_data->len, output_data,
4548 output_size, &output_length ),
4549 PSA_ERROR_INVALID_ARGUMENT );
4550
4551 psa_aead_abort( &operation );
4552
Paul Elliottf94bd992021-09-19 18:15:59 +01004553 /* Test for not sending any data after setting a non-zero length for it.*/
4554
Paul Elliottf94bd992021-09-19 18:15:59 +01004555 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4556
4557 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4558
4559 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4560 input_data->len ) );
4561
4562 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4563 additional_data->len ) );
4564
4565 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4566 finish_output_size,
4567 &output_part_length,
4568 tag_buffer, tag_length,
4569 &tag_size ),
4570 PSA_ERROR_INVALID_ARGUMENT );
4571
4572 psa_aead_abort( &operation );
4573
Paul Elliottb0450fe2021-09-01 15:06:26 +01004574 /* Test for sending too much additional data after setting lengths. */
4575
Paul Elliottb0450fe2021-09-01 15:06:26 +01004576 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4577
4578 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4579
4580 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4581
4582
4583 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4584 additional_data->len ),
4585 PSA_ERROR_INVALID_ARGUMENT );
4586
4587 psa_aead_abort( &operation );
4588
Paul Elliotta2a09b02021-09-22 14:56:40 +01004589 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01004590
4591 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4592
4593 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4594
4595 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4596 input_data->len ) );
4597
4598 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4599 additional_data->len ) );
4600
4601 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4602 1 ),
4603 PSA_ERROR_INVALID_ARGUMENT );
4604
4605 psa_aead_abort( &operation );
4606
Paul Elliottb0450fe2021-09-01 15:06:26 +01004607 /* Test for sending too much data after setting lengths. */
4608
Paul Elliottb0450fe2021-09-01 15:06:26 +01004609 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4610
4611 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4612
4613 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4614
4615 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4616 input_data->len, output_data,
4617 output_size, &output_length ),
4618 PSA_ERROR_INVALID_ARGUMENT );
4619
4620 psa_aead_abort( &operation );
4621
Paul Elliotta2a09b02021-09-22 14:56:40 +01004622 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01004623
4624 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4625
4626 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4627
4628 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4629 input_data->len ) );
4630
4631 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4632 additional_data->len ) );
4633
4634 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4635 input_data->len, output_data,
4636 output_size, &output_length ) );
4637
4638 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4639 1, output_data,
4640 output_size, &output_length ),
4641 PSA_ERROR_INVALID_ARGUMENT );
4642
4643 psa_aead_abort( &operation );
4644
Paul Elliottc23a9a02021-06-21 18:32:46 +01004645 /* Test sending additional data after data. */
4646
Paul Elliottc23a9a02021-06-21 18:32:46 +01004647 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4648
4649 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4650
4651 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4652 input_data->len, output_data,
4653 output_size, &output_length ) );
4654
4655 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4656 additional_data->len ),
4657 PSA_ERROR_BAD_STATE );
4658
4659 psa_aead_abort( &operation );
4660
Paul Elliott534d0b42021-06-22 19:15:20 +01004661 /* Test calling finish on decryption. */
4662
Paul Elliott534d0b42021-06-22 19:15:20 +01004663 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4664
4665 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4666
4667 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4668 finish_output_size,
4669 &output_part_length,
4670 tag_buffer, tag_length,
4671 &tag_size ),
4672 PSA_ERROR_BAD_STATE );
4673
4674 psa_aead_abort( &operation );
4675
4676 /* Test calling verify on encryption. */
4677
Paul Elliott534d0b42021-06-22 19:15:20 +01004678 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4679
4680 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4681
4682 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4683 finish_output_size,
4684 &output_part_length,
4685 tag_buffer,
4686 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01004687 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01004688
4689 psa_aead_abort( &operation );
4690
4691
Paul Elliottc23a9a02021-06-21 18:32:46 +01004692exit:
4693 psa_destroy_key( key );
4694 psa_aead_abort( &operation );
4695 mbedtls_free( output_data );
4696 mbedtls_free( final_data );
4697 PSA_DONE( );
4698}
4699/* END_CASE */
4700
4701/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004702void signature_size( int type_arg,
4703 int bits,
4704 int alg_arg,
4705 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004706{
4707 psa_key_type_t type = type_arg;
4708 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004709 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004710
Gilles Peskinefe11b722018-12-18 00:24:04 +01004711 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004712
Gilles Peskinee59236f2018-01-27 23:32:46 +01004713exit:
4714 ;
4715}
4716/* END_CASE */
4717
4718/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004719void sign_hash_deterministic( int key_type_arg, data_t *key_data,
4720 int alg_arg, data_t *input_data,
4721 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004722{
Ronald Cron5425a212020-08-04 14:58:35 +02004723 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004724 psa_key_type_t key_type = key_type_arg;
4725 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004726 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004727 unsigned char *signature = NULL;
4728 size_t signature_size;
4729 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004730 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004731
Gilles Peskine8817f612018-12-18 00:18:46 +01004732 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004733
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004734 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004735 psa_set_key_algorithm( &attributes, alg );
4736 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004737
Gilles Peskine049c7532019-05-15 20:22:09 +02004738 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004739 &key ) );
4740 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004741 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004742
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004743 /* Allocate a buffer which has the size advertized by the
4744 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004745 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004746 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004747 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004748 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004749 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004750
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004751 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004752 PSA_ASSERT( 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 Peskine9911b022018-06-29 17:30:48 +02004756 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004757 ASSERT_COMPARE( output_data->x, output_data->len,
4758 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004759
4760exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004761 /*
4762 * Key attributes may have been returned by psa_get_key_attributes()
4763 * thus reset them as required.
4764 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004765 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004766
Ronald Cron5425a212020-08-04 14:58:35 +02004767 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004768 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004769 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004770}
4771/* END_CASE */
4772
4773/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004774void sign_hash_fail( int key_type_arg, data_t *key_data,
4775 int alg_arg, data_t *input_data,
4776 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004777{
Ronald Cron5425a212020-08-04 14:58:35 +02004778 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004779 psa_key_type_t key_type = key_type_arg;
4780 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004781 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004782 psa_status_t actual_status;
4783 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004784 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004785 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004786 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004787
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004788 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004789
Gilles Peskine8817f612018-12-18 00:18:46 +01004790 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004791
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004792 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004793 psa_set_key_algorithm( &attributes, alg );
4794 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004795
Gilles Peskine049c7532019-05-15 20:22:09 +02004796 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004797 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004798
Ronald Cron5425a212020-08-04 14:58:35 +02004799 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004800 input_data->x, input_data->len,
4801 signature, signature_size,
4802 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004803 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004804 /* The value of *signature_length is unspecified on error, but
4805 * whatever it is, it should be less than signature_size, so that
4806 * if the caller tries to read *signature_length bytes without
4807 * checking the error code then they don't overflow a buffer. */
4808 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004809
4810exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004811 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004812 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004813 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004814 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004815}
4816/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004817
4818/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004819void sign_verify_hash( int key_type_arg, data_t *key_data,
4820 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02004821{
Ronald Cron5425a212020-08-04 14:58:35 +02004822 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004823 psa_key_type_t key_type = key_type_arg;
4824 psa_algorithm_t alg = alg_arg;
4825 size_t key_bits;
4826 unsigned char *signature = NULL;
4827 size_t signature_size;
4828 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004829 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004830
Gilles Peskine8817f612018-12-18 00:18:46 +01004831 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004832
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004833 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004834 psa_set_key_algorithm( &attributes, alg );
4835 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004836
Gilles Peskine049c7532019-05-15 20:22:09 +02004837 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004838 &key ) );
4839 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004840 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004841
4842 /* Allocate a buffer which has the size advertized by the
4843 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004844 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004845 key_bits, alg );
4846 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004847 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004848 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004849
4850 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004851 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004852 input_data->x, input_data->len,
4853 signature, signature_size,
4854 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004855 /* Check that the signature length looks sensible. */
4856 TEST_ASSERT( signature_length <= signature_size );
4857 TEST_ASSERT( signature_length > 0 );
4858
4859 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004860 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004861 input_data->x, input_data->len,
4862 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004863
4864 if( input_data->len != 0 )
4865 {
4866 /* Flip a bit in the input and verify that the signature is now
4867 * detected as invalid. Flip a bit at the beginning, not at the end,
4868 * because ECDSA may ignore the last few bits of the input. */
4869 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004870 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004871 input_data->x, input_data->len,
4872 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004873 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004874 }
4875
4876exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004877 /*
4878 * Key attributes may have been returned by psa_get_key_attributes()
4879 * thus reset them as required.
4880 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004881 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004882
Ronald Cron5425a212020-08-04 14:58:35 +02004883 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004884 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004885 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004886}
4887/* END_CASE */
4888
4889/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004890void verify_hash( int key_type_arg, data_t *key_data,
4891 int alg_arg, data_t *hash_data,
4892 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004893{
Ronald Cron5425a212020-08-04 14:58:35 +02004894 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004895 psa_key_type_t key_type = key_type_arg;
4896 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004897 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004898
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004899 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004900
Gilles Peskine8817f612018-12-18 00:18:46 +01004901 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004902
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004903 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004904 psa_set_key_algorithm( &attributes, alg );
4905 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004906
Gilles Peskine049c7532019-05-15 20:22:09 +02004907 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004908 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004909
Ronald Cron5425a212020-08-04 14:58:35 +02004910 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004911 hash_data->x, hash_data->len,
4912 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004913
itayzafrir5c753392018-05-08 11:18:38 +03004914exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004915 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004916 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004917 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004918}
4919/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004920
4921/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004922void verify_hash_fail( int key_type_arg, data_t *key_data,
4923 int alg_arg, data_t *hash_data,
4924 data_t *signature_data,
4925 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004926{
Ronald Cron5425a212020-08-04 14:58:35 +02004927 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004928 psa_key_type_t key_type = key_type_arg;
4929 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004930 psa_status_t actual_status;
4931 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004932 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004933
Gilles Peskine8817f612018-12-18 00:18:46 +01004934 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004935
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004936 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004937 psa_set_key_algorithm( &attributes, alg );
4938 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004939
Gilles Peskine049c7532019-05-15 20:22:09 +02004940 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004941 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004942
Ronald Cron5425a212020-08-04 14:58:35 +02004943 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004944 hash_data->x, hash_data->len,
4945 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004946 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004947
4948exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004949 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004950 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004951 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004952}
4953/* END_CASE */
4954
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004955/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02004956void sign_message_deterministic( int key_type_arg,
4957 data_t *key_data,
4958 int alg_arg,
4959 data_t *input_data,
4960 data_t *output_data )
4961{
4962 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4963 psa_key_type_t key_type = key_type_arg;
4964 psa_algorithm_t alg = alg_arg;
4965 size_t key_bits;
4966 unsigned char *signature = NULL;
4967 size_t signature_size;
4968 size_t signature_length = 0xdeadbeef;
4969 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4970
4971 PSA_ASSERT( psa_crypto_init( ) );
4972
4973 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4974 psa_set_key_algorithm( &attributes, alg );
4975 psa_set_key_type( &attributes, key_type );
4976
4977 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4978 &key ) );
4979 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4980 key_bits = psa_get_key_bits( &attributes );
4981
4982 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4983 TEST_ASSERT( signature_size != 0 );
4984 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4985 ASSERT_ALLOC( signature, signature_size );
4986
4987 PSA_ASSERT( psa_sign_message( key, alg,
4988 input_data->x, input_data->len,
4989 signature, signature_size,
4990 &signature_length ) );
4991
4992 ASSERT_COMPARE( output_data->x, output_data->len,
4993 signature, signature_length );
4994
4995exit:
4996 psa_reset_key_attributes( &attributes );
4997
4998 psa_destroy_key( key );
4999 mbedtls_free( signature );
5000 PSA_DONE( );
5001
5002}
5003/* END_CASE */
5004
5005/* BEGIN_CASE */
5006void sign_message_fail( int key_type_arg,
5007 data_t *key_data,
5008 int alg_arg,
5009 data_t *input_data,
5010 int signature_size_arg,
5011 int expected_status_arg )
5012{
5013 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5014 psa_key_type_t key_type = key_type_arg;
5015 psa_algorithm_t alg = alg_arg;
5016 size_t signature_size = signature_size_arg;
5017 psa_status_t actual_status;
5018 psa_status_t expected_status = expected_status_arg;
5019 unsigned char *signature = NULL;
5020 size_t signature_length = 0xdeadbeef;
5021 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5022
5023 ASSERT_ALLOC( signature, signature_size );
5024
5025 PSA_ASSERT( psa_crypto_init( ) );
5026
5027 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5028 psa_set_key_algorithm( &attributes, alg );
5029 psa_set_key_type( &attributes, key_type );
5030
5031 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5032 &key ) );
5033
5034 actual_status = psa_sign_message( key, alg,
5035 input_data->x, input_data->len,
5036 signature, signature_size,
5037 &signature_length );
5038 TEST_EQUAL( actual_status, expected_status );
5039 /* The value of *signature_length is unspecified on error, but
5040 * whatever it is, it should be less than signature_size, so that
5041 * if the caller tries to read *signature_length bytes without
5042 * checking the error code then they don't overflow a buffer. */
5043 TEST_ASSERT( signature_length <= signature_size );
5044
5045exit:
5046 psa_reset_key_attributes( &attributes );
5047 psa_destroy_key( key );
5048 mbedtls_free( signature );
5049 PSA_DONE( );
5050}
5051/* END_CASE */
5052
5053/* BEGIN_CASE */
5054void sign_verify_message( int key_type_arg,
5055 data_t *key_data,
5056 int alg_arg,
5057 data_t *input_data )
5058{
5059 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5060 psa_key_type_t key_type = key_type_arg;
5061 psa_algorithm_t alg = alg_arg;
5062 size_t key_bits;
5063 unsigned char *signature = NULL;
5064 size_t signature_size;
5065 size_t signature_length = 0xdeadbeef;
5066 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5067
5068 PSA_ASSERT( psa_crypto_init( ) );
5069
5070 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
5071 PSA_KEY_USAGE_VERIFY_MESSAGE );
5072 psa_set_key_algorithm( &attributes, alg );
5073 psa_set_key_type( &attributes, key_type );
5074
5075 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5076 &key ) );
5077 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5078 key_bits = psa_get_key_bits( &attributes );
5079
5080 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5081 TEST_ASSERT( signature_size != 0 );
5082 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5083 ASSERT_ALLOC( signature, signature_size );
5084
5085 PSA_ASSERT( psa_sign_message( key, alg,
5086 input_data->x, input_data->len,
5087 signature, signature_size,
5088 &signature_length ) );
5089 TEST_ASSERT( signature_length <= signature_size );
5090 TEST_ASSERT( signature_length > 0 );
5091
5092 PSA_ASSERT( psa_verify_message( key, alg,
5093 input_data->x, input_data->len,
5094 signature, signature_length ) );
5095
5096 if( input_data->len != 0 )
5097 {
5098 /* Flip a bit in the input and verify that the signature is now
5099 * detected as invalid. Flip a bit at the beginning, not at the end,
5100 * because ECDSA may ignore the last few bits of the input. */
5101 input_data->x[0] ^= 1;
5102 TEST_EQUAL( psa_verify_message( key, alg,
5103 input_data->x, input_data->len,
5104 signature, signature_length ),
5105 PSA_ERROR_INVALID_SIGNATURE );
5106 }
5107
5108exit:
5109 psa_reset_key_attributes( &attributes );
5110
5111 psa_destroy_key( key );
5112 mbedtls_free( signature );
5113 PSA_DONE( );
5114}
5115/* END_CASE */
5116
5117/* BEGIN_CASE */
5118void verify_message( int key_type_arg,
5119 data_t *key_data,
5120 int alg_arg,
5121 data_t *input_data,
5122 data_t *signature_data )
5123{
5124 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5125 psa_key_type_t key_type = key_type_arg;
5126 psa_algorithm_t alg = alg_arg;
5127 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5128
5129 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
5130
5131 PSA_ASSERT( psa_crypto_init( ) );
5132
5133 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5134 psa_set_key_algorithm( &attributes, alg );
5135 psa_set_key_type( &attributes, key_type );
5136
5137 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5138 &key ) );
5139
5140 PSA_ASSERT( psa_verify_message( key, alg,
5141 input_data->x, input_data->len,
5142 signature_data->x, signature_data->len ) );
5143
5144exit:
5145 psa_reset_key_attributes( &attributes );
5146 psa_destroy_key( key );
5147 PSA_DONE( );
5148}
5149/* END_CASE */
5150
5151/* BEGIN_CASE */
5152void verify_message_fail( int key_type_arg,
5153 data_t *key_data,
5154 int alg_arg,
5155 data_t *hash_data,
5156 data_t *signature_data,
5157 int expected_status_arg )
5158{
5159 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5160 psa_key_type_t key_type = key_type_arg;
5161 psa_algorithm_t alg = alg_arg;
5162 psa_status_t actual_status;
5163 psa_status_t expected_status = expected_status_arg;
5164 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5165
5166 PSA_ASSERT( psa_crypto_init( ) );
5167
5168 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5169 psa_set_key_algorithm( &attributes, alg );
5170 psa_set_key_type( &attributes, key_type );
5171
5172 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5173 &key ) );
5174
5175 actual_status = psa_verify_message( key, alg,
5176 hash_data->x, hash_data->len,
5177 signature_data->x,
5178 signature_data->len );
5179 TEST_EQUAL( actual_status, expected_status );
5180
5181exit:
5182 psa_reset_key_attributes( &attributes );
5183 psa_destroy_key( key );
5184 PSA_DONE( );
5185}
5186/* END_CASE */
5187
5188/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02005189void asymmetric_encrypt( int key_type_arg,
5190 data_t *key_data,
5191 int alg_arg,
5192 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02005193 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02005194 int expected_output_length_arg,
5195 int expected_status_arg )
5196{
Ronald Cron5425a212020-08-04 14:58:35 +02005197 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005198 psa_key_type_t key_type = key_type_arg;
5199 psa_algorithm_t alg = alg_arg;
5200 size_t expected_output_length = expected_output_length_arg;
5201 size_t key_bits;
5202 unsigned char *output = NULL;
5203 size_t output_size;
5204 size_t output_length = ~0;
5205 psa_status_t actual_status;
5206 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005207 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005208
Gilles Peskine8817f612018-12-18 00:18:46 +01005209 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005210
Gilles Peskine656896e2018-06-29 19:12:28 +02005211 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005212 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5213 psa_set_key_algorithm( &attributes, alg );
5214 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005215 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005216 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02005217
5218 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02005219 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005220 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005221
Gilles Peskine656896e2018-06-29 19:12:28 +02005222 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005223 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005224 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02005225
5226 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02005227 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02005228 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005229 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02005230 output, output_size,
5231 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005232 TEST_EQUAL( actual_status, expected_status );
5233 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02005234
Gilles Peskine68428122018-06-30 18:42:41 +02005235 /* If the label is empty, the test framework puts a non-null pointer
5236 * in label->x. Test that a null pointer works as well. */
5237 if( label->len == 0 )
5238 {
5239 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005240 if( output_size != 0 )
5241 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005242 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005243 input_data->x, input_data->len,
5244 NULL, label->len,
5245 output, output_size,
5246 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005247 TEST_EQUAL( actual_status, expected_status );
5248 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005249 }
5250
Gilles Peskine656896e2018-06-29 19:12:28 +02005251exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005252 /*
5253 * Key attributes may have been returned by psa_get_key_attributes()
5254 * thus reset them as required.
5255 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005256 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005257
Ronald Cron5425a212020-08-04 14:58:35 +02005258 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02005259 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005260 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02005261}
5262/* END_CASE */
5263
5264/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005265void asymmetric_encrypt_decrypt( int key_type_arg,
5266 data_t *key_data,
5267 int alg_arg,
5268 data_t *input_data,
5269 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005270{
Ronald Cron5425a212020-08-04 14:58:35 +02005271 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005272 psa_key_type_t key_type = key_type_arg;
5273 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005274 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005275 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005276 size_t output_size;
5277 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005278 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005279 size_t output2_size;
5280 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005281 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005282
Gilles Peskine8817f612018-12-18 00:18:46 +01005283 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005284
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005285 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5286 psa_set_key_algorithm( &attributes, alg );
5287 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005288
Gilles Peskine049c7532019-05-15 20:22:09 +02005289 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005290 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005291
5292 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02005293 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005294 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005295
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005296 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005297 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005298 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01005299
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005300 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01005301 TEST_ASSERT( output2_size <=
5302 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
5303 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005304 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005305
Gilles Peskineeebd7382018-06-08 18:11:54 +02005306 /* We test encryption by checking that encrypt-then-decrypt gives back
5307 * the original plaintext because of the non-optional random
5308 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02005309 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005310 input_data->x, input_data->len,
5311 label->x, label->len,
5312 output, output_size,
5313 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005314 /* We don't know what ciphertext length to expect, but check that
5315 * it looks sensible. */
5316 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005317
Ronald Cron5425a212020-08-04 14:58:35 +02005318 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005319 output, output_length,
5320 label->x, label->len,
5321 output2, output2_size,
5322 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005323 ASSERT_COMPARE( input_data->x, input_data->len,
5324 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005325
5326exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005327 /*
5328 * Key attributes may have been returned by psa_get_key_attributes()
5329 * thus reset them as required.
5330 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005331 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005332
Ronald Cron5425a212020-08-04 14:58:35 +02005333 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005334 mbedtls_free( output );
5335 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005336 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005337}
5338/* END_CASE */
5339
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005340/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005341void asymmetric_decrypt( int key_type_arg,
5342 data_t *key_data,
5343 int alg_arg,
5344 data_t *input_data,
5345 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02005346 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005347{
Ronald Cron5425a212020-08-04 14:58:35 +02005348 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005349 psa_key_type_t key_type = key_type_arg;
5350 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01005351 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005352 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03005353 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005354 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005355 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005356
Gilles Peskine8817f612018-12-18 00:18:46 +01005357 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005358
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005359 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5360 psa_set_key_algorithm( &attributes, alg );
5361 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005362
Gilles Peskine049c7532019-05-15 20:22:09 +02005363 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005364 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005365
gabor-mezei-armceface22021-01-21 12:26:17 +01005366 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5367 key_bits = psa_get_key_bits( &attributes );
5368
5369 /* Determine the maximum ciphertext length */
5370 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
5371 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
5372 ASSERT_ALLOC( output, output_size );
5373
Ronald Cron5425a212020-08-04 14:58:35 +02005374 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005375 input_data->x, input_data->len,
5376 label->x, label->len,
5377 output,
5378 output_size,
5379 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005380 ASSERT_COMPARE( expected_data->x, expected_data->len,
5381 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005382
Gilles Peskine68428122018-06-30 18:42:41 +02005383 /* If the label is empty, the test framework puts a non-null pointer
5384 * in label->x. Test that a null pointer works as well. */
5385 if( label->len == 0 )
5386 {
5387 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005388 if( output_size != 0 )
5389 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005390 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005391 input_data->x, input_data->len,
5392 NULL, label->len,
5393 output,
5394 output_size,
5395 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005396 ASSERT_COMPARE( expected_data->x, expected_data->len,
5397 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005398 }
5399
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005400exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005401 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005402 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005403 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005404 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005405}
5406/* END_CASE */
5407
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005408/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005409void asymmetric_decrypt_fail( int key_type_arg,
5410 data_t *key_data,
5411 int alg_arg,
5412 data_t *input_data,
5413 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00005414 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02005415 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005416{
Ronald Cron5425a212020-08-04 14:58:35 +02005417 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005418 psa_key_type_t key_type = key_type_arg;
5419 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005420 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00005421 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005422 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005423 psa_status_t actual_status;
5424 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005425 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005426
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005427 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005428
Gilles Peskine8817f612018-12-18 00:18:46 +01005429 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005430
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005431 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5432 psa_set_key_algorithm( &attributes, alg );
5433 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005434
Gilles Peskine049c7532019-05-15 20:22:09 +02005435 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005436 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005437
Ronald Cron5425a212020-08-04 14:58:35 +02005438 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005439 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005440 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005441 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02005442 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005443 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005444 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005445
Gilles Peskine68428122018-06-30 18:42:41 +02005446 /* If the label is empty, the test framework puts a non-null pointer
5447 * in label->x. Test that a null pointer works as well. */
5448 if( label->len == 0 )
5449 {
5450 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005451 if( output_size != 0 )
5452 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005453 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005454 input_data->x, input_data->len,
5455 NULL, label->len,
5456 output, output_size,
5457 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005458 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005459 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02005460 }
5461
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005462exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005463 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005464 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02005465 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005466 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005467}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005468/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02005469
5470/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005471void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00005472{
5473 /* Test each valid way of initializing the object, except for `= {0}`, as
5474 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
5475 * though it's OK by the C standard. We could test for this, but we'd need
5476 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005477 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005478 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
5479 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
5480 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00005481
5482 memset( &zero, 0, sizeof( zero ) );
5483
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005484 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005485 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005486 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005487 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005488 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005489 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005490 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005491
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005492 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005493 PSA_ASSERT( psa_key_derivation_abort(&func) );
5494 PSA_ASSERT( psa_key_derivation_abort(&init) );
5495 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00005496}
5497/* END_CASE */
5498
Janos Follath16de4a42019-06-13 16:32:24 +01005499/* BEGIN_CASE */
5500void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005501{
Gilles Peskineea0fb492018-07-12 17:17:20 +02005502 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005503 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005504 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005505
Gilles Peskine8817f612018-12-18 00:18:46 +01005506 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005507
Janos Follath16de4a42019-06-13 16:32:24 +01005508 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005509 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005510
5511exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005512 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005513 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005514}
5515/* END_CASE */
5516
Janos Follathaf3c2a02019-06-12 12:34:34 +01005517/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01005518void derive_set_capacity( int alg_arg, int capacity_arg,
5519 int expected_status_arg )
5520{
5521 psa_algorithm_t alg = alg_arg;
5522 size_t capacity = capacity_arg;
5523 psa_status_t expected_status = expected_status_arg;
5524 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5525
5526 PSA_ASSERT( psa_crypto_init( ) );
5527
5528 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5529
5530 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
5531 expected_status );
5532
5533exit:
5534 psa_key_derivation_abort( &operation );
5535 PSA_DONE( );
5536}
5537/* END_CASE */
5538
5539/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01005540void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02005541 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005542 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02005543 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005544 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02005545 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005546 int expected_status_arg3,
5547 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005548{
5549 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02005550 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
5551 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01005552 psa_status_t expected_statuses[] = {expected_status_arg1,
5553 expected_status_arg2,
5554 expected_status_arg3};
5555 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005556 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5557 MBEDTLS_SVC_KEY_ID_INIT,
5558 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01005559 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5560 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5561 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005562 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005563 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005564 psa_status_t expected_output_status = expected_output_status_arg;
5565 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01005566
5567 PSA_ASSERT( psa_crypto_init( ) );
5568
5569 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5570 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005571
5572 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5573
5574 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
5575 {
Gilles Peskineb8965192019-09-24 16:21:10 +02005576 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005577 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02005578 psa_set_key_type( &attributes, key_types[i] );
5579 PSA_ASSERT( psa_import_key( &attributes,
5580 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005581 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005582 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
5583 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
5584 {
5585 // When taking a private key as secret input, use key agreement
5586 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005587 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
5588 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005589 expected_statuses[i] );
5590 }
5591 else
5592 {
5593 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02005594 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005595 expected_statuses[i] );
5596 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02005597 }
5598 else
5599 {
5600 TEST_EQUAL( psa_key_derivation_input_bytes(
5601 &operation, steps[i],
5602 inputs[i]->x, inputs[i]->len ),
5603 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005604 }
5605 }
5606
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005607 if( output_key_type != PSA_KEY_TYPE_NONE )
5608 {
5609 psa_reset_key_attributes( &attributes );
5610 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
5611 psa_set_key_bits( &attributes, 8 );
5612 actual_output_status =
5613 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005614 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005615 }
5616 else
5617 {
5618 uint8_t buffer[1];
5619 actual_output_status =
5620 psa_key_derivation_output_bytes( &operation,
5621 buffer, sizeof( buffer ) );
5622 }
5623 TEST_EQUAL( actual_output_status, expected_output_status );
5624
Janos Follathaf3c2a02019-06-12 12:34:34 +01005625exit:
5626 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005627 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5628 psa_destroy_key( keys[i] );
5629 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005630 PSA_DONE( );
5631}
5632/* END_CASE */
5633
Janos Follathd958bb72019-07-03 15:02:16 +01005634/* BEGIN_CASE */
5635void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005636{
Janos Follathd958bb72019-07-03 15:02:16 +01005637 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005638 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02005639 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005640 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01005641 unsigned char input1[] = "Input 1";
5642 size_t input1_length = sizeof( input1 );
5643 unsigned char input2[] = "Input 2";
5644 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005645 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02005646 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02005647 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5648 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5649 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005650 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005651
Gilles Peskine8817f612018-12-18 00:18:46 +01005652 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005653
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005654 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5655 psa_set_key_algorithm( &attributes, alg );
5656 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005657
Gilles Peskine73676cb2019-05-15 20:15:10 +02005658 PSA_ASSERT( psa_import_key( &attributes,
5659 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02005660 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005661
5662 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005663 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5664 input1, input1_length,
5665 input2, input2_length,
5666 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01005667 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005668
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005669 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01005670 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005671 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005672
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005673 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005674
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005675 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02005676 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005677
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005678exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005679 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005680 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005681 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005682}
5683/* END_CASE */
5684
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005685/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005686void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005687{
5688 uint8_t output_buffer[16];
5689 size_t buffer_size = 16;
5690 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005691 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005692
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005693 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5694 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005695 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005696
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005697 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005698 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005699
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005700 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005701
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005702 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5703 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005704 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005705
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005706 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005707 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005708
5709exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005710 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005711}
5712/* END_CASE */
5713
5714/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005715void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02005716 int step1_arg, data_t *input1,
5717 int step2_arg, data_t *input2,
5718 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005719 int requested_capacity_arg,
5720 data_t *expected_output1,
5721 data_t *expected_output2 )
5722{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005723 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005724 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5725 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005726 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5727 MBEDTLS_SVC_KEY_ID_INIT,
5728 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005729 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005730 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005731 uint8_t *expected_outputs[2] =
5732 {expected_output1->x, expected_output2->x};
5733 size_t output_sizes[2] =
5734 {expected_output1->len, expected_output2->len};
5735 size_t output_buffer_size = 0;
5736 uint8_t *output_buffer = NULL;
5737 size_t expected_capacity;
5738 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005739 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005740 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005741 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005742
5743 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5744 {
5745 if( output_sizes[i] > output_buffer_size )
5746 output_buffer_size = output_sizes[i];
5747 if( output_sizes[i] == 0 )
5748 expected_outputs[i] = NULL;
5749 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005750 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005751 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005752
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005753 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5754 psa_set_key_algorithm( &attributes, alg );
5755 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005756
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005757 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005758 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5759 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5760 requested_capacity ) );
5761 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005762 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005763 switch( steps[i] )
5764 {
5765 case 0:
5766 break;
5767 case PSA_KEY_DERIVATION_INPUT_SECRET:
5768 PSA_ASSERT( psa_import_key( &attributes,
5769 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005770 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01005771
5772 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5773 {
5774 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
5775 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
5776 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
5777 }
5778
Gilles Peskine1468da72019-05-29 17:35:49 +02005779 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005780 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005781 break;
5782 default:
5783 PSA_ASSERT( psa_key_derivation_input_bytes(
5784 &operation, steps[i],
5785 inputs[i]->x, inputs[i]->len ) );
5786 break;
5787 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005788 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005789
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005790 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005791 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005792 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005793 expected_capacity = requested_capacity;
5794
5795 /* Expansion phase. */
5796 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5797 {
5798 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005799 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005800 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005801 if( expected_capacity == 0 && output_sizes[i] == 0 )
5802 {
5803 /* Reading 0 bytes when 0 bytes are available can go either way. */
5804 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005805 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005806 continue;
5807 }
5808 else if( expected_capacity == 0 ||
5809 output_sizes[i] > expected_capacity )
5810 {
5811 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005812 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005813 expected_capacity = 0;
5814 continue;
5815 }
5816 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005817 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005818 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005819 ASSERT_COMPARE( output_buffer, output_sizes[i],
5820 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005821 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005822 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005823 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005824 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005825 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005826 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005827 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005828
5829exit:
5830 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005831 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005832 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5833 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005834 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005835}
5836/* END_CASE */
5837
5838/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005839void derive_full( int alg_arg,
5840 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005841 data_t *input1,
5842 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005843 int requested_capacity_arg )
5844{
Ronald Cron5425a212020-08-04 14:58:35 +02005845 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005846 psa_algorithm_t alg = alg_arg;
5847 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005848 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005849 unsigned char output_buffer[16];
5850 size_t expected_capacity = requested_capacity;
5851 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005852 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005853
Gilles Peskine8817f612018-12-18 00:18:46 +01005854 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005855
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005856 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5857 psa_set_key_algorithm( &attributes, alg );
5858 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005859
Gilles Peskine049c7532019-05-15 20:22:09 +02005860 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005861 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005862
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005863 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5864 input1->x, input1->len,
5865 input2->x, input2->len,
5866 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01005867 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005868
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005869 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005870 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005871 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005872
5873 /* Expansion phase. */
5874 while( current_capacity > 0 )
5875 {
5876 size_t read_size = sizeof( output_buffer );
5877 if( read_size > current_capacity )
5878 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005879 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005880 output_buffer,
5881 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005882 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005883 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005884 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005885 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005886 }
5887
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005888 /* Check that the operation refuses to go over capacity. */
5889 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005890 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005891
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005892 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005893
5894exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005895 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005896 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005897 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005898}
5899/* END_CASE */
5900
Janos Follathe60c9052019-07-03 13:51:30 +01005901/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005902void derive_key_exercise( int alg_arg,
5903 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005904 data_t *input1,
5905 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005906 int derived_type_arg,
5907 int derived_bits_arg,
5908 int derived_usage_arg,
5909 int derived_alg_arg )
5910{
Ronald Cron5425a212020-08-04 14:58:35 +02005911 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5912 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005913 psa_algorithm_t alg = alg_arg;
5914 psa_key_type_t derived_type = derived_type_arg;
5915 size_t derived_bits = derived_bits_arg;
5916 psa_key_usage_t derived_usage = derived_usage_arg;
5917 psa_algorithm_t derived_alg = derived_alg_arg;
5918 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005919 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005920 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005921 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005922
Gilles Peskine8817f612018-12-18 00:18:46 +01005923 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005924
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005925 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5926 psa_set_key_algorithm( &attributes, alg );
5927 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005928 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005929 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005930
5931 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005932 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5933 input1->x, input1->len,
5934 input2->x, input2->len,
5935 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01005936 goto exit;
5937
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005938 psa_set_key_usage_flags( &attributes, derived_usage );
5939 psa_set_key_algorithm( &attributes, derived_alg );
5940 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005941 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005942 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005943 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005944
5945 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005946 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005947 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5948 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005949
5950 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005951 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005952 goto exit;
5953
5954exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005955 /*
5956 * Key attributes may have been returned by psa_get_key_attributes()
5957 * thus reset them as required.
5958 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005959 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005960
5961 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005962 psa_destroy_key( base_key );
5963 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005964 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005965}
5966/* END_CASE */
5967
Janos Follath42fd8882019-07-03 14:17:09 +01005968/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005969void derive_key_export( int alg_arg,
5970 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005971 data_t *input1,
5972 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005973 int bytes1_arg,
5974 int bytes2_arg )
5975{
Ronald Cron5425a212020-08-04 14:58:35 +02005976 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5977 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005978 psa_algorithm_t alg = alg_arg;
5979 size_t bytes1 = bytes1_arg;
5980 size_t bytes2 = bytes2_arg;
5981 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005982 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005983 uint8_t *output_buffer = NULL;
5984 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005985 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5986 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005987 size_t length;
5988
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005989 ASSERT_ALLOC( output_buffer, capacity );
5990 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005991 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005992
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005993 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5994 psa_set_key_algorithm( &base_attributes, alg );
5995 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005996 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005997 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005998
5999 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006000 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6001 input1->x, input1->len,
6002 input2->x, input2->len,
6003 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006004 goto exit;
6005
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006006 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006007 output_buffer,
6008 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006009 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006010
6011 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006012 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6013 input1->x, input1->len,
6014 input2->x, input2->len,
6015 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006016 goto exit;
6017
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006018 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6019 psa_set_key_algorithm( &derived_attributes, 0 );
6020 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006021 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006022 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006023 &derived_key ) );
6024 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006025 export_buffer, bytes1,
6026 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006027 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02006028 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006029 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006030 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006031 &derived_key ) );
6032 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006033 export_buffer + bytes1, bytes2,
6034 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006035 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006036
6037 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006038 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
6039 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006040
6041exit:
6042 mbedtls_free( output_buffer );
6043 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006044 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006045 psa_destroy_key( base_key );
6046 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006047 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006048}
6049/* END_CASE */
6050
6051/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006052void derive_key( int alg_arg,
6053 data_t *key_data, data_t *input1, data_t *input2,
6054 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006055 int expected_status_arg,
6056 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02006057{
Ronald Cron5425a212020-08-04 14:58:35 +02006058 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6059 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02006060 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006061 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02006062 size_t bits = bits_arg;
6063 psa_status_t expected_status = expected_status_arg;
6064 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6065 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6066 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
6067
6068 PSA_ASSERT( psa_crypto_init( ) );
6069
6070 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6071 psa_set_key_algorithm( &base_attributes, alg );
6072 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
6073 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006074 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02006075
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006076 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6077 input1->x, input1->len,
6078 input2->x, input2->len,
6079 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02006080 goto exit;
6081
6082 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6083 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006084 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02006085 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01006086
6087 psa_status_t status =
6088 psa_key_derivation_output_key( &derived_attributes,
6089 &operation,
6090 &derived_key );
6091 if( is_large_output > 0 )
6092 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6093 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02006094
6095exit:
6096 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006097 psa_destroy_key( base_key );
6098 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02006099 PSA_DONE( );
6100}
6101/* END_CASE */
6102
6103/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006104void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02006105 int our_key_type_arg, int our_key_alg_arg,
6106 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006107 int expected_status_arg )
6108{
Ronald Cron5425a212020-08-04 14:58:35 +02006109 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006110 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006111 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006112 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006113 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006114 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02006115 psa_status_t expected_status = expected_status_arg;
6116 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006117
Gilles Peskine8817f612018-12-18 00:18:46 +01006118 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006119
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006120 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006121 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006122 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006123 PSA_ASSERT( psa_import_key( &attributes,
6124 our_key_data->x, our_key_data->len,
6125 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006126
Gilles Peskine77f40d82019-04-11 21:27:06 +02006127 /* The tests currently include inputs that should fail at either step.
6128 * Test cases that fail at the setup step should be changed to call
6129 * key_derivation_setup instead, and this function should be renamed
6130 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006131 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02006132 if( status == PSA_SUCCESS )
6133 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006134 TEST_EQUAL( psa_key_derivation_key_agreement(
6135 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
6136 our_key,
6137 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02006138 expected_status );
6139 }
6140 else
6141 {
6142 TEST_ASSERT( status == expected_status );
6143 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02006144
6145exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006146 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006147 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006148 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006149}
6150/* END_CASE */
6151
6152/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02006153void raw_key_agreement( int alg_arg,
6154 int our_key_type_arg, data_t *our_key_data,
6155 data_t *peer_key_data,
6156 data_t *expected_output )
6157{
Ronald Cron5425a212020-08-04 14:58:35 +02006158 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006159 psa_algorithm_t alg = alg_arg;
6160 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006161 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006162 unsigned char *output = NULL;
6163 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01006164 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006165
6166 ASSERT_ALLOC( output, expected_output->len );
6167 PSA_ASSERT( psa_crypto_init( ) );
6168
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006169 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6170 psa_set_key_algorithm( &attributes, alg );
6171 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006172 PSA_ASSERT( psa_import_key( &attributes,
6173 our_key_data->x, our_key_data->len,
6174 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006175
gabor-mezei-armceface22021-01-21 12:26:17 +01006176 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
6177 key_bits = psa_get_key_bits( &attributes );
6178
Gilles Peskinebe697d82019-05-16 18:00:41 +02006179 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
6180 peer_key_data->x, peer_key_data->len,
6181 output, expected_output->len,
6182 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006183 ASSERT_COMPARE( output, output_length,
6184 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01006185 TEST_ASSERT( output_length <=
6186 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
6187 TEST_ASSERT( output_length <=
6188 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006189
6190exit:
6191 mbedtls_free( output );
6192 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006193 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006194}
6195/* END_CASE */
6196
6197/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02006198void key_agreement_capacity( int alg_arg,
6199 int our_key_type_arg, data_t *our_key_data,
6200 data_t *peer_key_data,
6201 int expected_capacity_arg )
6202{
Ronald Cron5425a212020-08-04 14:58:35 +02006203 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006204 psa_algorithm_t alg = alg_arg;
6205 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006206 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006207 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006208 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02006209 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02006210
Gilles Peskine8817f612018-12-18 00:18:46 +01006211 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006212
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006213 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6214 psa_set_key_algorithm( &attributes, alg );
6215 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006216 PSA_ASSERT( psa_import_key( &attributes,
6217 our_key_data->x, our_key_data->len,
6218 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006219
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006220 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006221 PSA_ASSERT( psa_key_derivation_key_agreement(
6222 &operation,
6223 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6224 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006225 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6226 {
6227 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006228 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006229 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006230 NULL, 0 ) );
6231 }
Gilles Peskine59685592018-09-18 12:11:34 +02006232
Gilles Peskinebf491972018-10-25 22:36:12 +02006233 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006234 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006235 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006236 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02006237
Gilles Peskinebf491972018-10-25 22:36:12 +02006238 /* Test the actual capacity by reading the output. */
6239 while( actual_capacity > sizeof( output ) )
6240 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006241 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006242 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02006243 actual_capacity -= sizeof( output );
6244 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006245 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006246 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006247 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006248 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02006249
Gilles Peskine59685592018-09-18 12:11:34 +02006250exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006251 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006252 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006253 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006254}
6255/* END_CASE */
6256
6257/* BEGIN_CASE */
6258void key_agreement_output( int alg_arg,
6259 int our_key_type_arg, data_t *our_key_data,
6260 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006261 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02006262{
Ronald Cron5425a212020-08-04 14:58:35 +02006263 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006264 psa_algorithm_t alg = alg_arg;
6265 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006266 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006267 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006268 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02006269
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006270 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
6271 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006272
Gilles Peskine8817f612018-12-18 00:18:46 +01006273 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006274
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006275 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6276 psa_set_key_algorithm( &attributes, alg );
6277 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006278 PSA_ASSERT( psa_import_key( &attributes,
6279 our_key_data->x, our_key_data->len,
6280 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006281
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006282 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006283 PSA_ASSERT( psa_key_derivation_key_agreement(
6284 &operation,
6285 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6286 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006287 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6288 {
6289 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006290 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006291 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006292 NULL, 0 ) );
6293 }
Gilles Peskine59685592018-09-18 12:11:34 +02006294
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006295 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006296 actual_output,
6297 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006298 ASSERT_COMPARE( actual_output, expected_output1->len,
6299 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006300 if( expected_output2->len != 0 )
6301 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006302 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006303 actual_output,
6304 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006305 ASSERT_COMPARE( actual_output, expected_output2->len,
6306 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006307 }
Gilles Peskine59685592018-09-18 12:11:34 +02006308
6309exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006310 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006311 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006312 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006313 mbedtls_free( actual_output );
6314}
6315/* END_CASE */
6316
6317/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02006318void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02006319{
Gilles Peskinea50d7392018-06-21 10:22:13 +02006320 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006321 unsigned char *output = NULL;
6322 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02006323 size_t i;
6324 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02006325
Simon Butcher49f8e312020-03-03 15:51:50 +00006326 TEST_ASSERT( bytes_arg >= 0 );
6327
Gilles Peskine91892022021-02-08 19:50:26 +01006328 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006329 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02006330
Gilles Peskine8817f612018-12-18 00:18:46 +01006331 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02006332
Gilles Peskinea50d7392018-06-21 10:22:13 +02006333 /* Run several times, to ensure that every output byte will be
6334 * nonzero at least once with overwhelming probability
6335 * (2^(-8*number_of_runs)). */
6336 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02006337 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006338 if( bytes != 0 )
6339 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01006340 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006341
Gilles Peskinea50d7392018-06-21 10:22:13 +02006342 for( i = 0; i < bytes; i++ )
6343 {
6344 if( output[i] != 0 )
6345 ++changed[i];
6346 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006347 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02006348
6349 /* Check that every byte was changed to nonzero at least once. This
6350 * validates that psa_generate_random is overwriting every byte of
6351 * the output buffer. */
6352 for( i = 0; i < bytes; i++ )
6353 {
6354 TEST_ASSERT( changed[i] != 0 );
6355 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006356
6357exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006358 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006359 mbedtls_free( output );
6360 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02006361}
6362/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02006363
6364/* BEGIN_CASE */
6365void generate_key( int type_arg,
6366 int bits_arg,
6367 int usage_arg,
6368 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006369 int expected_status_arg,
6370 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006371{
Ronald Cron5425a212020-08-04 14:58:35 +02006372 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006373 psa_key_type_t type = type_arg;
6374 psa_key_usage_t usage = usage_arg;
6375 size_t bits = bits_arg;
6376 psa_algorithm_t alg = alg_arg;
6377 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006378 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006379 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006380
Gilles Peskine8817f612018-12-18 00:18:46 +01006381 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006382
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006383 psa_set_key_usage_flags( &attributes, usage );
6384 psa_set_key_algorithm( &attributes, alg );
6385 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006386 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006387
6388 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01006389 psa_status_t status = psa_generate_key( &attributes, &key );
6390
6391 if( is_large_key > 0 )
6392 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6393 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006394 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006395 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006396
6397 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006398 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006399 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
6400 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006401
Gilles Peskine818ca122018-06-20 18:16:48 +02006402 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006403 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02006404 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006405
6406exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006407 /*
6408 * Key attributes may have been returned by psa_get_key_attributes()
6409 * thus reset them as required.
6410 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006411 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006412
Ronald Cron5425a212020-08-04 14:58:35 +02006413 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006414 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006415}
6416/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03006417
Ronald Cronee414c72021-03-18 18:50:08 +01006418/* 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 +02006419void generate_key_rsa( int bits_arg,
6420 data_t *e_arg,
6421 int expected_status_arg )
6422{
Ronald Cron5425a212020-08-04 14:58:35 +02006423 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006424 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006425 size_t bits = bits_arg;
6426 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
6427 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
6428 psa_status_t expected_status = expected_status_arg;
6429 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6430 uint8_t *exported = NULL;
6431 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006432 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006433 size_t exported_length = SIZE_MAX;
6434 uint8_t *e_read_buffer = NULL;
6435 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02006436 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006437 size_t e_read_length = SIZE_MAX;
6438
6439 if( e_arg->len == 0 ||
6440 ( e_arg->len == 3 &&
6441 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
6442 {
6443 is_default_public_exponent = 1;
6444 e_read_size = 0;
6445 }
6446 ASSERT_ALLOC( e_read_buffer, e_read_size );
6447 ASSERT_ALLOC( exported, exported_size );
6448
6449 PSA_ASSERT( psa_crypto_init( ) );
6450
6451 psa_set_key_usage_flags( &attributes, usage );
6452 psa_set_key_algorithm( &attributes, alg );
6453 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
6454 e_arg->x, e_arg->len ) );
6455 psa_set_key_bits( &attributes, bits );
6456
6457 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006458 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006459 if( expected_status != PSA_SUCCESS )
6460 goto exit;
6461
6462 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006463 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006464 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6465 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6466 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
6467 e_read_buffer, e_read_size,
6468 &e_read_length ) );
6469 if( is_default_public_exponent )
6470 TEST_EQUAL( e_read_length, 0 );
6471 else
6472 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
6473
6474 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006475 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02006476 goto exit;
6477
6478 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02006479 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006480 exported, exported_size,
6481 &exported_length ) );
6482 {
6483 uint8_t *p = exported;
6484 uint8_t *end = exported + exported_length;
6485 size_t len;
6486 /* RSAPublicKey ::= SEQUENCE {
6487 * modulus INTEGER, -- n
6488 * publicExponent INTEGER } -- e
6489 */
6490 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006491 MBEDTLS_ASN1_SEQUENCE |
6492 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01006493 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006494 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
6495 MBEDTLS_ASN1_INTEGER ) );
6496 if( len >= 1 && p[0] == 0 )
6497 {
6498 ++p;
6499 --len;
6500 }
6501 if( e_arg->len == 0 )
6502 {
6503 TEST_EQUAL( len, 3 );
6504 TEST_EQUAL( p[0], 1 );
6505 TEST_EQUAL( p[1], 0 );
6506 TEST_EQUAL( p[2], 1 );
6507 }
6508 else
6509 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
6510 }
6511
6512exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006513 /*
6514 * Key attributes may have been returned by psa_get_key_attributes() or
6515 * set by psa_set_key_domain_parameters() thus reset them as required.
6516 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006517 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006518
Ronald Cron5425a212020-08-04 14:58:35 +02006519 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006520 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006521 mbedtls_free( e_read_buffer );
6522 mbedtls_free( exported );
6523}
6524/* END_CASE */
6525
Darryl Greend49a4992018-06-18 17:27:26 +01006526/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006527void persistent_key_load_key_from_storage( data_t *data,
6528 int type_arg, int bits_arg,
6529 int usage_flags_arg, int alg_arg,
6530 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01006531{
Ronald Cron71016a92020-08-28 19:01:50 +02006532 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006533 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02006534 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6535 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006536 psa_key_type_t type = type_arg;
6537 size_t bits = bits_arg;
6538 psa_key_usage_t usage_flags = usage_flags_arg;
6539 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006540 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01006541 unsigned char *first_export = NULL;
6542 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006543 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006544 size_t first_exported_length;
6545 size_t second_exported_length;
6546
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006547 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6548 {
6549 ASSERT_ALLOC( first_export, export_size );
6550 ASSERT_ALLOC( second_export, export_size );
6551 }
Darryl Greend49a4992018-06-18 17:27:26 +01006552
Gilles Peskine8817f612018-12-18 00:18:46 +01006553 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006554
Gilles Peskinec87af662019-05-15 16:12:22 +02006555 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006556 psa_set_key_usage_flags( &attributes, usage_flags );
6557 psa_set_key_algorithm( &attributes, alg );
6558 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006559 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006560
Darryl Green0c6575a2018-11-07 16:05:30 +00006561 switch( generation_method )
6562 {
6563 case IMPORT_KEY:
6564 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02006565 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006566 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006567 break;
Darryl Greend49a4992018-06-18 17:27:26 +01006568
Darryl Green0c6575a2018-11-07 16:05:30 +00006569 case GENERATE_KEY:
6570 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006571 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006572 break;
6573
6574 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01006575#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006576 {
6577 /* Create base key */
6578 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
6579 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6580 psa_set_key_usage_flags( &base_attributes,
6581 PSA_KEY_USAGE_DERIVE );
6582 psa_set_key_algorithm( &base_attributes, derive_alg );
6583 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006584 PSA_ASSERT( psa_import_key( &base_attributes,
6585 data->x, data->len,
6586 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006587 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006588 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006589 PSA_ASSERT( psa_key_derivation_input_key(
6590 &operation,
6591 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006592 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006593 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006594 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006595 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
6596 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006597 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006598 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006599 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02006600 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006601 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01006602#else
6603 TEST_ASSUME( ! "KDF not supported in this configuration" );
6604#endif
6605 break;
6606
6607 default:
6608 TEST_ASSERT( ! "generation_method not implemented in test" );
6609 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00006610 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006611 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01006612
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006613 /* Export the key if permitted by the key policy. */
6614 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6615 {
Ronald Cron5425a212020-08-04 14:58:35 +02006616 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006617 first_export, export_size,
6618 &first_exported_length ) );
6619 if( generation_method == IMPORT_KEY )
6620 ASSERT_COMPARE( data->x, data->len,
6621 first_export, first_exported_length );
6622 }
Darryl Greend49a4992018-06-18 17:27:26 +01006623
6624 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02006625 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006626 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01006627 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006628
Darryl Greend49a4992018-06-18 17:27:26 +01006629 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02006630 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02006631 TEST_ASSERT( mbedtls_svc_key_id_equal(
6632 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006633 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
6634 PSA_KEY_LIFETIME_PERSISTENT );
6635 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6636 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6637 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
6638 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01006639
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006640 /* Export the key again if permitted by the key policy. */
6641 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00006642 {
Ronald Cron5425a212020-08-04 14:58:35 +02006643 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006644 second_export, export_size,
6645 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006646 ASSERT_COMPARE( first_export, first_exported_length,
6647 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006648 }
6649
6650 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006651 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00006652 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01006653
6654exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006655 /*
6656 * Key attributes may have been returned by psa_get_key_attributes()
6657 * thus reset them as required.
6658 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006659 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006660
Darryl Greend49a4992018-06-18 17:27:26 +01006661 mbedtls_free( first_export );
6662 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006663 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006664 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02006665 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006666 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01006667}
6668/* END_CASE */