blob: 7c988067adebbd141a6d2527a0640857e7c59963 [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;
319 psa_aead_operation_t operation;
320 unsigned char *output_data = NULL;
321 unsigned char *part_data = NULL;
322 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100323 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100324 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100325 size_t output_size = 0;
326 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100327 size_t output_length = 0;
328 size_t key_bits = 0;
329 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100330 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100331 size_t part_length = 0;
332 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100333 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100334 size_t ad_part_len = 0;
335 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100336 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100337 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
338 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
339
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100340 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100341 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100342
Paul Elliottd3f82412021-06-16 16:52:21 +0100343 PSA_ASSERT( psa_crypto_init( ) );
344
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100345 if( is_encrypt )
346 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
347 else
348 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
349
Paul Elliottd3f82412021-06-16 16:52:21 +0100350 psa_set_key_algorithm( &attributes, alg );
351 psa_set_key_type( &attributes, key_type );
352
353 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
354 &key ) );
355
356 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
357 key_bits = psa_get_key_bits( &attributes );
358
359 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
360
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100361 if( is_encrypt )
362 {
363 /* Tag gets written at end of buffer. */
364 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
365 ( input_data->len +
366 tag_length ) );
367 data_true_size = input_data->len;
368 }
369 else
370 {
371 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
372 ( input_data->len -
373 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100374
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100375 /* Do not want to attempt to decrypt tag. */
376 data_true_size = input_data->len - tag_length;
377 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100378
379 ASSERT_ALLOC( output_data, output_size );
380
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100381 if( is_encrypt )
382 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100383 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
384 TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100385 }
386 else
387 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100388 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
389 TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100390 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100391
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100392 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100393
394 operation = psa_aead_operation_init( );
395
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100396
397 if( is_encrypt )
398 status = psa_aead_encrypt_setup( &operation, key, alg );
399 else
400 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100401
402 /* If the operation is not supported, just skip and not fail in case the
403 * encryption involves a common limitation of cryptography hardwares and
404 * an alternative implementation. */
405 if( status == PSA_ERROR_NOT_SUPPORTED )
406 {
407 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
408 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
409 }
410
411 PSA_ASSERT( status );
412
Paul Elliott33746aa2021-09-15 16:40:40 +0100413 if( set_lengths_method == DO_NOT_SET_LENGTHS )
414 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
415 else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
Paul Elliottd3f82412021-06-16 16:52:21 +0100416 {
Paul Elliott33746aa2021-09-15 16:40:40 +0100417 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
418 data_true_size ) );
Paul Elliottebf91632021-07-22 17:54:42 +0100419 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
420 }
Paul Elliott33746aa2021-09-15 16:40:40 +0100421 else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
Paul Elliottebf91632021-07-22 17:54:42 +0100422 {
423 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
424
Paul Elliott33746aa2021-09-15 16:40:40 +0100425 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
426 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100427 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100428
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100429 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100430 {
431 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100432 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100433
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100434 for( part_offset = 0, part_count = 0;
435 part_offset < additional_data->len;
436 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100437 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100438 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100439 {
Paul Elliott329d5382021-07-22 17:10:45 +0100440 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100441 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100442 else if( additional_data->len - part_offset < ad_part_len )
443 {
444 part_length = additional_data->len - part_offset;
445 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100446 else
447 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100448 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100449 }
450
451 PSA_ASSERT( psa_aead_update_ad( &operation,
452 additional_data->x + part_offset,
453 part_length ) );
454
Paul Elliottd3f82412021-06-16 16:52:21 +0100455 }
456 }
457 else
458 {
459 /* Pass additional data in one go. */
460 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
461 additional_data->len ) );
462 }
463
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100464 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100465 {
466 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100467 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100468 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100469 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100470
471 ASSERT_ALLOC( part_data, part_data_size );
472
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100473 for( part_offset = 0, part_count = 0;
474 part_offset < data_true_size;
475 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100476 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100477 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100478 {
Paul Elliott329d5382021-07-22 17:10:45 +0100479 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100480 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100481 else if( ( data_true_size - part_offset ) < data_part_len )
482 {
483 part_length = ( data_true_size - part_offset );
484 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100485 else
486 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100487 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100488 }
489
490 PSA_ASSERT( psa_aead_update( &operation,
491 ( input_data->x + part_offset ),
492 part_length, part_data,
493 part_data_size,
494 &output_part_length ) );
495
496 if( output_data && output_part_length )
497 {
498 memcpy( ( output_data + part_offset ), part_data,
499 output_part_length );
500 }
501
Paul Elliottd3f82412021-06-16 16:52:21 +0100502 output_length += output_part_length;
503 }
504 }
505 else
506 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100507 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100508 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100509 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100510 output_size, &output_length ) );
511 }
512
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100513 if( is_encrypt )
514 PSA_ASSERT( psa_aead_finish( &operation, final_data,
515 final_output_size,
516 &output_part_length,
517 tag_buffer, tag_length,
518 &tag_size ) );
519 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100520 {
Paul Elliott9961a662021-09-17 19:19:02 +0100521 PSA_ASSERT( psa_aead_verify( &operation, final_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100522 final_output_size,
523 &output_part_length,
524 ( input_data->x + data_true_size ),
Paul Elliott9961a662021-09-17 19:19:02 +0100525 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100526 }
527
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100528 if( output_data && output_part_length )
529 memcpy( ( output_data + output_length ), final_data,
530 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100531
532 output_length += output_part_length;
533
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100534
535 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
536 * should be exact.*/
537 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100538 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100539 TEST_EQUAL( tag_length, tag_size );
540
541 if( output_data && tag_length )
542 memcpy( ( output_data + output_length ), tag_buffer,
543 tag_length );
544
545 output_length += tag_length;
546
547 TEST_EQUAL( output_length,
548 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
549 input_data->len ) );
550 TEST_ASSERT( output_length <=
551 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
552 }
553 else
554 {
555 TEST_EQUAL( output_length,
556 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
557 input_data->len ) );
558 TEST_ASSERT( output_length <=
559 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100560 }
561
Paul Elliottd3f82412021-06-16 16:52:21 +0100562
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100563 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100564 output_data, output_length );
565
Paul Elliottd3f82412021-06-16 16:52:21 +0100566
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100567 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100568
569exit:
570 psa_destroy_key( key );
571 psa_aead_abort( &operation );
572 mbedtls_free( output_data );
573 mbedtls_free( part_data );
574 mbedtls_free( final_data );
575 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100576
577 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100578}
579
Gilles Peskinee59236f2018-01-27 23:32:46 +0100580/* END_HEADER */
581
582/* BEGIN_DEPENDENCIES
583 * depends_on:MBEDTLS_PSA_CRYPTO_C
584 * END_DEPENDENCIES
585 */
586
587/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200588void static_checks( )
589{
590 size_t max_truncated_mac_size =
591 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
592
593 /* Check that the length for a truncated MAC always fits in the algorithm
594 * encoding. The shifted mask is the maximum truncated value. The
595 * untruncated algorithm may be one byte larger. */
596 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
597}
598/* END_CASE */
599
600/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200601void import_with_policy( int type_arg,
602 int usage_arg, int alg_arg,
603 int expected_status_arg )
604{
605 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
606 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200607 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200608 psa_key_type_t type = type_arg;
609 psa_key_usage_t usage = usage_arg;
610 psa_algorithm_t alg = alg_arg;
611 psa_status_t expected_status = expected_status_arg;
612 const uint8_t key_material[16] = {0};
613 psa_status_t status;
614
615 PSA_ASSERT( psa_crypto_init( ) );
616
617 psa_set_key_type( &attributes, type );
618 psa_set_key_usage_flags( &attributes, usage );
619 psa_set_key_algorithm( &attributes, alg );
620
621 status = psa_import_key( &attributes,
622 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200623 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200624 TEST_EQUAL( status, expected_status );
625 if( status != PSA_SUCCESS )
626 goto exit;
627
Ronald Cron5425a212020-08-04 14:58:35 +0200628 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200629 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
630 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
631 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200632 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200633
Ronald Cron5425a212020-08-04 14:58:35 +0200634 PSA_ASSERT( psa_destroy_key( key ) );
635 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200636
637exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100638 /*
639 * Key attributes may have been returned by psa_get_key_attributes()
640 * thus reset them as required.
641 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200642 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100643
644 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200645 PSA_DONE( );
646}
647/* END_CASE */
648
649/* BEGIN_CASE */
650void import_with_data( data_t *data, int type_arg,
651 int attr_bits_arg,
652 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200653{
654 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
655 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200656 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200657 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200658 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200659 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100660 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100661
Gilles Peskine8817f612018-12-18 00:18:46 +0100662 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100663
Gilles Peskine4747d192019-04-17 15:05:45 +0200664 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200665 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200666
Ronald Cron5425a212020-08-04 14:58:35 +0200667 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100668 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200669 if( status != PSA_SUCCESS )
670 goto exit;
671
Ronald Cron5425a212020-08-04 14:58:35 +0200672 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200673 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200674 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200675 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200676 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200677
Ronald Cron5425a212020-08-04 14:58:35 +0200678 PSA_ASSERT( psa_destroy_key( key ) );
679 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100680
681exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100682 /*
683 * Key attributes may have been returned by psa_get_key_attributes()
684 * thus reset them as required.
685 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200686 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100687
688 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200689 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100690}
691/* END_CASE */
692
693/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200694void import_large_key( int type_arg, int byte_size_arg,
695 int expected_status_arg )
696{
697 psa_key_type_t type = type_arg;
698 size_t byte_size = byte_size_arg;
699 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
700 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200701 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200702 psa_status_t status;
703 uint8_t *buffer = NULL;
704 size_t buffer_size = byte_size + 1;
705 size_t n;
706
Steven Cooreman69967ce2021-01-18 18:01:08 +0100707 /* Skip the test case if the target running the test cannot
708 * accomodate large keys due to heap size constraints */
709 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200710 memset( buffer, 'K', byte_size );
711
712 PSA_ASSERT( psa_crypto_init( ) );
713
714 /* Try importing the key */
715 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
716 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200717 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100718 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200719 TEST_EQUAL( status, expected_status );
720
721 if( status == PSA_SUCCESS )
722 {
Ronald Cron5425a212020-08-04 14:58:35 +0200723 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200724 TEST_EQUAL( psa_get_key_type( &attributes ), type );
725 TEST_EQUAL( psa_get_key_bits( &attributes ),
726 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200727 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200728 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200729 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200730 for( n = 0; n < byte_size; n++ )
731 TEST_EQUAL( buffer[n], 'K' );
732 for( n = byte_size; n < buffer_size; n++ )
733 TEST_EQUAL( buffer[n], 0 );
734 }
735
736exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100737 /*
738 * Key attributes may have been returned by psa_get_key_attributes()
739 * thus reset them as required.
740 */
741 psa_reset_key_attributes( &attributes );
742
Ronald Cron5425a212020-08-04 14:58:35 +0200743 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200744 PSA_DONE( );
745 mbedtls_free( buffer );
746}
747/* END_CASE */
748
749/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200750void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
751{
Ronald Cron5425a212020-08-04 14:58:35 +0200752 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200753 size_t bits = bits_arg;
754 psa_status_t expected_status = expected_status_arg;
755 psa_status_t status;
756 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200757 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200758 size_t buffer_size = /* Slight overapproximations */
759 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200760 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200761 unsigned char *p;
762 int ret;
763 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200764 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200765
Gilles Peskine8817f612018-12-18 00:18:46 +0100766 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200767 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200768
769 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
770 bits, keypair ) ) >= 0 );
771 length = ret;
772
773 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200774 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200775 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100776 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200777
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200778 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200779 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200780
781exit:
782 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200783 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200784}
785/* END_CASE */
786
787/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300788void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300789 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200790 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100791 int expected_bits,
792 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200793 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100794 int canonical_input )
795{
Ronald Cron5425a212020-08-04 14:58:35 +0200796 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100797 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200798 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200799 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100800 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100801 unsigned char *exported = NULL;
802 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100803 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100804 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100805 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200806 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200807 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100808
Moran Pekercb088e72018-07-17 17:36:59 +0300809 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200810 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100811 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200812 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100813 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100814
Gilles Peskine4747d192019-04-17 15:05:45 +0200815 psa_set_key_usage_flags( &attributes, usage_arg );
816 psa_set_key_algorithm( &attributes, alg );
817 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700818
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100819 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200820 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100821
822 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200823 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200824 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
825 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200826 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100827
828 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200829 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100830 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100831
832 /* The exported length must be set by psa_export_key() to a value between 0
833 * and export_size. On errors, the exported length must be 0. */
834 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
835 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
836 TEST_ASSERT( exported_length <= export_size );
837
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200838 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200839 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100840 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200841 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100842 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100843 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200844 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100845
Gilles Peskineea38a922021-02-13 00:05:16 +0100846 /* Run sanity checks on the exported key. For non-canonical inputs,
847 * this validates the canonical representations. For canonical inputs,
848 * this doesn't directly validate the implementation, but it still helps
849 * by cross-validating the test data with the sanity check code. */
850 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200851 goto exit;
852
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100853 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200854 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100855 else
856 {
Ronald Cron5425a212020-08-04 14:58:35 +0200857 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200858 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200859 &key2 ) );
860 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100861 reexported,
862 export_size,
863 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200864 ASSERT_COMPARE( exported, exported_length,
865 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200866 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100867 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100868 TEST_ASSERT( exported_length <=
869 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
870 psa_get_key_bits( &got_attributes ) ) );
871 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100872
873destroy:
874 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200875 PSA_ASSERT( psa_destroy_key( key ) );
876 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100877
878exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100879 /*
880 * Key attributes may have been returned by psa_get_key_attributes()
881 * thus reset them as required.
882 */
883 psa_reset_key_attributes( &got_attributes );
884
itayzafrir3e02b3b2018-06-12 17:06:52 +0300885 mbedtls_free( exported );
886 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200887 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100888}
889/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100890
Moran Pekerf709f4a2018-06-06 17:26:04 +0300891/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300892void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200893 int type_arg,
894 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100895 int export_size_delta,
896 int expected_export_status_arg,
897 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300898{
Ronald Cron5425a212020-08-04 14:58:35 +0200899 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300900 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200901 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200902 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300903 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300904 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100905 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100906 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200907 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300908
Gilles Peskine8817f612018-12-18 00:18:46 +0100909 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300910
Gilles Peskine4747d192019-04-17 15:05:45 +0200911 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
912 psa_set_key_algorithm( &attributes, alg );
913 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300914
915 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200916 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300917
Gilles Peskine49c25912018-10-29 15:15:31 +0100918 /* Export the public key */
919 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200920 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200921 exported, export_size,
922 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100923 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100924 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100925 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200926 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100927 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200928 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200929 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100930 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100931 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100932 TEST_ASSERT( expected_public_key->len <=
933 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
934 TEST_ASSERT( expected_public_key->len <=
935 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100936 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
937 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100938 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300939
940exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100941 /*
942 * Key attributes may have been returned by psa_get_key_attributes()
943 * thus reset them as required.
944 */
945 psa_reset_key_attributes( &attributes );
946
itayzafrir3e02b3b2018-06-12 17:06:52 +0300947 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200948 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200949 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300950}
951/* END_CASE */
952
Gilles Peskine20035e32018-02-03 22:44:14 +0100953/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200954void import_and_exercise_key( data_t *data,
955 int type_arg,
956 int bits_arg,
957 int alg_arg )
958{
Ronald Cron5425a212020-08-04 14:58:35 +0200959 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200960 psa_key_type_t type = type_arg;
961 size_t bits = bits_arg;
962 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100963 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200964 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200965 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200966
Gilles Peskine8817f612018-12-18 00:18:46 +0100967 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200968
Gilles Peskine4747d192019-04-17 15:05:45 +0200969 psa_set_key_usage_flags( &attributes, usage );
970 psa_set_key_algorithm( &attributes, alg );
971 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200972
973 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200974 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200975
976 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200977 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200978 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
979 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200980
981 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100982 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200983 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200984
Ronald Cron5425a212020-08-04 14:58:35 +0200985 PSA_ASSERT( psa_destroy_key( key ) );
986 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200987
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200988exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100989 /*
990 * Key attributes may have been returned by psa_get_key_attributes()
991 * thus reset them as required.
992 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200993 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100994
995 psa_reset_key_attributes( &attributes );
996 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200997 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200998}
999/* END_CASE */
1000
1001/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001002void effective_key_attributes( int type_arg, int expected_type_arg,
1003 int bits_arg, int expected_bits_arg,
1004 int usage_arg, int expected_usage_arg,
1005 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001006{
Ronald Cron5425a212020-08-04 14:58:35 +02001007 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001008 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001009 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001010 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001011 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001012 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001013 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001014 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001015 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001016 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001017
Gilles Peskine8817f612018-12-18 00:18:46 +01001018 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001019
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001020 psa_set_key_usage_flags( &attributes, usage );
1021 psa_set_key_algorithm( &attributes, alg );
1022 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001023 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001024
Ronald Cron5425a212020-08-04 14:58:35 +02001025 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001026 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001027
Ronald Cron5425a212020-08-04 14:58:35 +02001028 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001029 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1030 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1031 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1032 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001033
1034exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001035 /*
1036 * Key attributes may have been returned by psa_get_key_attributes()
1037 * thus reset them as required.
1038 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001039 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001040
1041 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001042 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001043}
1044/* END_CASE */
1045
1046/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001047void check_key_policy( int type_arg, int bits_arg,
1048 int usage_arg, int alg_arg )
1049{
1050 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1051 usage_arg, usage_arg, alg_arg, alg_arg );
1052 goto exit;
1053}
1054/* END_CASE */
1055
1056/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001057void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001058{
1059 /* Test each valid way of initializing the object, except for `= {0}`, as
1060 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1061 * though it's OK by the C standard. We could test for this, but we'd need
1062 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001063 psa_key_attributes_t func = psa_key_attributes_init( );
1064 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1065 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001066
1067 memset( &zero, 0, sizeof( zero ) );
1068
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001069 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1070 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1071 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001072
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001073 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1074 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1075 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1076
1077 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1078 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1079 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1080
1081 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1082 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1083 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1084
1085 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1086 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1087 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001088}
1089/* END_CASE */
1090
1091/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001092void mac_key_policy( int policy_usage,
1093 int policy_alg,
1094 int key_type,
1095 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001096 int exercise_alg,
1097 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001098{
Ronald Cron5425a212020-08-04 14:58:35 +02001099 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001100 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001101 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001102 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001103 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001104 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001105
Gilles Peskine8817f612018-12-18 00:18:46 +01001106 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001107
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001108 psa_set_key_usage_flags( &attributes, policy_usage );
1109 psa_set_key_algorithm( &attributes, policy_alg );
1110 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001111
Gilles Peskine049c7532019-05-15 20:22:09 +02001112 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001113 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001114
Ronald Cron5425a212020-08-04 14:58:35 +02001115 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001116 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001117 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001118 else
1119 TEST_EQUAL( status, expected_status );
1120
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001121 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001122
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001123 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001124 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001125 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001126 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001127 else
1128 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001129
1130exit:
1131 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001132 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001133 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001134}
1135/* END_CASE */
1136
1137/* BEGIN_CASE */
1138void cipher_key_policy( int policy_usage,
1139 int policy_alg,
1140 int key_type,
1141 data_t *key_data,
1142 int exercise_alg )
1143{
Ronald Cron5425a212020-08-04 14:58:35 +02001144 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001145 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001146 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001147 psa_status_t status;
1148
Gilles Peskine8817f612018-12-18 00:18:46 +01001149 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001150
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001151 psa_set_key_usage_flags( &attributes, policy_usage );
1152 psa_set_key_algorithm( &attributes, policy_alg );
1153 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001154
Gilles Peskine049c7532019-05-15 20:22:09 +02001155 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001156 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001157
Ronald Cron5425a212020-08-04 14:58:35 +02001158 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001159 if( policy_alg == exercise_alg &&
1160 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001161 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001162 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001163 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001164 psa_cipher_abort( &operation );
1165
Ronald Cron5425a212020-08-04 14:58:35 +02001166 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001167 if( policy_alg == exercise_alg &&
1168 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001169 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001170 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001171 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001172
1173exit:
1174 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001175 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001176 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001177}
1178/* END_CASE */
1179
1180/* BEGIN_CASE */
1181void aead_key_policy( int policy_usage,
1182 int policy_alg,
1183 int key_type,
1184 data_t *key_data,
1185 int nonce_length_arg,
1186 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001187 int exercise_alg,
1188 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001189{
Ronald Cron5425a212020-08-04 14:58:35 +02001190 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001191 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001192 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001193 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001194 unsigned char nonce[16] = {0};
1195 size_t nonce_length = nonce_length_arg;
1196 unsigned char tag[16];
1197 size_t tag_length = tag_length_arg;
1198 size_t output_length;
1199
1200 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1201 TEST_ASSERT( tag_length <= sizeof( tag ) );
1202
Gilles Peskine8817f612018-12-18 00:18:46 +01001203 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001204
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001205 psa_set_key_usage_flags( &attributes, policy_usage );
1206 psa_set_key_algorithm( &attributes, policy_alg );
1207 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001208
Gilles Peskine049c7532019-05-15 20:22:09 +02001209 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001210 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001211
Ronald Cron5425a212020-08-04 14:58:35 +02001212 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001213 nonce, nonce_length,
1214 NULL, 0,
1215 NULL, 0,
1216 tag, tag_length,
1217 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001218 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1219 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001220 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001221 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001222
1223 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001224 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001225 nonce, nonce_length,
1226 NULL, 0,
1227 tag, tag_length,
1228 NULL, 0,
1229 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001230 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1231 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1232 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001233 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001234 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001235 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001236
1237exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001238 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001239 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001240}
1241/* END_CASE */
1242
1243/* BEGIN_CASE */
1244void asymmetric_encryption_key_policy( int policy_usage,
1245 int policy_alg,
1246 int key_type,
1247 data_t *key_data,
1248 int exercise_alg )
1249{
Ronald Cron5425a212020-08-04 14:58:35 +02001250 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001251 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001252 psa_status_t status;
1253 size_t key_bits;
1254 size_t buffer_length;
1255 unsigned char *buffer = NULL;
1256 size_t output_length;
1257
Gilles Peskine8817f612018-12-18 00:18:46 +01001258 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001259
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001260 psa_set_key_usage_flags( &attributes, policy_usage );
1261 psa_set_key_algorithm( &attributes, policy_alg );
1262 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001263
Gilles Peskine049c7532019-05-15 20:22:09 +02001264 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001265 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001266
Ronald Cron5425a212020-08-04 14:58:35 +02001267 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001268 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001269 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1270 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001271 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001272
Ronald Cron5425a212020-08-04 14:58:35 +02001273 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001274 NULL, 0,
1275 NULL, 0,
1276 buffer, buffer_length,
1277 &output_length );
1278 if( policy_alg == exercise_alg &&
1279 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001280 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001281 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001282 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001283
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001284 if( buffer_length != 0 )
1285 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001286 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001287 buffer, buffer_length,
1288 NULL, 0,
1289 buffer, buffer_length,
1290 &output_length );
1291 if( policy_alg == exercise_alg &&
1292 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001293 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001294 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001295 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001296
1297exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001298 /*
1299 * Key attributes may have been returned by psa_get_key_attributes()
1300 * thus reset them as required.
1301 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001302 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001303
1304 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001305 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001306 mbedtls_free( buffer );
1307}
1308/* END_CASE */
1309
1310/* BEGIN_CASE */
1311void asymmetric_signature_key_policy( int policy_usage,
1312 int policy_alg,
1313 int key_type,
1314 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001315 int exercise_alg,
1316 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001317{
Ronald Cron5425a212020-08-04 14:58:35 +02001318 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001319 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001320 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001321 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1322 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1323 * compatible with the policy and `payload_length_arg` is supposed to be
1324 * a valid input length to sign. If `payload_length_arg <= 0`,
1325 * `exercise_alg` is supposed to be forbidden by the policy. */
1326 int compatible_alg = payload_length_arg > 0;
1327 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001328 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001329 size_t signature_length;
1330
Gilles Peskine8817f612018-12-18 00:18:46 +01001331 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001332
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001333 psa_set_key_usage_flags( &attributes, policy_usage );
1334 psa_set_key_algorithm( &attributes, policy_alg );
1335 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001336
Gilles Peskine049c7532019-05-15 20:22:09 +02001337 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001338 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001339
Ronald Cron5425a212020-08-04 14:58:35 +02001340 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001341 payload, payload_length,
1342 signature, sizeof( signature ),
1343 &signature_length );
1344 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001345 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001346 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001347 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001348
1349 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001350 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001351 payload, payload_length,
1352 signature, sizeof( signature ) );
1353 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001354 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001355 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001356 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001357
1358exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001359 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001360 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001361}
1362/* END_CASE */
1363
Janos Follathba3fab92019-06-11 14:50:16 +01001364/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001365void derive_key_policy( int policy_usage,
1366 int policy_alg,
1367 int key_type,
1368 data_t *key_data,
1369 int exercise_alg )
1370{
Ronald Cron5425a212020-08-04 14:58:35 +02001371 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001372 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001373 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001374 psa_status_t status;
1375
Gilles Peskine8817f612018-12-18 00:18:46 +01001376 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001377
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001378 psa_set_key_usage_flags( &attributes, policy_usage );
1379 psa_set_key_algorithm( &attributes, policy_alg );
1380 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001381
Gilles Peskine049c7532019-05-15 20:22:09 +02001382 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001383 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001384
Janos Follathba3fab92019-06-11 14:50:16 +01001385 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1386
1387 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1388 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001389 {
Janos Follathba3fab92019-06-11 14:50:16 +01001390 PSA_ASSERT( psa_key_derivation_input_bytes(
1391 &operation,
1392 PSA_KEY_DERIVATION_INPUT_SEED,
1393 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001394 }
Janos Follathba3fab92019-06-11 14:50:16 +01001395
1396 status = psa_key_derivation_input_key( &operation,
1397 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001398 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001399
Gilles Peskineea0fb492018-07-12 17:17:20 +02001400 if( policy_alg == exercise_alg &&
1401 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001402 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001403 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001404 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001405
1406exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001407 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001408 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001409 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001410}
1411/* END_CASE */
1412
1413/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001414void agreement_key_policy( int policy_usage,
1415 int policy_alg,
1416 int key_type_arg,
1417 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001418 int exercise_alg,
1419 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001420{
Ronald Cron5425a212020-08-04 14:58:35 +02001421 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001422 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001423 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001424 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001425 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001426 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001427
Gilles Peskine8817f612018-12-18 00:18:46 +01001428 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001429
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001430 psa_set_key_usage_flags( &attributes, policy_usage );
1431 psa_set_key_algorithm( &attributes, policy_alg );
1432 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001433
Gilles Peskine049c7532019-05-15 20:22:09 +02001434 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001435 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001436
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001437 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001438 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001439
Steven Cooremance48e852020-10-05 16:02:45 +02001440 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001441
1442exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001443 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001444 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001445 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001446}
1447/* END_CASE */
1448
1449/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001450void key_policy_alg2( int key_type_arg, data_t *key_data,
1451 int usage_arg, int alg_arg, int alg2_arg )
1452{
Ronald Cron5425a212020-08-04 14:58:35 +02001453 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001454 psa_key_type_t key_type = key_type_arg;
1455 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1456 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1457 psa_key_usage_t usage = usage_arg;
1458 psa_algorithm_t alg = alg_arg;
1459 psa_algorithm_t alg2 = alg2_arg;
1460
1461 PSA_ASSERT( psa_crypto_init( ) );
1462
1463 psa_set_key_usage_flags( &attributes, usage );
1464 psa_set_key_algorithm( &attributes, alg );
1465 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1466 psa_set_key_type( &attributes, key_type );
1467 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001468 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001469
Ronald Cron5425a212020-08-04 14:58:35 +02001470 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001471 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1472 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1473 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1474
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001475 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001476 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001477 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001478 goto exit;
1479
1480exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001481 /*
1482 * Key attributes may have been returned by psa_get_key_attributes()
1483 * thus reset them as required.
1484 */
1485 psa_reset_key_attributes( &got_attributes );
1486
Ronald Cron5425a212020-08-04 14:58:35 +02001487 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001488 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001489}
1490/* END_CASE */
1491
1492/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001493void raw_agreement_key_policy( int policy_usage,
1494 int policy_alg,
1495 int key_type_arg,
1496 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001497 int exercise_alg,
1498 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001499{
Ronald Cron5425a212020-08-04 14:58:35 +02001500 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001501 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001502 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001503 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001504 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001505 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001506
1507 PSA_ASSERT( psa_crypto_init( ) );
1508
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001509 psa_set_key_usage_flags( &attributes, policy_usage );
1510 psa_set_key_algorithm( &attributes, policy_alg );
1511 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001512
Gilles Peskine049c7532019-05-15 20:22:09 +02001513 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001514 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001515
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001516 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001517
Steven Cooremance48e852020-10-05 16:02:45 +02001518 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001519
1520exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001521 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001522 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001523 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001524}
1525/* END_CASE */
1526
1527/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001528void copy_success( int source_usage_arg,
1529 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001530 int type_arg, data_t *material,
1531 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001532 int target_usage_arg,
1533 int target_alg_arg, int target_alg2_arg,
1534 int expected_usage_arg,
1535 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001536{
Gilles Peskineca25db92019-04-19 11:43:08 +02001537 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1538 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001539 psa_key_usage_t expected_usage = expected_usage_arg;
1540 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001541 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001542 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1543 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001544 uint8_t *export_buffer = NULL;
1545
Gilles Peskine57ab7212019-01-28 13:03:09 +01001546 PSA_ASSERT( psa_crypto_init( ) );
1547
Gilles Peskineca25db92019-04-19 11:43:08 +02001548 /* Prepare the source key. */
1549 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1550 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001551 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001552 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001553 PSA_ASSERT( psa_import_key( &source_attributes,
1554 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001555 &source_key ) );
1556 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001557
Gilles Peskineca25db92019-04-19 11:43:08 +02001558 /* Prepare the target attributes. */
1559 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001560 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001561 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001562 /* Set volatile lifetime to reset the key identifier to 0. */
1563 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1564 }
1565
Gilles Peskineca25db92019-04-19 11:43:08 +02001566 if( target_usage_arg != -1 )
1567 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1568 if( target_alg_arg != -1 )
1569 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001570 if( target_alg2_arg != -1 )
1571 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001572
1573 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001574 PSA_ASSERT( psa_copy_key( source_key,
1575 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001576
1577 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001578 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001579
1580 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001581 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001582 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1583 psa_get_key_type( &target_attributes ) );
1584 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1585 psa_get_key_bits( &target_attributes ) );
1586 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1587 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001588 TEST_EQUAL( expected_alg2,
1589 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001590 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1591 {
1592 size_t length;
1593 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001594 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001595 material->len, &length ) );
1596 ASSERT_COMPARE( material->x, material->len,
1597 export_buffer, length );
1598 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001599
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001600 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001601 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001602 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001603 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001604
Ronald Cron5425a212020-08-04 14:58:35 +02001605 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001606
1607exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001608 /*
1609 * Source and target key attributes may have been returned by
1610 * psa_get_key_attributes() thus reset them as required.
1611 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001612 psa_reset_key_attributes( &source_attributes );
1613 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001614
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001615 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001616 mbedtls_free( export_buffer );
1617}
1618/* END_CASE */
1619
1620/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001621void copy_fail( int source_usage_arg,
1622 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001623 int type_arg, data_t *material,
1624 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001625 int target_usage_arg,
1626 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001627 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001628 int expected_status_arg )
1629{
1630 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1631 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001632 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1633 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001634 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001635
1636 PSA_ASSERT( psa_crypto_init( ) );
1637
1638 /* Prepare the source key. */
1639 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1640 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001641 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001642 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001643 PSA_ASSERT( psa_import_key( &source_attributes,
1644 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001645 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001646
1647 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001648 psa_set_key_id( &target_attributes, key_id );
1649 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001650 psa_set_key_type( &target_attributes, target_type_arg );
1651 psa_set_key_bits( &target_attributes, target_bits_arg );
1652 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1653 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001654 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001655
1656 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001657 TEST_EQUAL( psa_copy_key( source_key,
1658 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001659 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001660
Ronald Cron5425a212020-08-04 14:58:35 +02001661 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001662
Gilles Peskine4a644642019-05-03 17:14:08 +02001663exit:
1664 psa_reset_key_attributes( &source_attributes );
1665 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001666 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001667}
1668/* END_CASE */
1669
1670/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001671void hash_operation_init( )
1672{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001673 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001674 /* Test each valid way of initializing the object, except for `= {0}`, as
1675 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1676 * though it's OK by the C standard. We could test for this, but we'd need
1677 * to supress the Clang warning for the test. */
1678 psa_hash_operation_t func = psa_hash_operation_init( );
1679 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1680 psa_hash_operation_t zero;
1681
1682 memset( &zero, 0, sizeof( zero ) );
1683
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001684 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001685 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1686 PSA_ERROR_BAD_STATE );
1687 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1688 PSA_ERROR_BAD_STATE );
1689 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1690 PSA_ERROR_BAD_STATE );
1691
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001692 /* A default hash operation should be abortable without error. */
1693 PSA_ASSERT( psa_hash_abort( &func ) );
1694 PSA_ASSERT( psa_hash_abort( &init ) );
1695 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001696}
1697/* END_CASE */
1698
1699/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001700void hash_setup( int alg_arg,
1701 int expected_status_arg )
1702{
1703 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001704 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001705 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001706 psa_status_t status;
1707
Gilles Peskine8817f612018-12-18 00:18:46 +01001708 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001709
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001710 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001711 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001712
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001713 /* Whether setup succeeded or failed, abort must succeed. */
1714 PSA_ASSERT( psa_hash_abort( &operation ) );
1715
1716 /* If setup failed, reproduce the failure, so as to
1717 * test the resulting state of the operation object. */
1718 if( status != PSA_SUCCESS )
1719 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1720
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001721 /* Now the operation object should be reusable. */
1722#if defined(KNOWN_SUPPORTED_HASH_ALG)
1723 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1724 PSA_ASSERT( psa_hash_abort( &operation ) );
1725#endif
1726
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001727exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001728 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001729}
1730/* END_CASE */
1731
1732/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001733void hash_compute_fail( int alg_arg, data_t *input,
1734 int output_size_arg, int expected_status_arg )
1735{
1736 psa_algorithm_t alg = alg_arg;
1737 uint8_t *output = NULL;
1738 size_t output_size = output_size_arg;
1739 size_t output_length = INVALID_EXPORT_LENGTH;
1740 psa_status_t expected_status = expected_status_arg;
1741 psa_status_t status;
1742
1743 ASSERT_ALLOC( output, output_size );
1744
1745 PSA_ASSERT( psa_crypto_init( ) );
1746
1747 status = psa_hash_compute( alg, input->x, input->len,
1748 output, output_size, &output_length );
1749 TEST_EQUAL( status, expected_status );
1750 TEST_ASSERT( output_length <= output_size );
1751
1752exit:
1753 mbedtls_free( output );
1754 PSA_DONE( );
1755}
1756/* END_CASE */
1757
1758/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001759void hash_compare_fail( int alg_arg, data_t *input,
1760 data_t *reference_hash,
1761 int expected_status_arg )
1762{
1763 psa_algorithm_t alg = alg_arg;
1764 psa_status_t expected_status = expected_status_arg;
1765 psa_status_t status;
1766
1767 PSA_ASSERT( psa_crypto_init( ) );
1768
1769 status = psa_hash_compare( alg, input->x, input->len,
1770 reference_hash->x, reference_hash->len );
1771 TEST_EQUAL( status, expected_status );
1772
1773exit:
1774 PSA_DONE( );
1775}
1776/* END_CASE */
1777
1778/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001779void hash_compute_compare( int alg_arg, data_t *input,
1780 data_t *expected_output )
1781{
1782 psa_algorithm_t alg = alg_arg;
1783 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1784 size_t output_length = INVALID_EXPORT_LENGTH;
1785 size_t i;
1786
1787 PSA_ASSERT( psa_crypto_init( ) );
1788
1789 /* Compute with tight buffer */
1790 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001791 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001792 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001793 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001794 ASSERT_COMPARE( output, output_length,
1795 expected_output->x, expected_output->len );
1796
1797 /* Compute with larger buffer */
1798 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1799 output, sizeof( output ),
1800 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001801 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001802 ASSERT_COMPARE( output, output_length,
1803 expected_output->x, expected_output->len );
1804
1805 /* Compare with correct hash */
1806 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1807 output, output_length ) );
1808
1809 /* Compare with trailing garbage */
1810 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1811 output, output_length + 1 ),
1812 PSA_ERROR_INVALID_SIGNATURE );
1813
1814 /* Compare with truncated hash */
1815 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1816 output, output_length - 1 ),
1817 PSA_ERROR_INVALID_SIGNATURE );
1818
1819 /* Compare with corrupted value */
1820 for( i = 0; i < output_length; i++ )
1821 {
Chris Jones9634bb12021-01-20 15:56:42 +00001822 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001823 output[i] ^= 1;
1824 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1825 output, output_length ),
1826 PSA_ERROR_INVALID_SIGNATURE );
1827 output[i] ^= 1;
1828 }
1829
1830exit:
1831 PSA_DONE( );
1832}
1833/* END_CASE */
1834
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001835/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001836void hash_bad_order( )
1837{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001838 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001839 unsigned char input[] = "";
1840 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001841 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001842 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1843 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1844 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001845 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001846 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001847 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001848
Gilles Peskine8817f612018-12-18 00:18:46 +01001849 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001850
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001851 /* Call setup twice in a row. */
1852 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1853 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1854 PSA_ERROR_BAD_STATE );
1855 PSA_ASSERT( psa_hash_abort( &operation ) );
1856
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001857 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001858 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001859 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001860 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001861
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001862 /* Call update after finish. */
1863 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1864 PSA_ASSERT( psa_hash_finish( &operation,
1865 hash, sizeof( hash ), &hash_len ) );
1866 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001867 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001868 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001869
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001870 /* Call verify without calling setup beforehand. */
1871 TEST_EQUAL( psa_hash_verify( &operation,
1872 valid_hash, sizeof( valid_hash ) ),
1873 PSA_ERROR_BAD_STATE );
1874 PSA_ASSERT( psa_hash_abort( &operation ) );
1875
1876 /* Call verify after finish. */
1877 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1878 PSA_ASSERT( psa_hash_finish( &operation,
1879 hash, sizeof( hash ), &hash_len ) );
1880 TEST_EQUAL( psa_hash_verify( &operation,
1881 valid_hash, sizeof( valid_hash ) ),
1882 PSA_ERROR_BAD_STATE );
1883 PSA_ASSERT( psa_hash_abort( &operation ) );
1884
1885 /* Call verify twice in a row. */
1886 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1887 PSA_ASSERT( psa_hash_verify( &operation,
1888 valid_hash, sizeof( valid_hash ) ) );
1889 TEST_EQUAL( psa_hash_verify( &operation,
1890 valid_hash, sizeof( valid_hash ) ),
1891 PSA_ERROR_BAD_STATE );
1892 PSA_ASSERT( psa_hash_abort( &operation ) );
1893
1894 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001895 TEST_EQUAL( psa_hash_finish( &operation,
1896 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001897 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001898 PSA_ASSERT( psa_hash_abort( &operation ) );
1899
1900 /* Call finish twice in a row. */
1901 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1902 PSA_ASSERT( psa_hash_finish( &operation,
1903 hash, sizeof( hash ), &hash_len ) );
1904 TEST_EQUAL( psa_hash_finish( &operation,
1905 hash, sizeof( hash ), &hash_len ),
1906 PSA_ERROR_BAD_STATE );
1907 PSA_ASSERT( psa_hash_abort( &operation ) );
1908
1909 /* Call finish after calling verify. */
1910 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1911 PSA_ASSERT( psa_hash_verify( &operation,
1912 valid_hash, sizeof( valid_hash ) ) );
1913 TEST_EQUAL( psa_hash_finish( &operation,
1914 hash, sizeof( hash ), &hash_len ),
1915 PSA_ERROR_BAD_STATE );
1916 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001917
1918exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001919 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001920}
1921/* END_CASE */
1922
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001923/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001924void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001925{
1926 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001927 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1928 * appended to it */
1929 unsigned char hash[] = {
1930 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1931 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1932 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001933 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001934 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001935
Gilles Peskine8817f612018-12-18 00:18:46 +01001936 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001937
itayzafrir27e69452018-11-01 14:26:34 +02001938 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001939 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001940 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001941 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001942
itayzafrir27e69452018-11-01 14:26:34 +02001943 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001944 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001945 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001946 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001947
itayzafrir27e69452018-11-01 14:26:34 +02001948 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001949 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001950 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001951 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001952
itayzafrirec93d302018-10-18 18:01:10 +03001953exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001954 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001955}
1956/* END_CASE */
1957
Ronald Cronee414c72021-03-18 18:50:08 +01001958/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001959void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001960{
1961 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001962 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001963 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001964 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001965 size_t hash_len;
1966
Gilles Peskine8817f612018-12-18 00:18:46 +01001967 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001968
itayzafrir58028322018-10-25 10:22:01 +03001969 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001970 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001971 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001972 hash, expected_size - 1, &hash_len ),
1973 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001974
1975exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001976 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001977}
1978/* END_CASE */
1979
Ronald Cronee414c72021-03-18 18:50:08 +01001980/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001981void hash_clone_source_state( )
1982{
1983 psa_algorithm_t alg = PSA_ALG_SHA_256;
1984 unsigned char hash[PSA_HASH_MAX_SIZE];
1985 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1986 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1987 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1988 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1989 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1990 size_t hash_len;
1991
1992 PSA_ASSERT( psa_crypto_init( ) );
1993 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1994
1995 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1996 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1997 PSA_ASSERT( psa_hash_finish( &op_finished,
1998 hash, sizeof( hash ), &hash_len ) );
1999 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2000 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2001
2002 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2003 PSA_ERROR_BAD_STATE );
2004
2005 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2006 PSA_ASSERT( psa_hash_finish( &op_init,
2007 hash, sizeof( hash ), &hash_len ) );
2008 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2009 PSA_ASSERT( psa_hash_finish( &op_finished,
2010 hash, sizeof( hash ), &hash_len ) );
2011 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2012 PSA_ASSERT( psa_hash_finish( &op_aborted,
2013 hash, sizeof( hash ), &hash_len ) );
2014
2015exit:
2016 psa_hash_abort( &op_source );
2017 psa_hash_abort( &op_init );
2018 psa_hash_abort( &op_setup );
2019 psa_hash_abort( &op_finished );
2020 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002021 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002022}
2023/* END_CASE */
2024
Ronald Cronee414c72021-03-18 18:50:08 +01002025/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002026void hash_clone_target_state( )
2027{
2028 psa_algorithm_t alg = PSA_ALG_SHA_256;
2029 unsigned char hash[PSA_HASH_MAX_SIZE];
2030 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2031 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2032 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2033 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2034 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2035 size_t hash_len;
2036
2037 PSA_ASSERT( psa_crypto_init( ) );
2038
2039 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2040 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2041 PSA_ASSERT( psa_hash_finish( &op_finished,
2042 hash, sizeof( hash ), &hash_len ) );
2043 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2044 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2045
2046 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2047 PSA_ASSERT( psa_hash_finish( &op_target,
2048 hash, sizeof( hash ), &hash_len ) );
2049
2050 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2051 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2052 PSA_ERROR_BAD_STATE );
2053 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2054 PSA_ERROR_BAD_STATE );
2055
2056exit:
2057 psa_hash_abort( &op_target );
2058 psa_hash_abort( &op_init );
2059 psa_hash_abort( &op_setup );
2060 psa_hash_abort( &op_finished );
2061 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002062 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002063}
2064/* END_CASE */
2065
itayzafrir58028322018-10-25 10:22:01 +03002066/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002067void mac_operation_init( )
2068{
Jaeden Amero252ef282019-02-15 14:05:35 +00002069 const uint8_t input[1] = { 0 };
2070
Jaeden Amero769ce272019-01-04 11:48:03 +00002071 /* Test each valid way of initializing the object, except for `= {0}`, as
2072 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2073 * though it's OK by the C standard. We could test for this, but we'd need
2074 * to supress the Clang warning for the test. */
2075 psa_mac_operation_t func = psa_mac_operation_init( );
2076 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2077 psa_mac_operation_t zero;
2078
2079 memset( &zero, 0, sizeof( zero ) );
2080
Jaeden Amero252ef282019-02-15 14:05:35 +00002081 /* A freshly-initialized MAC operation should not be usable. */
2082 TEST_EQUAL( psa_mac_update( &func,
2083 input, sizeof( input ) ),
2084 PSA_ERROR_BAD_STATE );
2085 TEST_EQUAL( psa_mac_update( &init,
2086 input, sizeof( input ) ),
2087 PSA_ERROR_BAD_STATE );
2088 TEST_EQUAL( psa_mac_update( &zero,
2089 input, sizeof( input ) ),
2090 PSA_ERROR_BAD_STATE );
2091
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002092 /* A default MAC operation should be abortable without error. */
2093 PSA_ASSERT( psa_mac_abort( &func ) );
2094 PSA_ASSERT( psa_mac_abort( &init ) );
2095 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002096}
2097/* END_CASE */
2098
2099/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002100void mac_setup( int key_type_arg,
2101 data_t *key,
2102 int alg_arg,
2103 int expected_status_arg )
2104{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002105 psa_key_type_t key_type = key_type_arg;
2106 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002107 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002108 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002109 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2110#if defined(KNOWN_SUPPORTED_MAC_ALG)
2111 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2112#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002113
Gilles Peskine8817f612018-12-18 00:18:46 +01002114 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002115
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002116 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2117 &operation, &status ) )
2118 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002119 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002120
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002121 /* The operation object should be reusable. */
2122#if defined(KNOWN_SUPPORTED_MAC_ALG)
2123 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2124 smoke_test_key_data,
2125 sizeof( smoke_test_key_data ),
2126 KNOWN_SUPPORTED_MAC_ALG,
2127 &operation, &status ) )
2128 goto exit;
2129 TEST_EQUAL( status, PSA_SUCCESS );
2130#endif
2131
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002132exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002133 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002134}
2135/* END_CASE */
2136
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002137/* 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 +00002138void mac_bad_order( )
2139{
Ronald Cron5425a212020-08-04 14:58:35 +02002140 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002141 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2142 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002143 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002144 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2145 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2146 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002147 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002148 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2149 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2150 size_t sign_mac_length = 0;
2151 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2152 const uint8_t verify_mac[] = {
2153 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2154 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2155 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2156
2157 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002158 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002159 psa_set_key_algorithm( &attributes, alg );
2160 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002161
Ronald Cron5425a212020-08-04 14:58:35 +02002162 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2163 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002164
Jaeden Amero252ef282019-02-15 14:05:35 +00002165 /* Call update without calling setup beforehand. */
2166 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2167 PSA_ERROR_BAD_STATE );
2168 PSA_ASSERT( psa_mac_abort( &operation ) );
2169
2170 /* Call sign finish without calling setup beforehand. */
2171 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2172 &sign_mac_length),
2173 PSA_ERROR_BAD_STATE );
2174 PSA_ASSERT( psa_mac_abort( &operation ) );
2175
2176 /* Call verify finish without calling setup beforehand. */
2177 TEST_EQUAL( psa_mac_verify_finish( &operation,
2178 verify_mac, sizeof( verify_mac ) ),
2179 PSA_ERROR_BAD_STATE );
2180 PSA_ASSERT( psa_mac_abort( &operation ) );
2181
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002182 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002183 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2184 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002185 PSA_ERROR_BAD_STATE );
2186 PSA_ASSERT( psa_mac_abort( &operation ) );
2187
Jaeden Amero252ef282019-02-15 14:05:35 +00002188 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002189 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002190 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2191 PSA_ASSERT( psa_mac_sign_finish( &operation,
2192 sign_mac, sizeof( sign_mac ),
2193 &sign_mac_length ) );
2194 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2195 PSA_ERROR_BAD_STATE );
2196 PSA_ASSERT( psa_mac_abort( &operation ) );
2197
2198 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002199 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002200 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2201 PSA_ASSERT( psa_mac_verify_finish( &operation,
2202 verify_mac, sizeof( verify_mac ) ) );
2203 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2204 PSA_ERROR_BAD_STATE );
2205 PSA_ASSERT( psa_mac_abort( &operation ) );
2206
2207 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002208 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002209 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2210 PSA_ASSERT( psa_mac_sign_finish( &operation,
2211 sign_mac, sizeof( sign_mac ),
2212 &sign_mac_length ) );
2213 TEST_EQUAL( psa_mac_sign_finish( &operation,
2214 sign_mac, sizeof( sign_mac ),
2215 &sign_mac_length ),
2216 PSA_ERROR_BAD_STATE );
2217 PSA_ASSERT( psa_mac_abort( &operation ) );
2218
2219 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002220 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002221 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2222 PSA_ASSERT( psa_mac_verify_finish( &operation,
2223 verify_mac, sizeof( verify_mac ) ) );
2224 TEST_EQUAL( psa_mac_verify_finish( &operation,
2225 verify_mac, sizeof( verify_mac ) ),
2226 PSA_ERROR_BAD_STATE );
2227 PSA_ASSERT( psa_mac_abort( &operation ) );
2228
2229 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002230 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002231 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2232 TEST_EQUAL( psa_mac_verify_finish( &operation,
2233 verify_mac, sizeof( verify_mac ) ),
2234 PSA_ERROR_BAD_STATE );
2235 PSA_ASSERT( psa_mac_abort( &operation ) );
2236
2237 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002238 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002239 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2240 TEST_EQUAL( psa_mac_sign_finish( &operation,
2241 sign_mac, sizeof( sign_mac ),
2242 &sign_mac_length ),
2243 PSA_ERROR_BAD_STATE );
2244 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002245
Ronald Cron5425a212020-08-04 14:58:35 +02002246 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002247
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002248exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002249 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002250}
2251/* END_CASE */
2252
2253/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002254void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002255 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002256 int alg_arg,
2257 data_t *input,
2258 data_t *expected_mac )
2259{
Ronald Cron5425a212020-08-04 14:58:35 +02002260 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002261 psa_key_type_t key_type = key_type_arg;
2262 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002263 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002264 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002265 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002266 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002267 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002268 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002269 const size_t output_sizes_to_test[] = {
2270 0,
2271 1,
2272 expected_mac->len - 1,
2273 expected_mac->len,
2274 expected_mac->len + 1,
2275 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002276
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002277 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002278 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002279 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002280
Gilles Peskine8817f612018-12-18 00:18:46 +01002281 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002282
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002283 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002284 psa_set_key_algorithm( &attributes, alg );
2285 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002286
Ronald Cron5425a212020-08-04 14:58:35 +02002287 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2288 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002289
Gilles Peskine8b356b52020-08-25 23:44:59 +02002290 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2291 {
2292 const size_t output_size = output_sizes_to_test[i];
2293 psa_status_t expected_status =
2294 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2295 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002296
Chris Jones9634bb12021-01-20 15:56:42 +00002297 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002298 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002299
Gilles Peskine8b356b52020-08-25 23:44:59 +02002300 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002301 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002302 PSA_ASSERT( psa_mac_update( &operation,
2303 input->x, input->len ) );
2304 TEST_EQUAL( psa_mac_sign_finish( &operation,
2305 actual_mac, output_size,
2306 &mac_length ),
2307 expected_status );
2308 PSA_ASSERT( psa_mac_abort( &operation ) );
2309
2310 if( expected_status == PSA_SUCCESS )
2311 {
2312 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2313 actual_mac, mac_length );
2314 }
2315 mbedtls_free( actual_mac );
2316 actual_mac = NULL;
2317 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002318
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002319exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002320 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002321 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002322 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002323 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002324}
2325/* END_CASE */
2326
2327/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002328void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002329 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002330 int alg_arg,
2331 data_t *input,
2332 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002333{
Ronald Cron5425a212020-08-04 14:58:35 +02002334 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002335 psa_key_type_t key_type = key_type_arg;
2336 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002337 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002338 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002339 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002340
Gilles Peskine69c12672018-06-28 00:07:19 +02002341 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2342
Gilles Peskine8817f612018-12-18 00:18:46 +01002343 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002344
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002345 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002346 psa_set_key_algorithm( &attributes, alg );
2347 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002348
Ronald Cron5425a212020-08-04 14:58:35 +02002349 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2350 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002351
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002352 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002353 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002354 PSA_ASSERT( psa_mac_update( &operation,
2355 input->x, input->len ) );
2356 PSA_ASSERT( psa_mac_verify_finish( &operation,
2357 expected_mac->x,
2358 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002359
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002360 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002361 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002362 PSA_ASSERT( psa_mac_update( &operation,
2363 input->x, input->len ) );
2364 TEST_EQUAL( psa_mac_verify_finish( &operation,
2365 expected_mac->x,
2366 expected_mac->len - 1 ),
2367 PSA_ERROR_INVALID_SIGNATURE );
2368
2369 /* Test a MAC that's too long. */
2370 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2371 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002372 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002373 PSA_ASSERT( psa_mac_update( &operation,
2374 input->x, input->len ) );
2375 TEST_EQUAL( psa_mac_verify_finish( &operation,
2376 perturbed_mac,
2377 expected_mac->len + 1 ),
2378 PSA_ERROR_INVALID_SIGNATURE );
2379
2380 /* Test changing one byte. */
2381 for( size_t i = 0; i < expected_mac->len; i++ )
2382 {
Chris Jones9634bb12021-01-20 15:56:42 +00002383 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002384 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002385 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002386 PSA_ASSERT( psa_mac_update( &operation,
2387 input->x, input->len ) );
2388 TEST_EQUAL( psa_mac_verify_finish( &operation,
2389 perturbed_mac,
2390 expected_mac->len ),
2391 PSA_ERROR_INVALID_SIGNATURE );
2392 perturbed_mac[i] ^= 1;
2393 }
2394
Gilles Peskine8c9def32018-02-08 10:02:12 +01002395exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002396 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002397 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002398 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002399 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002400}
2401/* END_CASE */
2402
2403/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002404void cipher_operation_init( )
2405{
Jaeden Ameroab439972019-02-15 14:12:05 +00002406 const uint8_t input[1] = { 0 };
2407 unsigned char output[1] = { 0 };
2408 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002409 /* Test each valid way of initializing the object, except for `= {0}`, as
2410 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2411 * though it's OK by the C standard. We could test for this, but we'd need
2412 * to supress the Clang warning for the test. */
2413 psa_cipher_operation_t func = psa_cipher_operation_init( );
2414 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2415 psa_cipher_operation_t zero;
2416
2417 memset( &zero, 0, sizeof( zero ) );
2418
Jaeden Ameroab439972019-02-15 14:12:05 +00002419 /* A freshly-initialized cipher operation should not be usable. */
2420 TEST_EQUAL( psa_cipher_update( &func,
2421 input, sizeof( input ),
2422 output, sizeof( output ),
2423 &output_length ),
2424 PSA_ERROR_BAD_STATE );
2425 TEST_EQUAL( psa_cipher_update( &init,
2426 input, sizeof( input ),
2427 output, sizeof( output ),
2428 &output_length ),
2429 PSA_ERROR_BAD_STATE );
2430 TEST_EQUAL( psa_cipher_update( &zero,
2431 input, sizeof( input ),
2432 output, sizeof( output ),
2433 &output_length ),
2434 PSA_ERROR_BAD_STATE );
2435
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002436 /* A default cipher operation should be abortable without error. */
2437 PSA_ASSERT( psa_cipher_abort( &func ) );
2438 PSA_ASSERT( psa_cipher_abort( &init ) );
2439 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002440}
2441/* END_CASE */
2442
2443/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002444void cipher_setup( int key_type_arg,
2445 data_t *key,
2446 int alg_arg,
2447 int expected_status_arg )
2448{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002449 psa_key_type_t key_type = key_type_arg;
2450 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002451 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002452 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002453 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002454#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002455 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2456#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002457
Gilles Peskine8817f612018-12-18 00:18:46 +01002458 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002459
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002460 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2461 &operation, &status ) )
2462 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002463 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002464
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002465 /* The operation object should be reusable. */
2466#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2467 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2468 smoke_test_key_data,
2469 sizeof( smoke_test_key_data ),
2470 KNOWN_SUPPORTED_CIPHER_ALG,
2471 &operation, &status ) )
2472 goto exit;
2473 TEST_EQUAL( status, PSA_SUCCESS );
2474#endif
2475
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002476exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002477 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002478 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002479}
2480/* END_CASE */
2481
Ronald Cronee414c72021-03-18 18:50:08 +01002482/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002483void cipher_bad_order( )
2484{
Ronald Cron5425a212020-08-04 14:58:35 +02002485 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002486 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2487 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002488 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002489 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002490 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002491 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002492 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2493 0xaa, 0xaa, 0xaa, 0xaa };
2494 const uint8_t text[] = {
2495 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2496 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002497 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002498 size_t length = 0;
2499
2500 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002501 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2502 psa_set_key_algorithm( &attributes, alg );
2503 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002504 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2505 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002506
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002507 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002508 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2509 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002510 PSA_ERROR_BAD_STATE );
2511 PSA_ASSERT( psa_cipher_abort( &operation ) );
2512
2513 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002514 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2515 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002516 PSA_ERROR_BAD_STATE );
2517 PSA_ASSERT( psa_cipher_abort( &operation ) );
2518
Jaeden Ameroab439972019-02-15 14:12:05 +00002519 /* Generate an IV without calling setup beforehand. */
2520 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2521 buffer, sizeof( buffer ),
2522 &length ),
2523 PSA_ERROR_BAD_STATE );
2524 PSA_ASSERT( psa_cipher_abort( &operation ) );
2525
2526 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002527 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002528 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2529 buffer, sizeof( buffer ),
2530 &length ) );
2531 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2532 buffer, sizeof( buffer ),
2533 &length ),
2534 PSA_ERROR_BAD_STATE );
2535 PSA_ASSERT( psa_cipher_abort( &operation ) );
2536
2537 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002538 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002539 PSA_ASSERT( psa_cipher_set_iv( &operation,
2540 iv, sizeof( iv ) ) );
2541 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2542 buffer, sizeof( buffer ),
2543 &length ),
2544 PSA_ERROR_BAD_STATE );
2545 PSA_ASSERT( psa_cipher_abort( &operation ) );
2546
2547 /* Set an IV without calling setup beforehand. */
2548 TEST_EQUAL( psa_cipher_set_iv( &operation,
2549 iv, sizeof( iv ) ),
2550 PSA_ERROR_BAD_STATE );
2551 PSA_ASSERT( psa_cipher_abort( &operation ) );
2552
2553 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002554 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002555 PSA_ASSERT( psa_cipher_set_iv( &operation,
2556 iv, sizeof( iv ) ) );
2557 TEST_EQUAL( psa_cipher_set_iv( &operation,
2558 iv, sizeof( iv ) ),
2559 PSA_ERROR_BAD_STATE );
2560 PSA_ASSERT( psa_cipher_abort( &operation ) );
2561
2562 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002563 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002564 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2565 buffer, sizeof( buffer ),
2566 &length ) );
2567 TEST_EQUAL( psa_cipher_set_iv( &operation,
2568 iv, sizeof( iv ) ),
2569 PSA_ERROR_BAD_STATE );
2570 PSA_ASSERT( psa_cipher_abort( &operation ) );
2571
2572 /* Call update without calling setup beforehand. */
2573 TEST_EQUAL( psa_cipher_update( &operation,
2574 text, sizeof( text ),
2575 buffer, sizeof( buffer ),
2576 &length ),
2577 PSA_ERROR_BAD_STATE );
2578 PSA_ASSERT( psa_cipher_abort( &operation ) );
2579
2580 /* Call update without an IV where an IV is required. */
2581 TEST_EQUAL( psa_cipher_update( &operation,
2582 text, sizeof( text ),
2583 buffer, sizeof( buffer ),
2584 &length ),
2585 PSA_ERROR_BAD_STATE );
2586 PSA_ASSERT( psa_cipher_abort( &operation ) );
2587
2588 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002589 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002590 PSA_ASSERT( psa_cipher_set_iv( &operation,
2591 iv, sizeof( iv ) ) );
2592 PSA_ASSERT( psa_cipher_finish( &operation,
2593 buffer, sizeof( buffer ), &length ) );
2594 TEST_EQUAL( psa_cipher_update( &operation,
2595 text, sizeof( text ),
2596 buffer, sizeof( buffer ),
2597 &length ),
2598 PSA_ERROR_BAD_STATE );
2599 PSA_ASSERT( psa_cipher_abort( &operation ) );
2600
2601 /* Call finish without calling setup beforehand. */
2602 TEST_EQUAL( psa_cipher_finish( &operation,
2603 buffer, sizeof( buffer ), &length ),
2604 PSA_ERROR_BAD_STATE );
2605 PSA_ASSERT( psa_cipher_abort( &operation ) );
2606
2607 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002608 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002609 /* Not calling update means we are encrypting an empty buffer, which is OK
2610 * for cipher modes with padding. */
2611 TEST_EQUAL( psa_cipher_finish( &operation,
2612 buffer, sizeof( buffer ), &length ),
2613 PSA_ERROR_BAD_STATE );
2614 PSA_ASSERT( psa_cipher_abort( &operation ) );
2615
2616 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002617 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002618 PSA_ASSERT( psa_cipher_set_iv( &operation,
2619 iv, sizeof( iv ) ) );
2620 PSA_ASSERT( psa_cipher_finish( &operation,
2621 buffer, sizeof( buffer ), &length ) );
2622 TEST_EQUAL( psa_cipher_finish( &operation,
2623 buffer, sizeof( buffer ), &length ),
2624 PSA_ERROR_BAD_STATE );
2625 PSA_ASSERT( psa_cipher_abort( &operation ) );
2626
Ronald Cron5425a212020-08-04 14:58:35 +02002627 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002628
Jaeden Ameroab439972019-02-15 14:12:05 +00002629exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002630 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002631 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002632}
2633/* END_CASE */
2634
2635/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002636void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002637 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002638 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002639 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002640{
Ronald Cron5425a212020-08-04 14:58:35 +02002641 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002642 psa_status_t status;
2643 psa_key_type_t key_type = key_type_arg;
2644 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002645 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002646 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002647 size_t output_buffer_size = 0;
2648 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002649 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002650 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002651 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002652
Gilles Peskine8817f612018-12-18 00:18:46 +01002653 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002654
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002655 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2656 psa_set_key_algorithm( &attributes, alg );
2657 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002658
Ronald Cron5425a212020-08-04 14:58:35 +02002659 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2660 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002661
Ronald Cron5425a212020-08-04 14:58:35 +02002662 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002663
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002664 if( iv->len > 0 )
2665 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002666 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002667 }
2668
gabor-mezei-armceface22021-01-21 12:26:17 +01002669 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2670 TEST_ASSERT( output_buffer_size <=
2671 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002672 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002673
Gilles Peskine8817f612018-12-18 00:18:46 +01002674 PSA_ASSERT( psa_cipher_update( &operation,
2675 input->x, input->len,
2676 output, output_buffer_size,
2677 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002678 TEST_ASSERT( function_output_length <=
2679 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2680 TEST_ASSERT( function_output_length <=
2681 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002682 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002683
Gilles Peskine50e586b2018-06-08 14:28:46 +02002684 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002685 ( output_buffer_size == 0 ? NULL :
2686 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002687 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002688 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002689 TEST_ASSERT( function_output_length <=
2690 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2691 TEST_ASSERT( function_output_length <=
2692 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002693 total_output_length += function_output_length;
2694
Gilles Peskinefe11b722018-12-18 00:24:04 +01002695 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002696 if( expected_status == PSA_SUCCESS )
2697 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002698 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002699 ASSERT_COMPARE( expected_output->x, expected_output->len,
2700 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002701 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002702
Gilles Peskine50e586b2018-06-08 14:28:46 +02002703exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002704 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002705 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002706 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002707 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002708}
2709/* END_CASE */
2710
2711/* BEGIN_CASE */
2712void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002713 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002714 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002715 int first_part_size_arg,
2716 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002717 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002718{
Ronald Cron5425a212020-08-04 14:58:35 +02002719 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002720 psa_key_type_t key_type = key_type_arg;
2721 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002722 size_t first_part_size = first_part_size_arg;
2723 size_t output1_length = output1_length_arg;
2724 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002725 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002726 size_t output_buffer_size = 0;
2727 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002728 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002729 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002730 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002731
Gilles Peskine8817f612018-12-18 00:18:46 +01002732 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002733
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002734 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2735 psa_set_key_algorithm( &attributes, alg );
2736 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002737
Ronald Cron5425a212020-08-04 14:58:35 +02002738 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2739 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002740
Ronald Cron5425a212020-08-04 14:58:35 +02002741 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002742
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002743 if( iv->len > 0 )
2744 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002745 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002746 }
2747
gabor-mezei-armceface22021-01-21 12:26:17 +01002748 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2749 TEST_ASSERT( output_buffer_size <=
2750 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002751 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002752
Gilles Peskinee0866522019-02-19 19:44:00 +01002753 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002754 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2755 output, output_buffer_size,
2756 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002757 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002758 TEST_ASSERT( function_output_length <=
2759 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2760 TEST_ASSERT( function_output_length <=
2761 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002762 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002763
Gilles Peskine8817f612018-12-18 00:18:46 +01002764 PSA_ASSERT( psa_cipher_update( &operation,
2765 input->x + first_part_size,
2766 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002767 ( output_buffer_size == 0 ? NULL :
2768 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002769 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002770 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002771 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002772 TEST_ASSERT( function_output_length <=
2773 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2774 alg,
2775 input->len - first_part_size ) );
2776 TEST_ASSERT( function_output_length <=
2777 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002778 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002779
Gilles Peskine8817f612018-12-18 00:18:46 +01002780 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002781 ( output_buffer_size == 0 ? NULL :
2782 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002783 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002784 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002785 TEST_ASSERT( function_output_length <=
2786 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2787 TEST_ASSERT( function_output_length <=
2788 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002789 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002790 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002791
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002792 ASSERT_COMPARE( expected_output->x, expected_output->len,
2793 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002794
2795exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002796 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002797 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002798 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002799 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002800}
2801/* END_CASE */
2802
2803/* BEGIN_CASE */
2804void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002805 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002806 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002807 int first_part_size_arg,
2808 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002809 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002810{
Ronald Cron5425a212020-08-04 14:58:35 +02002811 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002812 psa_key_type_t key_type = key_type_arg;
2813 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002814 size_t first_part_size = first_part_size_arg;
2815 size_t output1_length = output1_length_arg;
2816 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002817 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002818 size_t output_buffer_size = 0;
2819 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002820 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002821 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002822 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002823
Gilles Peskine8817f612018-12-18 00:18:46 +01002824 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002825
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002826 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2827 psa_set_key_algorithm( &attributes, alg );
2828 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002829
Ronald Cron5425a212020-08-04 14:58:35 +02002830 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2831 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002832
Ronald Cron5425a212020-08-04 14:58:35 +02002833 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002834
Steven Cooreman177deba2020-09-07 17:14:14 +02002835 if( iv->len > 0 )
2836 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002837 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002838 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002839
gabor-mezei-armceface22021-01-21 12:26:17 +01002840 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2841 TEST_ASSERT( output_buffer_size <=
2842 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002843 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002844
Gilles Peskinee0866522019-02-19 19:44:00 +01002845 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002846 PSA_ASSERT( psa_cipher_update( &operation,
2847 input->x, first_part_size,
2848 output, output_buffer_size,
2849 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002850 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002851 TEST_ASSERT( function_output_length <=
2852 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2853 TEST_ASSERT( function_output_length <=
2854 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002855 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002856
Gilles Peskine8817f612018-12-18 00:18:46 +01002857 PSA_ASSERT( psa_cipher_update( &operation,
2858 input->x + first_part_size,
2859 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002860 ( output_buffer_size == 0 ? NULL :
2861 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002862 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002863 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002864 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002865 TEST_ASSERT( function_output_length <=
2866 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2867 alg,
2868 input->len - first_part_size ) );
2869 TEST_ASSERT( function_output_length <=
2870 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002871 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002872
Gilles Peskine8817f612018-12-18 00:18:46 +01002873 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002874 ( output_buffer_size == 0 ? NULL :
2875 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002876 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002877 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002878 TEST_ASSERT( function_output_length <=
2879 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2880 TEST_ASSERT( function_output_length <=
2881 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002882 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002883 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002884
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002885 ASSERT_COMPARE( expected_output->x, expected_output->len,
2886 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002887
2888exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002889 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002890 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002891 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002892 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002893}
2894/* END_CASE */
2895
Gilles Peskine50e586b2018-06-08 14:28:46 +02002896/* BEGIN_CASE */
2897void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002898 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002899 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002900 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002901{
Ronald Cron5425a212020-08-04 14:58:35 +02002902 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002903 psa_status_t status;
2904 psa_key_type_t key_type = key_type_arg;
2905 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002906 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002907 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002908 size_t output_buffer_size = 0;
2909 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002910 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002911 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002912 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002913
Gilles Peskine8817f612018-12-18 00:18:46 +01002914 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002915
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002916 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2917 psa_set_key_algorithm( &attributes, alg );
2918 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002919
Ronald Cron5425a212020-08-04 14:58:35 +02002920 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2921 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002922
Ronald Cron5425a212020-08-04 14:58:35 +02002923 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002924
Steven Cooreman177deba2020-09-07 17:14:14 +02002925 if( iv->len > 0 )
2926 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002927 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002928 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002929
gabor-mezei-armceface22021-01-21 12:26:17 +01002930 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2931 TEST_ASSERT( output_buffer_size <=
2932 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002933 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002934
Gilles Peskine8817f612018-12-18 00:18:46 +01002935 PSA_ASSERT( psa_cipher_update( &operation,
2936 input->x, input->len,
2937 output, output_buffer_size,
2938 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002939 TEST_ASSERT( function_output_length <=
2940 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2941 TEST_ASSERT( function_output_length <=
2942 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002943 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002944
Gilles Peskine50e586b2018-06-08 14:28:46 +02002945 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002946 ( output_buffer_size == 0 ? NULL :
2947 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002948 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002949 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002950 TEST_ASSERT( function_output_length <=
2951 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2952 TEST_ASSERT( function_output_length <=
2953 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002954 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002955 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002956
2957 if( expected_status == PSA_SUCCESS )
2958 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002959 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002960 ASSERT_COMPARE( expected_output->x, expected_output->len,
2961 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002962 }
2963
Gilles Peskine50e586b2018-06-08 14:28:46 +02002964exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002965 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002966 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002967 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002968 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002969}
2970/* END_CASE */
2971
Gilles Peskine50e586b2018-06-08 14:28:46 +02002972/* BEGIN_CASE */
2973void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002974 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002975 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002976{
Ronald Cron5425a212020-08-04 14:58:35 +02002977 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002978 psa_key_type_t key_type = key_type_arg;
2979 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002980 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002981 size_t iv_size = 16;
2982 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002983 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002984 size_t output1_size = 0;
2985 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002986 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002987 size_t output2_size = 0;
2988 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002989 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002990 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2991 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002992 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002993
Gilles Peskine8817f612018-12-18 00:18:46 +01002994 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002995
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002996 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2997 psa_set_key_algorithm( &attributes, alg );
2998 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002999
Ronald Cron5425a212020-08-04 14:58:35 +02003000 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3001 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003002
Ronald Cron5425a212020-08-04 14:58:35 +02003003 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3004 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003005
Steven Cooreman177deba2020-09-07 17:14:14 +02003006 if( alg != PSA_ALG_ECB_NO_PADDING )
3007 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003008 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3009 iv, iv_size,
3010 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003011 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003012 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3013 TEST_ASSERT( output1_size <=
3014 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003015 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003016
Gilles Peskine8817f612018-12-18 00:18:46 +01003017 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3018 output1, output1_size,
3019 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003020 TEST_ASSERT( output1_length <=
3021 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3022 TEST_ASSERT( output1_length <=
3023 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3024
Gilles Peskine8817f612018-12-18 00:18:46 +01003025 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003026 output1 + output1_length,
3027 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003028 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003029 TEST_ASSERT( function_output_length <=
3030 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3031 TEST_ASSERT( function_output_length <=
3032 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003033
Gilles Peskine048b7f02018-06-08 14:20:49 +02003034 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003035
Gilles Peskine8817f612018-12-18 00:18:46 +01003036 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003037
3038 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003039 TEST_ASSERT( output2_size <=
3040 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3041 TEST_ASSERT( output2_size <=
3042 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003043 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003044
Steven Cooreman177deba2020-09-07 17:14:14 +02003045 if( iv_length > 0 )
3046 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003047 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3048 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003049 }
3050
Gilles Peskine8817f612018-12-18 00:18:46 +01003051 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3052 output2, output2_size,
3053 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003054 TEST_ASSERT( output2_length <=
3055 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
3056 TEST_ASSERT( output2_length <=
3057 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
3058
Gilles Peskine048b7f02018-06-08 14:20:49 +02003059 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003060 PSA_ASSERT( psa_cipher_finish( &operation2,
3061 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003062 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003063 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003064 TEST_ASSERT( function_output_length <=
3065 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3066 TEST_ASSERT( function_output_length <=
3067 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03003068
Gilles Peskine048b7f02018-06-08 14:20:49 +02003069 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003070
Gilles Peskine8817f612018-12-18 00:18:46 +01003071 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003072
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003073 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003074
3075exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003076 psa_cipher_abort( &operation1 );
3077 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003078 mbedtls_free( output1 );
3079 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003080 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003081 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003082}
3083/* END_CASE */
3084
3085/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003086void cipher_verify_output_multipart( int alg_arg,
3087 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003088 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003089 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003090 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003091{
Ronald Cron5425a212020-08-04 14:58:35 +02003092 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003093 psa_key_type_t key_type = key_type_arg;
3094 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003095 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003096 unsigned char iv[16] = {0};
3097 size_t iv_size = 16;
3098 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003099 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003100 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003101 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003102 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003103 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003104 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003105 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003106 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3107 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003108 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003109
Gilles Peskine8817f612018-12-18 00:18:46 +01003110 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003111
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003112 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3113 psa_set_key_algorithm( &attributes, alg );
3114 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003115
Ronald Cron5425a212020-08-04 14:58:35 +02003116 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3117 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003118
Ronald Cron5425a212020-08-04 14:58:35 +02003119 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3120 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003121
Steven Cooreman177deba2020-09-07 17:14:14 +02003122 if( alg != PSA_ALG_ECB_NO_PADDING )
3123 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003124 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3125 iv, iv_size,
3126 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003127 }
3128
gabor-mezei-armceface22021-01-21 12:26:17 +01003129 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3130 TEST_ASSERT( output1_buffer_size <=
3131 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003132 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003133
Gilles Peskinee0866522019-02-19 19:44:00 +01003134 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003135
Gilles Peskine8817f612018-12-18 00:18:46 +01003136 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3137 output1, output1_buffer_size,
3138 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003139 TEST_ASSERT( function_output_length <=
3140 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3141 TEST_ASSERT( function_output_length <=
3142 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003143 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003144
Gilles Peskine8817f612018-12-18 00:18:46 +01003145 PSA_ASSERT( psa_cipher_update( &operation1,
3146 input->x + first_part_size,
3147 input->len - first_part_size,
3148 output1, output1_buffer_size,
3149 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003150 TEST_ASSERT( function_output_length <=
3151 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3152 alg,
3153 input->len - first_part_size ) );
3154 TEST_ASSERT( function_output_length <=
3155 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003156 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003157
Gilles Peskine8817f612018-12-18 00:18:46 +01003158 PSA_ASSERT( psa_cipher_finish( &operation1,
3159 output1 + output1_length,
3160 output1_buffer_size - output1_length,
3161 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003162 TEST_ASSERT( function_output_length <=
3163 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3164 TEST_ASSERT( function_output_length <=
3165 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003166 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003167
Gilles Peskine8817f612018-12-18 00:18:46 +01003168 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003169
Gilles Peskine048b7f02018-06-08 14:20:49 +02003170 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003171 TEST_ASSERT( output2_buffer_size <=
3172 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3173 TEST_ASSERT( output2_buffer_size <=
3174 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003175 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003176
Steven Cooreman177deba2020-09-07 17:14:14 +02003177 if( iv_length > 0 )
3178 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003179 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3180 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003181 }
Moran Pekerded84402018-06-06 16:36:50 +03003182
Gilles Peskine8817f612018-12-18 00:18:46 +01003183 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3184 output2, output2_buffer_size,
3185 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003186 TEST_ASSERT( function_output_length <=
3187 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3188 TEST_ASSERT( function_output_length <=
3189 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003190 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003191
Gilles Peskine8817f612018-12-18 00:18:46 +01003192 PSA_ASSERT( psa_cipher_update( &operation2,
3193 output1 + first_part_size,
3194 output1_length - first_part_size,
3195 output2, output2_buffer_size,
3196 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003197 TEST_ASSERT( function_output_length <=
3198 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3199 alg,
3200 output1_length - first_part_size ) );
3201 TEST_ASSERT( function_output_length <=
3202 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003203 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003204
Gilles Peskine8817f612018-12-18 00:18:46 +01003205 PSA_ASSERT( psa_cipher_finish( &operation2,
3206 output2 + output2_length,
3207 output2_buffer_size - output2_length,
3208 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003209 TEST_ASSERT( function_output_length <=
3210 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3211 TEST_ASSERT( function_output_length <=
3212 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003213 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003214
Gilles Peskine8817f612018-12-18 00:18:46 +01003215 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003216
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003217 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003218
3219exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003220 psa_cipher_abort( &operation1 );
3221 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003222 mbedtls_free( output1 );
3223 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003224 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003225 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003226}
3227/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003228
Gilles Peskine20035e32018-02-03 22:44:14 +01003229/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003230void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003231 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003232 data_t *nonce,
3233 data_t *additional_data,
3234 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003235 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003236{
Ronald Cron5425a212020-08-04 14:58:35 +02003237 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003238 psa_key_type_t key_type = key_type_arg;
3239 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003240 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003241 unsigned char *output_data = NULL;
3242 size_t output_size = 0;
3243 size_t output_length = 0;
3244 unsigned char *output_data2 = NULL;
3245 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003246 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003247 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003248 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003249
Gilles Peskine8817f612018-12-18 00:18:46 +01003250 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003251
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003252 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3253 psa_set_key_algorithm( &attributes, alg );
3254 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003255
Gilles Peskine049c7532019-05-15 20:22:09 +02003256 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003257 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003258 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3259 key_bits = psa_get_key_bits( &attributes );
3260
3261 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3262 alg );
3263 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3264 * should be exact. */
3265 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3266 expected_result != PSA_ERROR_NOT_SUPPORTED )
3267 {
3268 TEST_EQUAL( output_size,
3269 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3270 TEST_ASSERT( output_size <=
3271 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3272 }
3273 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003274
Steven Cooremanf49478b2021-02-15 15:19:25 +01003275 status = psa_aead_encrypt( key, alg,
3276 nonce->x, nonce->len,
3277 additional_data->x,
3278 additional_data->len,
3279 input_data->x, input_data->len,
3280 output_data, output_size,
3281 &output_length );
3282
3283 /* If the operation is not supported, just skip and not fail in case the
3284 * encryption involves a common limitation of cryptography hardwares and
3285 * an alternative implementation. */
3286 if( status == PSA_ERROR_NOT_SUPPORTED )
3287 {
3288 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3289 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3290 }
3291
3292 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003293
3294 if( PSA_SUCCESS == expected_result )
3295 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003296 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003297
Gilles Peskine003a4a92019-05-14 16:09:40 +02003298 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3299 * should be exact. */
3300 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003301 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003302
gabor-mezei-armceface22021-01-21 12:26:17 +01003303 TEST_ASSERT( input_data->len <=
3304 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3305
Ronald Cron5425a212020-08-04 14:58:35 +02003306 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003307 nonce->x, nonce->len,
3308 additional_data->x,
3309 additional_data->len,
3310 output_data, output_length,
3311 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003312 &output_length2 ),
3313 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003314
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003315 ASSERT_COMPARE( input_data->x, input_data->len,
3316 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003317 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003318
Gilles Peskinea1cac842018-06-11 19:33:02 +02003319exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003320 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003321 mbedtls_free( output_data );
3322 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003323 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003324}
3325/* END_CASE */
3326
3327/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003328void aead_encrypt( int key_type_arg, data_t *key_data,
3329 int alg_arg,
3330 data_t *nonce,
3331 data_t *additional_data,
3332 data_t *input_data,
3333 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003334{
Ronald Cron5425a212020-08-04 14:58:35 +02003335 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003336 psa_key_type_t key_type = key_type_arg;
3337 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003338 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003339 unsigned char *output_data = NULL;
3340 size_t output_size = 0;
3341 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003342 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003343 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003344
Gilles Peskine8817f612018-12-18 00:18:46 +01003345 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003346
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003347 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3348 psa_set_key_algorithm( &attributes, alg );
3349 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003350
Gilles Peskine049c7532019-05-15 20:22:09 +02003351 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003352 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003353 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3354 key_bits = psa_get_key_bits( &attributes );
3355
3356 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3357 alg );
3358 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3359 * should be exact. */
3360 TEST_EQUAL( output_size,
3361 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3362 TEST_ASSERT( output_size <=
3363 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3364 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003365
Steven Cooremand588ea12021-01-11 19:36:04 +01003366 status = psa_aead_encrypt( key, alg,
3367 nonce->x, nonce->len,
3368 additional_data->x, additional_data->len,
3369 input_data->x, input_data->len,
3370 output_data, output_size,
3371 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003372
Ronald Cron28a45ed2021-02-09 20:35:42 +01003373 /* If the operation is not supported, just skip and not fail in case the
3374 * encryption involves a common limitation of cryptography hardwares and
3375 * an alternative implementation. */
3376 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003377 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003378 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3379 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003380 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003381
3382 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003383 ASSERT_COMPARE( expected_result->x, expected_result->len,
3384 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003385
Gilles Peskinea1cac842018-06-11 19:33:02 +02003386exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003387 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003388 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003389 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003390}
3391/* END_CASE */
3392
3393/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003394void aead_decrypt( int key_type_arg, data_t *key_data,
3395 int alg_arg,
3396 data_t *nonce,
3397 data_t *additional_data,
3398 data_t *input_data,
3399 data_t *expected_data,
3400 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003401{
Ronald Cron5425a212020-08-04 14:58:35 +02003402 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003403 psa_key_type_t key_type = key_type_arg;
3404 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003405 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003406 unsigned char *output_data = NULL;
3407 size_t output_size = 0;
3408 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003409 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003410 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003411 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003412
Gilles Peskine8817f612018-12-18 00:18:46 +01003413 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003414
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003415 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3416 psa_set_key_algorithm( &attributes, alg );
3417 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003418
Gilles Peskine049c7532019-05-15 20:22:09 +02003419 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003420 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003421 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3422 key_bits = psa_get_key_bits( &attributes );
3423
3424 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3425 alg );
3426 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3427 expected_result != PSA_ERROR_NOT_SUPPORTED )
3428 {
3429 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3430 * should be exact. */
3431 TEST_EQUAL( output_size,
3432 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3433 TEST_ASSERT( output_size <=
3434 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3435 }
3436 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003437
Steven Cooremand588ea12021-01-11 19:36:04 +01003438 status = psa_aead_decrypt( key, alg,
3439 nonce->x, nonce->len,
3440 additional_data->x,
3441 additional_data->len,
3442 input_data->x, input_data->len,
3443 output_data, output_size,
3444 &output_length );
3445
Ronald Cron28a45ed2021-02-09 20:35:42 +01003446 /* If the operation is not supported, just skip and not fail in case the
3447 * decryption involves a common limitation of cryptography hardwares and
3448 * an alternative implementation. */
3449 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003450 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003451 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3452 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003453 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003454
3455 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003456
Gilles Peskine2d277862018-06-18 15:41:12 +02003457 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003458 ASSERT_COMPARE( expected_data->x, expected_data->len,
3459 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003460
Gilles Peskinea1cac842018-06-11 19:33:02 +02003461exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003462 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003463 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003464 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003465}
3466/* END_CASE */
3467
3468/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003469void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3470 int alg_arg,
3471 data_t *nonce,
3472 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003473 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003474 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003475 int do_test_data_chunked,
3476 int do_set_lengths,
3477 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003478{
Paul Elliottd3f82412021-06-16 16:52:21 +01003479 size_t ad_part_len = 0;
3480 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01003481 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003482
Paul Elliotte64deda2021-09-09 14:07:23 +01003483 /* Ensure that either one part of the test or the other is done, i.e this
3484 * test does something. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003485 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3486
3487 /* Temporary whilst we have algorithms that cannot support chunking */
3488 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003489 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003490 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3491 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003492 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003493 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003494
Paul Elliott33746aa2021-09-15 16:40:40 +01003495 if( do_set_lengths )
3496 {
3497 if( ad_part_len & 0x01 )
3498 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3499 else
3500 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3501 }
3502
Paul Elliott329d5382021-07-22 17:10:45 +01003503 /* Split ad into length(ad_part_len) parts. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003504 if( !aead_multipart_internal_func( key_type_arg, key_data,
3505 alg_arg, nonce,
3506 additional_data,
3507 ad_part_len,
3508 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003509 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003510 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003511 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003512 break;
3513
3514 /* length(0) part, length(ad_part_len) part, length(0) part... */
3515 mbedtls_test_set_step( 1000 + ad_part_len );
3516
3517 if( !aead_multipart_internal_func( key_type_arg, key_data,
3518 alg_arg, nonce,
3519 additional_data,
3520 ad_part_len,
3521 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003522 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003523 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003524 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003525 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003526 }
3527 }
Paul Elliottd3f82412021-06-16 16:52:21 +01003528
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003529 /* Temporary whilst we have algorithms that cannot support chunking */
3530 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003531 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003532 for( data_part_len = 1; data_part_len <= input_data->len;
3533 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003534 {
Paul Elliott329d5382021-07-22 17:10:45 +01003535 /* Split data into length(data_part_len) parts. */
3536 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003537
Paul Elliott33746aa2021-09-15 16:40:40 +01003538 if( do_set_lengths )
3539 {
3540 if( data_part_len & 0x01 )
3541 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3542 else
3543 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3544 }
3545
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003546 if( !aead_multipart_internal_func( key_type_arg, key_data,
3547 alg_arg, nonce,
3548 additional_data, -1,
3549 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003550 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003551 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003552 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003553 break;
3554
3555 /* length(0) part, length(data_part_len) part, length(0) part... */
3556 mbedtls_test_set_step( 3000 + data_part_len );
3557
3558 if( !aead_multipart_internal_func( key_type_arg, key_data,
3559 alg_arg, nonce,
3560 additional_data, -1,
3561 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003562 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003563 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003564 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003565 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003566 }
3567 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003568
Paul Elliott0023e0a2021-04-27 10:06:22 +01003569
Paul Elliott8fc45162021-06-23 16:06:01 +01003570 /* Goto is required to silence warnings about unused labels, as we
3571 * don't actually do any test assertions in this function. */
3572 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003573}
3574/* END_CASE */
3575
3576/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003577void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3578 int alg_arg,
3579 data_t *nonce,
3580 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003581 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003582 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003583 int do_test_data_chunked,
3584 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01003585 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003586{
Paul Elliottd3f82412021-06-16 16:52:21 +01003587 size_t ad_part_len = 0;
3588 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01003589 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003590
Paul Elliotte64deda2021-09-09 14:07:23 +01003591 /* Ensure that either one part of the test or the other is done, i.e this
3592 * test does something. */
3593 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3594
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003595 /* Temporary whilst we have algorithms that cannot support chunking */
3596 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003597 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003598 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3599 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003600 {
Paul Elliott329d5382021-07-22 17:10:45 +01003601 /* Split ad into length(ad_part_len) parts. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003602 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003603
Paul Elliott33746aa2021-09-15 16:40:40 +01003604 if( do_set_lengths )
3605 {
3606 if( ad_part_len & 0x01 )
3607 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3608 else
3609 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3610 }
3611
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003612 if( !aead_multipart_internal_func( key_type_arg, key_data,
3613 alg_arg, nonce,
3614 additional_data,
3615 ad_part_len,
3616 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003617 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003618 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003619 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003620 break;
3621
3622 /* length(0) part, length(ad_part_len) part, length(0) part... */
3623 mbedtls_test_set_step( 1000 + ad_part_len );
3624
3625 if( !aead_multipart_internal_func( key_type_arg, key_data,
3626 alg_arg, nonce,
3627 additional_data,
3628 ad_part_len,
3629 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003630 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003631 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003632 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003633 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003634 }
3635 }
3636
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003637 /* Temporary whilst we have algorithms that cannot support chunking */
3638 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003639 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003640 for( data_part_len = 1; data_part_len <= input_data->len;
3641 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003642 {
Paul Elliott329d5382021-07-22 17:10:45 +01003643 /* Split data into length(data_part_len) parts. */
3644 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003645
Paul Elliott33746aa2021-09-15 16:40:40 +01003646 if( do_set_lengths )
3647 {
3648 if( data_part_len & 0x01 )
3649 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3650 else
3651 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3652 }
3653
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003654 if( !aead_multipart_internal_func( key_type_arg, key_data,
3655 alg_arg, nonce,
3656 additional_data, -1,
3657 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003658 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003659 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003660 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003661 break;
3662
3663 /* length(0) part, length(data_part_len) part, length(0) part... */
3664 mbedtls_test_set_step( 3000 + data_part_len );
3665
3666 if( !aead_multipart_internal_func( key_type_arg, key_data,
3667 alg_arg, nonce,
3668 additional_data, -1,
3669 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003670 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003671 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003672 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003673 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003674 }
3675 }
3676
Paul Elliott8fc45162021-06-23 16:06:01 +01003677 /* Goto is required to silence warnings about unused labels, as we
3678 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003679 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003680}
3681/* END_CASE */
3682
3683/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003684void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
3685 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01003686 int nonce_length,
3687 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003688 data_t *additional_data,
3689 data_t *input_data,
3690 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003691{
3692
3693 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3694 psa_key_type_t key_type = key_type_arg;
3695 psa_algorithm_t alg = alg_arg;
3696 psa_aead_operation_t operation;
3697 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3698 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3699 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01003700 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01003701 size_t actual_nonce_length = 0;
3702 size_t expected_nonce_length = expected_nonce_length_arg;
3703 unsigned char *output = NULL;
3704 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003705 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01003706 size_t ciphertext_size = 0;
3707 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003708 size_t tag_length = 0;
3709 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003710
3711 PSA_ASSERT( psa_crypto_init( ) );
3712
3713 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
3714 psa_set_key_algorithm( & attributes, alg );
3715 psa_set_key_type( & attributes, key_type );
3716
3717 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3718 &key ) );
3719
3720 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3721
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003722 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3723
Paul Elliottf1277632021-08-24 18:11:37 +01003724 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003725
Paul Elliottf1277632021-08-24 18:11:37 +01003726 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003727
Paul Elliottf1277632021-08-24 18:11:37 +01003728 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003729
Paul Elliottf1277632021-08-24 18:11:37 +01003730 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003731
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003732 operation = psa_aead_operation_init( );
3733
3734 status = psa_aead_encrypt_setup( &operation, key, alg );
3735
3736 /* If the operation is not supported, just skip and not fail in case the
3737 * encryption involves a common limitation of cryptography hardwares and
3738 * an alternative implementation. */
3739 if( status == PSA_ERROR_NOT_SUPPORTED )
3740 {
3741 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01003742 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003743 }
3744
3745 PSA_ASSERT( status );
3746
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003747 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01003748 nonce_length,
3749 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003750
Paul Elliott693bf312021-07-23 17:40:41 +01003751 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003752
Paul Elliottf1277632021-08-24 18:11:37 +01003753 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01003754
Paul Elliottf1277632021-08-24 18:11:37 +01003755 TEST_ASSERT( actual_nonce_length < PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01003756
Paul Elliott693bf312021-07-23 17:40:41 +01003757 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003758 {
3759
3760 /* Ensure we can still complete operation. */
3761
3762 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3763 additional_data->len ) );
3764
3765 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01003766 output, output_size,
3767 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003768
Paul Elliottf1277632021-08-24 18:11:37 +01003769 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3770 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003771 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3772 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003773
3774exit:
3775 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01003776 mbedtls_free( output );
3777 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003778 psa_aead_abort( &operation );
3779 PSA_DONE( );
3780}
3781/* END_CASE */
3782
3783/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01003784void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
3785 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01003786 int nonce_length_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01003787 data_t *additional_data,
3788 data_t *input_data,
3789 int expected_status_arg )
3790{
3791
3792 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3793 psa_key_type_t key_type = key_type_arg;
3794 psa_algorithm_t alg = alg_arg;
3795 psa_aead_operation_t operation;
3796 uint8_t *nonce_buffer = NULL;
3797 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3798 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3799 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003800 unsigned char *output = NULL;
3801 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01003802 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01003803 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003804 size_t ciphertext_size = 0;
3805 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003806 size_t tag_length = 0;
3807 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01003808 size_t index = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003809
3810 PSA_ASSERT( psa_crypto_init( ) );
3811
3812 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3813 psa_set_key_algorithm( &attributes, alg );
3814 psa_set_key_type( &attributes, key_type );
3815
3816 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3817 &key ) );
3818
3819 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3820
3821 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3822
Paul Elliott6f0e7202021-08-25 12:57:18 +01003823 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003824
Paul Elliott6f0e7202021-08-25 12:57:18 +01003825 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01003826
Paul Elliott6f0e7202021-08-25 12:57:18 +01003827 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01003828
Paul Elliott6f0e7202021-08-25 12:57:18 +01003829 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003830
3831 operation = psa_aead_operation_init( );
3832
3833 status = psa_aead_encrypt_setup( &operation, key, alg );
3834
3835 /* If the operation is not supported, just skip and not fail in case the
3836 * encryption involves a common limitation of cryptography hardwares and
3837 * an alternative implementation. */
3838 if( status == PSA_ERROR_NOT_SUPPORTED )
3839 {
3840 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003841 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01003842 }
3843
3844 PSA_ASSERT( status );
3845
Paul Elliott4023ffd2021-09-10 16:21:22 +01003846 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
3847 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01003848 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01003849 /* Arbitrary size buffer, to test zero length valid buffer. */
3850 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003851 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01003852 }
3853 else
3854 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003855 /* If length is zero, then this will return NULL. */
3856 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003857 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01003858
Paul Elliott4023ffd2021-09-10 16:21:22 +01003859 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01003860 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003861 for( index = 0; index < nonce_length - 1; ++index )
3862 {
3863 nonce_buffer[index] = 'a' + index;
3864 }
Paul Elliott66696b52021-08-16 18:42:41 +01003865 }
Paul Elliott863864a2021-07-23 17:28:31 +01003866 }
3867
Paul Elliott6f0e7202021-08-25 12:57:18 +01003868 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01003869
Paul Elliott693bf312021-07-23 17:40:41 +01003870 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01003871
3872 if( expected_status == PSA_SUCCESS )
3873 {
3874 /* Ensure we can still complete operation. */
3875
3876 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3877 additional_data->len ) );
3878
3879 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01003880 output, output_size,
3881 &ciphertext_length ) );
Paul Elliott863864a2021-07-23 17:28:31 +01003882
Paul Elliott6f0e7202021-08-25 12:57:18 +01003883 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3884 &ciphertext_length, tag_buffer,
Paul Elliott863864a2021-07-23 17:28:31 +01003885 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3886 }
3887
3888exit:
3889 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01003890 mbedtls_free( output );
3891 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01003892 mbedtls_free( nonce_buffer );
3893 psa_aead_abort( &operation );
3894 PSA_DONE( );
3895}
3896/* END_CASE */
3897
3898/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01003899void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
3900 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003901 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01003902 data_t *nonce,
3903 data_t *additional_data,
3904 data_t *input_data,
3905 int expected_status_arg )
3906{
3907
3908 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3909 psa_key_type_t key_type = key_type_arg;
3910 psa_algorithm_t alg = alg_arg;
3911 psa_aead_operation_t operation;
3912 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3913 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3914 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01003915 unsigned char *output = NULL;
3916 unsigned char *ciphertext = NULL;
3917 size_t output_size = output_size_arg;
3918 size_t ciphertext_size = 0;
3919 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01003920 size_t tag_length = 0;
3921 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
3922
3923 PSA_ASSERT( psa_crypto_init( ) );
3924
3925 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3926 psa_set_key_algorithm( &attributes, alg );
3927 psa_set_key_type( &attributes, key_type );
3928
3929 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3930 &key ) );
3931
3932 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3933
Paul Elliottc6d11d02021-09-01 12:04:23 +01003934 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003935
Paul Elliottc6d11d02021-09-01 12:04:23 +01003936 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01003937
Paul Elliottc6d11d02021-09-01 12:04:23 +01003938 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003939
3940 operation = psa_aead_operation_init( );
3941
3942 status = psa_aead_encrypt_setup( &operation, key, alg );
3943
3944 /* If the operation is not supported, just skip and not fail in case the
3945 * encryption involves a common limitation of cryptography hardwares and
3946 * an alternative implementation. */
3947 if( status == PSA_ERROR_NOT_SUPPORTED )
3948 {
3949 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3950 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3951 }
3952
3953 PSA_ASSERT( status );
3954
3955 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3956
3957 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3958 additional_data->len ) );
3959
3960 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003961 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01003962
3963 TEST_EQUAL( status, expected_status );
3964
3965 if( expected_status == PSA_SUCCESS )
3966 {
3967 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01003968 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3969 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01003970 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3971 }
3972
3973exit:
3974 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01003975 mbedtls_free( output );
3976 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01003977 psa_aead_abort( &operation );
3978 PSA_DONE( );
3979}
3980/* END_CASE */
3981
Paul Elliott91b021e2021-07-23 18:52:31 +01003982/* BEGIN_CASE */
3983void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
3984 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01003985 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01003986 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01003987 data_t *nonce,
3988 data_t *additional_data,
3989 data_t *input_data,
3990 int expected_status_arg )
3991{
3992
3993 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3994 psa_key_type_t key_type = key_type_arg;
3995 psa_algorithm_t alg = alg_arg;
3996 psa_aead_operation_t operation;
3997 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3998 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3999 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004000 unsigned char *ciphertext = NULL;
4001 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01004002 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004003 size_t ciphertext_size = 0;
4004 size_t ciphertext_length = 0;
4005 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004006 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004007 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004008
4009 PSA_ASSERT( psa_crypto_init( ) );
4010
4011 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4012 psa_set_key_algorithm( &attributes, alg );
4013 psa_set_key_type( &attributes, key_type );
4014
4015 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4016 &key ) );
4017
4018 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4019
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004020 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004021
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004022 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004023
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004024 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004025
Paul Elliott719c1322021-09-13 18:27:22 +01004026 ASSERT_ALLOC( tag_buffer, tag_size );
4027
Paul Elliott91b021e2021-07-23 18:52:31 +01004028 operation = psa_aead_operation_init( );
4029
4030 status = psa_aead_encrypt_setup( &operation, key, alg );
4031
4032 /* If the operation is not supported, just skip and not fail in case the
4033 * encryption involves a common limitation of cryptography hardwares and
4034 * an alternative implementation. */
4035 if( status == PSA_ERROR_NOT_SUPPORTED )
4036 {
4037 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4038 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4039 }
4040
4041 PSA_ASSERT( status );
4042
4043 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4044
4045 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4046 additional_data->len ) );
4047
4048 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004049 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004050
4051 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004052 status = psa_aead_finish( &operation, finish_ciphertext,
4053 finish_ciphertext_size,
4054 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004055 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004056
4057 TEST_EQUAL( status, expected_status );
4058
4059exit:
4060 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004061 mbedtls_free( ciphertext );
4062 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01004063 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01004064 psa_aead_abort( &operation );
4065 PSA_DONE( );
4066}
4067/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004068
4069/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01004070void aead_multipart_verify( int key_type_arg, data_t *key_data,
4071 int alg_arg,
4072 data_t *nonce,
4073 data_t *additional_data,
4074 data_t *input_data,
4075 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004076 int tag_usage_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01004077 int expected_status_arg )
4078{
4079 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4080 psa_key_type_t key_type = key_type_arg;
4081 psa_algorithm_t alg = alg_arg;
4082 psa_aead_operation_t operation;
4083 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4084 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4085 psa_status_t expected_status = expected_status_arg;
4086 unsigned char *plaintext = NULL;
4087 unsigned char *finish_plaintext = NULL;
4088 size_t plaintext_size = 0;
4089 size_t plaintext_length = 0;
4090 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004091 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004092 unsigned char *tag_buffer = NULL;
4093 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01004094
4095 PSA_ASSERT( psa_crypto_init( ) );
4096
4097 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4098 psa_set_key_algorithm( &attributes, alg );
4099 psa_set_key_type( &attributes, key_type );
4100
4101 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4102 &key ) );
4103
4104 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4105
4106 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
4107 input_data->len );
4108
4109 ASSERT_ALLOC( plaintext, plaintext_size );
4110
4111 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
4112
4113 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
4114
4115 operation = psa_aead_operation_init( );
4116
4117 status = psa_aead_decrypt_setup( &operation, key, alg );
4118
4119 /* If the operation is not supported, just skip and not fail in case the
4120 * encryption involves a common limitation of cryptography hardwares and
4121 * an alternative implementation. */
4122 if( status == PSA_ERROR_NOT_SUPPORTED )
4123 {
4124 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4125 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4126 }
4127
4128 PSA_ASSERT( status );
4129
4130 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4131
4132 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4133 additional_data->len ) );
4134
4135 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4136 input_data->len,
4137 plaintext, plaintext_size,
4138 &plaintext_length ) );
4139
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004140 if( tag_usage == USE_GIVEN_TAG )
4141 {
4142 tag_buffer = tag->x;
4143 tag_size = tag->len;
4144 }
4145
Paul Elliott9961a662021-09-17 19:19:02 +01004146 status = psa_aead_verify( &operation, finish_plaintext,
4147 verify_plaintext_size,
4148 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004149 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01004150
4151 TEST_EQUAL( status, expected_status );
4152
4153exit:
4154 psa_destroy_key( key );
4155 mbedtls_free( plaintext );
4156 mbedtls_free( finish_plaintext );
4157 psa_aead_abort( &operation );
4158 PSA_DONE( );
4159}
4160/* END_CASE */
4161
Paul Elliott9961a662021-09-17 19:19:02 +01004162/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01004163void aead_multipart_setup( int key_type_arg, data_t *key_data,
4164 int alg_arg, int expected_status_arg )
4165{
4166 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4167 psa_key_type_t key_type = key_type_arg;
4168 psa_algorithm_t alg = alg_arg;
4169 psa_aead_operation_t operation;
4170 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4171 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4172 psa_status_t expected_status = expected_status_arg;
4173
4174 PSA_ASSERT( psa_crypto_init( ) );
4175
4176 psa_set_key_usage_flags( &attributes,
4177 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4178 psa_set_key_algorithm( &attributes, alg );
4179 psa_set_key_type( &attributes, key_type );
4180
4181 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4182 &key ) );
4183
Paul Elliott64555bd2021-09-20 16:44:44 +01004184 operation = psa_aead_operation_init( );
4185
Paul Elliott5221ef62021-09-19 17:33:03 +01004186 mbedtls_test_set_step( 0 );
4187
4188 status = psa_aead_encrypt_setup( &operation, key, alg );
4189
4190 TEST_EQUAL( status, expected_status );
4191
4192 psa_aead_abort( &operation );
4193
Paul Elliott5221ef62021-09-19 17:33:03 +01004194 mbedtls_test_set_step( 1 );
4195
4196 status = psa_aead_decrypt_setup( &operation, key, alg );
4197
4198 TEST_EQUAL(status, expected_status );
4199
4200exit:
4201 psa_destroy_key( key );
4202 psa_aead_abort( &operation );
4203 PSA_DONE( );
4204}
4205/* END_CASE */
4206
4207/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004208void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4209 int alg_arg,
4210 data_t *nonce,
4211 data_t *additional_data,
4212 data_t *input_data )
4213{
4214 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4215 psa_key_type_t key_type = key_type_arg;
4216 psa_algorithm_t alg = alg_arg;
4217 psa_aead_operation_t operation;
4218 unsigned char *output_data = NULL;
4219 unsigned char *final_data = NULL;
4220 size_t output_size = 0;
4221 size_t finish_output_size = 0;
4222 size_t output_length = 0;
4223 size_t key_bits = 0;
4224 size_t tag_length = 0;
4225 size_t tag_size = 0;
4226 size_t nonce_length = 0;
4227 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4228 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4229 size_t output_part_length = 0;
4230 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4231
4232 PSA_ASSERT( psa_crypto_init( ) );
4233
4234 psa_set_key_usage_flags( & attributes,
4235 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4236 psa_set_key_algorithm( & attributes, alg );
4237 psa_set_key_type( & attributes, key_type );
4238
4239 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4240 &key ) );
4241
4242 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4243 key_bits = psa_get_key_bits( &attributes );
4244
4245 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4246
4247 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4248
4249 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4250
4251 ASSERT_ALLOC( output_data, output_size );
4252
4253 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4254
4255 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4256
4257 ASSERT_ALLOC( final_data, finish_output_size );
4258
4259 /* Test all operations error without calling setup first. */
4260
4261 operation = psa_aead_operation_init( );
4262
4263 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4264 PSA_ERROR_BAD_STATE );
4265
4266 psa_aead_abort( &operation );
4267
Paul Elliottc23a9a02021-06-21 18:32:46 +01004268 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4269 PSA_AEAD_NONCE_MAX_SIZE,
4270 &nonce_length ),
4271 PSA_ERROR_BAD_STATE );
4272
4273 psa_aead_abort( &operation );
4274
Paul Elliott481be342021-07-16 17:38:47 +01004275 /* ------------------------------------------------------- */
4276
Paul Elliottc23a9a02021-06-21 18:32:46 +01004277 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4278 input_data->len ),
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_update_ad( &operation, additional_data->x,
4286 additional_data->len ),
4287 PSA_ERROR_BAD_STATE );
4288
4289 psa_aead_abort( &operation );
4290
Paul Elliott481be342021-07-16 17:38:47 +01004291 /* ------------------------------------------------------- */
4292
Paul Elliottc23a9a02021-06-21 18:32:46 +01004293 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4294 input_data->len, output_data,
4295 output_size, &output_length ),
4296 PSA_ERROR_BAD_STATE );
4297
4298 psa_aead_abort( &operation );
4299
Paul Elliott481be342021-07-16 17:38:47 +01004300 /* ------------------------------------------------------- */
4301
Paul Elliottc23a9a02021-06-21 18:32:46 +01004302 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4303 finish_output_size,
4304 &output_part_length,
4305 tag_buffer, tag_length,
4306 &tag_size ),
4307 PSA_ERROR_BAD_STATE );
4308
4309 psa_aead_abort( &operation );
4310
Paul Elliott481be342021-07-16 17:38:47 +01004311 /* ------------------------------------------------------- */
4312
Paul Elliottc23a9a02021-06-21 18:32:46 +01004313 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4314 finish_output_size,
4315 &output_part_length,
4316 tag_buffer,
4317 tag_length ),
4318 PSA_ERROR_BAD_STATE );
4319
4320 psa_aead_abort( &operation );
4321
4322 /* Test for double setups. */
4323
Paul Elliottc23a9a02021-06-21 18:32:46 +01004324 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4325
4326 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4327 PSA_ERROR_BAD_STATE );
4328
4329 psa_aead_abort( &operation );
4330
Paul Elliott481be342021-07-16 17:38:47 +01004331 /* ------------------------------------------------------- */
4332
Paul Elliottc23a9a02021-06-21 18:32:46 +01004333 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4334
4335 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4336 PSA_ERROR_BAD_STATE );
4337
4338 psa_aead_abort( &operation );
4339
Paul Elliott374a2be2021-07-16 17:53:40 +01004340 /* ------------------------------------------------------- */
4341
Paul Elliott374a2be2021-07-16 17:53:40 +01004342 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4343
4344 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4345 PSA_ERROR_BAD_STATE );
4346
4347 psa_aead_abort( &operation );
4348
4349 /* ------------------------------------------------------- */
4350
Paul Elliott374a2be2021-07-16 17:53:40 +01004351 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4352
4353 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4354 PSA_ERROR_BAD_STATE );
4355
4356 psa_aead_abort( &operation );
4357
Paul Elliottc23a9a02021-06-21 18:32:46 +01004358 /* Test for not setting a nonce. */
4359
Paul Elliottc23a9a02021-06-21 18:32:46 +01004360 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4361
4362 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4363 additional_data->len ),
4364 PSA_ERROR_BAD_STATE );
4365
4366 psa_aead_abort( &operation );
4367
Paul Elliott7f628422021-09-01 12:08:29 +01004368 /* ------------------------------------------------------- */
4369
Paul Elliott7f628422021-09-01 12:08:29 +01004370 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4371
4372 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4373 input_data->len, output_data,
4374 output_size, &output_length ),
4375 PSA_ERROR_BAD_STATE );
4376
4377 psa_aead_abort( &operation );
4378
Paul Elliottbdc2c682021-09-21 18:37:10 +01004379 /* ------------------------------------------------------- */
4380
Paul Elliottbdc2c682021-09-21 18:37:10 +01004381 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4382
4383 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4384 finish_output_size,
4385 &output_part_length,
4386 tag_buffer, tag_length,
4387 &tag_size ),
4388 PSA_ERROR_BAD_STATE );
4389
4390 psa_aead_abort( &operation );
4391
4392 /* ------------------------------------------------------- */
4393
Paul Elliottbdc2c682021-09-21 18:37:10 +01004394 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4395
4396 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4397 finish_output_size,
4398 &output_part_length,
4399 tag_buffer,
4400 tag_length ),
4401 PSA_ERROR_BAD_STATE );
4402
4403 psa_aead_abort( &operation );
4404
Paul Elliottc23a9a02021-06-21 18:32:46 +01004405 /* Test for double setting nonce. */
4406
Paul Elliottc23a9a02021-06-21 18:32:46 +01004407 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4408
4409 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4410
4411 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4412 PSA_ERROR_BAD_STATE );
4413
4414 psa_aead_abort( &operation );
4415
Paul Elliott374a2be2021-07-16 17:53:40 +01004416 /* Test for double generating nonce. */
4417
Paul Elliott374a2be2021-07-16 17:53:40 +01004418 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4419
4420 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4421 PSA_AEAD_NONCE_MAX_SIZE,
4422 &nonce_length ) );
4423
4424 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4425 PSA_AEAD_NONCE_MAX_SIZE,
4426 &nonce_length ),
4427 PSA_ERROR_BAD_STATE );
4428
4429
4430 psa_aead_abort( &operation );
4431
4432 /* Test for generate nonce then set and vice versa */
4433
Paul Elliott374a2be2021-07-16 17:53:40 +01004434 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4435
4436 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4437 PSA_AEAD_NONCE_MAX_SIZE,
4438 &nonce_length ) );
4439
4440 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4441 PSA_ERROR_BAD_STATE );
4442
4443 psa_aead_abort( &operation );
4444
4445 /* ------------------------------------------------------- */
4446
Paul Elliott374a2be2021-07-16 17:53:40 +01004447 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4448
4449 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4450
4451 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4452 PSA_AEAD_NONCE_MAX_SIZE,
4453 &nonce_length ),
4454 PSA_ERROR_BAD_STATE );
4455
4456 psa_aead_abort( &operation );
4457
Paul Elliott7220cae2021-06-22 17:25:57 +01004458 /* Test for generating nonce in decrypt setup. */
4459
Paul Elliott7220cae2021-06-22 17:25:57 +01004460 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4461
4462 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4463 PSA_AEAD_NONCE_MAX_SIZE,
4464 &nonce_length ),
4465 PSA_ERROR_BAD_STATE );
4466
4467 psa_aead_abort( &operation );
4468
Paul Elliottc23a9a02021-06-21 18:32:46 +01004469 /* Test for setting lengths twice. */
4470
Paul Elliottc23a9a02021-06-21 18:32:46 +01004471 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4472
4473 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4474
4475 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4476 input_data->len ) );
4477
4478 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4479 input_data->len ),
4480 PSA_ERROR_BAD_STATE );
4481
4482 psa_aead_abort( &operation );
4483
4484 /* Test for setting lengths after already starting data. */
4485
Paul Elliottc23a9a02021-06-21 18:32:46 +01004486 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4487
4488 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4489
Paul Elliottf94bd992021-09-19 18:15:59 +01004490 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4491 additional_data->len ) );
4492
4493 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4494 input_data->len ),
4495 PSA_ERROR_BAD_STATE );
4496
4497 psa_aead_abort( &operation );
4498
4499 /* ------------------------------------------------------- */
4500
Paul Elliottf94bd992021-09-19 18:15:59 +01004501 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4502
4503 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4504
Paul Elliottc23a9a02021-06-21 18:32:46 +01004505 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4506 input_data->len, output_data,
4507 output_size, &output_length ) );
4508
4509 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4510 input_data->len ),
4511 PSA_ERROR_BAD_STATE );
4512
4513 psa_aead_abort( &operation );
4514
Paul Elliott243080c2021-07-21 19:01:17 +01004515 /* Test for not sending any additional data or data after setting non zero
4516 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004517
Paul Elliottc23a9a02021-06-21 18:32:46 +01004518 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4519
4520 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4521
4522 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4523 input_data->len ) );
4524
4525 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4526 finish_output_size,
4527 &output_part_length,
4528 tag_buffer, tag_length,
4529 &tag_size ),
4530 PSA_ERROR_INVALID_ARGUMENT );
4531
4532 psa_aead_abort( &operation );
4533
Paul Elliott243080c2021-07-21 19:01:17 +01004534 /* Test for not sending any additional data or data after setting non-zero
4535 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004536
Paul Elliottc23a9a02021-06-21 18:32:46 +01004537 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4538
4539 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4540
4541 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4542 input_data->len ) );
4543
4544 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4545 finish_output_size,
4546 &output_part_length,
4547 tag_buffer,
4548 tag_length ),
4549 PSA_ERROR_INVALID_ARGUMENT );
4550
4551 psa_aead_abort( &operation );
4552
Paul Elliott243080c2021-07-21 19:01:17 +01004553 /* Test for not sending any additional data after setting a non-zero length
4554 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004555
Paul Elliottc23a9a02021-06-21 18:32:46 +01004556 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4557
4558 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4559
4560 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4561 input_data->len ) );
4562
4563 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4564 input_data->len, output_data,
4565 output_size, &output_length ),
4566 PSA_ERROR_INVALID_ARGUMENT );
4567
4568 psa_aead_abort( &operation );
4569
Paul Elliottf94bd992021-09-19 18:15:59 +01004570 /* Test for not sending any data after setting a non-zero length for it.*/
4571
Paul Elliottf94bd992021-09-19 18:15:59 +01004572 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4573
4574 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4575
4576 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4577 input_data->len ) );
4578
4579 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4580 additional_data->len ) );
4581
4582 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4583 finish_output_size,
4584 &output_part_length,
4585 tag_buffer, tag_length,
4586 &tag_size ),
4587 PSA_ERROR_INVALID_ARGUMENT );
4588
4589 psa_aead_abort( &operation );
4590
Paul Elliottb0450fe2021-09-01 15:06:26 +01004591 /* Test for sending too much additional data after setting lengths. */
4592
Paul Elliottb0450fe2021-09-01 15:06:26 +01004593 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4594
4595 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4596
4597 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4598
4599
4600 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4601 additional_data->len ),
4602 PSA_ERROR_INVALID_ARGUMENT );
4603
4604 psa_aead_abort( &operation );
4605
Paul Elliotta2a09b02021-09-22 14:56:40 +01004606 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01004607
4608 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4609
4610 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4611
4612 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4613 input_data->len ) );
4614
4615 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4616 additional_data->len ) );
4617
4618 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4619 1 ),
4620 PSA_ERROR_INVALID_ARGUMENT );
4621
4622 psa_aead_abort( &operation );
4623
Paul Elliottb0450fe2021-09-01 15:06:26 +01004624 /* Test for sending too much data after setting lengths. */
4625
Paul Elliottb0450fe2021-09-01 15:06:26 +01004626 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4627
4628 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4629
4630 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4631
4632 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4633 input_data->len, output_data,
4634 output_size, &output_length ),
4635 PSA_ERROR_INVALID_ARGUMENT );
4636
4637 psa_aead_abort( &operation );
4638
Paul Elliotta2a09b02021-09-22 14:56:40 +01004639 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01004640
4641 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4642
4643 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4644
4645 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4646 input_data->len ) );
4647
4648 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4649 additional_data->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( &operation, input_data->x,
4656 1, output_data,
4657 output_size, &output_length ),
4658 PSA_ERROR_INVALID_ARGUMENT );
4659
4660 psa_aead_abort( &operation );
4661
Paul Elliottc23a9a02021-06-21 18:32:46 +01004662 /* Test sending additional data after data. */
4663
Paul Elliottc23a9a02021-06-21 18:32:46 +01004664 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4665
4666 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4667
4668 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4669 input_data->len, output_data,
4670 output_size, &output_length ) );
4671
4672 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4673 additional_data->len ),
4674 PSA_ERROR_BAD_STATE );
4675
4676 psa_aead_abort( &operation );
4677
Paul Elliott534d0b42021-06-22 19:15:20 +01004678 /* Test calling finish on decryption. */
4679
Paul Elliott534d0b42021-06-22 19:15:20 +01004680 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4681
4682 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4683
4684 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4685 finish_output_size,
4686 &output_part_length,
4687 tag_buffer, tag_length,
4688 &tag_size ),
4689 PSA_ERROR_BAD_STATE );
4690
4691 psa_aead_abort( &operation );
4692
4693 /* Test calling verify on encryption. */
4694
Paul Elliott534d0b42021-06-22 19:15:20 +01004695 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4696
4697 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4698
4699 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4700 finish_output_size,
4701 &output_part_length,
4702 tag_buffer,
4703 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01004704 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01004705
4706 psa_aead_abort( &operation );
4707
4708
Paul Elliottc23a9a02021-06-21 18:32:46 +01004709exit:
4710 psa_destroy_key( key );
4711 psa_aead_abort( &operation );
4712 mbedtls_free( output_data );
4713 mbedtls_free( final_data );
4714 PSA_DONE( );
4715}
4716/* END_CASE */
4717
4718/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004719void signature_size( int type_arg,
4720 int bits,
4721 int alg_arg,
4722 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004723{
4724 psa_key_type_t type = type_arg;
4725 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004726 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004727
Gilles Peskinefe11b722018-12-18 00:24:04 +01004728 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004729
Gilles Peskinee59236f2018-01-27 23:32:46 +01004730exit:
4731 ;
4732}
4733/* END_CASE */
4734
4735/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004736void sign_hash_deterministic( int key_type_arg, data_t *key_data,
4737 int alg_arg, data_t *input_data,
4738 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004739{
Ronald Cron5425a212020-08-04 14:58:35 +02004740 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004741 psa_key_type_t key_type = key_type_arg;
4742 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004743 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004744 unsigned char *signature = NULL;
4745 size_t signature_size;
4746 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004747 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004748
Gilles Peskine8817f612018-12-18 00:18:46 +01004749 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004750
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004751 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004752 psa_set_key_algorithm( &attributes, alg );
4753 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004754
Gilles Peskine049c7532019-05-15 20:22:09 +02004755 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004756 &key ) );
4757 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004758 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004759
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004760 /* Allocate a buffer which has the size advertized by the
4761 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004762 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004763 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004764 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004765 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004766 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004767
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004768 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004769 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004770 input_data->x, input_data->len,
4771 signature, signature_size,
4772 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004773 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004774 ASSERT_COMPARE( output_data->x, output_data->len,
4775 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004776
4777exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004778 /*
4779 * Key attributes may have been returned by psa_get_key_attributes()
4780 * thus reset them as required.
4781 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004782 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004783
Ronald Cron5425a212020-08-04 14:58:35 +02004784 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004785 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004786 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004787}
4788/* END_CASE */
4789
4790/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004791void sign_hash_fail( int key_type_arg, data_t *key_data,
4792 int alg_arg, data_t *input_data,
4793 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004794{
Ronald Cron5425a212020-08-04 14:58:35 +02004795 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004796 psa_key_type_t key_type = key_type_arg;
4797 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004798 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004799 psa_status_t actual_status;
4800 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004801 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004802 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004803 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004804
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004805 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004806
Gilles Peskine8817f612018-12-18 00:18:46 +01004807 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004808
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004809 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004810 psa_set_key_algorithm( &attributes, alg );
4811 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004812
Gilles Peskine049c7532019-05-15 20:22:09 +02004813 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004814 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004815
Ronald Cron5425a212020-08-04 14:58:35 +02004816 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004817 input_data->x, input_data->len,
4818 signature, signature_size,
4819 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004820 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004821 /* The value of *signature_length is unspecified on error, but
4822 * whatever it is, it should be less than signature_size, so that
4823 * if the caller tries to read *signature_length bytes without
4824 * checking the error code then they don't overflow a buffer. */
4825 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004826
4827exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004828 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004829 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004830 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004831 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004832}
4833/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004834
4835/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004836void sign_verify_hash( int key_type_arg, data_t *key_data,
4837 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02004838{
Ronald Cron5425a212020-08-04 14:58:35 +02004839 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004840 psa_key_type_t key_type = key_type_arg;
4841 psa_algorithm_t alg = alg_arg;
4842 size_t key_bits;
4843 unsigned char *signature = NULL;
4844 size_t signature_size;
4845 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004846 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004847
Gilles Peskine8817f612018-12-18 00:18:46 +01004848 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004849
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004850 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004851 psa_set_key_algorithm( &attributes, alg );
4852 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004853
Gilles Peskine049c7532019-05-15 20:22:09 +02004854 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004855 &key ) );
4856 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004857 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004858
4859 /* Allocate a buffer which has the size advertized by the
4860 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004861 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004862 key_bits, alg );
4863 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004864 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004865 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004866
4867 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004868 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004869 input_data->x, input_data->len,
4870 signature, signature_size,
4871 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004872 /* Check that the signature length looks sensible. */
4873 TEST_ASSERT( signature_length <= signature_size );
4874 TEST_ASSERT( signature_length > 0 );
4875
4876 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004877 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004878 input_data->x, input_data->len,
4879 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004880
4881 if( input_data->len != 0 )
4882 {
4883 /* Flip a bit in the input and verify that the signature is now
4884 * detected as invalid. Flip a bit at the beginning, not at the end,
4885 * because ECDSA may ignore the last few bits of the input. */
4886 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004887 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004888 input_data->x, input_data->len,
4889 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004890 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004891 }
4892
4893exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004894 /*
4895 * Key attributes may have been returned by psa_get_key_attributes()
4896 * thus reset them as required.
4897 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004898 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004899
Ronald Cron5425a212020-08-04 14:58:35 +02004900 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004901 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004902 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004903}
4904/* END_CASE */
4905
4906/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004907void verify_hash( int key_type_arg, data_t *key_data,
4908 int alg_arg, data_t *hash_data,
4909 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004910{
Ronald Cron5425a212020-08-04 14:58:35 +02004911 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004912 psa_key_type_t key_type = key_type_arg;
4913 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004914 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004915
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004916 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004917
Gilles Peskine8817f612018-12-18 00:18:46 +01004918 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004919
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004920 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004921 psa_set_key_algorithm( &attributes, alg );
4922 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004923
Gilles Peskine049c7532019-05-15 20:22:09 +02004924 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004925 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004926
Ronald Cron5425a212020-08-04 14:58:35 +02004927 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004928 hash_data->x, hash_data->len,
4929 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004930
itayzafrir5c753392018-05-08 11:18:38 +03004931exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004932 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004933 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004934 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004935}
4936/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004937
4938/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004939void verify_hash_fail( int key_type_arg, data_t *key_data,
4940 int alg_arg, data_t *hash_data,
4941 data_t *signature_data,
4942 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004943{
Ronald Cron5425a212020-08-04 14:58:35 +02004944 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004945 psa_key_type_t key_type = key_type_arg;
4946 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004947 psa_status_t actual_status;
4948 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004949 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004950
Gilles Peskine8817f612018-12-18 00:18:46 +01004951 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004952
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004953 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004954 psa_set_key_algorithm( &attributes, alg );
4955 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004956
Gilles Peskine049c7532019-05-15 20:22:09 +02004957 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004958 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004959
Ronald Cron5425a212020-08-04 14:58:35 +02004960 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004961 hash_data->x, hash_data->len,
4962 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004963 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004964
4965exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004966 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004967 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004968 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004969}
4970/* END_CASE */
4971
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004972/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02004973void sign_message_deterministic( int key_type_arg,
4974 data_t *key_data,
4975 int alg_arg,
4976 data_t *input_data,
4977 data_t *output_data )
4978{
4979 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4980 psa_key_type_t key_type = key_type_arg;
4981 psa_algorithm_t alg = alg_arg;
4982 size_t key_bits;
4983 unsigned char *signature = NULL;
4984 size_t signature_size;
4985 size_t signature_length = 0xdeadbeef;
4986 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4987
4988 PSA_ASSERT( psa_crypto_init( ) );
4989
4990 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4991 psa_set_key_algorithm( &attributes, alg );
4992 psa_set_key_type( &attributes, key_type );
4993
4994 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4995 &key ) );
4996 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4997 key_bits = psa_get_key_bits( &attributes );
4998
4999 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5000 TEST_ASSERT( signature_size != 0 );
5001 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5002 ASSERT_ALLOC( signature, signature_size );
5003
5004 PSA_ASSERT( psa_sign_message( key, alg,
5005 input_data->x, input_data->len,
5006 signature, signature_size,
5007 &signature_length ) );
5008
5009 ASSERT_COMPARE( output_data->x, output_data->len,
5010 signature, signature_length );
5011
5012exit:
5013 psa_reset_key_attributes( &attributes );
5014
5015 psa_destroy_key( key );
5016 mbedtls_free( signature );
5017 PSA_DONE( );
5018
5019}
5020/* END_CASE */
5021
5022/* BEGIN_CASE */
5023void sign_message_fail( int key_type_arg,
5024 data_t *key_data,
5025 int alg_arg,
5026 data_t *input_data,
5027 int signature_size_arg,
5028 int expected_status_arg )
5029{
5030 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5031 psa_key_type_t key_type = key_type_arg;
5032 psa_algorithm_t alg = alg_arg;
5033 size_t signature_size = signature_size_arg;
5034 psa_status_t actual_status;
5035 psa_status_t expected_status = expected_status_arg;
5036 unsigned char *signature = NULL;
5037 size_t signature_length = 0xdeadbeef;
5038 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5039
5040 ASSERT_ALLOC( signature, signature_size );
5041
5042 PSA_ASSERT( psa_crypto_init( ) );
5043
5044 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5045 psa_set_key_algorithm( &attributes, alg );
5046 psa_set_key_type( &attributes, key_type );
5047
5048 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5049 &key ) );
5050
5051 actual_status = psa_sign_message( key, alg,
5052 input_data->x, input_data->len,
5053 signature, signature_size,
5054 &signature_length );
5055 TEST_EQUAL( actual_status, expected_status );
5056 /* The value of *signature_length is unspecified on error, but
5057 * whatever it is, it should be less than signature_size, so that
5058 * if the caller tries to read *signature_length bytes without
5059 * checking the error code then they don't overflow a buffer. */
5060 TEST_ASSERT( signature_length <= signature_size );
5061
5062exit:
5063 psa_reset_key_attributes( &attributes );
5064 psa_destroy_key( key );
5065 mbedtls_free( signature );
5066 PSA_DONE( );
5067}
5068/* END_CASE */
5069
5070/* BEGIN_CASE */
5071void sign_verify_message( int key_type_arg,
5072 data_t *key_data,
5073 int alg_arg,
5074 data_t *input_data )
5075{
5076 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5077 psa_key_type_t key_type = key_type_arg;
5078 psa_algorithm_t alg = alg_arg;
5079 size_t key_bits;
5080 unsigned char *signature = NULL;
5081 size_t signature_size;
5082 size_t signature_length = 0xdeadbeef;
5083 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5084
5085 PSA_ASSERT( psa_crypto_init( ) );
5086
5087 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
5088 PSA_KEY_USAGE_VERIFY_MESSAGE );
5089 psa_set_key_algorithm( &attributes, alg );
5090 psa_set_key_type( &attributes, key_type );
5091
5092 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5093 &key ) );
5094 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5095 key_bits = psa_get_key_bits( &attributes );
5096
5097 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5098 TEST_ASSERT( signature_size != 0 );
5099 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5100 ASSERT_ALLOC( signature, signature_size );
5101
5102 PSA_ASSERT( psa_sign_message( key, alg,
5103 input_data->x, input_data->len,
5104 signature, signature_size,
5105 &signature_length ) );
5106 TEST_ASSERT( signature_length <= signature_size );
5107 TEST_ASSERT( signature_length > 0 );
5108
5109 PSA_ASSERT( psa_verify_message( key, alg,
5110 input_data->x, input_data->len,
5111 signature, signature_length ) );
5112
5113 if( input_data->len != 0 )
5114 {
5115 /* Flip a bit in the input and verify that the signature is now
5116 * detected as invalid. Flip a bit at the beginning, not at the end,
5117 * because ECDSA may ignore the last few bits of the input. */
5118 input_data->x[0] ^= 1;
5119 TEST_EQUAL( psa_verify_message( key, alg,
5120 input_data->x, input_data->len,
5121 signature, signature_length ),
5122 PSA_ERROR_INVALID_SIGNATURE );
5123 }
5124
5125exit:
5126 psa_reset_key_attributes( &attributes );
5127
5128 psa_destroy_key( key );
5129 mbedtls_free( signature );
5130 PSA_DONE( );
5131}
5132/* END_CASE */
5133
5134/* BEGIN_CASE */
5135void verify_message( int key_type_arg,
5136 data_t *key_data,
5137 int alg_arg,
5138 data_t *input_data,
5139 data_t *signature_data )
5140{
5141 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5142 psa_key_type_t key_type = key_type_arg;
5143 psa_algorithm_t alg = alg_arg;
5144 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5145
5146 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
5147
5148 PSA_ASSERT( psa_crypto_init( ) );
5149
5150 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5151 psa_set_key_algorithm( &attributes, alg );
5152 psa_set_key_type( &attributes, key_type );
5153
5154 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5155 &key ) );
5156
5157 PSA_ASSERT( psa_verify_message( key, alg,
5158 input_data->x, input_data->len,
5159 signature_data->x, signature_data->len ) );
5160
5161exit:
5162 psa_reset_key_attributes( &attributes );
5163 psa_destroy_key( key );
5164 PSA_DONE( );
5165}
5166/* END_CASE */
5167
5168/* BEGIN_CASE */
5169void verify_message_fail( int key_type_arg,
5170 data_t *key_data,
5171 int alg_arg,
5172 data_t *hash_data,
5173 data_t *signature_data,
5174 int expected_status_arg )
5175{
5176 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5177 psa_key_type_t key_type = key_type_arg;
5178 psa_algorithm_t alg = alg_arg;
5179 psa_status_t actual_status;
5180 psa_status_t expected_status = expected_status_arg;
5181 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5182
5183 PSA_ASSERT( psa_crypto_init( ) );
5184
5185 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5186 psa_set_key_algorithm( &attributes, alg );
5187 psa_set_key_type( &attributes, key_type );
5188
5189 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5190 &key ) );
5191
5192 actual_status = psa_verify_message( key, alg,
5193 hash_data->x, hash_data->len,
5194 signature_data->x,
5195 signature_data->len );
5196 TEST_EQUAL( actual_status, expected_status );
5197
5198exit:
5199 psa_reset_key_attributes( &attributes );
5200 psa_destroy_key( key );
5201 PSA_DONE( );
5202}
5203/* END_CASE */
5204
5205/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02005206void asymmetric_encrypt( int key_type_arg,
5207 data_t *key_data,
5208 int alg_arg,
5209 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02005210 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02005211 int expected_output_length_arg,
5212 int expected_status_arg )
5213{
Ronald Cron5425a212020-08-04 14:58:35 +02005214 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005215 psa_key_type_t key_type = key_type_arg;
5216 psa_algorithm_t alg = alg_arg;
5217 size_t expected_output_length = expected_output_length_arg;
5218 size_t key_bits;
5219 unsigned char *output = NULL;
5220 size_t output_size;
5221 size_t output_length = ~0;
5222 psa_status_t actual_status;
5223 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005224 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005225
Gilles Peskine8817f612018-12-18 00:18:46 +01005226 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005227
Gilles Peskine656896e2018-06-29 19:12:28 +02005228 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005229 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5230 psa_set_key_algorithm( &attributes, alg );
5231 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005232 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005233 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02005234
5235 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02005236 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005237 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005238
Gilles Peskine656896e2018-06-29 19:12:28 +02005239 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005240 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005241 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02005242
5243 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02005244 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02005245 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005246 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02005247 output, output_size,
5248 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005249 TEST_EQUAL( actual_status, expected_status );
5250 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02005251
Gilles Peskine68428122018-06-30 18:42:41 +02005252 /* If the label is empty, the test framework puts a non-null pointer
5253 * in label->x. Test that a null pointer works as well. */
5254 if( label->len == 0 )
5255 {
5256 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005257 if( output_size != 0 )
5258 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005259 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005260 input_data->x, input_data->len,
5261 NULL, label->len,
5262 output, output_size,
5263 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005264 TEST_EQUAL( actual_status, expected_status );
5265 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005266 }
5267
Gilles Peskine656896e2018-06-29 19:12:28 +02005268exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005269 /*
5270 * Key attributes may have been returned by psa_get_key_attributes()
5271 * thus reset them as required.
5272 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005273 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005274
Ronald Cron5425a212020-08-04 14:58:35 +02005275 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02005276 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005277 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02005278}
5279/* END_CASE */
5280
5281/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005282void asymmetric_encrypt_decrypt( int key_type_arg,
5283 data_t *key_data,
5284 int alg_arg,
5285 data_t *input_data,
5286 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005287{
Ronald Cron5425a212020-08-04 14:58:35 +02005288 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005289 psa_key_type_t key_type = key_type_arg;
5290 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005291 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005292 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005293 size_t output_size;
5294 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005295 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005296 size_t output2_size;
5297 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005298 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005299
Gilles Peskine8817f612018-12-18 00:18:46 +01005300 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005301
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005302 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5303 psa_set_key_algorithm( &attributes, alg );
5304 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005305
Gilles Peskine049c7532019-05-15 20:22:09 +02005306 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005307 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005308
5309 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02005310 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005311 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005312
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005313 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005314 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005315 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01005316
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005317 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01005318 TEST_ASSERT( output2_size <=
5319 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
5320 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005321 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005322
Gilles Peskineeebd7382018-06-08 18:11:54 +02005323 /* We test encryption by checking that encrypt-then-decrypt gives back
5324 * the original plaintext because of the non-optional random
5325 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02005326 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005327 input_data->x, input_data->len,
5328 label->x, label->len,
5329 output, output_size,
5330 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005331 /* We don't know what ciphertext length to expect, but check that
5332 * it looks sensible. */
5333 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005334
Ronald Cron5425a212020-08-04 14:58:35 +02005335 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005336 output, output_length,
5337 label->x, label->len,
5338 output2, output2_size,
5339 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005340 ASSERT_COMPARE( input_data->x, input_data->len,
5341 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005342
5343exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005344 /*
5345 * Key attributes may have been returned by psa_get_key_attributes()
5346 * thus reset them as required.
5347 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005348 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005349
Ronald Cron5425a212020-08-04 14:58:35 +02005350 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005351 mbedtls_free( output );
5352 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005353 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005354}
5355/* END_CASE */
5356
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005357/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005358void asymmetric_decrypt( int key_type_arg,
5359 data_t *key_data,
5360 int alg_arg,
5361 data_t *input_data,
5362 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02005363 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005364{
Ronald Cron5425a212020-08-04 14:58:35 +02005365 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005366 psa_key_type_t key_type = key_type_arg;
5367 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01005368 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005369 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03005370 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005371 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005372 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005373
Gilles Peskine8817f612018-12-18 00:18:46 +01005374 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005375
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005376 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5377 psa_set_key_algorithm( &attributes, alg );
5378 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005379
Gilles Peskine049c7532019-05-15 20:22:09 +02005380 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005381 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005382
gabor-mezei-armceface22021-01-21 12:26:17 +01005383 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5384 key_bits = psa_get_key_bits( &attributes );
5385
5386 /* Determine the maximum ciphertext length */
5387 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
5388 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
5389 ASSERT_ALLOC( output, output_size );
5390
Ronald Cron5425a212020-08-04 14:58:35 +02005391 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005392 input_data->x, input_data->len,
5393 label->x, label->len,
5394 output,
5395 output_size,
5396 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005397 ASSERT_COMPARE( expected_data->x, expected_data->len,
5398 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005399
Gilles Peskine68428122018-06-30 18:42:41 +02005400 /* If the label is empty, the test framework puts a non-null pointer
5401 * in label->x. Test that a null pointer works as well. */
5402 if( label->len == 0 )
5403 {
5404 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005405 if( output_size != 0 )
5406 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005407 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005408 input_data->x, input_data->len,
5409 NULL, label->len,
5410 output,
5411 output_size,
5412 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005413 ASSERT_COMPARE( expected_data->x, expected_data->len,
5414 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005415 }
5416
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005417exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005418 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005419 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005420 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005421 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005422}
5423/* END_CASE */
5424
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005425/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005426void asymmetric_decrypt_fail( int key_type_arg,
5427 data_t *key_data,
5428 int alg_arg,
5429 data_t *input_data,
5430 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00005431 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02005432 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005433{
Ronald Cron5425a212020-08-04 14:58:35 +02005434 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005435 psa_key_type_t key_type = key_type_arg;
5436 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005437 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00005438 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005439 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005440 psa_status_t actual_status;
5441 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005442 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005443
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005444 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005445
Gilles Peskine8817f612018-12-18 00:18:46 +01005446 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005447
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005448 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5449 psa_set_key_algorithm( &attributes, alg );
5450 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005451
Gilles Peskine049c7532019-05-15 20:22:09 +02005452 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005453 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005454
Ronald Cron5425a212020-08-04 14:58:35 +02005455 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005456 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005457 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005458 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02005459 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005460 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005461 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005462
Gilles Peskine68428122018-06-30 18:42:41 +02005463 /* If the label is empty, the test framework puts a non-null pointer
5464 * in label->x. Test that a null pointer works as well. */
5465 if( label->len == 0 )
5466 {
5467 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005468 if( output_size != 0 )
5469 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005470 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005471 input_data->x, input_data->len,
5472 NULL, label->len,
5473 output, output_size,
5474 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005475 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005476 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02005477 }
5478
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005479exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005480 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005481 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02005482 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005483 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005484}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005485/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02005486
5487/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005488void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00005489{
5490 /* Test each valid way of initializing the object, except for `= {0}`, as
5491 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
5492 * though it's OK by the C standard. We could test for this, but we'd need
5493 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005494 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005495 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
5496 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
5497 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00005498
5499 memset( &zero, 0, sizeof( zero ) );
5500
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005501 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005502 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005503 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005504 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005505 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005506 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005507 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005508
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005509 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005510 PSA_ASSERT( psa_key_derivation_abort(&func) );
5511 PSA_ASSERT( psa_key_derivation_abort(&init) );
5512 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00005513}
5514/* END_CASE */
5515
Janos Follath16de4a42019-06-13 16:32:24 +01005516/* BEGIN_CASE */
5517void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005518{
Gilles Peskineea0fb492018-07-12 17:17:20 +02005519 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005520 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005521 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005522
Gilles Peskine8817f612018-12-18 00:18:46 +01005523 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005524
Janos Follath16de4a42019-06-13 16:32:24 +01005525 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005526 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005527
5528exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005529 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005530 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005531}
5532/* END_CASE */
5533
Janos Follathaf3c2a02019-06-12 12:34:34 +01005534/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01005535void derive_set_capacity( int alg_arg, int capacity_arg,
5536 int expected_status_arg )
5537{
5538 psa_algorithm_t alg = alg_arg;
5539 size_t capacity = capacity_arg;
5540 psa_status_t expected_status = expected_status_arg;
5541 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5542
5543 PSA_ASSERT( psa_crypto_init( ) );
5544
5545 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5546
5547 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
5548 expected_status );
5549
5550exit:
5551 psa_key_derivation_abort( &operation );
5552 PSA_DONE( );
5553}
5554/* END_CASE */
5555
5556/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01005557void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02005558 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005559 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02005560 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005561 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02005562 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005563 int expected_status_arg3,
5564 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005565{
5566 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02005567 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
5568 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01005569 psa_status_t expected_statuses[] = {expected_status_arg1,
5570 expected_status_arg2,
5571 expected_status_arg3};
5572 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005573 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5574 MBEDTLS_SVC_KEY_ID_INIT,
5575 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01005576 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5577 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5578 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005579 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005580 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005581 psa_status_t expected_output_status = expected_output_status_arg;
5582 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01005583
5584 PSA_ASSERT( psa_crypto_init( ) );
5585
5586 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5587 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005588
5589 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5590
5591 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
5592 {
Gilles Peskineb8965192019-09-24 16:21:10 +02005593 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005594 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02005595 psa_set_key_type( &attributes, key_types[i] );
5596 PSA_ASSERT( psa_import_key( &attributes,
5597 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005598 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005599 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
5600 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
5601 {
5602 // When taking a private key as secret input, use key agreement
5603 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005604 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
5605 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005606 expected_statuses[i] );
5607 }
5608 else
5609 {
5610 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02005611 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005612 expected_statuses[i] );
5613 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02005614 }
5615 else
5616 {
5617 TEST_EQUAL( psa_key_derivation_input_bytes(
5618 &operation, steps[i],
5619 inputs[i]->x, inputs[i]->len ),
5620 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005621 }
5622 }
5623
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005624 if( output_key_type != PSA_KEY_TYPE_NONE )
5625 {
5626 psa_reset_key_attributes( &attributes );
5627 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
5628 psa_set_key_bits( &attributes, 8 );
5629 actual_output_status =
5630 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005631 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005632 }
5633 else
5634 {
5635 uint8_t buffer[1];
5636 actual_output_status =
5637 psa_key_derivation_output_bytes( &operation,
5638 buffer, sizeof( buffer ) );
5639 }
5640 TEST_EQUAL( actual_output_status, expected_output_status );
5641
Janos Follathaf3c2a02019-06-12 12:34:34 +01005642exit:
5643 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005644 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5645 psa_destroy_key( keys[i] );
5646 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005647 PSA_DONE( );
5648}
5649/* END_CASE */
5650
Janos Follathd958bb72019-07-03 15:02:16 +01005651/* BEGIN_CASE */
5652void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005653{
Janos Follathd958bb72019-07-03 15:02:16 +01005654 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005655 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02005656 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005657 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01005658 unsigned char input1[] = "Input 1";
5659 size_t input1_length = sizeof( input1 );
5660 unsigned char input2[] = "Input 2";
5661 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005662 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02005663 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02005664 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5665 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5666 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005667 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005668
Gilles Peskine8817f612018-12-18 00:18:46 +01005669 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005670
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005671 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5672 psa_set_key_algorithm( &attributes, alg );
5673 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005674
Gilles Peskine73676cb2019-05-15 20:15:10 +02005675 PSA_ASSERT( psa_import_key( &attributes,
5676 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02005677 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005678
5679 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005680 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5681 input1, input1_length,
5682 input2, input2_length,
5683 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01005684 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005685
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005686 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01005687 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005688 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005689
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005690 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005691
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005692 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02005693 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005694
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005695exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005696 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005697 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005698 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005699}
5700/* END_CASE */
5701
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005702/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005703void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005704{
5705 uint8_t output_buffer[16];
5706 size_t buffer_size = 16;
5707 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005708 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005709
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005710 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5711 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005712 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005713
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005714 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005715 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005716
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005717 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005718
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005719 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5720 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005721 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005722
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005723 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005724 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005725
5726exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005727 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005728}
5729/* END_CASE */
5730
5731/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005732void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02005733 int step1_arg, data_t *input1,
5734 int step2_arg, data_t *input2,
5735 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005736 int requested_capacity_arg,
5737 data_t *expected_output1,
5738 data_t *expected_output2 )
5739{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005740 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005741 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5742 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005743 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5744 MBEDTLS_SVC_KEY_ID_INIT,
5745 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005746 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005747 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005748 uint8_t *expected_outputs[2] =
5749 {expected_output1->x, expected_output2->x};
5750 size_t output_sizes[2] =
5751 {expected_output1->len, expected_output2->len};
5752 size_t output_buffer_size = 0;
5753 uint8_t *output_buffer = NULL;
5754 size_t expected_capacity;
5755 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005756 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005757 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005758 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005759
5760 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5761 {
5762 if( output_sizes[i] > output_buffer_size )
5763 output_buffer_size = output_sizes[i];
5764 if( output_sizes[i] == 0 )
5765 expected_outputs[i] = NULL;
5766 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005767 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005768 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005769
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005770 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5771 psa_set_key_algorithm( &attributes, alg );
5772 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005773
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005774 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005775 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5776 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5777 requested_capacity ) );
5778 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005779 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005780 switch( steps[i] )
5781 {
5782 case 0:
5783 break;
5784 case PSA_KEY_DERIVATION_INPUT_SECRET:
5785 PSA_ASSERT( psa_import_key( &attributes,
5786 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005787 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01005788
5789 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5790 {
5791 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
5792 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
5793 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
5794 }
5795
Gilles Peskine1468da72019-05-29 17:35:49 +02005796 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005797 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005798 break;
5799 default:
5800 PSA_ASSERT( psa_key_derivation_input_bytes(
5801 &operation, steps[i],
5802 inputs[i]->x, inputs[i]->len ) );
5803 break;
5804 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005805 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005806
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005807 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005808 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005809 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005810 expected_capacity = requested_capacity;
5811
5812 /* Expansion phase. */
5813 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5814 {
5815 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005816 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005817 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005818 if( expected_capacity == 0 && output_sizes[i] == 0 )
5819 {
5820 /* Reading 0 bytes when 0 bytes are available can go either way. */
5821 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005822 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005823 continue;
5824 }
5825 else if( expected_capacity == 0 ||
5826 output_sizes[i] > expected_capacity )
5827 {
5828 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005829 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005830 expected_capacity = 0;
5831 continue;
5832 }
5833 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005834 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005835 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005836 ASSERT_COMPARE( output_buffer, output_sizes[i],
5837 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005838 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005839 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005840 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005841 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005842 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005843 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005844 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005845
5846exit:
5847 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005848 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005849 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5850 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005851 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005852}
5853/* END_CASE */
5854
5855/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005856void derive_full( int alg_arg,
5857 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005858 data_t *input1,
5859 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005860 int requested_capacity_arg )
5861{
Ronald Cron5425a212020-08-04 14:58:35 +02005862 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005863 psa_algorithm_t alg = alg_arg;
5864 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005865 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005866 unsigned char output_buffer[16];
5867 size_t expected_capacity = requested_capacity;
5868 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005869 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005870
Gilles Peskine8817f612018-12-18 00:18:46 +01005871 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005872
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005873 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5874 psa_set_key_algorithm( &attributes, alg );
5875 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005876
Gilles Peskine049c7532019-05-15 20:22:09 +02005877 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005878 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005879
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005880 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5881 input1->x, input1->len,
5882 input2->x, input2->len,
5883 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01005884 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005885
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005886 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005887 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005888 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005889
5890 /* Expansion phase. */
5891 while( current_capacity > 0 )
5892 {
5893 size_t read_size = sizeof( output_buffer );
5894 if( read_size > current_capacity )
5895 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005896 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005897 output_buffer,
5898 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005899 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005900 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005901 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005902 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005903 }
5904
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005905 /* Check that the operation refuses to go over capacity. */
5906 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005907 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005908
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005909 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005910
5911exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005912 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005913 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005914 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005915}
5916/* END_CASE */
5917
Janos Follathe60c9052019-07-03 13:51:30 +01005918/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005919void derive_key_exercise( int alg_arg,
5920 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005921 data_t *input1,
5922 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005923 int derived_type_arg,
5924 int derived_bits_arg,
5925 int derived_usage_arg,
5926 int derived_alg_arg )
5927{
Ronald Cron5425a212020-08-04 14:58:35 +02005928 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5929 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005930 psa_algorithm_t alg = alg_arg;
5931 psa_key_type_t derived_type = derived_type_arg;
5932 size_t derived_bits = derived_bits_arg;
5933 psa_key_usage_t derived_usage = derived_usage_arg;
5934 psa_algorithm_t derived_alg = derived_alg_arg;
5935 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005936 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005937 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005938 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005939
Gilles Peskine8817f612018-12-18 00:18:46 +01005940 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005941
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005942 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5943 psa_set_key_algorithm( &attributes, alg );
5944 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005945 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005946 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005947
5948 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005949 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5950 input1->x, input1->len,
5951 input2->x, input2->len,
5952 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01005953 goto exit;
5954
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005955 psa_set_key_usage_flags( &attributes, derived_usage );
5956 psa_set_key_algorithm( &attributes, derived_alg );
5957 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005958 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005959 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005960 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005961
5962 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005963 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005964 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5965 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005966
5967 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005968 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005969 goto exit;
5970
5971exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005972 /*
5973 * Key attributes may have been returned by psa_get_key_attributes()
5974 * thus reset them as required.
5975 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005976 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005977
5978 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005979 psa_destroy_key( base_key );
5980 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005981 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005982}
5983/* END_CASE */
5984
Janos Follath42fd8882019-07-03 14:17:09 +01005985/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005986void derive_key_export( int alg_arg,
5987 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005988 data_t *input1,
5989 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005990 int bytes1_arg,
5991 int bytes2_arg )
5992{
Ronald Cron5425a212020-08-04 14:58:35 +02005993 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5994 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005995 psa_algorithm_t alg = alg_arg;
5996 size_t bytes1 = bytes1_arg;
5997 size_t bytes2 = bytes2_arg;
5998 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005999 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006000 uint8_t *output_buffer = NULL;
6001 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006002 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6003 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006004 size_t length;
6005
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006006 ASSERT_ALLOC( output_buffer, capacity );
6007 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01006008 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006009
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006010 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6011 psa_set_key_algorithm( &base_attributes, alg );
6012 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006013 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006014 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006015
6016 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006017 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6018 input1->x, input1->len,
6019 input2->x, input2->len,
6020 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006021 goto exit;
6022
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006023 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006024 output_buffer,
6025 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006026 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006027
6028 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006029 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6030 input1->x, input1->len,
6031 input2->x, input2->len,
6032 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006033 goto exit;
6034
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006035 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6036 psa_set_key_algorithm( &derived_attributes, 0 );
6037 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006038 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006039 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006040 &derived_key ) );
6041 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006042 export_buffer, bytes1,
6043 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006044 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02006045 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006046 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006047 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006048 &derived_key ) );
6049 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006050 export_buffer + bytes1, bytes2,
6051 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006052 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006053
6054 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006055 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
6056 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006057
6058exit:
6059 mbedtls_free( output_buffer );
6060 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006061 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006062 psa_destroy_key( base_key );
6063 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006064 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006065}
6066/* END_CASE */
6067
6068/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006069void derive_key( int alg_arg,
6070 data_t *key_data, data_t *input1, data_t *input2,
6071 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006072 int expected_status_arg,
6073 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02006074{
Ronald Cron5425a212020-08-04 14:58:35 +02006075 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6076 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02006077 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006078 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02006079 size_t bits = bits_arg;
6080 psa_status_t expected_status = expected_status_arg;
6081 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6082 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6083 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
6084
6085 PSA_ASSERT( psa_crypto_init( ) );
6086
6087 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6088 psa_set_key_algorithm( &base_attributes, alg );
6089 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
6090 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006091 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02006092
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006093 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6094 input1->x, input1->len,
6095 input2->x, input2->len,
6096 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02006097 goto exit;
6098
6099 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6100 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006101 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02006102 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01006103
6104 psa_status_t status =
6105 psa_key_derivation_output_key( &derived_attributes,
6106 &operation,
6107 &derived_key );
6108 if( is_large_output > 0 )
6109 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6110 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02006111
6112exit:
6113 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006114 psa_destroy_key( base_key );
6115 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02006116 PSA_DONE( );
6117}
6118/* END_CASE */
6119
6120/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006121void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02006122 int our_key_type_arg, int our_key_alg_arg,
6123 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006124 int expected_status_arg )
6125{
Ronald Cron5425a212020-08-04 14:58:35 +02006126 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006127 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006128 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006129 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006130 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006131 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02006132 psa_status_t expected_status = expected_status_arg;
6133 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006134
Gilles Peskine8817f612018-12-18 00:18:46 +01006135 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006136
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006137 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006138 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006139 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006140 PSA_ASSERT( psa_import_key( &attributes,
6141 our_key_data->x, our_key_data->len,
6142 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006143
Gilles Peskine77f40d82019-04-11 21:27:06 +02006144 /* The tests currently include inputs that should fail at either step.
6145 * Test cases that fail at the setup step should be changed to call
6146 * key_derivation_setup instead, and this function should be renamed
6147 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006148 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02006149 if( status == PSA_SUCCESS )
6150 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006151 TEST_EQUAL( psa_key_derivation_key_agreement(
6152 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
6153 our_key,
6154 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02006155 expected_status );
6156 }
6157 else
6158 {
6159 TEST_ASSERT( status == expected_status );
6160 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02006161
6162exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006163 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006164 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006165 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006166}
6167/* END_CASE */
6168
6169/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02006170void raw_key_agreement( int alg_arg,
6171 int our_key_type_arg, data_t *our_key_data,
6172 data_t *peer_key_data,
6173 data_t *expected_output )
6174{
Ronald Cron5425a212020-08-04 14:58:35 +02006175 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006176 psa_algorithm_t alg = alg_arg;
6177 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006178 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006179 unsigned char *output = NULL;
6180 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01006181 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006182
6183 ASSERT_ALLOC( output, expected_output->len );
6184 PSA_ASSERT( psa_crypto_init( ) );
6185
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006186 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6187 psa_set_key_algorithm( &attributes, alg );
6188 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006189 PSA_ASSERT( psa_import_key( &attributes,
6190 our_key_data->x, our_key_data->len,
6191 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006192
gabor-mezei-armceface22021-01-21 12:26:17 +01006193 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
6194 key_bits = psa_get_key_bits( &attributes );
6195
Gilles Peskinebe697d82019-05-16 18:00:41 +02006196 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
6197 peer_key_data->x, peer_key_data->len,
6198 output, expected_output->len,
6199 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006200 ASSERT_COMPARE( output, output_length,
6201 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01006202 TEST_ASSERT( output_length <=
6203 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
6204 TEST_ASSERT( output_length <=
6205 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006206
6207exit:
6208 mbedtls_free( output );
6209 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006210 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006211}
6212/* END_CASE */
6213
6214/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02006215void key_agreement_capacity( int alg_arg,
6216 int our_key_type_arg, data_t *our_key_data,
6217 data_t *peer_key_data,
6218 int expected_capacity_arg )
6219{
Ronald Cron5425a212020-08-04 14:58:35 +02006220 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006221 psa_algorithm_t alg = alg_arg;
6222 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006223 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006224 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006225 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02006226 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02006227
Gilles Peskine8817f612018-12-18 00:18:46 +01006228 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006229
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006230 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6231 psa_set_key_algorithm( &attributes, alg );
6232 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006233 PSA_ASSERT( psa_import_key( &attributes,
6234 our_key_data->x, our_key_data->len,
6235 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006236
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006237 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006238 PSA_ASSERT( psa_key_derivation_key_agreement(
6239 &operation,
6240 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6241 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006242 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6243 {
6244 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006245 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006246 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006247 NULL, 0 ) );
6248 }
Gilles Peskine59685592018-09-18 12:11:34 +02006249
Gilles Peskinebf491972018-10-25 22:36:12 +02006250 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006251 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006252 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006253 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02006254
Gilles Peskinebf491972018-10-25 22:36:12 +02006255 /* Test the actual capacity by reading the output. */
6256 while( actual_capacity > sizeof( output ) )
6257 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006258 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006259 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02006260 actual_capacity -= sizeof( output );
6261 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006262 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006263 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006264 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006265 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02006266
Gilles Peskine59685592018-09-18 12:11:34 +02006267exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006268 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006269 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006270 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006271}
6272/* END_CASE */
6273
6274/* BEGIN_CASE */
6275void key_agreement_output( int alg_arg,
6276 int our_key_type_arg, data_t *our_key_data,
6277 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006278 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02006279{
Ronald Cron5425a212020-08-04 14:58:35 +02006280 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006281 psa_algorithm_t alg = alg_arg;
6282 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006283 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006284 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006285 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02006286
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006287 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
6288 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006289
Gilles Peskine8817f612018-12-18 00:18:46 +01006290 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006291
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006292 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6293 psa_set_key_algorithm( &attributes, alg );
6294 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006295 PSA_ASSERT( psa_import_key( &attributes,
6296 our_key_data->x, our_key_data->len,
6297 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006298
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006299 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006300 PSA_ASSERT( psa_key_derivation_key_agreement(
6301 &operation,
6302 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6303 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006304 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6305 {
6306 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006307 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006308 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006309 NULL, 0 ) );
6310 }
Gilles Peskine59685592018-09-18 12:11:34 +02006311
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006312 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006313 actual_output,
6314 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006315 ASSERT_COMPARE( actual_output, expected_output1->len,
6316 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006317 if( expected_output2->len != 0 )
6318 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006319 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006320 actual_output,
6321 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006322 ASSERT_COMPARE( actual_output, expected_output2->len,
6323 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006324 }
Gilles Peskine59685592018-09-18 12:11:34 +02006325
6326exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006327 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006328 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006329 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006330 mbedtls_free( actual_output );
6331}
6332/* END_CASE */
6333
6334/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02006335void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02006336{
Gilles Peskinea50d7392018-06-21 10:22:13 +02006337 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006338 unsigned char *output = NULL;
6339 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02006340 size_t i;
6341 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02006342
Simon Butcher49f8e312020-03-03 15:51:50 +00006343 TEST_ASSERT( bytes_arg >= 0 );
6344
Gilles Peskine91892022021-02-08 19:50:26 +01006345 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006346 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02006347
Gilles Peskine8817f612018-12-18 00:18:46 +01006348 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02006349
Gilles Peskinea50d7392018-06-21 10:22:13 +02006350 /* Run several times, to ensure that every output byte will be
6351 * nonzero at least once with overwhelming probability
6352 * (2^(-8*number_of_runs)). */
6353 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02006354 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006355 if( bytes != 0 )
6356 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01006357 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006358
Gilles Peskinea50d7392018-06-21 10:22:13 +02006359 for( i = 0; i < bytes; i++ )
6360 {
6361 if( output[i] != 0 )
6362 ++changed[i];
6363 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006364 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02006365
6366 /* Check that every byte was changed to nonzero at least once. This
6367 * validates that psa_generate_random is overwriting every byte of
6368 * the output buffer. */
6369 for( i = 0; i < bytes; i++ )
6370 {
6371 TEST_ASSERT( changed[i] != 0 );
6372 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006373
6374exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006375 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006376 mbedtls_free( output );
6377 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02006378}
6379/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02006380
6381/* BEGIN_CASE */
6382void generate_key( int type_arg,
6383 int bits_arg,
6384 int usage_arg,
6385 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006386 int expected_status_arg,
6387 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006388{
Ronald Cron5425a212020-08-04 14:58:35 +02006389 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006390 psa_key_type_t type = type_arg;
6391 psa_key_usage_t usage = usage_arg;
6392 size_t bits = bits_arg;
6393 psa_algorithm_t alg = alg_arg;
6394 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006395 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006396 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006397
Gilles Peskine8817f612018-12-18 00:18:46 +01006398 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006399
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006400 psa_set_key_usage_flags( &attributes, usage );
6401 psa_set_key_algorithm( &attributes, alg );
6402 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006403 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006404
6405 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01006406 psa_status_t status = psa_generate_key( &attributes, &key );
6407
6408 if( is_large_key > 0 )
6409 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6410 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006411 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006412 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006413
6414 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006415 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006416 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
6417 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006418
Gilles Peskine818ca122018-06-20 18:16:48 +02006419 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006420 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02006421 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006422
6423exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006424 /*
6425 * Key attributes may have been returned by psa_get_key_attributes()
6426 * thus reset them as required.
6427 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006428 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006429
Ronald Cron5425a212020-08-04 14:58:35 +02006430 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006431 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006432}
6433/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03006434
Ronald Cronee414c72021-03-18 18:50:08 +01006435/* 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 +02006436void generate_key_rsa( int bits_arg,
6437 data_t *e_arg,
6438 int expected_status_arg )
6439{
Ronald Cron5425a212020-08-04 14:58:35 +02006440 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006441 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006442 size_t bits = bits_arg;
6443 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
6444 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
6445 psa_status_t expected_status = expected_status_arg;
6446 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6447 uint8_t *exported = NULL;
6448 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006449 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006450 size_t exported_length = SIZE_MAX;
6451 uint8_t *e_read_buffer = NULL;
6452 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02006453 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006454 size_t e_read_length = SIZE_MAX;
6455
6456 if( e_arg->len == 0 ||
6457 ( e_arg->len == 3 &&
6458 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
6459 {
6460 is_default_public_exponent = 1;
6461 e_read_size = 0;
6462 }
6463 ASSERT_ALLOC( e_read_buffer, e_read_size );
6464 ASSERT_ALLOC( exported, exported_size );
6465
6466 PSA_ASSERT( psa_crypto_init( ) );
6467
6468 psa_set_key_usage_flags( &attributes, usage );
6469 psa_set_key_algorithm( &attributes, alg );
6470 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
6471 e_arg->x, e_arg->len ) );
6472 psa_set_key_bits( &attributes, bits );
6473
6474 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006475 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006476 if( expected_status != PSA_SUCCESS )
6477 goto exit;
6478
6479 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006480 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006481 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6482 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6483 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
6484 e_read_buffer, e_read_size,
6485 &e_read_length ) );
6486 if( is_default_public_exponent )
6487 TEST_EQUAL( e_read_length, 0 );
6488 else
6489 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
6490
6491 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006492 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02006493 goto exit;
6494
6495 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02006496 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006497 exported, exported_size,
6498 &exported_length ) );
6499 {
6500 uint8_t *p = exported;
6501 uint8_t *end = exported + exported_length;
6502 size_t len;
6503 /* RSAPublicKey ::= SEQUENCE {
6504 * modulus INTEGER, -- n
6505 * publicExponent INTEGER } -- e
6506 */
6507 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006508 MBEDTLS_ASN1_SEQUENCE |
6509 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01006510 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006511 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
6512 MBEDTLS_ASN1_INTEGER ) );
6513 if( len >= 1 && p[0] == 0 )
6514 {
6515 ++p;
6516 --len;
6517 }
6518 if( e_arg->len == 0 )
6519 {
6520 TEST_EQUAL( len, 3 );
6521 TEST_EQUAL( p[0], 1 );
6522 TEST_EQUAL( p[1], 0 );
6523 TEST_EQUAL( p[2], 1 );
6524 }
6525 else
6526 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
6527 }
6528
6529exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006530 /*
6531 * Key attributes may have been returned by psa_get_key_attributes() or
6532 * set by psa_set_key_domain_parameters() thus reset them as required.
6533 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006534 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006535
Ronald Cron5425a212020-08-04 14:58:35 +02006536 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006537 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006538 mbedtls_free( e_read_buffer );
6539 mbedtls_free( exported );
6540}
6541/* END_CASE */
6542
Darryl Greend49a4992018-06-18 17:27:26 +01006543/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006544void persistent_key_load_key_from_storage( data_t *data,
6545 int type_arg, int bits_arg,
6546 int usage_flags_arg, int alg_arg,
6547 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01006548{
Ronald Cron71016a92020-08-28 19:01:50 +02006549 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006550 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02006551 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6552 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006553 psa_key_type_t type = type_arg;
6554 size_t bits = bits_arg;
6555 psa_key_usage_t usage_flags = usage_flags_arg;
6556 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006557 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01006558 unsigned char *first_export = NULL;
6559 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006560 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006561 size_t first_exported_length;
6562 size_t second_exported_length;
6563
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006564 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6565 {
6566 ASSERT_ALLOC( first_export, export_size );
6567 ASSERT_ALLOC( second_export, export_size );
6568 }
Darryl Greend49a4992018-06-18 17:27:26 +01006569
Gilles Peskine8817f612018-12-18 00:18:46 +01006570 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006571
Gilles Peskinec87af662019-05-15 16:12:22 +02006572 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006573 psa_set_key_usage_flags( &attributes, usage_flags );
6574 psa_set_key_algorithm( &attributes, alg );
6575 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006576 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006577
Darryl Green0c6575a2018-11-07 16:05:30 +00006578 switch( generation_method )
6579 {
6580 case IMPORT_KEY:
6581 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02006582 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006583 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006584 break;
Darryl Greend49a4992018-06-18 17:27:26 +01006585
Darryl Green0c6575a2018-11-07 16:05:30 +00006586 case GENERATE_KEY:
6587 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006588 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006589 break;
6590
6591 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01006592#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006593 {
6594 /* Create base key */
6595 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
6596 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6597 psa_set_key_usage_flags( &base_attributes,
6598 PSA_KEY_USAGE_DERIVE );
6599 psa_set_key_algorithm( &base_attributes, derive_alg );
6600 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006601 PSA_ASSERT( psa_import_key( &base_attributes,
6602 data->x, data->len,
6603 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006604 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006605 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006606 PSA_ASSERT( psa_key_derivation_input_key(
6607 &operation,
6608 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006609 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006610 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006611 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006612 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
6613 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006614 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006615 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006616 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02006617 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006618 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01006619#else
6620 TEST_ASSUME( ! "KDF not supported in this configuration" );
6621#endif
6622 break;
6623
6624 default:
6625 TEST_ASSERT( ! "generation_method not implemented in test" );
6626 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00006627 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006628 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01006629
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006630 /* Export the key if permitted by the key policy. */
6631 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6632 {
Ronald Cron5425a212020-08-04 14:58:35 +02006633 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006634 first_export, export_size,
6635 &first_exported_length ) );
6636 if( generation_method == IMPORT_KEY )
6637 ASSERT_COMPARE( data->x, data->len,
6638 first_export, first_exported_length );
6639 }
Darryl Greend49a4992018-06-18 17:27:26 +01006640
6641 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02006642 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006643 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01006644 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006645
Darryl Greend49a4992018-06-18 17:27:26 +01006646 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02006647 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02006648 TEST_ASSERT( mbedtls_svc_key_id_equal(
6649 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006650 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
6651 PSA_KEY_LIFETIME_PERSISTENT );
6652 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6653 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6654 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
6655 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01006656
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006657 /* Export the key again if permitted by the key policy. */
6658 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00006659 {
Ronald Cron5425a212020-08-04 14:58:35 +02006660 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006661 second_export, export_size,
6662 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006663 ASSERT_COMPARE( first_export, first_exported_length,
6664 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006665 }
6666
6667 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006668 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00006669 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01006670
6671exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006672 /*
6673 * Key attributes may have been returned by psa_get_key_attributes()
6674 * thus reset them as required.
6675 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006676 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006677
Darryl Greend49a4992018-06-18 17:27:26 +01006678 mbedtls_free( first_export );
6679 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006680 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006681 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02006682 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006683 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01006684}
6685/* END_CASE */