blob: cd97c506305bf569a59722352b69e02d472c6cc0 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010018
Jaeden Amerof24c7f82018-06-27 17:20:43 +010019/** An invalid export length that will never be set by psa_export_key(). */
20static const size_t INVALID_EXPORT_LENGTH = ~0U;
21
Gilles Peskinea7aa4422018-08-14 15:17:54 +020022/** Test if a buffer contains a constant byte value.
23 *
24 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020025 *
26 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020027 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020028 * \param size Size of the buffer in bytes.
29 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020030 * \return 1 if the buffer is all-bits-zero.
31 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020032 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020033static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020034{
35 size_t i;
36 for( i = 0; i < size; i++ )
37 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020038 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020039 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020040 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020041 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042}
Gilles Peskine818ca122018-06-20 18:16:48 +020043
Gilles Peskine0b352bc2018-06-28 00:16:11 +020044/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
45static int asn1_write_10x( unsigned char **p,
46 unsigned char *start,
47 size_t bits,
48 unsigned char x )
49{
50 int ret;
51 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020052 if( bits == 0 )
53 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
54 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020055 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030056 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020057 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
58 *p -= len;
59 ( *p )[len-1] = x;
60 if( bits % 8 == 0 )
61 ( *p )[1] |= 1;
62 else
63 ( *p )[0] |= 1 << ( bits % 8 );
64 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
65 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
66 MBEDTLS_ASN1_INTEGER ) );
67 return( len );
68}
69
70static int construct_fake_rsa_key( unsigned char *buffer,
71 size_t buffer_size,
72 unsigned char **p,
73 size_t bits,
74 int keypair )
75{
76 size_t half_bits = ( bits + 1 ) / 2;
77 int ret;
78 int len = 0;
79 /* Construct something that looks like a DER encoding of
80 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
81 * RSAPrivateKey ::= SEQUENCE {
82 * version Version,
83 * modulus INTEGER, -- n
84 * publicExponent INTEGER, -- e
85 * privateExponent INTEGER, -- d
86 * prime1 INTEGER, -- p
87 * prime2 INTEGER, -- q
88 * exponent1 INTEGER, -- d mod (p-1)
89 * exponent2 INTEGER, -- d mod (q-1)
90 * coefficient INTEGER, -- (inverse of q) mod p
91 * otherPrimeInfos OtherPrimeInfos OPTIONAL
92 * }
93 * Or, for a public key, the same structure with only
94 * version, modulus and publicExponent.
95 */
96 *p = buffer + buffer_size;
97 if( keypair )
98 {
99 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
100 asn1_write_10x( p, buffer, half_bits, 1 ) );
101 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
102 asn1_write_10x( p, buffer, half_bits, 1 ) );
103 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
104 asn1_write_10x( p, buffer, half_bits, 1 ) );
105 MBEDTLS_ASN1_CHK_ADD( len, /* q */
106 asn1_write_10x( p, buffer, half_bits, 1 ) );
107 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
108 asn1_write_10x( p, buffer, half_bits, 3 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* d */
110 asn1_write_10x( p, buffer, bits, 1 ) );
111 }
112 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
113 asn1_write_10x( p, buffer, 17, 1 ) );
114 MBEDTLS_ASN1_CHK_ADD( len, /* n */
115 asn1_write_10x( p, buffer, bits, 1 ) );
116 if( keypair )
117 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
118 mbedtls_asn1_write_int( p, buffer, 0 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
120 {
121 const unsigned char tag =
122 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
123 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
124 }
125 return( len );
126}
127
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100128int exercise_mac_setup( psa_key_type_t key_type,
129 const unsigned char *key_bytes,
130 size_t key_length,
131 psa_algorithm_t alg,
132 psa_mac_operation_t *operation,
133 psa_status_t *status )
134{
Ronald Cron5425a212020-08-04 14:58:35 +0200135 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200136 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100137
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100138 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200139 psa_set_key_algorithm( &attributes, alg );
140 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200141 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100142
Ronald Cron5425a212020-08-04 14:58:35 +0200143 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100144 /* Whether setup succeeded or failed, abort must succeed. */
145 PSA_ASSERT( psa_mac_abort( operation ) );
146 /* If setup failed, reproduce the failure, so that the caller can
147 * test the resulting state of the operation object. */
148 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100149 {
Ronald Cron5425a212020-08-04 14:58:35 +0200150 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100151 }
152
Ronald Cron5425a212020-08-04 14:58:35 +0200153 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100154 return( 1 );
155
156exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200157 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100158 return( 0 );
159}
160
161int exercise_cipher_setup( psa_key_type_t key_type,
162 const unsigned char *key_bytes,
163 size_t key_length,
164 psa_algorithm_t alg,
165 psa_cipher_operation_t *operation,
166 psa_status_t *status )
167{
Ronald Cron5425a212020-08-04 14:58:35 +0200168 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200169 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100170
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200171 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
172 psa_set_key_algorithm( &attributes, alg );
173 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200174 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100175
Ronald Cron5425a212020-08-04 14:58:35 +0200176 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100177 /* Whether setup succeeded or failed, abort must succeed. */
178 PSA_ASSERT( psa_cipher_abort( operation ) );
179 /* If setup failed, reproduce the failure, so that the caller can
180 * test the resulting state of the operation object. */
181 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100182 {
Ronald Cron5425a212020-08-04 14:58:35 +0200183 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100184 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185 }
186
Ronald Cron5425a212020-08-04 14:58:35 +0200187 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100188 return( 1 );
189
190exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200191 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100192 return( 0 );
193}
194
Ronald Cron5425a212020-08-04 14:58:35 +0200195static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200196{
197 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200198 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200199 uint8_t buffer[1];
200 size_t length;
201 int ok = 0;
202
Ronald Cronecfb2372020-07-23 17:13:42 +0200203 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200204 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
205 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
206 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200207 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000208 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200209 TEST_EQUAL(
210 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
211 TEST_EQUAL(
212 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200213 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200214 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
215 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
216 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
217 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
218
Ronald Cron5425a212020-08-04 14:58:35 +0200219 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000220 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200221 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200222 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000223 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200224
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200225 ok = 1;
226
227exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100228 /*
229 * Key attributes may have been returned by psa_get_key_attributes()
230 * thus reset them as required.
231 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200232 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100233
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200234 return( ok );
235}
236
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200237/* Assert that a key isn't reported as having a slot number. */
238#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
239#define ASSERT_NO_SLOT_NUMBER( attributes ) \
240 do \
241 { \
242 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
243 TEST_EQUAL( psa_get_key_slot_number( \
244 attributes, \
245 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
246 PSA_ERROR_INVALID_ARGUMENT ); \
247 } \
248 while( 0 )
249#else /* MBEDTLS_PSA_CRYPTO_SE_C */
250#define ASSERT_NO_SLOT_NUMBER( attributes ) \
251 ( (void) 0 )
252#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
253
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100254/* An overapproximation of the amount of storage needed for a key of the
255 * given type and with the given content. The API doesn't make it easy
256 * to find a good value for the size. The current implementation doesn't
257 * care about the value anyway. */
258#define KEY_BITS_FROM_DATA( type, data ) \
259 ( data )->len
260
Darryl Green0c6575a2018-11-07 16:05:30 +0000261typedef enum {
262 IMPORT_KEY = 0,
263 GENERATE_KEY = 1,
264 DERIVE_KEY = 2
265} generate_method;
266
Paul Elliott33746aa2021-09-15 16:40:40 +0100267typedef enum
268{
269 DO_NOT_SET_LENGTHS = 0,
270 SET_LENGTHS_BEFORE_NONCE = 1,
271 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100272} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100273
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100274typedef enum
275{
276 USE_NULL_TAG = 0,
277 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100278} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100279
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100280/*!
281 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100282 * \param key_type_arg Type of key passed in
283 * \param key_data The encryption / decryption key data
284 * \param alg_arg The type of algorithm used
285 * \param nonce Nonce data
286 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100287 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100288 * feed additional data in to be encrypted /
289 * decrypted. If -1, no chunking.
290 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100291 * \param data_part_len_arg If not -1, the length of chunks to feed
292 * the data in to be encrypted / decrypted. If
293 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100294 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100295 * expected here, this controls whether or not
296 * to set lengths, and in what order with
297 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100298 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100299 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100300 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100301 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100302 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100303 */
304static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
305 int alg_arg,
306 data_t *nonce,
307 data_t *additional_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100308 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100309 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100310 int data_part_len_arg,
Paul Elliottbb979e72021-09-22 12:54:42 +0100311 set_lengths_method_t set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100312 data_t *expected_output,
Paul Elliott329d5382021-07-22 17:10:45 +0100313 int is_encrypt,
Paul Elliott33746aa2021-09-15 16:40:40 +0100314 int do_zero_parts )
Paul Elliottd3f82412021-06-16 16:52:21 +0100315{
316 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
317 psa_key_type_t key_type = key_type_arg;
318 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100319 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100320 unsigned char *output_data = NULL;
321 unsigned char *part_data = NULL;
322 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100323 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100324 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100325 size_t output_size = 0;
326 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100327 size_t output_length = 0;
328 size_t key_bits = 0;
329 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100330 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100331 size_t part_length = 0;
332 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100333 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100334 size_t ad_part_len = 0;
335 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100336 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100337 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
338 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
339
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100340 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100341 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100342
Paul Elliottd3f82412021-06-16 16:52:21 +0100343 PSA_ASSERT( psa_crypto_init( ) );
344
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100345 if( is_encrypt )
346 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
347 else
348 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
349
Paul Elliottd3f82412021-06-16 16:52:21 +0100350 psa_set_key_algorithm( &attributes, alg );
351 psa_set_key_type( &attributes, key_type );
352
353 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
354 &key ) );
355
356 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
357 key_bits = psa_get_key_bits( &attributes );
358
359 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
360
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100361 if( is_encrypt )
362 {
363 /* Tag gets written at end of buffer. */
364 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
365 ( input_data->len +
366 tag_length ) );
367 data_true_size = input_data->len;
368 }
369 else
370 {
371 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
372 ( input_data->len -
373 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100374
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100375 /* Do not want to attempt to decrypt tag. */
376 data_true_size = input_data->len - tag_length;
377 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100378
379 ASSERT_ALLOC( output_data, output_size );
380
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100381 if( is_encrypt )
382 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100383 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
384 TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100385 }
386 else
387 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100388 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
389 TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100390 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100391
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100392 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100393
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100394 if( is_encrypt )
395 status = psa_aead_encrypt_setup( &operation, key, alg );
396 else
397 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100398
399 /* If the operation is not supported, just skip and not fail in case the
400 * encryption involves a common limitation of cryptography hardwares and
401 * an alternative implementation. */
402 if( status == PSA_ERROR_NOT_SUPPORTED )
403 {
404 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
405 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
406 }
407
408 PSA_ASSERT( status );
409
Paul Elliott33746aa2021-09-15 16:40:40 +0100410 if( set_lengths_method == DO_NOT_SET_LENGTHS )
411 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
412 else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
Paul Elliottd3f82412021-06-16 16:52:21 +0100413 {
Paul Elliott33746aa2021-09-15 16:40:40 +0100414 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
415 data_true_size ) );
Paul Elliottebf91632021-07-22 17:54:42 +0100416 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
417 }
Paul Elliott33746aa2021-09-15 16:40:40 +0100418 else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
Paul Elliottebf91632021-07-22 17:54:42 +0100419 {
420 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
421
Paul Elliott33746aa2021-09-15 16:40:40 +0100422 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
423 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100424 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100425
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100426 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100427 {
428 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100429 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100430
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100431 for( part_offset = 0, part_count = 0;
432 part_offset < additional_data->len;
433 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100434 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100435 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100436 {
Paul Elliott329d5382021-07-22 17:10:45 +0100437 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100438 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100439 else if( additional_data->len - part_offset < ad_part_len )
440 {
441 part_length = additional_data->len - part_offset;
442 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100443 else
444 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100445 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100446 }
447
448 PSA_ASSERT( psa_aead_update_ad( &operation,
449 additional_data->x + part_offset,
450 part_length ) );
451
Paul Elliottd3f82412021-06-16 16:52:21 +0100452 }
453 }
454 else
455 {
456 /* Pass additional data in one go. */
457 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
458 additional_data->len ) );
459 }
460
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100461 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100462 {
463 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100464 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100465 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100466 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100467
468 ASSERT_ALLOC( part_data, part_data_size );
469
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100470 for( part_offset = 0, part_count = 0;
471 part_offset < data_true_size;
472 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100473 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100474 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100475 {
Paul Elliott329d5382021-07-22 17:10:45 +0100476 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100477 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100478 else if( ( data_true_size - part_offset ) < data_part_len )
479 {
480 part_length = ( data_true_size - part_offset );
481 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100482 else
483 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100484 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100485 }
486
487 PSA_ASSERT( psa_aead_update( &operation,
488 ( input_data->x + part_offset ),
489 part_length, part_data,
490 part_data_size,
491 &output_part_length ) );
492
493 if( output_data && output_part_length )
494 {
495 memcpy( ( output_data + part_offset ), part_data,
496 output_part_length );
497 }
498
Paul Elliottd3f82412021-06-16 16:52:21 +0100499 output_length += output_part_length;
500 }
501 }
502 else
503 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100504 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100505 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100506 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100507 output_size, &output_length ) );
508 }
509
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100510 if( is_encrypt )
511 PSA_ASSERT( psa_aead_finish( &operation, final_data,
512 final_output_size,
513 &output_part_length,
514 tag_buffer, tag_length,
515 &tag_size ) );
516 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100517 {
Paul Elliott9961a662021-09-17 19:19:02 +0100518 PSA_ASSERT( psa_aead_verify( &operation, final_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100519 final_output_size,
520 &output_part_length,
521 ( input_data->x + data_true_size ),
Paul Elliott9961a662021-09-17 19:19:02 +0100522 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100523 }
524
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100525 if( output_data && output_part_length )
526 memcpy( ( output_data + output_length ), final_data,
527 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100528
529 output_length += output_part_length;
530
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100531
532 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
533 * should be exact.*/
534 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100535 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100536 TEST_EQUAL( tag_length, tag_size );
537
538 if( output_data && tag_length )
539 memcpy( ( output_data + output_length ), tag_buffer,
540 tag_length );
541
542 output_length += tag_length;
543
544 TEST_EQUAL( output_length,
545 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
546 input_data->len ) );
547 TEST_ASSERT( output_length <=
548 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
549 }
550 else
551 {
552 TEST_EQUAL( output_length,
553 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
554 input_data->len ) );
555 TEST_ASSERT( output_length <=
556 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100557 }
558
Paul Elliottd3f82412021-06-16 16:52:21 +0100559
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100560 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100561 output_data, output_length );
562
Paul Elliottd3f82412021-06-16 16:52:21 +0100563
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100564 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100565
566exit:
567 psa_destroy_key( key );
568 psa_aead_abort( &operation );
569 mbedtls_free( output_data );
570 mbedtls_free( part_data );
571 mbedtls_free( final_data );
572 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100573
574 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100575}
576
Gilles Peskinee59236f2018-01-27 23:32:46 +0100577/* END_HEADER */
578
579/* BEGIN_DEPENDENCIES
580 * depends_on:MBEDTLS_PSA_CRYPTO_C
581 * END_DEPENDENCIES
582 */
583
584/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200585void static_checks( )
586{
587 size_t max_truncated_mac_size =
588 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
589
590 /* Check that the length for a truncated MAC always fits in the algorithm
591 * encoding. The shifted mask is the maximum truncated value. The
592 * untruncated algorithm may be one byte larger. */
593 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
594}
595/* END_CASE */
596
597/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200598void import_with_policy( int type_arg,
599 int usage_arg, int alg_arg,
600 int expected_status_arg )
601{
602 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
603 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200604 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200605 psa_key_type_t type = type_arg;
606 psa_key_usage_t usage = usage_arg;
607 psa_algorithm_t alg = alg_arg;
608 psa_status_t expected_status = expected_status_arg;
609 const uint8_t key_material[16] = {0};
610 psa_status_t status;
611
612 PSA_ASSERT( psa_crypto_init( ) );
613
614 psa_set_key_type( &attributes, type );
615 psa_set_key_usage_flags( &attributes, usage );
616 psa_set_key_algorithm( &attributes, alg );
617
618 status = psa_import_key( &attributes,
619 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200620 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200621 TEST_EQUAL( status, expected_status );
622 if( status != PSA_SUCCESS )
623 goto exit;
624
Ronald Cron5425a212020-08-04 14:58:35 +0200625 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200626 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
627 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
628 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200629 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200630
Ronald Cron5425a212020-08-04 14:58:35 +0200631 PSA_ASSERT( psa_destroy_key( key ) );
632 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200633
634exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100635 /*
636 * Key attributes may have been returned by psa_get_key_attributes()
637 * thus reset them as required.
638 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200639 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100640
641 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200642 PSA_DONE( );
643}
644/* END_CASE */
645
646/* BEGIN_CASE */
647void import_with_data( data_t *data, int type_arg,
648 int attr_bits_arg,
649 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200650{
651 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
652 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200653 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200654 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200655 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200656 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100657 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100658
Gilles Peskine8817f612018-12-18 00:18:46 +0100659 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100660
Gilles Peskine4747d192019-04-17 15:05:45 +0200661 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200662 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200663
Ronald Cron5425a212020-08-04 14:58:35 +0200664 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100665 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200666 if( status != PSA_SUCCESS )
667 goto exit;
668
Ronald Cron5425a212020-08-04 14:58:35 +0200669 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200670 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200671 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200672 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200673 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200674
Ronald Cron5425a212020-08-04 14:58:35 +0200675 PSA_ASSERT( psa_destroy_key( key ) );
676 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100677
678exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100679 /*
680 * Key attributes may have been returned by psa_get_key_attributes()
681 * thus reset them as required.
682 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200683 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100684
685 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200686 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100687}
688/* END_CASE */
689
690/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200691void import_large_key( int type_arg, int byte_size_arg,
692 int expected_status_arg )
693{
694 psa_key_type_t type = type_arg;
695 size_t byte_size = byte_size_arg;
696 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
697 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200698 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200699 psa_status_t status;
700 uint8_t *buffer = NULL;
701 size_t buffer_size = byte_size + 1;
702 size_t n;
703
Steven Cooreman69967ce2021-01-18 18:01:08 +0100704 /* Skip the test case if the target running the test cannot
705 * accomodate large keys due to heap size constraints */
706 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200707 memset( buffer, 'K', byte_size );
708
709 PSA_ASSERT( psa_crypto_init( ) );
710
711 /* Try importing the key */
712 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
713 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200714 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100715 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200716 TEST_EQUAL( status, expected_status );
717
718 if( status == PSA_SUCCESS )
719 {
Ronald Cron5425a212020-08-04 14:58:35 +0200720 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200721 TEST_EQUAL( psa_get_key_type( &attributes ), type );
722 TEST_EQUAL( psa_get_key_bits( &attributes ),
723 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200724 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200725 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200726 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200727 for( n = 0; n < byte_size; n++ )
728 TEST_EQUAL( buffer[n], 'K' );
729 for( n = byte_size; n < buffer_size; n++ )
730 TEST_EQUAL( buffer[n], 0 );
731 }
732
733exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100734 /*
735 * Key attributes may have been returned by psa_get_key_attributes()
736 * thus reset them as required.
737 */
738 psa_reset_key_attributes( &attributes );
739
Ronald Cron5425a212020-08-04 14:58:35 +0200740 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200741 PSA_DONE( );
742 mbedtls_free( buffer );
743}
744/* END_CASE */
745
746/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200747void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
748{
Ronald Cron5425a212020-08-04 14:58:35 +0200749 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200750 size_t bits = bits_arg;
751 psa_status_t expected_status = expected_status_arg;
752 psa_status_t status;
753 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200754 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200755 size_t buffer_size = /* Slight overapproximations */
756 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200757 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200758 unsigned char *p;
759 int ret;
760 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200761 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200762
Gilles Peskine8817f612018-12-18 00:18:46 +0100763 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200764 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200765
766 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
767 bits, keypair ) ) >= 0 );
768 length = ret;
769
770 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200771 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200772 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100773 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200774
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200775 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200776 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200777
778exit:
779 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200780 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200781}
782/* END_CASE */
783
784/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300785void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300786 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200787 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100788 int expected_bits,
789 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200790 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100791 int canonical_input )
792{
Ronald Cron5425a212020-08-04 14:58:35 +0200793 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100794 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200795 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200796 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100797 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100798 unsigned char *exported = NULL;
799 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100800 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100801 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100802 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200803 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200804 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100805
Moran Pekercb088e72018-07-17 17:36:59 +0300806 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200807 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100808 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200809 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100810 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100811
Gilles Peskine4747d192019-04-17 15:05:45 +0200812 psa_set_key_usage_flags( &attributes, usage_arg );
813 psa_set_key_algorithm( &attributes, alg );
814 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700815
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100816 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200817 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100818
819 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200820 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200821 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
822 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200823 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100824
825 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200826 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100827 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100828
829 /* The exported length must be set by psa_export_key() to a value between 0
830 * and export_size. On errors, the exported length must be 0. */
831 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
832 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
833 TEST_ASSERT( exported_length <= export_size );
834
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200835 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200836 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100837 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200838 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100839 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100840 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200841 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100842
Gilles Peskineea38a922021-02-13 00:05:16 +0100843 /* Run sanity checks on the exported key. For non-canonical inputs,
844 * this validates the canonical representations. For canonical inputs,
845 * this doesn't directly validate the implementation, but it still helps
846 * by cross-validating the test data with the sanity check code. */
847 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200848 goto exit;
849
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100850 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200851 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100852 else
853 {
Ronald Cron5425a212020-08-04 14:58:35 +0200854 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200855 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200856 &key2 ) );
857 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100858 reexported,
859 export_size,
860 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200861 ASSERT_COMPARE( exported, exported_length,
862 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200863 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100864 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100865 TEST_ASSERT( exported_length <=
866 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
867 psa_get_key_bits( &got_attributes ) ) );
868 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100869
870destroy:
871 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200872 PSA_ASSERT( psa_destroy_key( key ) );
873 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100874
875exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100876 /*
877 * Key attributes may have been returned by psa_get_key_attributes()
878 * thus reset them as required.
879 */
880 psa_reset_key_attributes( &got_attributes );
881
itayzafrir3e02b3b2018-06-12 17:06:52 +0300882 mbedtls_free( exported );
883 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200884 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100885}
886/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100887
Moran Pekerf709f4a2018-06-06 17:26:04 +0300888/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300889void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200890 int type_arg,
891 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100892 int export_size_delta,
893 int expected_export_status_arg,
894 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300895{
Ronald Cron5425a212020-08-04 14:58:35 +0200896 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300897 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200898 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200899 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300900 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300901 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100902 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100903 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200904 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300905
Gilles Peskine8817f612018-12-18 00:18:46 +0100906 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300907
Gilles Peskine4747d192019-04-17 15:05:45 +0200908 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
909 psa_set_key_algorithm( &attributes, alg );
910 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300911
912 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200913 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300914
Gilles Peskine49c25912018-10-29 15:15:31 +0100915 /* Export the public key */
916 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200917 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200918 exported, export_size,
919 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100920 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100921 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100922 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200923 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100924 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200925 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200926 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100927 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100928 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100929 TEST_ASSERT( expected_public_key->len <=
930 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
931 TEST_ASSERT( expected_public_key->len <=
932 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100933 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
934 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100935 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300936
937exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100938 /*
939 * Key attributes may have been returned by psa_get_key_attributes()
940 * thus reset them as required.
941 */
942 psa_reset_key_attributes( &attributes );
943
itayzafrir3e02b3b2018-06-12 17:06:52 +0300944 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200945 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200946 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300947}
948/* END_CASE */
949
Gilles Peskine20035e32018-02-03 22:44:14 +0100950/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200951void import_and_exercise_key( data_t *data,
952 int type_arg,
953 int bits_arg,
954 int alg_arg )
955{
Ronald Cron5425a212020-08-04 14:58:35 +0200956 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200957 psa_key_type_t type = type_arg;
958 size_t bits = bits_arg;
959 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100960 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200961 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200962 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200963
Gilles Peskine8817f612018-12-18 00:18:46 +0100964 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200965
Gilles Peskine4747d192019-04-17 15:05:45 +0200966 psa_set_key_usage_flags( &attributes, usage );
967 psa_set_key_algorithm( &attributes, alg );
968 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200969
970 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200971 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200972
973 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200974 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200975 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
976 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200977
978 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100979 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200980 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200981
Ronald Cron5425a212020-08-04 14:58:35 +0200982 PSA_ASSERT( psa_destroy_key( key ) );
983 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200984
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200985exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100986 /*
987 * Key attributes may have been returned by psa_get_key_attributes()
988 * thus reset them as required.
989 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200990 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100991
992 psa_reset_key_attributes( &attributes );
993 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200994 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200995}
996/* END_CASE */
997
998/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100999void effective_key_attributes( int type_arg, int expected_type_arg,
1000 int bits_arg, int expected_bits_arg,
1001 int usage_arg, int expected_usage_arg,
1002 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001003{
Ronald Cron5425a212020-08-04 14:58:35 +02001004 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001005 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001006 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001007 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001008 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001009 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001010 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001011 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001012 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001013 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001014
Gilles Peskine8817f612018-12-18 00:18:46 +01001015 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001016
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001017 psa_set_key_usage_flags( &attributes, usage );
1018 psa_set_key_algorithm( &attributes, alg );
1019 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001020 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001021
Ronald Cron5425a212020-08-04 14:58:35 +02001022 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001023 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001024
Ronald Cron5425a212020-08-04 14:58:35 +02001025 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001026 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1027 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1028 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1029 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001030
1031exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001032 /*
1033 * Key attributes may have been returned by psa_get_key_attributes()
1034 * thus reset them as required.
1035 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001036 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001037
1038 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001039 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001040}
1041/* END_CASE */
1042
1043/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001044void check_key_policy( int type_arg, int bits_arg,
1045 int usage_arg, int alg_arg )
1046{
1047 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1048 usage_arg, usage_arg, alg_arg, alg_arg );
1049 goto exit;
1050}
1051/* END_CASE */
1052
1053/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001054void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001055{
1056 /* Test each valid way of initializing the object, except for `= {0}`, as
1057 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1058 * though it's OK by the C standard. We could test for this, but we'd need
1059 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001060 psa_key_attributes_t func = psa_key_attributes_init( );
1061 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1062 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001063
1064 memset( &zero, 0, sizeof( zero ) );
1065
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001066 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1067 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1068 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001069
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001070 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1071 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1072 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1073
1074 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1075 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1076 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1077
1078 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1079 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1080 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1081
1082 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1083 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1084 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001085}
1086/* END_CASE */
1087
1088/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001089void mac_key_policy( int policy_usage,
1090 int policy_alg,
1091 int key_type,
1092 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001093 int exercise_alg,
1094 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001095{
Ronald Cron5425a212020-08-04 14:58:35 +02001096 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001097 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001098 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001099 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001100 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001101 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001102
Gilles Peskine8817f612018-12-18 00:18:46 +01001103 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001104
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001105 psa_set_key_usage_flags( &attributes, policy_usage );
1106 psa_set_key_algorithm( &attributes, policy_alg );
1107 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001108
Gilles Peskine049c7532019-05-15 20:22:09 +02001109 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001110 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001111
Ronald Cron5425a212020-08-04 14:58:35 +02001112 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001113 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001114 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001115 else
1116 TEST_EQUAL( status, expected_status );
1117
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001118 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001119
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001120 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001121 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001122 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001123 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001124 else
1125 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001126
1127exit:
1128 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001129 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001130 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001131}
1132/* END_CASE */
1133
1134/* BEGIN_CASE */
1135void cipher_key_policy( int policy_usage,
1136 int policy_alg,
1137 int key_type,
1138 data_t *key_data,
1139 int exercise_alg )
1140{
Ronald Cron5425a212020-08-04 14:58:35 +02001141 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001142 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001143 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001144 psa_status_t status;
1145
Gilles Peskine8817f612018-12-18 00:18:46 +01001146 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001147
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001148 psa_set_key_usage_flags( &attributes, policy_usage );
1149 psa_set_key_algorithm( &attributes, policy_alg );
1150 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001151
Gilles Peskine049c7532019-05-15 20:22:09 +02001152 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001153 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001154
Ronald Cron5425a212020-08-04 14:58:35 +02001155 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001156 if( policy_alg == exercise_alg &&
1157 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001158 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001159 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001160 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001161 psa_cipher_abort( &operation );
1162
Ronald Cron5425a212020-08-04 14:58:35 +02001163 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001164 if( policy_alg == exercise_alg &&
1165 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001166 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001167 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001168 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001169
1170exit:
1171 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001172 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001173 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001174}
1175/* END_CASE */
1176
1177/* BEGIN_CASE */
1178void aead_key_policy( int policy_usage,
1179 int policy_alg,
1180 int key_type,
1181 data_t *key_data,
1182 int nonce_length_arg,
1183 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001184 int exercise_alg,
1185 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001186{
Ronald Cron5425a212020-08-04 14:58:35 +02001187 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001188 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001189 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001190 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001191 unsigned char nonce[16] = {0};
1192 size_t nonce_length = nonce_length_arg;
1193 unsigned char tag[16];
1194 size_t tag_length = tag_length_arg;
1195 size_t output_length;
1196
1197 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1198 TEST_ASSERT( tag_length <= sizeof( tag ) );
1199
Gilles Peskine8817f612018-12-18 00:18:46 +01001200 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001201
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001202 psa_set_key_usage_flags( &attributes, policy_usage );
1203 psa_set_key_algorithm( &attributes, policy_alg );
1204 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001205
Gilles Peskine049c7532019-05-15 20:22:09 +02001206 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001207 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001208
Ronald Cron5425a212020-08-04 14:58:35 +02001209 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001210 nonce, nonce_length,
1211 NULL, 0,
1212 NULL, 0,
1213 tag, tag_length,
1214 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001215 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1216 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001217 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001218 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001219
1220 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001221 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001222 nonce, nonce_length,
1223 NULL, 0,
1224 tag, tag_length,
1225 NULL, 0,
1226 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001227 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1228 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1229 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001230 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001231 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001232 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001233
1234exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001235 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001236 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001237}
1238/* END_CASE */
1239
1240/* BEGIN_CASE */
1241void asymmetric_encryption_key_policy( int policy_usage,
1242 int policy_alg,
1243 int key_type,
1244 data_t *key_data,
1245 int exercise_alg )
1246{
Ronald Cron5425a212020-08-04 14:58:35 +02001247 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001248 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001249 psa_status_t status;
1250 size_t key_bits;
1251 size_t buffer_length;
1252 unsigned char *buffer = NULL;
1253 size_t output_length;
1254
Gilles Peskine8817f612018-12-18 00:18:46 +01001255 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001256
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001257 psa_set_key_usage_flags( &attributes, policy_usage );
1258 psa_set_key_algorithm( &attributes, policy_alg );
1259 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001260
Gilles Peskine049c7532019-05-15 20:22:09 +02001261 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001262 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001263
Ronald Cron5425a212020-08-04 14:58:35 +02001264 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001265 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001266 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1267 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001268 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001269
Ronald Cron5425a212020-08-04 14:58:35 +02001270 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001271 NULL, 0,
1272 NULL, 0,
1273 buffer, buffer_length,
1274 &output_length );
1275 if( policy_alg == exercise_alg &&
1276 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001277 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001278 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001279 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001280
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001281 if( buffer_length != 0 )
1282 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001283 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001284 buffer, buffer_length,
1285 NULL, 0,
1286 buffer, buffer_length,
1287 &output_length );
1288 if( policy_alg == exercise_alg &&
1289 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001290 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001291 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001292 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001293
1294exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001295 /*
1296 * Key attributes may have been returned by psa_get_key_attributes()
1297 * thus reset them as required.
1298 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001299 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001300
1301 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001302 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001303 mbedtls_free( buffer );
1304}
1305/* END_CASE */
1306
1307/* BEGIN_CASE */
1308void asymmetric_signature_key_policy( int policy_usage,
1309 int policy_alg,
1310 int key_type,
1311 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001312 int exercise_alg,
1313 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001314{
Ronald Cron5425a212020-08-04 14:58:35 +02001315 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001316 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001317 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001318 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1319 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1320 * compatible with the policy and `payload_length_arg` is supposed to be
1321 * a valid input length to sign. If `payload_length_arg <= 0`,
1322 * `exercise_alg` is supposed to be forbidden by the policy. */
1323 int compatible_alg = payload_length_arg > 0;
1324 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001325 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001326 size_t signature_length;
1327
Gilles Peskine8817f612018-12-18 00:18:46 +01001328 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001329
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001330 psa_set_key_usage_flags( &attributes, policy_usage );
1331 psa_set_key_algorithm( &attributes, policy_alg );
1332 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001333
Gilles Peskine049c7532019-05-15 20:22:09 +02001334 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001335 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001336
Ronald Cron5425a212020-08-04 14:58:35 +02001337 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001338 payload, payload_length,
1339 signature, sizeof( signature ),
1340 &signature_length );
1341 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001342 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001343 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001344 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001345
1346 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001347 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001348 payload, payload_length,
1349 signature, sizeof( signature ) );
1350 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001351 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001352 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001353 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001354
1355exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001356 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001357 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001358}
1359/* END_CASE */
1360
Janos Follathba3fab92019-06-11 14:50:16 +01001361/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001362void derive_key_policy( int policy_usage,
1363 int policy_alg,
1364 int key_type,
1365 data_t *key_data,
1366 int exercise_alg )
1367{
Ronald Cron5425a212020-08-04 14:58:35 +02001368 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001369 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001370 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001371 psa_status_t status;
1372
Gilles Peskine8817f612018-12-18 00:18:46 +01001373 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001374
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001375 psa_set_key_usage_flags( &attributes, policy_usage );
1376 psa_set_key_algorithm( &attributes, policy_alg );
1377 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001378
Gilles Peskine049c7532019-05-15 20:22:09 +02001379 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001380 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001381
Janos Follathba3fab92019-06-11 14:50:16 +01001382 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1383
1384 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1385 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001386 {
Janos Follathba3fab92019-06-11 14:50:16 +01001387 PSA_ASSERT( psa_key_derivation_input_bytes(
1388 &operation,
1389 PSA_KEY_DERIVATION_INPUT_SEED,
1390 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001391 }
Janos Follathba3fab92019-06-11 14:50:16 +01001392
1393 status = psa_key_derivation_input_key( &operation,
1394 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001395 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001396
Gilles Peskineea0fb492018-07-12 17:17:20 +02001397 if( policy_alg == exercise_alg &&
1398 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001399 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001400 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001401 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001402
1403exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001404 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001405 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001406 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001407}
1408/* END_CASE */
1409
1410/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001411void agreement_key_policy( int policy_usage,
1412 int policy_alg,
1413 int key_type_arg,
1414 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001415 int exercise_alg,
1416 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001417{
Ronald Cron5425a212020-08-04 14:58:35 +02001418 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001419 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001420 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001421 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001422 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001423 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001424
Gilles Peskine8817f612018-12-18 00:18:46 +01001425 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001426
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001427 psa_set_key_usage_flags( &attributes, policy_usage );
1428 psa_set_key_algorithm( &attributes, policy_alg );
1429 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001430
Gilles Peskine049c7532019-05-15 20:22:09 +02001431 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001432 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001433
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001434 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001435 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001436
Steven Cooremance48e852020-10-05 16:02:45 +02001437 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001438
1439exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001440 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001441 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001442 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001443}
1444/* END_CASE */
1445
1446/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001447void key_policy_alg2( int key_type_arg, data_t *key_data,
1448 int usage_arg, int alg_arg, int alg2_arg )
1449{
Ronald Cron5425a212020-08-04 14:58:35 +02001450 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001451 psa_key_type_t key_type = key_type_arg;
1452 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1453 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1454 psa_key_usage_t usage = usage_arg;
1455 psa_algorithm_t alg = alg_arg;
1456 psa_algorithm_t alg2 = alg2_arg;
1457
1458 PSA_ASSERT( psa_crypto_init( ) );
1459
1460 psa_set_key_usage_flags( &attributes, usage );
1461 psa_set_key_algorithm( &attributes, alg );
1462 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1463 psa_set_key_type( &attributes, key_type );
1464 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001465 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001466
Ronald Cron5425a212020-08-04 14:58:35 +02001467 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001468 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1469 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1470 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1471
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001472 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001473 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001474 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001475 goto exit;
1476
1477exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001478 /*
1479 * Key attributes may have been returned by psa_get_key_attributes()
1480 * thus reset them as required.
1481 */
1482 psa_reset_key_attributes( &got_attributes );
1483
Ronald Cron5425a212020-08-04 14:58:35 +02001484 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001485 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001486}
1487/* END_CASE */
1488
1489/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001490void raw_agreement_key_policy( int policy_usage,
1491 int policy_alg,
1492 int key_type_arg,
1493 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001494 int exercise_alg,
1495 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001496{
Ronald Cron5425a212020-08-04 14:58:35 +02001497 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001498 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001499 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001500 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001501 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001502 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001503
1504 PSA_ASSERT( psa_crypto_init( ) );
1505
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001506 psa_set_key_usage_flags( &attributes, policy_usage );
1507 psa_set_key_algorithm( &attributes, policy_alg );
1508 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001509
Gilles Peskine049c7532019-05-15 20:22:09 +02001510 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001511 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001512
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001513 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001514
Steven Cooremance48e852020-10-05 16:02:45 +02001515 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001516
1517exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001518 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001519 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001520 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001521}
1522/* END_CASE */
1523
1524/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001525void copy_success( int source_usage_arg,
1526 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001527 int type_arg, data_t *material,
1528 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001529 int target_usage_arg,
1530 int target_alg_arg, int target_alg2_arg,
1531 int expected_usage_arg,
1532 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001533{
Gilles Peskineca25db92019-04-19 11:43:08 +02001534 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1535 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001536 psa_key_usage_t expected_usage = expected_usage_arg;
1537 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001538 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001539 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1540 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001541 uint8_t *export_buffer = NULL;
1542
Gilles Peskine57ab7212019-01-28 13:03:09 +01001543 PSA_ASSERT( psa_crypto_init( ) );
1544
Gilles Peskineca25db92019-04-19 11:43:08 +02001545 /* Prepare the source key. */
1546 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1547 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001548 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001549 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001550 PSA_ASSERT( psa_import_key( &source_attributes,
1551 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001552 &source_key ) );
1553 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001554
Gilles Peskineca25db92019-04-19 11:43:08 +02001555 /* Prepare the target attributes. */
1556 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001557 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001558 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001559 /* Set volatile lifetime to reset the key identifier to 0. */
1560 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1561 }
1562
Gilles Peskineca25db92019-04-19 11:43:08 +02001563 if( target_usage_arg != -1 )
1564 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1565 if( target_alg_arg != -1 )
1566 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001567 if( target_alg2_arg != -1 )
1568 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001569
1570 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001571 PSA_ASSERT( psa_copy_key( source_key,
1572 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001573
1574 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001575 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001576
1577 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001578 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001579 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1580 psa_get_key_type( &target_attributes ) );
1581 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1582 psa_get_key_bits( &target_attributes ) );
1583 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1584 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001585 TEST_EQUAL( expected_alg2,
1586 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001587 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1588 {
1589 size_t length;
1590 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001591 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001592 material->len, &length ) );
1593 ASSERT_COMPARE( material->x, material->len,
1594 export_buffer, length );
1595 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001596
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001597 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001598 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001599 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001600 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001601
Ronald Cron5425a212020-08-04 14:58:35 +02001602 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001603
1604exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001605 /*
1606 * Source and target key attributes may have been returned by
1607 * psa_get_key_attributes() thus reset them as required.
1608 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001609 psa_reset_key_attributes( &source_attributes );
1610 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001611
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001612 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001613 mbedtls_free( export_buffer );
1614}
1615/* END_CASE */
1616
1617/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001618void copy_fail( int source_usage_arg,
1619 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001620 int type_arg, data_t *material,
1621 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001622 int target_usage_arg,
1623 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001624 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001625 int expected_status_arg )
1626{
1627 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1628 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001629 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1630 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001631 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001632
1633 PSA_ASSERT( psa_crypto_init( ) );
1634
1635 /* Prepare the source key. */
1636 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1637 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001638 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001639 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001640 PSA_ASSERT( psa_import_key( &source_attributes,
1641 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001642 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001643
1644 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001645 psa_set_key_id( &target_attributes, key_id );
1646 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001647 psa_set_key_type( &target_attributes, target_type_arg );
1648 psa_set_key_bits( &target_attributes, target_bits_arg );
1649 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1650 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001651 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001652
1653 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001654 TEST_EQUAL( psa_copy_key( source_key,
1655 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001656 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001657
Ronald Cron5425a212020-08-04 14:58:35 +02001658 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001659
Gilles Peskine4a644642019-05-03 17:14:08 +02001660exit:
1661 psa_reset_key_attributes( &source_attributes );
1662 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001663 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001664}
1665/* END_CASE */
1666
1667/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001668void hash_operation_init( )
1669{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001670 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001671 /* Test each valid way of initializing the object, except for `= {0}`, as
1672 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1673 * though it's OK by the C standard. We could test for this, but we'd need
1674 * to supress the Clang warning for the test. */
1675 psa_hash_operation_t func = psa_hash_operation_init( );
1676 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1677 psa_hash_operation_t zero;
1678
1679 memset( &zero, 0, sizeof( zero ) );
1680
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001681 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001682 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1683 PSA_ERROR_BAD_STATE );
1684 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1685 PSA_ERROR_BAD_STATE );
1686 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1687 PSA_ERROR_BAD_STATE );
1688
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001689 /* A default hash operation should be abortable without error. */
1690 PSA_ASSERT( psa_hash_abort( &func ) );
1691 PSA_ASSERT( psa_hash_abort( &init ) );
1692 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001693}
1694/* END_CASE */
1695
1696/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001697void hash_setup( int alg_arg,
1698 int expected_status_arg )
1699{
1700 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001701 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001702 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001703 psa_status_t status;
1704
Gilles Peskine8817f612018-12-18 00:18:46 +01001705 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001706
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001707 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001708 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001709
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001710 /* Whether setup succeeded or failed, abort must succeed. */
1711 PSA_ASSERT( psa_hash_abort( &operation ) );
1712
1713 /* If setup failed, reproduce the failure, so as to
1714 * test the resulting state of the operation object. */
1715 if( status != PSA_SUCCESS )
1716 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1717
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001718 /* Now the operation object should be reusable. */
1719#if defined(KNOWN_SUPPORTED_HASH_ALG)
1720 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1721 PSA_ASSERT( psa_hash_abort( &operation ) );
1722#endif
1723
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001724exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001725 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001726}
1727/* END_CASE */
1728
1729/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001730void hash_compute_fail( int alg_arg, data_t *input,
1731 int output_size_arg, int expected_status_arg )
1732{
1733 psa_algorithm_t alg = alg_arg;
1734 uint8_t *output = NULL;
1735 size_t output_size = output_size_arg;
1736 size_t output_length = INVALID_EXPORT_LENGTH;
1737 psa_status_t expected_status = expected_status_arg;
1738 psa_status_t status;
1739
1740 ASSERT_ALLOC( output, output_size );
1741
1742 PSA_ASSERT( psa_crypto_init( ) );
1743
1744 status = psa_hash_compute( alg, input->x, input->len,
1745 output, output_size, &output_length );
1746 TEST_EQUAL( status, expected_status );
1747 TEST_ASSERT( output_length <= output_size );
1748
1749exit:
1750 mbedtls_free( output );
1751 PSA_DONE( );
1752}
1753/* END_CASE */
1754
1755/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001756void hash_compare_fail( int alg_arg, data_t *input,
1757 data_t *reference_hash,
1758 int expected_status_arg )
1759{
1760 psa_algorithm_t alg = alg_arg;
1761 psa_status_t expected_status = expected_status_arg;
1762 psa_status_t status;
1763
1764 PSA_ASSERT( psa_crypto_init( ) );
1765
1766 status = psa_hash_compare( alg, input->x, input->len,
1767 reference_hash->x, reference_hash->len );
1768 TEST_EQUAL( status, expected_status );
1769
1770exit:
1771 PSA_DONE( );
1772}
1773/* END_CASE */
1774
1775/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001776void hash_compute_compare( int alg_arg, data_t *input,
1777 data_t *expected_output )
1778{
1779 psa_algorithm_t alg = alg_arg;
1780 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1781 size_t output_length = INVALID_EXPORT_LENGTH;
1782 size_t i;
1783
1784 PSA_ASSERT( psa_crypto_init( ) );
1785
1786 /* Compute with tight buffer */
1787 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001788 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001789 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001790 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001791 ASSERT_COMPARE( output, output_length,
1792 expected_output->x, expected_output->len );
1793
1794 /* Compute with larger buffer */
1795 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1796 output, sizeof( output ),
1797 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001798 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001799 ASSERT_COMPARE( output, output_length,
1800 expected_output->x, expected_output->len );
1801
1802 /* Compare with correct hash */
1803 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1804 output, output_length ) );
1805
1806 /* Compare with trailing garbage */
1807 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1808 output, output_length + 1 ),
1809 PSA_ERROR_INVALID_SIGNATURE );
1810
1811 /* Compare with truncated hash */
1812 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1813 output, output_length - 1 ),
1814 PSA_ERROR_INVALID_SIGNATURE );
1815
1816 /* Compare with corrupted value */
1817 for( i = 0; i < output_length; i++ )
1818 {
Chris Jones9634bb12021-01-20 15:56:42 +00001819 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001820 output[i] ^= 1;
1821 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1822 output, output_length ),
1823 PSA_ERROR_INVALID_SIGNATURE );
1824 output[i] ^= 1;
1825 }
1826
1827exit:
1828 PSA_DONE( );
1829}
1830/* END_CASE */
1831
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001832/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001833void hash_bad_order( )
1834{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001835 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001836 unsigned char input[] = "";
1837 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001838 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001839 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1840 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1841 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001842 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001843 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001844 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001845
Gilles Peskine8817f612018-12-18 00:18:46 +01001846 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001847
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001848 /* Call setup twice in a row. */
1849 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1850 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1851 PSA_ERROR_BAD_STATE );
1852 PSA_ASSERT( psa_hash_abort( &operation ) );
1853
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001854 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001855 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001856 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001857 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001858
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001859 /* Call update after finish. */
1860 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1861 PSA_ASSERT( psa_hash_finish( &operation,
1862 hash, sizeof( hash ), &hash_len ) );
1863 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001864 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001865 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001866
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001867 /* Call verify without calling setup beforehand. */
1868 TEST_EQUAL( psa_hash_verify( &operation,
1869 valid_hash, sizeof( valid_hash ) ),
1870 PSA_ERROR_BAD_STATE );
1871 PSA_ASSERT( psa_hash_abort( &operation ) );
1872
1873 /* Call verify after finish. */
1874 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1875 PSA_ASSERT( psa_hash_finish( &operation,
1876 hash, sizeof( hash ), &hash_len ) );
1877 TEST_EQUAL( psa_hash_verify( &operation,
1878 valid_hash, sizeof( valid_hash ) ),
1879 PSA_ERROR_BAD_STATE );
1880 PSA_ASSERT( psa_hash_abort( &operation ) );
1881
1882 /* Call verify twice in a row. */
1883 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1884 PSA_ASSERT( psa_hash_verify( &operation,
1885 valid_hash, sizeof( valid_hash ) ) );
1886 TEST_EQUAL( psa_hash_verify( &operation,
1887 valid_hash, sizeof( valid_hash ) ),
1888 PSA_ERROR_BAD_STATE );
1889 PSA_ASSERT( psa_hash_abort( &operation ) );
1890
1891 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001892 TEST_EQUAL( psa_hash_finish( &operation,
1893 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001894 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001895 PSA_ASSERT( psa_hash_abort( &operation ) );
1896
1897 /* Call finish twice in a row. */
1898 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1899 PSA_ASSERT( psa_hash_finish( &operation,
1900 hash, sizeof( hash ), &hash_len ) );
1901 TEST_EQUAL( psa_hash_finish( &operation,
1902 hash, sizeof( hash ), &hash_len ),
1903 PSA_ERROR_BAD_STATE );
1904 PSA_ASSERT( psa_hash_abort( &operation ) );
1905
1906 /* Call finish after calling verify. */
1907 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1908 PSA_ASSERT( psa_hash_verify( &operation,
1909 valid_hash, sizeof( valid_hash ) ) );
1910 TEST_EQUAL( psa_hash_finish( &operation,
1911 hash, sizeof( hash ), &hash_len ),
1912 PSA_ERROR_BAD_STATE );
1913 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001914
1915exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001916 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001917}
1918/* END_CASE */
1919
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001920/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001921void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001922{
1923 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001924 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1925 * appended to it */
1926 unsigned char hash[] = {
1927 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1928 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1929 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001930 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001931 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001932
Gilles Peskine8817f612018-12-18 00:18:46 +01001933 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001934
itayzafrir27e69452018-11-01 14:26:34 +02001935 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001936 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001937 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001938 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001939
itayzafrir27e69452018-11-01 14:26:34 +02001940 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001941 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001942 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001943 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001944
itayzafrir27e69452018-11-01 14:26:34 +02001945 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001946 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001947 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001948 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001949
itayzafrirec93d302018-10-18 18:01:10 +03001950exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001951 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001952}
1953/* END_CASE */
1954
Ronald Cronee414c72021-03-18 18:50:08 +01001955/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001956void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001957{
1958 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001959 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001960 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001961 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001962 size_t hash_len;
1963
Gilles Peskine8817f612018-12-18 00:18:46 +01001964 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001965
itayzafrir58028322018-10-25 10:22:01 +03001966 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001967 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001968 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001969 hash, expected_size - 1, &hash_len ),
1970 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001971
1972exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001973 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001974}
1975/* END_CASE */
1976
Ronald Cronee414c72021-03-18 18:50:08 +01001977/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001978void hash_clone_source_state( )
1979{
1980 psa_algorithm_t alg = PSA_ALG_SHA_256;
1981 unsigned char hash[PSA_HASH_MAX_SIZE];
1982 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1983 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1984 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1985 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1986 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1987 size_t hash_len;
1988
1989 PSA_ASSERT( psa_crypto_init( ) );
1990 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1991
1992 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1993 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1994 PSA_ASSERT( psa_hash_finish( &op_finished,
1995 hash, sizeof( hash ), &hash_len ) );
1996 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1997 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1998
1999 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2000 PSA_ERROR_BAD_STATE );
2001
2002 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2003 PSA_ASSERT( psa_hash_finish( &op_init,
2004 hash, sizeof( hash ), &hash_len ) );
2005 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2006 PSA_ASSERT( psa_hash_finish( &op_finished,
2007 hash, sizeof( hash ), &hash_len ) );
2008 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2009 PSA_ASSERT( psa_hash_finish( &op_aborted,
2010 hash, sizeof( hash ), &hash_len ) );
2011
2012exit:
2013 psa_hash_abort( &op_source );
2014 psa_hash_abort( &op_init );
2015 psa_hash_abort( &op_setup );
2016 psa_hash_abort( &op_finished );
2017 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002018 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002019}
2020/* END_CASE */
2021
Ronald Cronee414c72021-03-18 18:50:08 +01002022/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002023void hash_clone_target_state( )
2024{
2025 psa_algorithm_t alg = PSA_ALG_SHA_256;
2026 unsigned char hash[PSA_HASH_MAX_SIZE];
2027 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2028 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2029 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2030 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2031 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2032 size_t hash_len;
2033
2034 PSA_ASSERT( psa_crypto_init( ) );
2035
2036 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2037 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2038 PSA_ASSERT( psa_hash_finish( &op_finished,
2039 hash, sizeof( hash ), &hash_len ) );
2040 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2041 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2042
2043 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2044 PSA_ASSERT( psa_hash_finish( &op_target,
2045 hash, sizeof( hash ), &hash_len ) );
2046
2047 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2048 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2049 PSA_ERROR_BAD_STATE );
2050 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2051 PSA_ERROR_BAD_STATE );
2052
2053exit:
2054 psa_hash_abort( &op_target );
2055 psa_hash_abort( &op_init );
2056 psa_hash_abort( &op_setup );
2057 psa_hash_abort( &op_finished );
2058 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002059 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002060}
2061/* END_CASE */
2062
itayzafrir58028322018-10-25 10:22:01 +03002063/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002064void mac_operation_init( )
2065{
Jaeden Amero252ef282019-02-15 14:05:35 +00002066 const uint8_t input[1] = { 0 };
2067
Jaeden Amero769ce272019-01-04 11:48:03 +00002068 /* Test each valid way of initializing the object, except for `= {0}`, as
2069 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2070 * though it's OK by the C standard. We could test for this, but we'd need
2071 * to supress the Clang warning for the test. */
2072 psa_mac_operation_t func = psa_mac_operation_init( );
2073 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2074 psa_mac_operation_t zero;
2075
2076 memset( &zero, 0, sizeof( zero ) );
2077
Jaeden Amero252ef282019-02-15 14:05:35 +00002078 /* A freshly-initialized MAC operation should not be usable. */
2079 TEST_EQUAL( psa_mac_update( &func,
2080 input, sizeof( input ) ),
2081 PSA_ERROR_BAD_STATE );
2082 TEST_EQUAL( psa_mac_update( &init,
2083 input, sizeof( input ) ),
2084 PSA_ERROR_BAD_STATE );
2085 TEST_EQUAL( psa_mac_update( &zero,
2086 input, sizeof( input ) ),
2087 PSA_ERROR_BAD_STATE );
2088
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002089 /* A default MAC operation should be abortable without error. */
2090 PSA_ASSERT( psa_mac_abort( &func ) );
2091 PSA_ASSERT( psa_mac_abort( &init ) );
2092 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002093}
2094/* END_CASE */
2095
2096/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002097void mac_setup( int key_type_arg,
2098 data_t *key,
2099 int alg_arg,
2100 int expected_status_arg )
2101{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002102 psa_key_type_t key_type = key_type_arg;
2103 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002104 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002105 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002106 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2107#if defined(KNOWN_SUPPORTED_MAC_ALG)
2108 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2109#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002110
Gilles Peskine8817f612018-12-18 00:18:46 +01002111 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002112
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002113 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2114 &operation, &status ) )
2115 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002116 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002117
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002118 /* The operation object should be reusable. */
2119#if defined(KNOWN_SUPPORTED_MAC_ALG)
2120 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2121 smoke_test_key_data,
2122 sizeof( smoke_test_key_data ),
2123 KNOWN_SUPPORTED_MAC_ALG,
2124 &operation, &status ) )
2125 goto exit;
2126 TEST_EQUAL( status, PSA_SUCCESS );
2127#endif
2128
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002129exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002130 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002131}
2132/* END_CASE */
2133
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002134/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_HMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256 */
Jaeden Amero252ef282019-02-15 14:05:35 +00002135void mac_bad_order( )
2136{
Ronald Cron5425a212020-08-04 14:58:35 +02002137 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002138 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2139 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002140 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002141 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2142 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2143 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002144 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002145 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2146 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2147 size_t sign_mac_length = 0;
2148 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2149 const uint8_t verify_mac[] = {
2150 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2151 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2152 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2153
2154 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002155 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002156 psa_set_key_algorithm( &attributes, alg );
2157 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002158
Ronald Cron5425a212020-08-04 14:58:35 +02002159 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2160 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002161
Jaeden Amero252ef282019-02-15 14:05:35 +00002162 /* Call update without calling setup beforehand. */
2163 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2164 PSA_ERROR_BAD_STATE );
2165 PSA_ASSERT( psa_mac_abort( &operation ) );
2166
2167 /* Call sign finish without calling setup beforehand. */
2168 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2169 &sign_mac_length),
2170 PSA_ERROR_BAD_STATE );
2171 PSA_ASSERT( psa_mac_abort( &operation ) );
2172
2173 /* Call verify finish without calling setup beforehand. */
2174 TEST_EQUAL( psa_mac_verify_finish( &operation,
2175 verify_mac, sizeof( verify_mac ) ),
2176 PSA_ERROR_BAD_STATE );
2177 PSA_ASSERT( psa_mac_abort( &operation ) );
2178
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002179 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002180 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2181 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002182 PSA_ERROR_BAD_STATE );
2183 PSA_ASSERT( psa_mac_abort( &operation ) );
2184
Jaeden Amero252ef282019-02-15 14:05:35 +00002185 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002186 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002187 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2188 PSA_ASSERT( psa_mac_sign_finish( &operation,
2189 sign_mac, sizeof( sign_mac ),
2190 &sign_mac_length ) );
2191 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2192 PSA_ERROR_BAD_STATE );
2193 PSA_ASSERT( psa_mac_abort( &operation ) );
2194
2195 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002196 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002197 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2198 PSA_ASSERT( psa_mac_verify_finish( &operation,
2199 verify_mac, sizeof( verify_mac ) ) );
2200 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2201 PSA_ERROR_BAD_STATE );
2202 PSA_ASSERT( psa_mac_abort( &operation ) );
2203
2204 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002205 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002206 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2207 PSA_ASSERT( psa_mac_sign_finish( &operation,
2208 sign_mac, sizeof( sign_mac ),
2209 &sign_mac_length ) );
2210 TEST_EQUAL( psa_mac_sign_finish( &operation,
2211 sign_mac, sizeof( sign_mac ),
2212 &sign_mac_length ),
2213 PSA_ERROR_BAD_STATE );
2214 PSA_ASSERT( psa_mac_abort( &operation ) );
2215
2216 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002217 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002218 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2219 PSA_ASSERT( psa_mac_verify_finish( &operation,
2220 verify_mac, sizeof( verify_mac ) ) );
2221 TEST_EQUAL( psa_mac_verify_finish( &operation,
2222 verify_mac, sizeof( verify_mac ) ),
2223 PSA_ERROR_BAD_STATE );
2224 PSA_ASSERT( psa_mac_abort( &operation ) );
2225
2226 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002227 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002228 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2229 TEST_EQUAL( psa_mac_verify_finish( &operation,
2230 verify_mac, sizeof( verify_mac ) ),
2231 PSA_ERROR_BAD_STATE );
2232 PSA_ASSERT( psa_mac_abort( &operation ) );
2233
2234 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002235 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002236 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2237 TEST_EQUAL( psa_mac_sign_finish( &operation,
2238 sign_mac, sizeof( sign_mac ),
2239 &sign_mac_length ),
2240 PSA_ERROR_BAD_STATE );
2241 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002242
Ronald Cron5425a212020-08-04 14:58:35 +02002243 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002244
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002245exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002246 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002247}
2248/* END_CASE */
2249
2250/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002251void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002252 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002253 int alg_arg,
2254 data_t *input,
2255 data_t *expected_mac )
2256{
Ronald Cron5425a212020-08-04 14:58:35 +02002257 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002258 psa_key_type_t key_type = key_type_arg;
2259 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002260 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002261 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002262 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002263 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002264 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002265 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002266 const size_t output_sizes_to_test[] = {
2267 0,
2268 1,
2269 expected_mac->len - 1,
2270 expected_mac->len,
2271 expected_mac->len + 1,
2272 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002273
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002274 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002275 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002276 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002277
Gilles Peskine8817f612018-12-18 00:18:46 +01002278 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002279
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002280 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002281 psa_set_key_algorithm( &attributes, alg );
2282 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002283
Ronald Cron5425a212020-08-04 14:58:35 +02002284 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2285 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002286
Gilles Peskine8b356b52020-08-25 23:44:59 +02002287 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2288 {
2289 const size_t output_size = output_sizes_to_test[i];
2290 psa_status_t expected_status =
2291 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2292 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002293
Chris Jones9634bb12021-01-20 15:56:42 +00002294 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002295 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002296
Gilles Peskine8b356b52020-08-25 23:44:59 +02002297 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002298 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002299 PSA_ASSERT( psa_mac_update( &operation,
2300 input->x, input->len ) );
2301 TEST_EQUAL( psa_mac_sign_finish( &operation,
2302 actual_mac, output_size,
2303 &mac_length ),
2304 expected_status );
2305 PSA_ASSERT( psa_mac_abort( &operation ) );
2306
2307 if( expected_status == PSA_SUCCESS )
2308 {
2309 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2310 actual_mac, mac_length );
2311 }
2312 mbedtls_free( actual_mac );
2313 actual_mac = NULL;
2314 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002315
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002316exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002317 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002318 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002319 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002320 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002321}
2322/* END_CASE */
2323
2324/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002325void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002326 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002327 int alg_arg,
2328 data_t *input,
2329 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002330{
Ronald Cron5425a212020-08-04 14:58:35 +02002331 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002332 psa_key_type_t key_type = key_type_arg;
2333 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002334 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002335 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002336 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002337
Gilles Peskine69c12672018-06-28 00:07:19 +02002338 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2339
Gilles Peskine8817f612018-12-18 00:18:46 +01002340 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002341
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002342 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002343 psa_set_key_algorithm( &attributes, alg );
2344 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002345
Ronald Cron5425a212020-08-04 14:58:35 +02002346 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2347 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002348
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002349 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002350 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002351 PSA_ASSERT( psa_mac_update( &operation,
2352 input->x, input->len ) );
2353 PSA_ASSERT( psa_mac_verify_finish( &operation,
2354 expected_mac->x,
2355 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002356
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002357 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002358 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002359 PSA_ASSERT( psa_mac_update( &operation,
2360 input->x, input->len ) );
2361 TEST_EQUAL( psa_mac_verify_finish( &operation,
2362 expected_mac->x,
2363 expected_mac->len - 1 ),
2364 PSA_ERROR_INVALID_SIGNATURE );
2365
2366 /* Test a MAC that's too long. */
2367 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2368 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002369 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002370 PSA_ASSERT( psa_mac_update( &operation,
2371 input->x, input->len ) );
2372 TEST_EQUAL( psa_mac_verify_finish( &operation,
2373 perturbed_mac,
2374 expected_mac->len + 1 ),
2375 PSA_ERROR_INVALID_SIGNATURE );
2376
2377 /* Test changing one byte. */
2378 for( size_t i = 0; i < expected_mac->len; i++ )
2379 {
Chris Jones9634bb12021-01-20 15:56:42 +00002380 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002381 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002382 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002383 PSA_ASSERT( psa_mac_update( &operation,
2384 input->x, input->len ) );
2385 TEST_EQUAL( psa_mac_verify_finish( &operation,
2386 perturbed_mac,
2387 expected_mac->len ),
2388 PSA_ERROR_INVALID_SIGNATURE );
2389 perturbed_mac[i] ^= 1;
2390 }
2391
Gilles Peskine8c9def32018-02-08 10:02:12 +01002392exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002393 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002394 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002395 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002396 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002397}
2398/* END_CASE */
2399
2400/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002401void cipher_operation_init( )
2402{
Jaeden Ameroab439972019-02-15 14:12:05 +00002403 const uint8_t input[1] = { 0 };
2404 unsigned char output[1] = { 0 };
2405 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002406 /* Test each valid way of initializing the object, except for `= {0}`, as
2407 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2408 * though it's OK by the C standard. We could test for this, but we'd need
2409 * to supress the Clang warning for the test. */
2410 psa_cipher_operation_t func = psa_cipher_operation_init( );
2411 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2412 psa_cipher_operation_t zero;
2413
2414 memset( &zero, 0, sizeof( zero ) );
2415
Jaeden Ameroab439972019-02-15 14:12:05 +00002416 /* A freshly-initialized cipher operation should not be usable. */
2417 TEST_EQUAL( psa_cipher_update( &func,
2418 input, sizeof( input ),
2419 output, sizeof( output ),
2420 &output_length ),
2421 PSA_ERROR_BAD_STATE );
2422 TEST_EQUAL( psa_cipher_update( &init,
2423 input, sizeof( input ),
2424 output, sizeof( output ),
2425 &output_length ),
2426 PSA_ERROR_BAD_STATE );
2427 TEST_EQUAL( psa_cipher_update( &zero,
2428 input, sizeof( input ),
2429 output, sizeof( output ),
2430 &output_length ),
2431 PSA_ERROR_BAD_STATE );
2432
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002433 /* A default cipher operation should be abortable without error. */
2434 PSA_ASSERT( psa_cipher_abort( &func ) );
2435 PSA_ASSERT( psa_cipher_abort( &init ) );
2436 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002437}
2438/* END_CASE */
2439
2440/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002441void cipher_setup( int key_type_arg,
2442 data_t *key,
2443 int alg_arg,
2444 int expected_status_arg )
2445{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002446 psa_key_type_t key_type = key_type_arg;
2447 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002448 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002449 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002450 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002451#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002452 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2453#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002454
Gilles Peskine8817f612018-12-18 00:18:46 +01002455 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002456
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002457 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2458 &operation, &status ) )
2459 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002460 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002461
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002462 /* The operation object should be reusable. */
2463#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2464 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2465 smoke_test_key_data,
2466 sizeof( smoke_test_key_data ),
2467 KNOWN_SUPPORTED_CIPHER_ALG,
2468 &operation, &status ) )
2469 goto exit;
2470 TEST_EQUAL( status, PSA_SUCCESS );
2471#endif
2472
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002473exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002474 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002475 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002476}
2477/* END_CASE */
2478
Ronald Cronee414c72021-03-18 18:50:08 +01002479/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002480void cipher_bad_order( )
2481{
Ronald Cron5425a212020-08-04 14:58:35 +02002482 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002483 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2484 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002485 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002486 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002487 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002488 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002489 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2490 0xaa, 0xaa, 0xaa, 0xaa };
2491 const uint8_t text[] = {
2492 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2493 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002494 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002495 size_t length = 0;
2496
2497 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002498 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2499 psa_set_key_algorithm( &attributes, alg );
2500 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002501 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2502 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002503
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002504 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002505 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2506 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002507 PSA_ERROR_BAD_STATE );
2508 PSA_ASSERT( psa_cipher_abort( &operation ) );
2509
2510 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002511 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2512 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002513 PSA_ERROR_BAD_STATE );
2514 PSA_ASSERT( psa_cipher_abort( &operation ) );
2515
Jaeden Ameroab439972019-02-15 14:12:05 +00002516 /* Generate an IV without calling setup beforehand. */
2517 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2518 buffer, sizeof( buffer ),
2519 &length ),
2520 PSA_ERROR_BAD_STATE );
2521 PSA_ASSERT( psa_cipher_abort( &operation ) );
2522
2523 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002524 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002525 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2526 buffer, sizeof( buffer ),
2527 &length ) );
2528 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2529 buffer, sizeof( buffer ),
2530 &length ),
2531 PSA_ERROR_BAD_STATE );
2532 PSA_ASSERT( psa_cipher_abort( &operation ) );
2533
2534 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002535 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002536 PSA_ASSERT( psa_cipher_set_iv( &operation,
2537 iv, sizeof( iv ) ) );
2538 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2539 buffer, sizeof( buffer ),
2540 &length ),
2541 PSA_ERROR_BAD_STATE );
2542 PSA_ASSERT( psa_cipher_abort( &operation ) );
2543
2544 /* Set an IV without calling setup beforehand. */
2545 TEST_EQUAL( psa_cipher_set_iv( &operation,
2546 iv, sizeof( iv ) ),
2547 PSA_ERROR_BAD_STATE );
2548 PSA_ASSERT( psa_cipher_abort( &operation ) );
2549
2550 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002551 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002552 PSA_ASSERT( psa_cipher_set_iv( &operation,
2553 iv, sizeof( iv ) ) );
2554 TEST_EQUAL( psa_cipher_set_iv( &operation,
2555 iv, sizeof( iv ) ),
2556 PSA_ERROR_BAD_STATE );
2557 PSA_ASSERT( psa_cipher_abort( &operation ) );
2558
2559 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002560 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002561 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2562 buffer, sizeof( buffer ),
2563 &length ) );
2564 TEST_EQUAL( psa_cipher_set_iv( &operation,
2565 iv, sizeof( iv ) ),
2566 PSA_ERROR_BAD_STATE );
2567 PSA_ASSERT( psa_cipher_abort( &operation ) );
2568
2569 /* Call update without calling setup beforehand. */
2570 TEST_EQUAL( psa_cipher_update( &operation,
2571 text, sizeof( text ),
2572 buffer, sizeof( buffer ),
2573 &length ),
2574 PSA_ERROR_BAD_STATE );
2575 PSA_ASSERT( psa_cipher_abort( &operation ) );
2576
2577 /* Call update without an IV where an IV is required. */
2578 TEST_EQUAL( psa_cipher_update( &operation,
2579 text, sizeof( text ),
2580 buffer, sizeof( buffer ),
2581 &length ),
2582 PSA_ERROR_BAD_STATE );
2583 PSA_ASSERT( psa_cipher_abort( &operation ) );
2584
2585 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002586 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002587 PSA_ASSERT( psa_cipher_set_iv( &operation,
2588 iv, sizeof( iv ) ) );
2589 PSA_ASSERT( psa_cipher_finish( &operation,
2590 buffer, sizeof( buffer ), &length ) );
2591 TEST_EQUAL( psa_cipher_update( &operation,
2592 text, sizeof( text ),
2593 buffer, sizeof( buffer ),
2594 &length ),
2595 PSA_ERROR_BAD_STATE );
2596 PSA_ASSERT( psa_cipher_abort( &operation ) );
2597
2598 /* Call finish without calling setup beforehand. */
2599 TEST_EQUAL( psa_cipher_finish( &operation,
2600 buffer, sizeof( buffer ), &length ),
2601 PSA_ERROR_BAD_STATE );
2602 PSA_ASSERT( psa_cipher_abort( &operation ) );
2603
2604 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002605 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002606 /* Not calling update means we are encrypting an empty buffer, which is OK
2607 * for cipher modes with padding. */
2608 TEST_EQUAL( psa_cipher_finish( &operation,
2609 buffer, sizeof( buffer ), &length ),
2610 PSA_ERROR_BAD_STATE );
2611 PSA_ASSERT( psa_cipher_abort( &operation ) );
2612
2613 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002614 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002615 PSA_ASSERT( psa_cipher_set_iv( &operation,
2616 iv, sizeof( iv ) ) );
2617 PSA_ASSERT( psa_cipher_finish( &operation,
2618 buffer, sizeof( buffer ), &length ) );
2619 TEST_EQUAL( psa_cipher_finish( &operation,
2620 buffer, sizeof( buffer ), &length ),
2621 PSA_ERROR_BAD_STATE );
2622 PSA_ASSERT( psa_cipher_abort( &operation ) );
2623
Ronald Cron5425a212020-08-04 14:58:35 +02002624 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002625
Jaeden Ameroab439972019-02-15 14:12:05 +00002626exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002627 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002628 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002629}
2630/* END_CASE */
2631
2632/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002633void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002634 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002635 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002636 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002637{
Ronald Cron5425a212020-08-04 14:58:35 +02002638 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002639 psa_status_t status;
2640 psa_key_type_t key_type = key_type_arg;
2641 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002642 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002643 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002644 size_t output_buffer_size = 0;
2645 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002646 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002647 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002648 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002649
Gilles Peskine8817f612018-12-18 00:18:46 +01002650 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002651
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002652 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2653 psa_set_key_algorithm( &attributes, alg );
2654 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002655
Ronald Cron5425a212020-08-04 14:58:35 +02002656 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2657 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002658
Ronald Cron5425a212020-08-04 14:58:35 +02002659 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002660
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002661 if( iv->len > 0 )
2662 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002663 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002664 }
2665
gabor-mezei-armceface22021-01-21 12:26:17 +01002666 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2667 TEST_ASSERT( output_buffer_size <=
2668 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002669 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002670
Gilles Peskine8817f612018-12-18 00:18:46 +01002671 PSA_ASSERT( psa_cipher_update( &operation,
2672 input->x, input->len,
2673 output, output_buffer_size,
2674 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002675 TEST_ASSERT( function_output_length <=
2676 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2677 TEST_ASSERT( function_output_length <=
2678 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002679 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002680
Gilles Peskine50e586b2018-06-08 14:28:46 +02002681 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002682 ( output_buffer_size == 0 ? NULL :
2683 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002684 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002685 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002686 TEST_ASSERT( function_output_length <=
2687 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2688 TEST_ASSERT( function_output_length <=
2689 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002690 total_output_length += function_output_length;
2691
Gilles Peskinefe11b722018-12-18 00:24:04 +01002692 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002693 if( expected_status == PSA_SUCCESS )
2694 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002695 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002696 ASSERT_COMPARE( expected_output->x, expected_output->len,
2697 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002698 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002699
Gilles Peskine50e586b2018-06-08 14:28:46 +02002700exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002701 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002702 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002703 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002704 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002705}
2706/* END_CASE */
2707
2708/* BEGIN_CASE */
2709void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002710 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002711 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002712 int first_part_size_arg,
2713 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002714 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002715{
Ronald Cron5425a212020-08-04 14:58:35 +02002716 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002717 psa_key_type_t key_type = key_type_arg;
2718 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002719 size_t first_part_size = first_part_size_arg;
2720 size_t output1_length = output1_length_arg;
2721 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002722 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002723 size_t output_buffer_size = 0;
2724 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002725 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002726 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002727 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002728
Gilles Peskine8817f612018-12-18 00:18:46 +01002729 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002730
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002731 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2732 psa_set_key_algorithm( &attributes, alg );
2733 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002734
Ronald Cron5425a212020-08-04 14:58:35 +02002735 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2736 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002737
Ronald Cron5425a212020-08-04 14:58:35 +02002738 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002739
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002740 if( iv->len > 0 )
2741 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002742 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002743 }
2744
gabor-mezei-armceface22021-01-21 12:26:17 +01002745 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2746 TEST_ASSERT( output_buffer_size <=
2747 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002748 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002749
Gilles Peskinee0866522019-02-19 19:44:00 +01002750 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002751 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2752 output, output_buffer_size,
2753 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002754 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002755 TEST_ASSERT( function_output_length <=
2756 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2757 TEST_ASSERT( function_output_length <=
2758 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002759 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002760
Gilles Peskine8817f612018-12-18 00:18:46 +01002761 PSA_ASSERT( psa_cipher_update( &operation,
2762 input->x + first_part_size,
2763 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002764 ( output_buffer_size == 0 ? NULL :
2765 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002766 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002767 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002768 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002769 TEST_ASSERT( function_output_length <=
2770 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2771 alg,
2772 input->len - first_part_size ) );
2773 TEST_ASSERT( function_output_length <=
2774 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002775 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002776
Gilles Peskine8817f612018-12-18 00:18:46 +01002777 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002778 ( output_buffer_size == 0 ? NULL :
2779 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002780 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002781 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002782 TEST_ASSERT( function_output_length <=
2783 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2784 TEST_ASSERT( function_output_length <=
2785 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002786 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002787 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002788
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002789 ASSERT_COMPARE( expected_output->x, expected_output->len,
2790 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002791
2792exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002793 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002794 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002795 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002796 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002797}
2798/* END_CASE */
2799
2800/* BEGIN_CASE */
2801void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002802 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002803 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002804 int first_part_size_arg,
2805 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002806 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002807{
Ronald Cron5425a212020-08-04 14:58:35 +02002808 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002809 psa_key_type_t key_type = key_type_arg;
2810 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002811 size_t first_part_size = first_part_size_arg;
2812 size_t output1_length = output1_length_arg;
2813 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002814 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002815 size_t output_buffer_size = 0;
2816 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002817 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002818 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002819 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002820
Gilles Peskine8817f612018-12-18 00:18:46 +01002821 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002822
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002823 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2824 psa_set_key_algorithm( &attributes, alg );
2825 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002826
Ronald Cron5425a212020-08-04 14:58:35 +02002827 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2828 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002829
Ronald Cron5425a212020-08-04 14:58:35 +02002830 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002831
Steven Cooreman177deba2020-09-07 17:14:14 +02002832 if( iv->len > 0 )
2833 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002834 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002835 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002836
gabor-mezei-armceface22021-01-21 12:26:17 +01002837 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2838 TEST_ASSERT( output_buffer_size <=
2839 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002840 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002841
Gilles Peskinee0866522019-02-19 19:44:00 +01002842 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002843 PSA_ASSERT( psa_cipher_update( &operation,
2844 input->x, first_part_size,
2845 output, output_buffer_size,
2846 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002847 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002848 TEST_ASSERT( function_output_length <=
2849 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2850 TEST_ASSERT( function_output_length <=
2851 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002852 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002853
Gilles Peskine8817f612018-12-18 00:18:46 +01002854 PSA_ASSERT( psa_cipher_update( &operation,
2855 input->x + first_part_size,
2856 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002857 ( output_buffer_size == 0 ? NULL :
2858 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002859 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002860 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002861 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002862 TEST_ASSERT( function_output_length <=
2863 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2864 alg,
2865 input->len - first_part_size ) );
2866 TEST_ASSERT( function_output_length <=
2867 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002868 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002869
Gilles Peskine8817f612018-12-18 00:18:46 +01002870 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002871 ( output_buffer_size == 0 ? NULL :
2872 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002873 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002874 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002875 TEST_ASSERT( function_output_length <=
2876 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2877 TEST_ASSERT( function_output_length <=
2878 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002879 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002880 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002881
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002882 ASSERT_COMPARE( expected_output->x, expected_output->len,
2883 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002884
2885exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002886 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002887 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002888 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002889 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002890}
2891/* END_CASE */
2892
Gilles Peskine50e586b2018-06-08 14:28:46 +02002893/* BEGIN_CASE */
2894void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002895 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002896 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002897 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002898{
Ronald Cron5425a212020-08-04 14:58:35 +02002899 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002900 psa_status_t status;
2901 psa_key_type_t key_type = key_type_arg;
2902 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002903 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002904 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002905 size_t output_buffer_size = 0;
2906 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002907 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002908 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002909 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002910
Gilles Peskine8817f612018-12-18 00:18:46 +01002911 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002912
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002913 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2914 psa_set_key_algorithm( &attributes, alg );
2915 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002916
Ronald Cron5425a212020-08-04 14:58:35 +02002917 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2918 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002919
Ronald Cron5425a212020-08-04 14:58:35 +02002920 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002921
Steven Cooreman177deba2020-09-07 17:14:14 +02002922 if( iv->len > 0 )
2923 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002924 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002925 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002926
gabor-mezei-armceface22021-01-21 12:26:17 +01002927 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2928 TEST_ASSERT( output_buffer_size <=
2929 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002930 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002931
Gilles Peskine8817f612018-12-18 00:18:46 +01002932 PSA_ASSERT( psa_cipher_update( &operation,
2933 input->x, input->len,
2934 output, output_buffer_size,
2935 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002936 TEST_ASSERT( function_output_length <=
2937 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2938 TEST_ASSERT( function_output_length <=
2939 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002940 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002941
Gilles Peskine50e586b2018-06-08 14:28:46 +02002942 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002943 ( output_buffer_size == 0 ? NULL :
2944 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002945 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002946 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002947 TEST_ASSERT( function_output_length <=
2948 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2949 TEST_ASSERT( function_output_length <=
2950 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002951 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002952 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002953
2954 if( expected_status == PSA_SUCCESS )
2955 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002956 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002957 ASSERT_COMPARE( expected_output->x, expected_output->len,
2958 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002959 }
2960
Gilles Peskine50e586b2018-06-08 14:28:46 +02002961exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002962 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002963 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002964 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002965 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002966}
2967/* END_CASE */
2968
Gilles Peskine50e586b2018-06-08 14:28:46 +02002969/* BEGIN_CASE */
2970void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002971 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002972 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002973{
Ronald Cron5425a212020-08-04 14:58:35 +02002974 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002975 psa_key_type_t key_type = key_type_arg;
2976 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002977 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002978 size_t iv_size = 16;
2979 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002980 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002981 size_t output1_size = 0;
2982 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002983 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002984 size_t output2_size = 0;
2985 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002986 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002987 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2988 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002989 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002990
Gilles Peskine8817f612018-12-18 00:18:46 +01002991 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002992
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002993 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2994 psa_set_key_algorithm( &attributes, alg );
2995 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002996
Ronald Cron5425a212020-08-04 14:58:35 +02002997 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2998 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002999
Ronald Cron5425a212020-08-04 14:58:35 +02003000 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3001 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003002
Steven Cooreman177deba2020-09-07 17:14:14 +02003003 if( alg != PSA_ALG_ECB_NO_PADDING )
3004 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003005 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3006 iv, iv_size,
3007 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003008 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003009 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3010 TEST_ASSERT( output1_size <=
3011 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003012 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003013
Gilles Peskine8817f612018-12-18 00:18:46 +01003014 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3015 output1, output1_size,
3016 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003017 TEST_ASSERT( output1_length <=
3018 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3019 TEST_ASSERT( output1_length <=
3020 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3021
Gilles Peskine8817f612018-12-18 00:18:46 +01003022 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003023 output1 + output1_length,
3024 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003025 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003026 TEST_ASSERT( function_output_length <=
3027 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3028 TEST_ASSERT( function_output_length <=
3029 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003030
Gilles Peskine048b7f02018-06-08 14:20:49 +02003031 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003032
Gilles Peskine8817f612018-12-18 00:18:46 +01003033 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003034
3035 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003036 TEST_ASSERT( output2_size <=
3037 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3038 TEST_ASSERT( output2_size <=
3039 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003040 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003041
Steven Cooreman177deba2020-09-07 17:14:14 +02003042 if( iv_length > 0 )
3043 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003044 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3045 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003046 }
3047
Gilles Peskine8817f612018-12-18 00:18:46 +01003048 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3049 output2, output2_size,
3050 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003051 TEST_ASSERT( output2_length <=
3052 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
3053 TEST_ASSERT( output2_length <=
3054 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
3055
Gilles Peskine048b7f02018-06-08 14:20:49 +02003056 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003057 PSA_ASSERT( psa_cipher_finish( &operation2,
3058 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003059 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003060 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003061 TEST_ASSERT( function_output_length <=
3062 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3063 TEST_ASSERT( function_output_length <=
3064 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03003065
Gilles Peskine048b7f02018-06-08 14:20:49 +02003066 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003067
Gilles Peskine8817f612018-12-18 00:18:46 +01003068 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003069
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003070 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003071
3072exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003073 psa_cipher_abort( &operation1 );
3074 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003075 mbedtls_free( output1 );
3076 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003077 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003078 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003079}
3080/* END_CASE */
3081
3082/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003083void cipher_verify_output_multipart( int alg_arg,
3084 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003085 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003086 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003087 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003088{
Ronald Cron5425a212020-08-04 14:58:35 +02003089 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003090 psa_key_type_t key_type = key_type_arg;
3091 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003092 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003093 unsigned char iv[16] = {0};
3094 size_t iv_size = 16;
3095 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003096 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003097 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003098 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003099 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003100 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003101 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003102 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003103 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3104 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003105 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003106
Gilles Peskine8817f612018-12-18 00:18:46 +01003107 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003108
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003109 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3110 psa_set_key_algorithm( &attributes, alg );
3111 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003112
Ronald Cron5425a212020-08-04 14:58:35 +02003113 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3114 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003115
Ronald Cron5425a212020-08-04 14:58:35 +02003116 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3117 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003118
Steven Cooreman177deba2020-09-07 17:14:14 +02003119 if( alg != PSA_ALG_ECB_NO_PADDING )
3120 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003121 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3122 iv, iv_size,
3123 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003124 }
3125
gabor-mezei-armceface22021-01-21 12:26:17 +01003126 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3127 TEST_ASSERT( output1_buffer_size <=
3128 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003129 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003130
Gilles Peskinee0866522019-02-19 19:44:00 +01003131 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003132
Gilles Peskine8817f612018-12-18 00:18:46 +01003133 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3134 output1, output1_buffer_size,
3135 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003136 TEST_ASSERT( function_output_length <=
3137 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3138 TEST_ASSERT( function_output_length <=
3139 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003140 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003141
Gilles Peskine8817f612018-12-18 00:18:46 +01003142 PSA_ASSERT( psa_cipher_update( &operation1,
3143 input->x + first_part_size,
3144 input->len - first_part_size,
3145 output1, output1_buffer_size,
3146 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003147 TEST_ASSERT( function_output_length <=
3148 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3149 alg,
3150 input->len - first_part_size ) );
3151 TEST_ASSERT( function_output_length <=
3152 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003153 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003154
Gilles Peskine8817f612018-12-18 00:18:46 +01003155 PSA_ASSERT( psa_cipher_finish( &operation1,
3156 output1 + output1_length,
3157 output1_buffer_size - output1_length,
3158 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003159 TEST_ASSERT( function_output_length <=
3160 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3161 TEST_ASSERT( function_output_length <=
3162 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003163 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003164
Gilles Peskine8817f612018-12-18 00:18:46 +01003165 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003166
Gilles Peskine048b7f02018-06-08 14:20:49 +02003167 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003168 TEST_ASSERT( output2_buffer_size <=
3169 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3170 TEST_ASSERT( output2_buffer_size <=
3171 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003172 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003173
Steven Cooreman177deba2020-09-07 17:14:14 +02003174 if( iv_length > 0 )
3175 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003176 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3177 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003178 }
Moran Pekerded84402018-06-06 16:36:50 +03003179
Gilles Peskine8817f612018-12-18 00:18:46 +01003180 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3181 output2, output2_buffer_size,
3182 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003183 TEST_ASSERT( function_output_length <=
3184 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3185 TEST_ASSERT( function_output_length <=
3186 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003187 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003188
Gilles Peskine8817f612018-12-18 00:18:46 +01003189 PSA_ASSERT( psa_cipher_update( &operation2,
3190 output1 + first_part_size,
3191 output1_length - first_part_size,
3192 output2, output2_buffer_size,
3193 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003194 TEST_ASSERT( function_output_length <=
3195 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3196 alg,
3197 output1_length - first_part_size ) );
3198 TEST_ASSERT( function_output_length <=
3199 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003200 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003201
Gilles Peskine8817f612018-12-18 00:18:46 +01003202 PSA_ASSERT( psa_cipher_finish( &operation2,
3203 output2 + output2_length,
3204 output2_buffer_size - output2_length,
3205 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003206 TEST_ASSERT( function_output_length <=
3207 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3208 TEST_ASSERT( function_output_length <=
3209 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003210 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003211
Gilles Peskine8817f612018-12-18 00:18:46 +01003212 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003213
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003214 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003215
3216exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003217 psa_cipher_abort( &operation1 );
3218 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003219 mbedtls_free( output1 );
3220 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003221 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003222 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003223}
3224/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003225
Gilles Peskine20035e32018-02-03 22:44:14 +01003226/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003227void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003228 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003229 data_t *nonce,
3230 data_t *additional_data,
3231 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003232 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003233{
Ronald Cron5425a212020-08-04 14:58:35 +02003234 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003235 psa_key_type_t key_type = key_type_arg;
3236 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003237 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003238 unsigned char *output_data = NULL;
3239 size_t output_size = 0;
3240 size_t output_length = 0;
3241 unsigned char *output_data2 = NULL;
3242 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003243 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003244 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003245 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003246
Gilles Peskine8817f612018-12-18 00:18:46 +01003247 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003248
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003249 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3250 psa_set_key_algorithm( &attributes, alg );
3251 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003252
Gilles Peskine049c7532019-05-15 20:22:09 +02003253 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003254 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003255 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3256 key_bits = psa_get_key_bits( &attributes );
3257
3258 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3259 alg );
3260 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3261 * should be exact. */
3262 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3263 expected_result != PSA_ERROR_NOT_SUPPORTED )
3264 {
3265 TEST_EQUAL( output_size,
3266 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3267 TEST_ASSERT( output_size <=
3268 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3269 }
3270 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003271
Steven Cooremanf49478b2021-02-15 15:19:25 +01003272 status = psa_aead_encrypt( key, alg,
3273 nonce->x, nonce->len,
3274 additional_data->x,
3275 additional_data->len,
3276 input_data->x, input_data->len,
3277 output_data, output_size,
3278 &output_length );
3279
3280 /* If the operation is not supported, just skip and not fail in case the
3281 * encryption involves a common limitation of cryptography hardwares and
3282 * an alternative implementation. */
3283 if( status == PSA_ERROR_NOT_SUPPORTED )
3284 {
3285 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3286 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3287 }
3288
3289 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003290
3291 if( PSA_SUCCESS == expected_result )
3292 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003293 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003294
Gilles Peskine003a4a92019-05-14 16:09:40 +02003295 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3296 * should be exact. */
3297 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003298 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003299
gabor-mezei-armceface22021-01-21 12:26:17 +01003300 TEST_ASSERT( input_data->len <=
3301 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3302
Ronald Cron5425a212020-08-04 14:58:35 +02003303 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003304 nonce->x, nonce->len,
3305 additional_data->x,
3306 additional_data->len,
3307 output_data, output_length,
3308 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003309 &output_length2 ),
3310 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003311
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003312 ASSERT_COMPARE( input_data->x, input_data->len,
3313 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003314 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003315
Gilles Peskinea1cac842018-06-11 19:33:02 +02003316exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003317 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003318 mbedtls_free( output_data );
3319 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003320 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003321}
3322/* END_CASE */
3323
3324/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003325void aead_encrypt( int key_type_arg, data_t *key_data,
3326 int alg_arg,
3327 data_t *nonce,
3328 data_t *additional_data,
3329 data_t *input_data,
3330 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003331{
Ronald Cron5425a212020-08-04 14:58:35 +02003332 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003333 psa_key_type_t key_type = key_type_arg;
3334 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003335 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003336 unsigned char *output_data = NULL;
3337 size_t output_size = 0;
3338 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003339 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003340 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003341
Gilles Peskine8817f612018-12-18 00:18:46 +01003342 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003343
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003344 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3345 psa_set_key_algorithm( &attributes, alg );
3346 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003347
Gilles Peskine049c7532019-05-15 20:22:09 +02003348 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003349 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003350 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3351 key_bits = psa_get_key_bits( &attributes );
3352
3353 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3354 alg );
3355 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3356 * should be exact. */
3357 TEST_EQUAL( output_size,
3358 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3359 TEST_ASSERT( output_size <=
3360 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3361 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003362
Steven Cooremand588ea12021-01-11 19:36:04 +01003363 status = psa_aead_encrypt( key, alg,
3364 nonce->x, nonce->len,
3365 additional_data->x, additional_data->len,
3366 input_data->x, input_data->len,
3367 output_data, output_size,
3368 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003369
Ronald Cron28a45ed2021-02-09 20:35:42 +01003370 /* If the operation is not supported, just skip and not fail in case the
3371 * encryption involves a common limitation of cryptography hardwares and
3372 * an alternative implementation. */
3373 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003374 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003375 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3376 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003377 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003378
3379 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003380 ASSERT_COMPARE( expected_result->x, expected_result->len,
3381 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003382
Gilles Peskinea1cac842018-06-11 19:33:02 +02003383exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003384 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003385 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003386 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003387}
3388/* END_CASE */
3389
3390/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003391void aead_decrypt( int key_type_arg, data_t *key_data,
3392 int alg_arg,
3393 data_t *nonce,
3394 data_t *additional_data,
3395 data_t *input_data,
3396 data_t *expected_data,
3397 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003398{
Ronald Cron5425a212020-08-04 14:58:35 +02003399 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003400 psa_key_type_t key_type = key_type_arg;
3401 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003402 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003403 unsigned char *output_data = NULL;
3404 size_t output_size = 0;
3405 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003406 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003407 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003408 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003409
Gilles Peskine8817f612018-12-18 00:18:46 +01003410 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003411
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003412 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3413 psa_set_key_algorithm( &attributes, alg );
3414 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003415
Gilles Peskine049c7532019-05-15 20:22:09 +02003416 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003417 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003418 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3419 key_bits = psa_get_key_bits( &attributes );
3420
3421 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3422 alg );
3423 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3424 expected_result != PSA_ERROR_NOT_SUPPORTED )
3425 {
3426 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3427 * should be exact. */
3428 TEST_EQUAL( output_size,
3429 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3430 TEST_ASSERT( output_size <=
3431 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3432 }
3433 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003434
Steven Cooremand588ea12021-01-11 19:36:04 +01003435 status = psa_aead_decrypt( key, alg,
3436 nonce->x, nonce->len,
3437 additional_data->x,
3438 additional_data->len,
3439 input_data->x, input_data->len,
3440 output_data, output_size,
3441 &output_length );
3442
Ronald Cron28a45ed2021-02-09 20:35:42 +01003443 /* If the operation is not supported, just skip and not fail in case the
3444 * decryption involves a common limitation of cryptography hardwares and
3445 * an alternative implementation. */
3446 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003447 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003448 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3449 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003450 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003451
3452 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003453
Gilles Peskine2d277862018-06-18 15:41:12 +02003454 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003455 ASSERT_COMPARE( expected_data->x, expected_data->len,
3456 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003457
Gilles Peskinea1cac842018-06-11 19:33:02 +02003458exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003459 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003460 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003461 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003462}
3463/* END_CASE */
3464
3465/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003466void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3467 int alg_arg,
3468 data_t *nonce,
3469 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003470 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003471 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003472 int do_test_data_chunked,
3473 int do_set_lengths,
3474 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003475{
Paul Elliottd3f82412021-06-16 16:52:21 +01003476 size_t ad_part_len = 0;
3477 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01003478 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003479
Paul Elliotte64deda2021-09-09 14:07:23 +01003480 /* Ensure that either one part of the test or the other is done, i.e this
3481 * test does something. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003482 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3483
3484 /* Temporary whilst we have algorithms that cannot support chunking */
3485 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003486 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003487 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3488 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003489 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003490 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003491
Paul Elliott33746aa2021-09-15 16:40:40 +01003492 if( do_set_lengths )
3493 {
3494 if( ad_part_len & 0x01 )
3495 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3496 else
3497 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3498 }
3499
Paul Elliott329d5382021-07-22 17:10:45 +01003500 /* Split ad into length(ad_part_len) parts. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003501 if( !aead_multipart_internal_func( key_type_arg, key_data,
3502 alg_arg, nonce,
3503 additional_data,
3504 ad_part_len,
3505 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003506 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003507 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003508 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003509 break;
3510
3511 /* length(0) part, length(ad_part_len) part, length(0) part... */
3512 mbedtls_test_set_step( 1000 + ad_part_len );
3513
3514 if( !aead_multipart_internal_func( key_type_arg, key_data,
3515 alg_arg, nonce,
3516 additional_data,
3517 ad_part_len,
3518 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003519 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003520 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003521 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003522 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003523 }
3524 }
Paul Elliottd3f82412021-06-16 16:52:21 +01003525
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003526 /* Temporary whilst we have algorithms that cannot support chunking */
3527 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003528 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003529 for( data_part_len = 1; data_part_len <= input_data->len;
3530 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003531 {
Paul Elliott329d5382021-07-22 17:10:45 +01003532 /* Split data into length(data_part_len) parts. */
3533 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003534
Paul Elliott33746aa2021-09-15 16:40:40 +01003535 if( do_set_lengths )
3536 {
3537 if( data_part_len & 0x01 )
3538 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3539 else
3540 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3541 }
3542
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003543 if( !aead_multipart_internal_func( key_type_arg, key_data,
3544 alg_arg, nonce,
3545 additional_data, -1,
3546 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003547 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003548 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003549 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003550 break;
3551
3552 /* length(0) part, length(data_part_len) part, length(0) part... */
3553 mbedtls_test_set_step( 3000 + data_part_len );
3554
3555 if( !aead_multipart_internal_func( key_type_arg, key_data,
3556 alg_arg, nonce,
3557 additional_data, -1,
3558 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003559 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003560 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003561 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003562 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003563 }
3564 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003565
Paul Elliott0023e0a2021-04-27 10:06:22 +01003566
Paul Elliott8fc45162021-06-23 16:06:01 +01003567 /* Goto is required to silence warnings about unused labels, as we
3568 * don't actually do any test assertions in this function. */
3569 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003570}
3571/* END_CASE */
3572
3573/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003574void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3575 int alg_arg,
3576 data_t *nonce,
3577 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003578 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003579 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003580 int do_test_data_chunked,
3581 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01003582 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003583{
Paul Elliottd3f82412021-06-16 16:52:21 +01003584 size_t ad_part_len = 0;
3585 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01003586 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003587
Paul Elliotte64deda2021-09-09 14:07:23 +01003588 /* Ensure that either one part of the test or the other is done, i.e this
3589 * test does something. */
3590 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3591
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003592 /* Temporary whilst we have algorithms that cannot support chunking */
3593 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003594 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003595 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3596 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003597 {
Paul Elliott329d5382021-07-22 17:10:45 +01003598 /* Split ad into length(ad_part_len) parts. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003599 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003600
Paul Elliott33746aa2021-09-15 16:40:40 +01003601 if( do_set_lengths )
3602 {
3603 if( ad_part_len & 0x01 )
3604 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3605 else
3606 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3607 }
3608
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003609 if( !aead_multipart_internal_func( key_type_arg, key_data,
3610 alg_arg, nonce,
3611 additional_data,
3612 ad_part_len,
3613 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003614 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003615 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003616 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003617 break;
3618
3619 /* length(0) part, length(ad_part_len) part, length(0) part... */
3620 mbedtls_test_set_step( 1000 + ad_part_len );
3621
3622 if( !aead_multipart_internal_func( key_type_arg, key_data,
3623 alg_arg, nonce,
3624 additional_data,
3625 ad_part_len,
3626 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003627 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003628 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003629 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003630 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003631 }
3632 }
3633
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003634 /* Temporary whilst we have algorithms that cannot support chunking */
3635 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003636 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003637 for( data_part_len = 1; data_part_len <= input_data->len;
3638 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003639 {
Paul Elliott329d5382021-07-22 17:10:45 +01003640 /* Split data into length(data_part_len) parts. */
3641 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003642
Paul Elliott33746aa2021-09-15 16:40:40 +01003643 if( do_set_lengths )
3644 {
3645 if( data_part_len & 0x01 )
3646 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3647 else
3648 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3649 }
3650
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003651 if( !aead_multipart_internal_func( key_type_arg, key_data,
3652 alg_arg, nonce,
3653 additional_data, -1,
3654 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003655 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003656 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003657 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003658 break;
3659
3660 /* length(0) part, length(data_part_len) part, length(0) part... */
3661 mbedtls_test_set_step( 3000 + data_part_len );
3662
3663 if( !aead_multipart_internal_func( key_type_arg, key_data,
3664 alg_arg, nonce,
3665 additional_data, -1,
3666 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003667 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003668 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003669 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003670 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003671 }
3672 }
3673
Paul Elliott8fc45162021-06-23 16:06:01 +01003674 /* Goto is required to silence warnings about unused labels, as we
3675 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003676 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003677}
3678/* END_CASE */
3679
3680/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003681void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
3682 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01003683 int nonce_length,
3684 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003685 data_t *additional_data,
3686 data_t *input_data,
3687 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003688{
3689
3690 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3691 psa_key_type_t key_type = key_type_arg;
3692 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01003693 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003694 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
3695 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3696 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01003697 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01003698 size_t actual_nonce_length = 0;
3699 size_t expected_nonce_length = expected_nonce_length_arg;
3700 unsigned char *output = NULL;
3701 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003702 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01003703 size_t ciphertext_size = 0;
3704 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003705 size_t tag_length = 0;
3706 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003707
3708 PSA_ASSERT( psa_crypto_init( ) );
3709
3710 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
3711 psa_set_key_algorithm( & attributes, alg );
3712 psa_set_key_type( & attributes, key_type );
3713
3714 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3715 &key ) );
3716
3717 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3718
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003719 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3720
Paul Elliottf1277632021-08-24 18:11:37 +01003721 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003722
Paul Elliottf1277632021-08-24 18:11:37 +01003723 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003724
Paul Elliottf1277632021-08-24 18:11:37 +01003725 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003726
Paul Elliottf1277632021-08-24 18:11:37 +01003727 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003728
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003729 status = psa_aead_encrypt_setup( &operation, key, alg );
3730
3731 /* If the operation is not supported, just skip and not fail in case the
3732 * encryption involves a common limitation of cryptography hardwares and
3733 * an alternative implementation. */
3734 if( status == PSA_ERROR_NOT_SUPPORTED )
3735 {
3736 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01003737 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003738 }
3739
3740 PSA_ASSERT( status );
3741
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003742 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01003743 nonce_length,
3744 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003745
Paul Elliott693bf312021-07-23 17:40:41 +01003746 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003747
Paul Elliottf1277632021-08-24 18:11:37 +01003748 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01003749
Paul Elliott88ecbe12021-09-22 17:23:03 +01003750 if( expected_status == PSA_SUCCESS )
3751 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
3752 alg ) );
3753
Paul Elliottf1277632021-08-24 18:11:37 +01003754 TEST_ASSERT( actual_nonce_length < PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01003755
Paul Elliott693bf312021-07-23 17:40:41 +01003756 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003757 {
3758
3759 /* Ensure we can still complete operation. */
3760
3761 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3762 additional_data->len ) );
3763
3764 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01003765 output, output_size,
3766 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003767
Paul Elliottf1277632021-08-24 18:11:37 +01003768 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3769 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01003770 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3771 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003772
3773exit:
3774 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01003775 mbedtls_free( output );
3776 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003777 psa_aead_abort( &operation );
3778 PSA_DONE( );
3779}
3780/* END_CASE */
3781
3782/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01003783void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
3784 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01003785 int nonce_length_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01003786 data_t *additional_data,
3787 data_t *input_data,
3788 int expected_status_arg )
3789{
3790
3791 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3792 psa_key_type_t key_type = key_type_arg;
3793 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01003794 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01003795 uint8_t *nonce_buffer = NULL;
3796 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3797 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3798 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003799 unsigned char *output = NULL;
3800 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01003801 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01003802 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003803 size_t ciphertext_size = 0;
3804 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003805 size_t tag_length = 0;
3806 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01003807 size_t index = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01003808
3809 PSA_ASSERT( psa_crypto_init( ) );
3810
3811 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3812 psa_set_key_algorithm( &attributes, alg );
3813 psa_set_key_type( &attributes, key_type );
3814
3815 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3816 &key ) );
3817
3818 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3819
3820 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
3821
Paul Elliott6f0e7202021-08-25 12:57:18 +01003822 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003823
Paul Elliott6f0e7202021-08-25 12:57:18 +01003824 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01003825
Paul Elliott6f0e7202021-08-25 12:57:18 +01003826 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01003827
Paul Elliott6f0e7202021-08-25 12:57:18 +01003828 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01003829
Paul Elliott863864a2021-07-23 17:28:31 +01003830 status = psa_aead_encrypt_setup( &operation, key, alg );
3831
3832 /* If the operation is not supported, just skip and not fail in case the
3833 * encryption involves a common limitation of cryptography hardwares and
3834 * an alternative implementation. */
3835 if( status == PSA_ERROR_NOT_SUPPORTED )
3836 {
3837 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003838 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01003839 }
3840
3841 PSA_ASSERT( status );
3842
Paul Elliott4023ffd2021-09-10 16:21:22 +01003843 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
3844 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01003845 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01003846 /* Arbitrary size buffer, to test zero length valid buffer. */
3847 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01003848 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01003849 }
3850 else
3851 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003852 /* If length is zero, then this will return NULL. */
3853 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01003854 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01003855
Paul Elliott4023ffd2021-09-10 16:21:22 +01003856 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01003857 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01003858 for( index = 0; index < nonce_length - 1; ++index )
3859 {
3860 nonce_buffer[index] = 'a' + index;
3861 }
Paul Elliott66696b52021-08-16 18:42:41 +01003862 }
Paul Elliott863864a2021-07-23 17:28:31 +01003863 }
3864
Paul Elliott6f0e7202021-08-25 12:57:18 +01003865 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01003866
Paul Elliott693bf312021-07-23 17:40:41 +01003867 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01003868
3869 if( expected_status == PSA_SUCCESS )
3870 {
3871 /* Ensure we can still complete operation. */
3872
3873 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3874 additional_data->len ) );
3875
3876 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01003877 output, output_size,
3878 &ciphertext_length ) );
Paul Elliott863864a2021-07-23 17:28:31 +01003879
Paul Elliott6f0e7202021-08-25 12:57:18 +01003880 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3881 &ciphertext_length, tag_buffer,
Paul Elliott863864a2021-07-23 17:28:31 +01003882 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3883 }
3884
3885exit:
3886 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01003887 mbedtls_free( output );
3888 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01003889 mbedtls_free( nonce_buffer );
3890 psa_aead_abort( &operation );
3891 PSA_DONE( );
3892}
3893/* END_CASE */
3894
3895/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01003896void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
3897 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003898 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01003899 data_t *nonce,
3900 data_t *additional_data,
3901 data_t *input_data,
3902 int expected_status_arg )
3903{
3904
3905 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3906 psa_key_type_t key_type = key_type_arg;
3907 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01003908 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01003909 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3910 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3911 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01003912 unsigned char *output = NULL;
3913 unsigned char *ciphertext = NULL;
3914 size_t output_size = output_size_arg;
3915 size_t ciphertext_size = 0;
3916 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01003917 size_t tag_length = 0;
3918 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
3919
3920 PSA_ASSERT( psa_crypto_init( ) );
3921
3922 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3923 psa_set_key_algorithm( &attributes, alg );
3924 psa_set_key_type( &attributes, key_type );
3925
3926 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3927 &key ) );
3928
3929 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3930
Paul Elliottc6d11d02021-09-01 12:04:23 +01003931 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003932
Paul Elliottc6d11d02021-09-01 12:04:23 +01003933 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01003934
Paul Elliottc6d11d02021-09-01 12:04:23 +01003935 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01003936
Paul Elliott43fbda62021-07-23 18:30:59 +01003937 status = psa_aead_encrypt_setup( &operation, key, alg );
3938
3939 /* If the operation is not supported, just skip and not fail in case the
3940 * encryption involves a common limitation of cryptography hardwares and
3941 * an alternative implementation. */
3942 if( status == PSA_ERROR_NOT_SUPPORTED )
3943 {
3944 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3945 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3946 }
3947
3948 PSA_ASSERT( status );
3949
3950 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
3951
3952 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
3953 additional_data->len ) );
3954
3955 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01003956 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01003957
3958 TEST_EQUAL( status, expected_status );
3959
3960 if( expected_status == PSA_SUCCESS )
3961 {
3962 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01003963 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
3964 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01003965 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
3966 }
3967
3968exit:
3969 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01003970 mbedtls_free( output );
3971 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01003972 psa_aead_abort( &operation );
3973 PSA_DONE( );
3974}
3975/* END_CASE */
3976
Paul Elliott91b021e2021-07-23 18:52:31 +01003977/* BEGIN_CASE */
3978void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
3979 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01003980 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01003981 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01003982 data_t *nonce,
3983 data_t *additional_data,
3984 data_t *input_data,
3985 int expected_status_arg )
3986{
3987
3988 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3989 psa_key_type_t key_type = key_type_arg;
3990 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01003991 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01003992 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3993 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3994 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01003995 unsigned char *ciphertext = NULL;
3996 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01003997 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01003998 size_t ciphertext_size = 0;
3999 size_t ciphertext_length = 0;
4000 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004001 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004002 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004003
4004 PSA_ASSERT( psa_crypto_init( ) );
4005
4006 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4007 psa_set_key_algorithm( &attributes, alg );
4008 psa_set_key_type( &attributes, key_type );
4009
4010 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4011 &key ) );
4012
4013 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4014
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004015 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004016
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004017 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004018
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004019 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004020
Paul Elliott719c1322021-09-13 18:27:22 +01004021 ASSERT_ALLOC( tag_buffer, tag_size );
4022
Paul Elliott91b021e2021-07-23 18:52:31 +01004023 status = psa_aead_encrypt_setup( &operation, key, alg );
4024
4025 /* If the operation is not supported, just skip and not fail in case the
4026 * encryption involves a common limitation of cryptography hardwares and
4027 * an alternative implementation. */
4028 if( status == PSA_ERROR_NOT_SUPPORTED )
4029 {
4030 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4031 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4032 }
4033
4034 PSA_ASSERT( status );
4035
4036 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4037
4038 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4039 additional_data->len ) );
4040
4041 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004042 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004043
4044 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004045 status = psa_aead_finish( &operation, finish_ciphertext,
4046 finish_ciphertext_size,
4047 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004048 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004049
4050 TEST_EQUAL( status, expected_status );
4051
4052exit:
4053 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004054 mbedtls_free( ciphertext );
4055 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01004056 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01004057 psa_aead_abort( &operation );
4058 PSA_DONE( );
4059}
4060/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004061
4062/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01004063void aead_multipart_verify( int key_type_arg, data_t *key_data,
4064 int alg_arg,
4065 data_t *nonce,
4066 data_t *additional_data,
4067 data_t *input_data,
4068 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004069 int tag_usage_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01004070 int expected_status_arg )
4071{
4072 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4073 psa_key_type_t key_type = key_type_arg;
4074 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004075 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01004076 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4077 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4078 psa_status_t expected_status = expected_status_arg;
4079 unsigned char *plaintext = NULL;
4080 unsigned char *finish_plaintext = NULL;
4081 size_t plaintext_size = 0;
4082 size_t plaintext_length = 0;
4083 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004084 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004085 unsigned char *tag_buffer = NULL;
4086 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01004087
4088 PSA_ASSERT( psa_crypto_init( ) );
4089
4090 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4091 psa_set_key_algorithm( &attributes, alg );
4092 psa_set_key_type( &attributes, key_type );
4093
4094 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4095 &key ) );
4096
4097 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4098
4099 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
4100 input_data->len );
4101
4102 ASSERT_ALLOC( plaintext, plaintext_size );
4103
4104 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
4105
4106 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
4107
Paul Elliott9961a662021-09-17 19:19:02 +01004108 status = psa_aead_decrypt_setup( &operation, key, alg );
4109
4110 /* If the operation is not supported, just skip and not fail in case the
4111 * encryption involves a common limitation of cryptography hardwares and
4112 * an alternative implementation. */
4113 if( status == PSA_ERROR_NOT_SUPPORTED )
4114 {
4115 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4116 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4117 }
4118
4119 PSA_ASSERT( status );
4120
4121 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4122
4123 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4124 additional_data->len ) );
4125
4126 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4127 input_data->len,
4128 plaintext, plaintext_size,
4129 &plaintext_length ) );
4130
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004131 if( tag_usage == USE_GIVEN_TAG )
4132 {
4133 tag_buffer = tag->x;
4134 tag_size = tag->len;
4135 }
4136
Paul Elliott9961a662021-09-17 19:19:02 +01004137 status = psa_aead_verify( &operation, finish_plaintext,
4138 verify_plaintext_size,
4139 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004140 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01004141
4142 TEST_EQUAL( status, expected_status );
4143
4144exit:
4145 psa_destroy_key( key );
4146 mbedtls_free( plaintext );
4147 mbedtls_free( finish_plaintext );
4148 psa_aead_abort( &operation );
4149 PSA_DONE( );
4150}
4151/* END_CASE */
4152
Paul Elliott9961a662021-09-17 19:19:02 +01004153/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01004154void aead_multipart_setup( int key_type_arg, data_t *key_data,
4155 int alg_arg, int expected_status_arg )
4156{
4157 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4158 psa_key_type_t key_type = key_type_arg;
4159 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004160 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01004161 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4162 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4163 psa_status_t expected_status = expected_status_arg;
4164
4165 PSA_ASSERT( psa_crypto_init( ) );
4166
4167 psa_set_key_usage_flags( &attributes,
4168 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4169 psa_set_key_algorithm( &attributes, alg );
4170 psa_set_key_type( &attributes, key_type );
4171
4172 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4173 &key ) );
4174
4175 mbedtls_test_set_step( 0 );
4176
4177 status = psa_aead_encrypt_setup( &operation, key, alg );
4178
4179 TEST_EQUAL( status, expected_status );
4180
4181 psa_aead_abort( &operation );
4182
Paul Elliott5221ef62021-09-19 17:33:03 +01004183 mbedtls_test_set_step( 1 );
4184
4185 status = psa_aead_decrypt_setup( &operation, key, alg );
4186
4187 TEST_EQUAL(status, expected_status );
4188
4189exit:
4190 psa_destroy_key( key );
4191 psa_aead_abort( &operation );
4192 PSA_DONE( );
4193}
4194/* END_CASE */
4195
4196/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004197void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4198 int alg_arg,
4199 data_t *nonce,
4200 data_t *additional_data,
4201 data_t *input_data )
4202{
4203 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4204 psa_key_type_t key_type = key_type_arg;
4205 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004206 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01004207 unsigned char *output_data = NULL;
4208 unsigned char *final_data = NULL;
4209 size_t output_size = 0;
4210 size_t finish_output_size = 0;
4211 size_t output_length = 0;
4212 size_t key_bits = 0;
4213 size_t tag_length = 0;
4214 size_t tag_size = 0;
4215 size_t nonce_length = 0;
4216 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4217 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4218 size_t output_part_length = 0;
4219 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4220
4221 PSA_ASSERT( psa_crypto_init( ) );
4222
4223 psa_set_key_usage_flags( & attributes,
4224 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4225 psa_set_key_algorithm( & attributes, alg );
4226 psa_set_key_type( & attributes, key_type );
4227
4228 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4229 &key ) );
4230
4231 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4232 key_bits = psa_get_key_bits( &attributes );
4233
4234 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4235
4236 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4237
4238 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4239
4240 ASSERT_ALLOC( output_data, output_size );
4241
4242 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4243
4244 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4245
4246 ASSERT_ALLOC( final_data, finish_output_size );
4247
4248 /* Test all operations error without calling setup first. */
4249
Paul Elliottc23a9a02021-06-21 18:32:46 +01004250 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4251 PSA_ERROR_BAD_STATE );
4252
4253 psa_aead_abort( &operation );
4254
Paul Elliottc23a9a02021-06-21 18:32:46 +01004255 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4256 PSA_AEAD_NONCE_MAX_SIZE,
4257 &nonce_length ),
4258 PSA_ERROR_BAD_STATE );
4259
4260 psa_aead_abort( &operation );
4261
Paul Elliott481be342021-07-16 17:38:47 +01004262 /* ------------------------------------------------------- */
4263
Paul Elliottc23a9a02021-06-21 18:32:46 +01004264 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4265 input_data->len ),
4266 PSA_ERROR_BAD_STATE );
4267
4268 psa_aead_abort( &operation );
4269
Paul Elliott481be342021-07-16 17:38:47 +01004270 /* ------------------------------------------------------- */
4271
Paul Elliottc23a9a02021-06-21 18:32:46 +01004272 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4273 additional_data->len ),
4274 PSA_ERROR_BAD_STATE );
4275
4276 psa_aead_abort( &operation );
4277
Paul Elliott481be342021-07-16 17:38:47 +01004278 /* ------------------------------------------------------- */
4279
Paul Elliottc23a9a02021-06-21 18:32:46 +01004280 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4281 input_data->len, output_data,
4282 output_size, &output_length ),
4283 PSA_ERROR_BAD_STATE );
4284
4285 psa_aead_abort( &operation );
4286
Paul Elliott481be342021-07-16 17:38:47 +01004287 /* ------------------------------------------------------- */
4288
Paul Elliottc23a9a02021-06-21 18:32:46 +01004289 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4290 finish_output_size,
4291 &output_part_length,
4292 tag_buffer, tag_length,
4293 &tag_size ),
4294 PSA_ERROR_BAD_STATE );
4295
4296 psa_aead_abort( &operation );
4297
Paul Elliott481be342021-07-16 17:38:47 +01004298 /* ------------------------------------------------------- */
4299
Paul Elliottc23a9a02021-06-21 18:32:46 +01004300 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4301 finish_output_size,
4302 &output_part_length,
4303 tag_buffer,
4304 tag_length ),
4305 PSA_ERROR_BAD_STATE );
4306
4307 psa_aead_abort( &operation );
4308
4309 /* Test for double setups. */
4310
Paul Elliottc23a9a02021-06-21 18:32:46 +01004311 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4312
4313 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4314 PSA_ERROR_BAD_STATE );
4315
4316 psa_aead_abort( &operation );
4317
Paul Elliott481be342021-07-16 17:38:47 +01004318 /* ------------------------------------------------------- */
4319
Paul Elliottc23a9a02021-06-21 18:32:46 +01004320 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4321
4322 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4323 PSA_ERROR_BAD_STATE );
4324
4325 psa_aead_abort( &operation );
4326
Paul Elliott374a2be2021-07-16 17:53:40 +01004327 /* ------------------------------------------------------- */
4328
Paul Elliott374a2be2021-07-16 17:53:40 +01004329 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4330
4331 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4332 PSA_ERROR_BAD_STATE );
4333
4334 psa_aead_abort( &operation );
4335
4336 /* ------------------------------------------------------- */
4337
Paul Elliott374a2be2021-07-16 17:53:40 +01004338 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4339
4340 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4341 PSA_ERROR_BAD_STATE );
4342
4343 psa_aead_abort( &operation );
4344
Paul Elliottc23a9a02021-06-21 18:32:46 +01004345 /* Test for not setting a nonce. */
4346
Paul Elliottc23a9a02021-06-21 18:32:46 +01004347 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4348
4349 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4350 additional_data->len ),
4351 PSA_ERROR_BAD_STATE );
4352
4353 psa_aead_abort( &operation );
4354
Paul Elliott7f628422021-09-01 12:08:29 +01004355 /* ------------------------------------------------------- */
4356
Paul Elliott7f628422021-09-01 12:08:29 +01004357 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4358
4359 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4360 input_data->len, output_data,
4361 output_size, &output_length ),
4362 PSA_ERROR_BAD_STATE );
4363
4364 psa_aead_abort( &operation );
4365
Paul Elliottbdc2c682021-09-21 18:37:10 +01004366 /* ------------------------------------------------------- */
4367
Paul Elliottbdc2c682021-09-21 18:37:10 +01004368 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4369
4370 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4371 finish_output_size,
4372 &output_part_length,
4373 tag_buffer, tag_length,
4374 &tag_size ),
4375 PSA_ERROR_BAD_STATE );
4376
4377 psa_aead_abort( &operation );
4378
4379 /* ------------------------------------------------------- */
4380
Paul Elliottbdc2c682021-09-21 18:37:10 +01004381 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4382
4383 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4384 finish_output_size,
4385 &output_part_length,
4386 tag_buffer,
4387 tag_length ),
4388 PSA_ERROR_BAD_STATE );
4389
4390 psa_aead_abort( &operation );
4391
Paul Elliottc23a9a02021-06-21 18:32:46 +01004392 /* Test for double setting nonce. */
4393
Paul Elliottc23a9a02021-06-21 18:32:46 +01004394 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4395
4396 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4397
4398 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4399 PSA_ERROR_BAD_STATE );
4400
4401 psa_aead_abort( &operation );
4402
Paul Elliott374a2be2021-07-16 17:53:40 +01004403 /* Test for double generating nonce. */
4404
Paul Elliott374a2be2021-07-16 17:53:40 +01004405 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4406
4407 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4408 PSA_AEAD_NONCE_MAX_SIZE,
4409 &nonce_length ) );
4410
4411 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4412 PSA_AEAD_NONCE_MAX_SIZE,
4413 &nonce_length ),
4414 PSA_ERROR_BAD_STATE );
4415
4416
4417 psa_aead_abort( &operation );
4418
4419 /* Test for generate nonce then set and vice versa */
4420
Paul Elliott374a2be2021-07-16 17:53:40 +01004421 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4422
4423 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4424 PSA_AEAD_NONCE_MAX_SIZE,
4425 &nonce_length ) );
4426
4427 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4428 PSA_ERROR_BAD_STATE );
4429
4430 psa_aead_abort( &operation );
4431
4432 /* ------------------------------------------------------- */
4433
Paul Elliott374a2be2021-07-16 17:53:40 +01004434 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4435
4436 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4437
4438 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4439 PSA_AEAD_NONCE_MAX_SIZE,
4440 &nonce_length ),
4441 PSA_ERROR_BAD_STATE );
4442
4443 psa_aead_abort( &operation );
4444
Paul Elliott7220cae2021-06-22 17:25:57 +01004445 /* Test for generating nonce in decrypt setup. */
4446
Paul Elliott7220cae2021-06-22 17:25:57 +01004447 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4448
4449 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4450 PSA_AEAD_NONCE_MAX_SIZE,
4451 &nonce_length ),
4452 PSA_ERROR_BAD_STATE );
4453
4454 psa_aead_abort( &operation );
4455
Paul Elliottc23a9a02021-06-21 18:32:46 +01004456 /* Test for setting lengths twice. */
4457
Paul Elliottc23a9a02021-06-21 18:32:46 +01004458 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4459
4460 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4461
4462 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4463 input_data->len ) );
4464
4465 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4466 input_data->len ),
4467 PSA_ERROR_BAD_STATE );
4468
4469 psa_aead_abort( &operation );
4470
4471 /* Test for setting lengths after already starting data. */
4472
Paul Elliottc23a9a02021-06-21 18:32:46 +01004473 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4474
4475 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4476
Paul Elliottf94bd992021-09-19 18:15:59 +01004477 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4478 additional_data->len ) );
4479
4480 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4481 input_data->len ),
4482 PSA_ERROR_BAD_STATE );
4483
4484 psa_aead_abort( &operation );
4485
4486 /* ------------------------------------------------------- */
4487
Paul Elliottf94bd992021-09-19 18:15:59 +01004488 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4489
4490 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4491
Paul Elliottc23a9a02021-06-21 18:32:46 +01004492 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4493 input_data->len, output_data,
4494 output_size, &output_length ) );
4495
4496 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4497 input_data->len ),
4498 PSA_ERROR_BAD_STATE );
4499
4500 psa_aead_abort( &operation );
4501
Paul Elliott243080c2021-07-21 19:01:17 +01004502 /* Test for not sending any additional data or data after setting non zero
4503 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004504
Paul Elliottc23a9a02021-06-21 18:32:46 +01004505 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4506
4507 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4508
4509 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4510 input_data->len ) );
4511
4512 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4513 finish_output_size,
4514 &output_part_length,
4515 tag_buffer, tag_length,
4516 &tag_size ),
4517 PSA_ERROR_INVALID_ARGUMENT );
4518
4519 psa_aead_abort( &operation );
4520
Paul Elliott243080c2021-07-21 19:01:17 +01004521 /* Test for not sending any additional data or data after setting non-zero
4522 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004523
Paul Elliottc23a9a02021-06-21 18:32:46 +01004524 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4525
4526 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4527
4528 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4529 input_data->len ) );
4530
4531 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4532 finish_output_size,
4533 &output_part_length,
4534 tag_buffer,
4535 tag_length ),
4536 PSA_ERROR_INVALID_ARGUMENT );
4537
4538 psa_aead_abort( &operation );
4539
Paul Elliott243080c2021-07-21 19:01:17 +01004540 /* Test for not sending any additional data after setting a non-zero length
4541 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004542
Paul Elliottc23a9a02021-06-21 18:32:46 +01004543 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4544
4545 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4546
4547 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4548 input_data->len ) );
4549
4550 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4551 input_data->len, output_data,
4552 output_size, &output_length ),
4553 PSA_ERROR_INVALID_ARGUMENT );
4554
4555 psa_aead_abort( &operation );
4556
Paul Elliottf94bd992021-09-19 18:15:59 +01004557 /* Test for not sending any data after setting a non-zero length for it.*/
4558
Paul Elliottf94bd992021-09-19 18:15:59 +01004559 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4560
4561 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4562
4563 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4564 input_data->len ) );
4565
4566 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4567 additional_data->len ) );
4568
4569 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4570 finish_output_size,
4571 &output_part_length,
4572 tag_buffer, tag_length,
4573 &tag_size ),
4574 PSA_ERROR_INVALID_ARGUMENT );
4575
4576 psa_aead_abort( &operation );
4577
Paul Elliottb0450fe2021-09-01 15:06:26 +01004578 /* Test for sending too much additional data after setting lengths. */
4579
Paul Elliottb0450fe2021-09-01 15:06:26 +01004580 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4581
4582 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4583
4584 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4585
4586
4587 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4588 additional_data->len ),
4589 PSA_ERROR_INVALID_ARGUMENT );
4590
4591 psa_aead_abort( &operation );
4592
Paul Elliotta2a09b02021-09-22 14:56:40 +01004593 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01004594
4595 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4596
4597 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4598
4599 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4600 input_data->len ) );
4601
4602 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4603 additional_data->len ) );
4604
4605 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4606 1 ),
4607 PSA_ERROR_INVALID_ARGUMENT );
4608
4609 psa_aead_abort( &operation );
4610
Paul Elliottb0450fe2021-09-01 15:06:26 +01004611 /* Test for sending too much data after setting lengths. */
4612
Paul Elliottb0450fe2021-09-01 15:06:26 +01004613 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4614
4615 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4616
4617 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4618
4619 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4620 input_data->len, output_data,
4621 output_size, &output_length ),
4622 PSA_ERROR_INVALID_ARGUMENT );
4623
4624 psa_aead_abort( &operation );
4625
Paul Elliotta2a09b02021-09-22 14:56:40 +01004626 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01004627
4628 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4629
4630 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4631
4632 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4633 input_data->len ) );
4634
4635 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4636 additional_data->len ) );
4637
4638 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4639 input_data->len, output_data,
4640 output_size, &output_length ) );
4641
4642 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4643 1, output_data,
4644 output_size, &output_length ),
4645 PSA_ERROR_INVALID_ARGUMENT );
4646
4647 psa_aead_abort( &operation );
4648
Paul Elliottc23a9a02021-06-21 18:32:46 +01004649 /* Test sending additional data after data. */
4650
Paul Elliottc23a9a02021-06-21 18:32:46 +01004651 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4652
4653 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4654
4655 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4656 input_data->len, output_data,
4657 output_size, &output_length ) );
4658
4659 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4660 additional_data->len ),
4661 PSA_ERROR_BAD_STATE );
4662
4663 psa_aead_abort( &operation );
4664
Paul Elliott534d0b42021-06-22 19:15:20 +01004665 /* Test calling finish on decryption. */
4666
Paul Elliott534d0b42021-06-22 19:15:20 +01004667 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4668
4669 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4670
4671 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4672 finish_output_size,
4673 &output_part_length,
4674 tag_buffer, tag_length,
4675 &tag_size ),
4676 PSA_ERROR_BAD_STATE );
4677
4678 psa_aead_abort( &operation );
4679
4680 /* Test calling verify on encryption. */
4681
Paul Elliott534d0b42021-06-22 19:15:20 +01004682 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4683
4684 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4685
4686 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4687 finish_output_size,
4688 &output_part_length,
4689 tag_buffer,
4690 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01004691 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01004692
4693 psa_aead_abort( &operation );
4694
4695
Paul Elliottc23a9a02021-06-21 18:32:46 +01004696exit:
4697 psa_destroy_key( key );
4698 psa_aead_abort( &operation );
4699 mbedtls_free( output_data );
4700 mbedtls_free( final_data );
4701 PSA_DONE( );
4702}
4703/* END_CASE */
4704
4705/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004706void signature_size( int type_arg,
4707 int bits,
4708 int alg_arg,
4709 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004710{
4711 psa_key_type_t type = type_arg;
4712 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004713 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004714
Gilles Peskinefe11b722018-12-18 00:24:04 +01004715 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004716
Gilles Peskinee59236f2018-01-27 23:32:46 +01004717exit:
4718 ;
4719}
4720/* END_CASE */
4721
4722/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004723void sign_hash_deterministic( int key_type_arg, data_t *key_data,
4724 int alg_arg, data_t *input_data,
4725 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004726{
Ronald Cron5425a212020-08-04 14:58:35 +02004727 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004728 psa_key_type_t key_type = key_type_arg;
4729 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004730 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004731 unsigned char *signature = NULL;
4732 size_t signature_size;
4733 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004734 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004735
Gilles Peskine8817f612018-12-18 00:18:46 +01004736 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004737
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004738 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004739 psa_set_key_algorithm( &attributes, alg );
4740 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004741
Gilles Peskine049c7532019-05-15 20:22:09 +02004742 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004743 &key ) );
4744 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004745 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004746
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004747 /* Allocate a buffer which has the size advertized by the
4748 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004749 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004750 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004751 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004752 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004753 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004754
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004755 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004756 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004757 input_data->x, input_data->len,
4758 signature, signature_size,
4759 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004760 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004761 ASSERT_COMPARE( output_data->x, output_data->len,
4762 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004763
4764exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004765 /*
4766 * Key attributes may have been returned by psa_get_key_attributes()
4767 * thus reset them as required.
4768 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004769 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004770
Ronald Cron5425a212020-08-04 14:58:35 +02004771 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004772 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004773 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004774}
4775/* END_CASE */
4776
4777/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004778void sign_hash_fail( int key_type_arg, data_t *key_data,
4779 int alg_arg, data_t *input_data,
4780 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004781{
Ronald Cron5425a212020-08-04 14:58:35 +02004782 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004783 psa_key_type_t key_type = key_type_arg;
4784 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004785 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004786 psa_status_t actual_status;
4787 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004788 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004789 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004790 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004791
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004792 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004793
Gilles Peskine8817f612018-12-18 00:18:46 +01004794 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004795
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004796 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004797 psa_set_key_algorithm( &attributes, alg );
4798 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004799
Gilles Peskine049c7532019-05-15 20:22:09 +02004800 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004801 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004802
Ronald Cron5425a212020-08-04 14:58:35 +02004803 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004804 input_data->x, input_data->len,
4805 signature, signature_size,
4806 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004807 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004808 /* The value of *signature_length is unspecified on error, but
4809 * whatever it is, it should be less than signature_size, so that
4810 * if the caller tries to read *signature_length bytes without
4811 * checking the error code then they don't overflow a buffer. */
4812 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004813
4814exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004815 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004816 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004817 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004818 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004819}
4820/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004821
4822/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004823void sign_verify_hash( int key_type_arg, data_t *key_data,
4824 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02004825{
Ronald Cron5425a212020-08-04 14:58:35 +02004826 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004827 psa_key_type_t key_type = key_type_arg;
4828 psa_algorithm_t alg = alg_arg;
4829 size_t key_bits;
4830 unsigned char *signature = NULL;
4831 size_t signature_size;
4832 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004833 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004834
Gilles Peskine8817f612018-12-18 00:18:46 +01004835 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004836
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004837 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004838 psa_set_key_algorithm( &attributes, alg );
4839 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004840
Gilles Peskine049c7532019-05-15 20:22:09 +02004841 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004842 &key ) );
4843 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004844 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004845
4846 /* Allocate a buffer which has the size advertized by the
4847 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004848 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004849 key_bits, alg );
4850 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004851 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004852 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004853
4854 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004855 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004856 input_data->x, input_data->len,
4857 signature, signature_size,
4858 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004859 /* Check that the signature length looks sensible. */
4860 TEST_ASSERT( signature_length <= signature_size );
4861 TEST_ASSERT( signature_length > 0 );
4862
4863 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004864 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004865 input_data->x, input_data->len,
4866 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004867
4868 if( input_data->len != 0 )
4869 {
4870 /* Flip a bit in the input and verify that the signature is now
4871 * detected as invalid. Flip a bit at the beginning, not at the end,
4872 * because ECDSA may ignore the last few bits of the input. */
4873 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004874 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004875 input_data->x, input_data->len,
4876 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004877 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004878 }
4879
4880exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004881 /*
4882 * Key attributes may have been returned by psa_get_key_attributes()
4883 * thus reset them as required.
4884 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004885 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004886
Ronald Cron5425a212020-08-04 14:58:35 +02004887 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004888 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004889 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004890}
4891/* END_CASE */
4892
4893/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004894void verify_hash( int key_type_arg, data_t *key_data,
4895 int alg_arg, data_t *hash_data,
4896 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004897{
Ronald Cron5425a212020-08-04 14:58:35 +02004898 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004899 psa_key_type_t key_type = key_type_arg;
4900 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004901 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004902
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004903 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004904
Gilles Peskine8817f612018-12-18 00:18:46 +01004905 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004906
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004907 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004908 psa_set_key_algorithm( &attributes, alg );
4909 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004910
Gilles Peskine049c7532019-05-15 20:22:09 +02004911 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004912 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004913
Ronald Cron5425a212020-08-04 14:58:35 +02004914 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004915 hash_data->x, hash_data->len,
4916 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004917
itayzafrir5c753392018-05-08 11:18:38 +03004918exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004919 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004920 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004921 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004922}
4923/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004924
4925/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02004926void verify_hash_fail( int key_type_arg, data_t *key_data,
4927 int alg_arg, data_t *hash_data,
4928 data_t *signature_data,
4929 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004930{
Ronald Cron5425a212020-08-04 14:58:35 +02004931 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004932 psa_key_type_t key_type = key_type_arg;
4933 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004934 psa_status_t actual_status;
4935 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004936 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004937
Gilles Peskine8817f612018-12-18 00:18:46 +01004938 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004939
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004940 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004941 psa_set_key_algorithm( &attributes, alg );
4942 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004943
Gilles Peskine049c7532019-05-15 20:22:09 +02004944 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004945 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004946
Ronald Cron5425a212020-08-04 14:58:35 +02004947 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004948 hash_data->x, hash_data->len,
4949 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004950 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004951
4952exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004953 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004954 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004955 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004956}
4957/* END_CASE */
4958
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004959/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02004960void sign_message_deterministic( int key_type_arg,
4961 data_t *key_data,
4962 int alg_arg,
4963 data_t *input_data,
4964 data_t *output_data )
4965{
4966 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4967 psa_key_type_t key_type = key_type_arg;
4968 psa_algorithm_t alg = alg_arg;
4969 size_t key_bits;
4970 unsigned char *signature = NULL;
4971 size_t signature_size;
4972 size_t signature_length = 0xdeadbeef;
4973 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4974
4975 PSA_ASSERT( psa_crypto_init( ) );
4976
4977 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
4978 psa_set_key_algorithm( &attributes, alg );
4979 psa_set_key_type( &attributes, key_type );
4980
4981 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4982 &key ) );
4983 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4984 key_bits = psa_get_key_bits( &attributes );
4985
4986 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4987 TEST_ASSERT( signature_size != 0 );
4988 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
4989 ASSERT_ALLOC( signature, signature_size );
4990
4991 PSA_ASSERT( psa_sign_message( key, alg,
4992 input_data->x, input_data->len,
4993 signature, signature_size,
4994 &signature_length ) );
4995
4996 ASSERT_COMPARE( output_data->x, output_data->len,
4997 signature, signature_length );
4998
4999exit:
5000 psa_reset_key_attributes( &attributes );
5001
5002 psa_destroy_key( key );
5003 mbedtls_free( signature );
5004 PSA_DONE( );
5005
5006}
5007/* END_CASE */
5008
5009/* BEGIN_CASE */
5010void sign_message_fail( int key_type_arg,
5011 data_t *key_data,
5012 int alg_arg,
5013 data_t *input_data,
5014 int signature_size_arg,
5015 int expected_status_arg )
5016{
5017 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5018 psa_key_type_t key_type = key_type_arg;
5019 psa_algorithm_t alg = alg_arg;
5020 size_t signature_size = signature_size_arg;
5021 psa_status_t actual_status;
5022 psa_status_t expected_status = expected_status_arg;
5023 unsigned char *signature = NULL;
5024 size_t signature_length = 0xdeadbeef;
5025 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5026
5027 ASSERT_ALLOC( signature, signature_size );
5028
5029 PSA_ASSERT( psa_crypto_init( ) );
5030
5031 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5032 psa_set_key_algorithm( &attributes, alg );
5033 psa_set_key_type( &attributes, key_type );
5034
5035 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5036 &key ) );
5037
5038 actual_status = psa_sign_message( key, alg,
5039 input_data->x, input_data->len,
5040 signature, signature_size,
5041 &signature_length );
5042 TEST_EQUAL( actual_status, expected_status );
5043 /* The value of *signature_length is unspecified on error, but
5044 * whatever it is, it should be less than signature_size, so that
5045 * if the caller tries to read *signature_length bytes without
5046 * checking the error code then they don't overflow a buffer. */
5047 TEST_ASSERT( signature_length <= signature_size );
5048
5049exit:
5050 psa_reset_key_attributes( &attributes );
5051 psa_destroy_key( key );
5052 mbedtls_free( signature );
5053 PSA_DONE( );
5054}
5055/* END_CASE */
5056
5057/* BEGIN_CASE */
5058void sign_verify_message( int key_type_arg,
5059 data_t *key_data,
5060 int alg_arg,
5061 data_t *input_data )
5062{
5063 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5064 psa_key_type_t key_type = key_type_arg;
5065 psa_algorithm_t alg = alg_arg;
5066 size_t key_bits;
5067 unsigned char *signature = NULL;
5068 size_t signature_size;
5069 size_t signature_length = 0xdeadbeef;
5070 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5071
5072 PSA_ASSERT( psa_crypto_init( ) );
5073
5074 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
5075 PSA_KEY_USAGE_VERIFY_MESSAGE );
5076 psa_set_key_algorithm( &attributes, alg );
5077 psa_set_key_type( &attributes, key_type );
5078
5079 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5080 &key ) );
5081 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5082 key_bits = psa_get_key_bits( &attributes );
5083
5084 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5085 TEST_ASSERT( signature_size != 0 );
5086 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5087 ASSERT_ALLOC( signature, signature_size );
5088
5089 PSA_ASSERT( psa_sign_message( key, alg,
5090 input_data->x, input_data->len,
5091 signature, signature_size,
5092 &signature_length ) );
5093 TEST_ASSERT( signature_length <= signature_size );
5094 TEST_ASSERT( signature_length > 0 );
5095
5096 PSA_ASSERT( psa_verify_message( key, alg,
5097 input_data->x, input_data->len,
5098 signature, signature_length ) );
5099
5100 if( input_data->len != 0 )
5101 {
5102 /* Flip a bit in the input and verify that the signature is now
5103 * detected as invalid. Flip a bit at the beginning, not at the end,
5104 * because ECDSA may ignore the last few bits of the input. */
5105 input_data->x[0] ^= 1;
5106 TEST_EQUAL( psa_verify_message( key, alg,
5107 input_data->x, input_data->len,
5108 signature, signature_length ),
5109 PSA_ERROR_INVALID_SIGNATURE );
5110 }
5111
5112exit:
5113 psa_reset_key_attributes( &attributes );
5114
5115 psa_destroy_key( key );
5116 mbedtls_free( signature );
5117 PSA_DONE( );
5118}
5119/* END_CASE */
5120
5121/* BEGIN_CASE */
5122void verify_message( int key_type_arg,
5123 data_t *key_data,
5124 int alg_arg,
5125 data_t *input_data,
5126 data_t *signature_data )
5127{
5128 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5129 psa_key_type_t key_type = key_type_arg;
5130 psa_algorithm_t alg = alg_arg;
5131 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5132
5133 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
5134
5135 PSA_ASSERT( psa_crypto_init( ) );
5136
5137 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5138 psa_set_key_algorithm( &attributes, alg );
5139 psa_set_key_type( &attributes, key_type );
5140
5141 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5142 &key ) );
5143
5144 PSA_ASSERT( psa_verify_message( key, alg,
5145 input_data->x, input_data->len,
5146 signature_data->x, signature_data->len ) );
5147
5148exit:
5149 psa_reset_key_attributes( &attributes );
5150 psa_destroy_key( key );
5151 PSA_DONE( );
5152}
5153/* END_CASE */
5154
5155/* BEGIN_CASE */
5156void verify_message_fail( int key_type_arg,
5157 data_t *key_data,
5158 int alg_arg,
5159 data_t *hash_data,
5160 data_t *signature_data,
5161 int expected_status_arg )
5162{
5163 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5164 psa_key_type_t key_type = key_type_arg;
5165 psa_algorithm_t alg = alg_arg;
5166 psa_status_t actual_status;
5167 psa_status_t expected_status = expected_status_arg;
5168 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5169
5170 PSA_ASSERT( psa_crypto_init( ) );
5171
5172 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5173 psa_set_key_algorithm( &attributes, alg );
5174 psa_set_key_type( &attributes, key_type );
5175
5176 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5177 &key ) );
5178
5179 actual_status = psa_verify_message( key, alg,
5180 hash_data->x, hash_data->len,
5181 signature_data->x,
5182 signature_data->len );
5183 TEST_EQUAL( actual_status, expected_status );
5184
5185exit:
5186 psa_reset_key_attributes( &attributes );
5187 psa_destroy_key( key );
5188 PSA_DONE( );
5189}
5190/* END_CASE */
5191
5192/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02005193void asymmetric_encrypt( int key_type_arg,
5194 data_t *key_data,
5195 int alg_arg,
5196 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02005197 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02005198 int expected_output_length_arg,
5199 int expected_status_arg )
5200{
Ronald Cron5425a212020-08-04 14:58:35 +02005201 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005202 psa_key_type_t key_type = key_type_arg;
5203 psa_algorithm_t alg = alg_arg;
5204 size_t expected_output_length = expected_output_length_arg;
5205 size_t key_bits;
5206 unsigned char *output = NULL;
5207 size_t output_size;
5208 size_t output_length = ~0;
5209 psa_status_t actual_status;
5210 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005211 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005212
Gilles Peskine8817f612018-12-18 00:18:46 +01005213 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005214
Gilles Peskine656896e2018-06-29 19:12:28 +02005215 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005216 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5217 psa_set_key_algorithm( &attributes, alg );
5218 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005219 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005220 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02005221
5222 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02005223 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005224 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005225
Gilles Peskine656896e2018-06-29 19:12:28 +02005226 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005227 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005228 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02005229
5230 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02005231 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02005232 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005233 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02005234 output, output_size,
5235 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005236 TEST_EQUAL( actual_status, expected_status );
5237 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02005238
Gilles Peskine68428122018-06-30 18:42:41 +02005239 /* If the label is empty, the test framework puts a non-null pointer
5240 * in label->x. Test that a null pointer works as well. */
5241 if( label->len == 0 )
5242 {
5243 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005244 if( output_size != 0 )
5245 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005246 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005247 input_data->x, input_data->len,
5248 NULL, label->len,
5249 output, output_size,
5250 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005251 TEST_EQUAL( actual_status, expected_status );
5252 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005253 }
5254
Gilles Peskine656896e2018-06-29 19:12:28 +02005255exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005256 /*
5257 * Key attributes may have been returned by psa_get_key_attributes()
5258 * thus reset them as required.
5259 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005260 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005261
Ronald Cron5425a212020-08-04 14:58:35 +02005262 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02005263 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005264 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02005265}
5266/* END_CASE */
5267
5268/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005269void asymmetric_encrypt_decrypt( int key_type_arg,
5270 data_t *key_data,
5271 int alg_arg,
5272 data_t *input_data,
5273 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005274{
Ronald Cron5425a212020-08-04 14:58:35 +02005275 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005276 psa_key_type_t key_type = key_type_arg;
5277 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005278 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005279 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005280 size_t output_size;
5281 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005282 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005283 size_t output2_size;
5284 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005285 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005286
Gilles Peskine8817f612018-12-18 00:18:46 +01005287 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005288
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005289 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5290 psa_set_key_algorithm( &attributes, alg );
5291 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005292
Gilles Peskine049c7532019-05-15 20:22:09 +02005293 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005294 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005295
5296 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02005297 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005298 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005299
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005300 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005301 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005302 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01005303
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005304 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01005305 TEST_ASSERT( output2_size <=
5306 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
5307 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005308 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005309
Gilles Peskineeebd7382018-06-08 18:11:54 +02005310 /* We test encryption by checking that encrypt-then-decrypt gives back
5311 * the original plaintext because of the non-optional random
5312 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02005313 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005314 input_data->x, input_data->len,
5315 label->x, label->len,
5316 output, output_size,
5317 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005318 /* We don't know what ciphertext length to expect, but check that
5319 * it looks sensible. */
5320 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005321
Ronald Cron5425a212020-08-04 14:58:35 +02005322 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005323 output, output_length,
5324 label->x, label->len,
5325 output2, output2_size,
5326 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005327 ASSERT_COMPARE( input_data->x, input_data->len,
5328 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005329
5330exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005331 /*
5332 * Key attributes may have been returned by psa_get_key_attributes()
5333 * thus reset them as required.
5334 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005335 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005336
Ronald Cron5425a212020-08-04 14:58:35 +02005337 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005338 mbedtls_free( output );
5339 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005340 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005341}
5342/* END_CASE */
5343
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005344/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005345void asymmetric_decrypt( int key_type_arg,
5346 data_t *key_data,
5347 int alg_arg,
5348 data_t *input_data,
5349 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02005350 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005351{
Ronald Cron5425a212020-08-04 14:58:35 +02005352 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005353 psa_key_type_t key_type = key_type_arg;
5354 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01005355 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005356 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03005357 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005358 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005359 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005360
Gilles Peskine8817f612018-12-18 00:18:46 +01005361 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005362
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005363 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5364 psa_set_key_algorithm( &attributes, alg );
5365 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005366
Gilles Peskine049c7532019-05-15 20:22:09 +02005367 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005368 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005369
gabor-mezei-armceface22021-01-21 12:26:17 +01005370 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5371 key_bits = psa_get_key_bits( &attributes );
5372
5373 /* Determine the maximum ciphertext length */
5374 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
5375 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
5376 ASSERT_ALLOC( output, output_size );
5377
Ronald Cron5425a212020-08-04 14:58:35 +02005378 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005379 input_data->x, input_data->len,
5380 label->x, label->len,
5381 output,
5382 output_size,
5383 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005384 ASSERT_COMPARE( expected_data->x, expected_data->len,
5385 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005386
Gilles Peskine68428122018-06-30 18:42:41 +02005387 /* If the label is empty, the test framework puts a non-null pointer
5388 * in label->x. Test that a null pointer works as well. */
5389 if( label->len == 0 )
5390 {
5391 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005392 if( output_size != 0 )
5393 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005394 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005395 input_data->x, input_data->len,
5396 NULL, label->len,
5397 output,
5398 output_size,
5399 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005400 ASSERT_COMPARE( expected_data->x, expected_data->len,
5401 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005402 }
5403
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005404exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005405 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005406 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005407 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005408 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005409}
5410/* END_CASE */
5411
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005412/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005413void asymmetric_decrypt_fail( int key_type_arg,
5414 data_t *key_data,
5415 int alg_arg,
5416 data_t *input_data,
5417 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00005418 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02005419 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005420{
Ronald Cron5425a212020-08-04 14:58:35 +02005421 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005422 psa_key_type_t key_type = key_type_arg;
5423 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005424 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00005425 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005426 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005427 psa_status_t actual_status;
5428 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005429 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005430
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005431 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005432
Gilles Peskine8817f612018-12-18 00:18:46 +01005433 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005434
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005435 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5436 psa_set_key_algorithm( &attributes, alg );
5437 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005438
Gilles Peskine049c7532019-05-15 20:22:09 +02005439 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005440 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005441
Ronald Cron5425a212020-08-04 14:58:35 +02005442 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005443 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005444 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005445 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02005446 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005447 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005448 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005449
Gilles Peskine68428122018-06-30 18:42:41 +02005450 /* If the label is empty, the test framework puts a non-null pointer
5451 * in label->x. Test that a null pointer works as well. */
5452 if( label->len == 0 )
5453 {
5454 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005455 if( output_size != 0 )
5456 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005457 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005458 input_data->x, input_data->len,
5459 NULL, label->len,
5460 output, output_size,
5461 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005462 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005463 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02005464 }
5465
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005466exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005467 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005468 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02005469 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005470 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005471}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005472/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02005473
5474/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005475void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00005476{
5477 /* Test each valid way of initializing the object, except for `= {0}`, as
5478 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
5479 * though it's OK by the C standard. We could test for this, but we'd need
5480 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005481 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005482 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
5483 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
5484 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00005485
5486 memset( &zero, 0, sizeof( zero ) );
5487
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005488 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005489 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005490 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005491 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005492 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005493 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005494 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005495
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005496 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005497 PSA_ASSERT( psa_key_derivation_abort(&func) );
5498 PSA_ASSERT( psa_key_derivation_abort(&init) );
5499 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00005500}
5501/* END_CASE */
5502
Janos Follath16de4a42019-06-13 16:32:24 +01005503/* BEGIN_CASE */
5504void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005505{
Gilles Peskineea0fb492018-07-12 17:17:20 +02005506 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005507 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005508 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005509
Gilles Peskine8817f612018-12-18 00:18:46 +01005510 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005511
Janos Follath16de4a42019-06-13 16:32:24 +01005512 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005513 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005514
5515exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005516 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005517 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005518}
5519/* END_CASE */
5520
Janos Follathaf3c2a02019-06-12 12:34:34 +01005521/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01005522void derive_set_capacity( int alg_arg, int capacity_arg,
5523 int expected_status_arg )
5524{
5525 psa_algorithm_t alg = alg_arg;
5526 size_t capacity = capacity_arg;
5527 psa_status_t expected_status = expected_status_arg;
5528 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5529
5530 PSA_ASSERT( psa_crypto_init( ) );
5531
5532 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5533
5534 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
5535 expected_status );
5536
5537exit:
5538 psa_key_derivation_abort( &operation );
5539 PSA_DONE( );
5540}
5541/* END_CASE */
5542
5543/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01005544void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02005545 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005546 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02005547 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005548 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02005549 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005550 int expected_status_arg3,
5551 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005552{
5553 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02005554 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
5555 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01005556 psa_status_t expected_statuses[] = {expected_status_arg1,
5557 expected_status_arg2,
5558 expected_status_arg3};
5559 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005560 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5561 MBEDTLS_SVC_KEY_ID_INIT,
5562 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01005563 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5564 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5565 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005566 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005567 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005568 psa_status_t expected_output_status = expected_output_status_arg;
5569 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01005570
5571 PSA_ASSERT( psa_crypto_init( ) );
5572
5573 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5574 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005575
5576 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5577
5578 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
5579 {
Gilles Peskineb8965192019-09-24 16:21:10 +02005580 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005581 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02005582 psa_set_key_type( &attributes, key_types[i] );
5583 PSA_ASSERT( psa_import_key( &attributes,
5584 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005585 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005586 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
5587 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
5588 {
5589 // When taking a private key as secret input, use key agreement
5590 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005591 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
5592 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005593 expected_statuses[i] );
5594 }
5595 else
5596 {
5597 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02005598 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005599 expected_statuses[i] );
5600 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02005601 }
5602 else
5603 {
5604 TEST_EQUAL( psa_key_derivation_input_bytes(
5605 &operation, steps[i],
5606 inputs[i]->x, inputs[i]->len ),
5607 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005608 }
5609 }
5610
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005611 if( output_key_type != PSA_KEY_TYPE_NONE )
5612 {
5613 psa_reset_key_attributes( &attributes );
5614 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
5615 psa_set_key_bits( &attributes, 8 );
5616 actual_output_status =
5617 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005618 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005619 }
5620 else
5621 {
5622 uint8_t buffer[1];
5623 actual_output_status =
5624 psa_key_derivation_output_bytes( &operation,
5625 buffer, sizeof( buffer ) );
5626 }
5627 TEST_EQUAL( actual_output_status, expected_output_status );
5628
Janos Follathaf3c2a02019-06-12 12:34:34 +01005629exit:
5630 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005631 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5632 psa_destroy_key( keys[i] );
5633 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005634 PSA_DONE( );
5635}
5636/* END_CASE */
5637
Janos Follathd958bb72019-07-03 15:02:16 +01005638/* BEGIN_CASE */
5639void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005640{
Janos Follathd958bb72019-07-03 15:02:16 +01005641 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005642 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02005643 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005644 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01005645 unsigned char input1[] = "Input 1";
5646 size_t input1_length = sizeof( input1 );
5647 unsigned char input2[] = "Input 2";
5648 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005649 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02005650 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02005651 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5652 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5653 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005654 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005655
Gilles Peskine8817f612018-12-18 00:18:46 +01005656 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005657
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005658 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5659 psa_set_key_algorithm( &attributes, alg );
5660 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005661
Gilles Peskine73676cb2019-05-15 20:15:10 +02005662 PSA_ASSERT( psa_import_key( &attributes,
5663 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02005664 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005665
5666 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005667 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5668 input1, input1_length,
5669 input2, input2_length,
5670 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01005671 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005672
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005673 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01005674 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005675 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005676
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005677 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005678
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005679 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02005680 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005681
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005682exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005683 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005684 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005685 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005686}
5687/* END_CASE */
5688
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005689/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005690void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005691{
5692 uint8_t output_buffer[16];
5693 size_t buffer_size = 16;
5694 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005695 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005696
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005697 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5698 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005699 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005700
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005701 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005702 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005703
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005704 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005705
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005706 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
5707 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005708 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005709
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005710 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005711 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005712
5713exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005714 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005715}
5716/* END_CASE */
5717
5718/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005719void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02005720 int step1_arg, data_t *input1,
5721 int step2_arg, data_t *input2,
5722 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005723 int requested_capacity_arg,
5724 data_t *expected_output1,
5725 data_t *expected_output2 )
5726{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005727 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02005728 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
5729 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005730 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5731 MBEDTLS_SVC_KEY_ID_INIT,
5732 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005733 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005734 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005735 uint8_t *expected_outputs[2] =
5736 {expected_output1->x, expected_output2->x};
5737 size_t output_sizes[2] =
5738 {expected_output1->len, expected_output2->len};
5739 size_t output_buffer_size = 0;
5740 uint8_t *output_buffer = NULL;
5741 size_t expected_capacity;
5742 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005743 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005744 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02005745 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005746
5747 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5748 {
5749 if( output_sizes[i] > output_buffer_size )
5750 output_buffer_size = output_sizes[i];
5751 if( output_sizes[i] == 0 )
5752 expected_outputs[i] = NULL;
5753 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005754 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005755 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005756
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005757 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5758 psa_set_key_algorithm( &attributes, alg );
5759 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005760
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005761 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005762 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5763 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5764 requested_capacity ) );
5765 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005766 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005767 switch( steps[i] )
5768 {
5769 case 0:
5770 break;
5771 case PSA_KEY_DERIVATION_INPUT_SECRET:
5772 PSA_ASSERT( psa_import_key( &attributes,
5773 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005774 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01005775
5776 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
5777 {
5778 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
5779 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
5780 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
5781 }
5782
Gilles Peskine1468da72019-05-29 17:35:49 +02005783 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005784 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005785 break;
5786 default:
5787 PSA_ASSERT( psa_key_derivation_input_bytes(
5788 &operation, steps[i],
5789 inputs[i]->x, inputs[i]->len ) );
5790 break;
5791 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005792 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005793
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005794 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005795 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005796 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005797 expected_capacity = requested_capacity;
5798
5799 /* Expansion phase. */
5800 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5801 {
5802 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005803 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005804 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005805 if( expected_capacity == 0 && output_sizes[i] == 0 )
5806 {
5807 /* Reading 0 bytes when 0 bytes are available can go either way. */
5808 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005809 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005810 continue;
5811 }
5812 else if( expected_capacity == 0 ||
5813 output_sizes[i] > expected_capacity )
5814 {
5815 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005816 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005817 expected_capacity = 0;
5818 continue;
5819 }
5820 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005821 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005822 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005823 ASSERT_COMPARE( output_buffer, output_sizes[i],
5824 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005825 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005826 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005827 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005828 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005829 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005830 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005831 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005832
5833exit:
5834 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005835 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005836 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5837 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005838 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005839}
5840/* END_CASE */
5841
5842/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005843void derive_full( int alg_arg,
5844 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005845 data_t *input1,
5846 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005847 int requested_capacity_arg )
5848{
Ronald Cron5425a212020-08-04 14:58:35 +02005849 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005850 psa_algorithm_t alg = alg_arg;
5851 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005852 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005853 unsigned char output_buffer[16];
5854 size_t expected_capacity = requested_capacity;
5855 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005856 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005857
Gilles Peskine8817f612018-12-18 00:18:46 +01005858 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005859
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005860 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5861 psa_set_key_algorithm( &attributes, alg );
5862 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005863
Gilles Peskine049c7532019-05-15 20:22:09 +02005864 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005865 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005866
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005867 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5868 input1->x, input1->len,
5869 input2->x, input2->len,
5870 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01005871 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005872
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005873 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005874 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005875 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005876
5877 /* Expansion phase. */
5878 while( current_capacity > 0 )
5879 {
5880 size_t read_size = sizeof( output_buffer );
5881 if( read_size > current_capacity )
5882 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005883 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005884 output_buffer,
5885 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005886 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005887 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005888 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005889 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005890 }
5891
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005892 /* Check that the operation refuses to go over capacity. */
5893 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005894 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005895
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005896 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005897
5898exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005899 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005900 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005901 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005902}
5903/* END_CASE */
5904
Janos Follathe60c9052019-07-03 13:51:30 +01005905/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005906void derive_key_exercise( int alg_arg,
5907 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005908 data_t *input1,
5909 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005910 int derived_type_arg,
5911 int derived_bits_arg,
5912 int derived_usage_arg,
5913 int derived_alg_arg )
5914{
Ronald Cron5425a212020-08-04 14:58:35 +02005915 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5916 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005917 psa_algorithm_t alg = alg_arg;
5918 psa_key_type_t derived_type = derived_type_arg;
5919 size_t derived_bits = derived_bits_arg;
5920 psa_key_usage_t derived_usage = derived_usage_arg;
5921 psa_algorithm_t derived_alg = derived_alg_arg;
5922 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005923 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005924 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005925 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005926
Gilles Peskine8817f612018-12-18 00:18:46 +01005927 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005928
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005929 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5930 psa_set_key_algorithm( &attributes, alg );
5931 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005932 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005933 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005934
5935 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005936 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5937 input1->x, input1->len,
5938 input2->x, input2->len,
5939 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01005940 goto exit;
5941
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005942 psa_set_key_usage_flags( &attributes, derived_usage );
5943 psa_set_key_algorithm( &attributes, derived_alg );
5944 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005945 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005946 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005947 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005948
5949 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005950 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005951 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5952 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005953
5954 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005955 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005956 goto exit;
5957
5958exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005959 /*
5960 * Key attributes may have been returned by psa_get_key_attributes()
5961 * thus reset them as required.
5962 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005963 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005964
5965 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005966 psa_destroy_key( base_key );
5967 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005968 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005969}
5970/* END_CASE */
5971
Janos Follath42fd8882019-07-03 14:17:09 +01005972/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005973void derive_key_export( int alg_arg,
5974 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005975 data_t *input1,
5976 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005977 int bytes1_arg,
5978 int bytes2_arg )
5979{
Ronald Cron5425a212020-08-04 14:58:35 +02005980 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5981 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005982 psa_algorithm_t alg = alg_arg;
5983 size_t bytes1 = bytes1_arg;
5984 size_t bytes2 = bytes2_arg;
5985 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005986 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005987 uint8_t *output_buffer = NULL;
5988 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005989 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5990 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005991 size_t length;
5992
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005993 ASSERT_ALLOC( output_buffer, capacity );
5994 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005995 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005996
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005997 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5998 psa_set_key_algorithm( &base_attributes, alg );
5999 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006000 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006001 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006002
6003 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006004 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6005 input1->x, input1->len,
6006 input2->x, input2->len,
6007 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006008 goto exit;
6009
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006010 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006011 output_buffer,
6012 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006013 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006014
6015 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006016 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6017 input1->x, input1->len,
6018 input2->x, input2->len,
6019 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006020 goto exit;
6021
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006022 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6023 psa_set_key_algorithm( &derived_attributes, 0 );
6024 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006025 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006026 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006027 &derived_key ) );
6028 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006029 export_buffer, bytes1,
6030 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006031 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02006032 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006033 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006034 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006035 &derived_key ) );
6036 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006037 export_buffer + bytes1, bytes2,
6038 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006039 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006040
6041 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006042 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
6043 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006044
6045exit:
6046 mbedtls_free( output_buffer );
6047 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006048 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006049 psa_destroy_key( base_key );
6050 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006051 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006052}
6053/* END_CASE */
6054
6055/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006056void derive_key( int alg_arg,
6057 data_t *key_data, data_t *input1, data_t *input2,
6058 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006059 int expected_status_arg,
6060 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02006061{
Ronald Cron5425a212020-08-04 14:58:35 +02006062 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6063 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02006064 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006065 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02006066 size_t bits = bits_arg;
6067 psa_status_t expected_status = expected_status_arg;
6068 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6069 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6070 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
6071
6072 PSA_ASSERT( psa_crypto_init( ) );
6073
6074 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6075 psa_set_key_algorithm( &base_attributes, alg );
6076 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
6077 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006078 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02006079
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006080 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6081 input1->x, input1->len,
6082 input2->x, input2->len,
6083 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02006084 goto exit;
6085
6086 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6087 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006088 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02006089 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01006090
6091 psa_status_t status =
6092 psa_key_derivation_output_key( &derived_attributes,
6093 &operation,
6094 &derived_key );
6095 if( is_large_output > 0 )
6096 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6097 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02006098
6099exit:
6100 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006101 psa_destroy_key( base_key );
6102 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02006103 PSA_DONE( );
6104}
6105/* END_CASE */
6106
6107/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006108void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02006109 int our_key_type_arg, int our_key_alg_arg,
6110 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006111 int expected_status_arg )
6112{
Ronald Cron5425a212020-08-04 14:58:35 +02006113 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006114 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006115 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006116 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006117 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006118 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02006119 psa_status_t expected_status = expected_status_arg;
6120 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006121
Gilles Peskine8817f612018-12-18 00:18:46 +01006122 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006123
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006124 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006125 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006126 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006127 PSA_ASSERT( psa_import_key( &attributes,
6128 our_key_data->x, our_key_data->len,
6129 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006130
Gilles Peskine77f40d82019-04-11 21:27:06 +02006131 /* The tests currently include inputs that should fail at either step.
6132 * Test cases that fail at the setup step should be changed to call
6133 * key_derivation_setup instead, and this function should be renamed
6134 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006135 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02006136 if( status == PSA_SUCCESS )
6137 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006138 TEST_EQUAL( psa_key_derivation_key_agreement(
6139 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
6140 our_key,
6141 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02006142 expected_status );
6143 }
6144 else
6145 {
6146 TEST_ASSERT( status == expected_status );
6147 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02006148
6149exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006150 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006151 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006152 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006153}
6154/* END_CASE */
6155
6156/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02006157void raw_key_agreement( int alg_arg,
6158 int our_key_type_arg, data_t *our_key_data,
6159 data_t *peer_key_data,
6160 data_t *expected_output )
6161{
Ronald Cron5425a212020-08-04 14:58:35 +02006162 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006163 psa_algorithm_t alg = alg_arg;
6164 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006165 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006166 unsigned char *output = NULL;
6167 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01006168 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006169
6170 ASSERT_ALLOC( output, expected_output->len );
6171 PSA_ASSERT( psa_crypto_init( ) );
6172
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006173 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6174 psa_set_key_algorithm( &attributes, alg );
6175 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006176 PSA_ASSERT( psa_import_key( &attributes,
6177 our_key_data->x, our_key_data->len,
6178 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006179
gabor-mezei-armceface22021-01-21 12:26:17 +01006180 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
6181 key_bits = psa_get_key_bits( &attributes );
6182
Gilles Peskinebe697d82019-05-16 18:00:41 +02006183 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
6184 peer_key_data->x, peer_key_data->len,
6185 output, expected_output->len,
6186 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006187 ASSERT_COMPARE( output, output_length,
6188 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01006189 TEST_ASSERT( output_length <=
6190 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
6191 TEST_ASSERT( output_length <=
6192 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006193
6194exit:
6195 mbedtls_free( output );
6196 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006197 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006198}
6199/* END_CASE */
6200
6201/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02006202void key_agreement_capacity( int alg_arg,
6203 int our_key_type_arg, data_t *our_key_data,
6204 data_t *peer_key_data,
6205 int expected_capacity_arg )
6206{
Ronald Cron5425a212020-08-04 14:58:35 +02006207 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006208 psa_algorithm_t alg = alg_arg;
6209 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006210 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006211 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006212 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02006213 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02006214
Gilles Peskine8817f612018-12-18 00:18:46 +01006215 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006216
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006217 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6218 psa_set_key_algorithm( &attributes, alg );
6219 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006220 PSA_ASSERT( psa_import_key( &attributes,
6221 our_key_data->x, our_key_data->len,
6222 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006223
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006224 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006225 PSA_ASSERT( psa_key_derivation_key_agreement(
6226 &operation,
6227 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6228 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006229 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6230 {
6231 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006232 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006233 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006234 NULL, 0 ) );
6235 }
Gilles Peskine59685592018-09-18 12:11:34 +02006236
Gilles Peskinebf491972018-10-25 22:36:12 +02006237 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006238 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006239 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006240 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02006241
Gilles Peskinebf491972018-10-25 22:36:12 +02006242 /* Test the actual capacity by reading the output. */
6243 while( actual_capacity > sizeof( output ) )
6244 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006245 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006246 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02006247 actual_capacity -= sizeof( output );
6248 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006249 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006250 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006251 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006252 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02006253
Gilles Peskine59685592018-09-18 12:11:34 +02006254exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006255 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006256 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006257 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006258}
6259/* END_CASE */
6260
6261/* BEGIN_CASE */
6262void key_agreement_output( int alg_arg,
6263 int our_key_type_arg, data_t *our_key_data,
6264 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006265 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02006266{
Ronald Cron5425a212020-08-04 14:58:35 +02006267 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006268 psa_algorithm_t alg = alg_arg;
6269 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006270 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006271 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006272 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02006273
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006274 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
6275 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006276
Gilles Peskine8817f612018-12-18 00:18:46 +01006277 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006278
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006279 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6280 psa_set_key_algorithm( &attributes, alg );
6281 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006282 PSA_ASSERT( psa_import_key( &attributes,
6283 our_key_data->x, our_key_data->len,
6284 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006285
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006286 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006287 PSA_ASSERT( psa_key_derivation_key_agreement(
6288 &operation,
6289 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6290 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006291 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6292 {
6293 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006294 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006295 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006296 NULL, 0 ) );
6297 }
Gilles Peskine59685592018-09-18 12:11:34 +02006298
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006299 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006300 actual_output,
6301 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006302 ASSERT_COMPARE( actual_output, expected_output1->len,
6303 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006304 if( expected_output2->len != 0 )
6305 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006306 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006307 actual_output,
6308 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006309 ASSERT_COMPARE( actual_output, expected_output2->len,
6310 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006311 }
Gilles Peskine59685592018-09-18 12:11:34 +02006312
6313exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006314 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006315 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006316 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006317 mbedtls_free( actual_output );
6318}
6319/* END_CASE */
6320
6321/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02006322void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02006323{
Gilles Peskinea50d7392018-06-21 10:22:13 +02006324 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006325 unsigned char *output = NULL;
6326 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02006327 size_t i;
6328 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02006329
Simon Butcher49f8e312020-03-03 15:51:50 +00006330 TEST_ASSERT( bytes_arg >= 0 );
6331
Gilles Peskine91892022021-02-08 19:50:26 +01006332 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006333 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02006334
Gilles Peskine8817f612018-12-18 00:18:46 +01006335 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02006336
Gilles Peskinea50d7392018-06-21 10:22:13 +02006337 /* Run several times, to ensure that every output byte will be
6338 * nonzero at least once with overwhelming probability
6339 * (2^(-8*number_of_runs)). */
6340 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02006341 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006342 if( bytes != 0 )
6343 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01006344 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006345
Gilles Peskinea50d7392018-06-21 10:22:13 +02006346 for( i = 0; i < bytes; i++ )
6347 {
6348 if( output[i] != 0 )
6349 ++changed[i];
6350 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006351 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02006352
6353 /* Check that every byte was changed to nonzero at least once. This
6354 * validates that psa_generate_random is overwriting every byte of
6355 * the output buffer. */
6356 for( i = 0; i < bytes; i++ )
6357 {
6358 TEST_ASSERT( changed[i] != 0 );
6359 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006360
6361exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006362 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006363 mbedtls_free( output );
6364 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02006365}
6366/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02006367
6368/* BEGIN_CASE */
6369void generate_key( int type_arg,
6370 int bits_arg,
6371 int usage_arg,
6372 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006373 int expected_status_arg,
6374 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006375{
Ronald Cron5425a212020-08-04 14:58:35 +02006376 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006377 psa_key_type_t type = type_arg;
6378 psa_key_usage_t usage = usage_arg;
6379 size_t bits = bits_arg;
6380 psa_algorithm_t alg = alg_arg;
6381 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006382 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006383 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006384
Gilles Peskine8817f612018-12-18 00:18:46 +01006385 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006386
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006387 psa_set_key_usage_flags( &attributes, usage );
6388 psa_set_key_algorithm( &attributes, alg );
6389 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006390 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006391
6392 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01006393 psa_status_t status = psa_generate_key( &attributes, &key );
6394
6395 if( is_large_key > 0 )
6396 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6397 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006398 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006399 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006400
6401 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006402 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006403 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
6404 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006405
Gilles Peskine818ca122018-06-20 18:16:48 +02006406 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006407 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02006408 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006409
6410exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006411 /*
6412 * Key attributes may have been returned by psa_get_key_attributes()
6413 * thus reset them as required.
6414 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006415 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006416
Ronald Cron5425a212020-08-04 14:58:35 +02006417 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006418 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006419}
6420/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03006421
Ronald Cronee414c72021-03-18 18:50:08 +01006422/* 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 +02006423void generate_key_rsa( int bits_arg,
6424 data_t *e_arg,
6425 int expected_status_arg )
6426{
Ronald Cron5425a212020-08-04 14:58:35 +02006427 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006428 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006429 size_t bits = bits_arg;
6430 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
6431 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
6432 psa_status_t expected_status = expected_status_arg;
6433 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6434 uint8_t *exported = NULL;
6435 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006436 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006437 size_t exported_length = SIZE_MAX;
6438 uint8_t *e_read_buffer = NULL;
6439 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02006440 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006441 size_t e_read_length = SIZE_MAX;
6442
6443 if( e_arg->len == 0 ||
6444 ( e_arg->len == 3 &&
6445 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
6446 {
6447 is_default_public_exponent = 1;
6448 e_read_size = 0;
6449 }
6450 ASSERT_ALLOC( e_read_buffer, e_read_size );
6451 ASSERT_ALLOC( exported, exported_size );
6452
6453 PSA_ASSERT( psa_crypto_init( ) );
6454
6455 psa_set_key_usage_flags( &attributes, usage );
6456 psa_set_key_algorithm( &attributes, alg );
6457 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
6458 e_arg->x, e_arg->len ) );
6459 psa_set_key_bits( &attributes, bits );
6460
6461 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006462 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006463 if( expected_status != PSA_SUCCESS )
6464 goto exit;
6465
6466 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006467 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006468 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6469 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6470 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
6471 e_read_buffer, e_read_size,
6472 &e_read_length ) );
6473 if( is_default_public_exponent )
6474 TEST_EQUAL( e_read_length, 0 );
6475 else
6476 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
6477
6478 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006479 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02006480 goto exit;
6481
6482 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02006483 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006484 exported, exported_size,
6485 &exported_length ) );
6486 {
6487 uint8_t *p = exported;
6488 uint8_t *end = exported + exported_length;
6489 size_t len;
6490 /* RSAPublicKey ::= SEQUENCE {
6491 * modulus INTEGER, -- n
6492 * publicExponent INTEGER } -- e
6493 */
6494 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006495 MBEDTLS_ASN1_SEQUENCE |
6496 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01006497 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006498 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
6499 MBEDTLS_ASN1_INTEGER ) );
6500 if( len >= 1 && p[0] == 0 )
6501 {
6502 ++p;
6503 --len;
6504 }
6505 if( e_arg->len == 0 )
6506 {
6507 TEST_EQUAL( len, 3 );
6508 TEST_EQUAL( p[0], 1 );
6509 TEST_EQUAL( p[1], 0 );
6510 TEST_EQUAL( p[2], 1 );
6511 }
6512 else
6513 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
6514 }
6515
6516exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006517 /*
6518 * Key attributes may have been returned by psa_get_key_attributes() or
6519 * set by psa_set_key_domain_parameters() thus reset them as required.
6520 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006521 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006522
Ronald Cron5425a212020-08-04 14:58:35 +02006523 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006524 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006525 mbedtls_free( e_read_buffer );
6526 mbedtls_free( exported );
6527}
6528/* END_CASE */
6529
Darryl Greend49a4992018-06-18 17:27:26 +01006530/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006531void persistent_key_load_key_from_storage( data_t *data,
6532 int type_arg, int bits_arg,
6533 int usage_flags_arg, int alg_arg,
6534 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01006535{
Ronald Cron71016a92020-08-28 19:01:50 +02006536 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006537 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02006538 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6539 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006540 psa_key_type_t type = type_arg;
6541 size_t bits = bits_arg;
6542 psa_key_usage_t usage_flags = usage_flags_arg;
6543 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006544 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01006545 unsigned char *first_export = NULL;
6546 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006547 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006548 size_t first_exported_length;
6549 size_t second_exported_length;
6550
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006551 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6552 {
6553 ASSERT_ALLOC( first_export, export_size );
6554 ASSERT_ALLOC( second_export, export_size );
6555 }
Darryl Greend49a4992018-06-18 17:27:26 +01006556
Gilles Peskine8817f612018-12-18 00:18:46 +01006557 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006558
Gilles Peskinec87af662019-05-15 16:12:22 +02006559 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006560 psa_set_key_usage_flags( &attributes, usage_flags );
6561 psa_set_key_algorithm( &attributes, alg );
6562 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006563 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006564
Darryl Green0c6575a2018-11-07 16:05:30 +00006565 switch( generation_method )
6566 {
6567 case IMPORT_KEY:
6568 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02006569 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006570 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006571 break;
Darryl Greend49a4992018-06-18 17:27:26 +01006572
Darryl Green0c6575a2018-11-07 16:05:30 +00006573 case GENERATE_KEY:
6574 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006575 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006576 break;
6577
6578 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01006579#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006580 {
6581 /* Create base key */
6582 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
6583 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6584 psa_set_key_usage_flags( &base_attributes,
6585 PSA_KEY_USAGE_DERIVE );
6586 psa_set_key_algorithm( &base_attributes, derive_alg );
6587 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006588 PSA_ASSERT( psa_import_key( &base_attributes,
6589 data->x, data->len,
6590 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006591 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006592 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006593 PSA_ASSERT( psa_key_derivation_input_key(
6594 &operation,
6595 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006596 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006597 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006598 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006599 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
6600 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006601 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006602 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006603 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02006604 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006605 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01006606#else
6607 TEST_ASSUME( ! "KDF not supported in this configuration" );
6608#endif
6609 break;
6610
6611 default:
6612 TEST_ASSERT( ! "generation_method not implemented in test" );
6613 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00006614 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006615 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01006616
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006617 /* Export the key if permitted by the key policy. */
6618 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6619 {
Ronald Cron5425a212020-08-04 14:58:35 +02006620 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006621 first_export, export_size,
6622 &first_exported_length ) );
6623 if( generation_method == IMPORT_KEY )
6624 ASSERT_COMPARE( data->x, data->len,
6625 first_export, first_exported_length );
6626 }
Darryl Greend49a4992018-06-18 17:27:26 +01006627
6628 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02006629 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006630 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01006631 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006632
Darryl Greend49a4992018-06-18 17:27:26 +01006633 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02006634 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02006635 TEST_ASSERT( mbedtls_svc_key_id_equal(
6636 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006637 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
6638 PSA_KEY_LIFETIME_PERSISTENT );
6639 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6640 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6641 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
6642 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01006643
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006644 /* Export the key again if permitted by the key policy. */
6645 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00006646 {
Ronald Cron5425a212020-08-04 14:58:35 +02006647 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006648 second_export, export_size,
6649 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006650 ASSERT_COMPARE( first_export, first_exported_length,
6651 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006652 }
6653
6654 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006655 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00006656 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01006657
6658exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006659 /*
6660 * Key attributes may have been returned by psa_get_key_attributes()
6661 * thus reset them as required.
6662 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006663 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006664
Darryl Greend49a4992018-06-18 17:27:26 +01006665 mbedtls_free( first_export );
6666 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006667 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006668 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02006669 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006670 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01006671}
6672/* END_CASE */