blob: 7ae672573eaaa821e2a321013c042d2ef75b5b22 [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 ),
Ronald Cron432e19c2020-09-17 14:12:30 +0200208 PSA_ERROR_DOES_NOT_EXIST );
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 ),
Ronald Cron432e19c2020-09-17 14:12:30 +0200220 PSA_ERROR_DOES_NOT_EXIST );
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 ),
Ronald Cron432e19c2020-09-17 14:12:30 +0200223 PSA_ERROR_DOES_NOT_EXIST );
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
Gilles Peskinee59236f2018-01-27 23:32:46 +0100267/* END_HEADER */
268
269/* BEGIN_DEPENDENCIES
270 * depends_on:MBEDTLS_PSA_CRYPTO_C
271 * END_DEPENDENCIES
272 */
273
274/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200275void static_checks( )
276{
277 size_t max_truncated_mac_size =
278 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
279
280 /* Check that the length for a truncated MAC always fits in the algorithm
281 * encoding. The shifted mask is the maximum truncated value. The
282 * untruncated algorithm may be one byte larger. */
283 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +0100284
285#if defined(MBEDTLS_TEST_DEPRECATED)
286 /* Check deprecated constants. */
287 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
288 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
289 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
290 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
291 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
292 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
293 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
294 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100295
Paul Elliott8ff510a2020-06-02 17:19:28 +0100296 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
297 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
298 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
299 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
300 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
301 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
302 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
303 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
304 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
305 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
306 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
307 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
308 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
309 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
310 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
311 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
312 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
313 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
314 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
315 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
316 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
317 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
318 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
319 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
320 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
321 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
322 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
323 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
324 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
325 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
326
327 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
328 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
329 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
330 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
331 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
332 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
333 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
334 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100335
Paul Elliott75e27032020-06-03 15:17:39 +0100336 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
337 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
338 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
339 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
340 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
341
342 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
343 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100344#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200345}
346/* END_CASE */
347
348/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200349void import_with_policy( int type_arg,
350 int usage_arg, int alg_arg,
351 int expected_status_arg )
352{
353 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
354 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200355 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200356 psa_key_type_t type = type_arg;
357 psa_key_usage_t usage = usage_arg;
358 psa_algorithm_t alg = alg_arg;
359 psa_status_t expected_status = expected_status_arg;
360 const uint8_t key_material[16] = {0};
361 psa_status_t status;
362
363 PSA_ASSERT( psa_crypto_init( ) );
364
365 psa_set_key_type( &attributes, type );
366 psa_set_key_usage_flags( &attributes, usage );
367 psa_set_key_algorithm( &attributes, alg );
368
369 status = psa_import_key( &attributes,
370 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200371 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200372 TEST_EQUAL( status, expected_status );
373 if( status != PSA_SUCCESS )
374 goto exit;
375
Ronald Cron5425a212020-08-04 14:58:35 +0200376 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200377 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
378 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
379 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200380 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200381
Ronald Cron5425a212020-08-04 14:58:35 +0200382 PSA_ASSERT( psa_destroy_key( key ) );
383 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200384
385exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100386 /*
387 * Key attributes may have been returned by psa_get_key_attributes()
388 * thus reset them as required.
389 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200390 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100391
392 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200393 PSA_DONE( );
394}
395/* END_CASE */
396
397/* BEGIN_CASE */
398void import_with_data( data_t *data, int type_arg,
399 int attr_bits_arg,
400 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200401{
402 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
403 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200404 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200405 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200406 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200407 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100408 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100409
Gilles Peskine8817f612018-12-18 00:18:46 +0100410 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100411
Gilles Peskine4747d192019-04-17 15:05:45 +0200412 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200413 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200414
Ronald Cron5425a212020-08-04 14:58:35 +0200415 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100416 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200417 if( status != PSA_SUCCESS )
418 goto exit;
419
Ronald Cron5425a212020-08-04 14:58:35 +0200420 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200421 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200422 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200423 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200424 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200425
Ronald Cron5425a212020-08-04 14:58:35 +0200426 PSA_ASSERT( psa_destroy_key( key ) );
427 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100428
429exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100430 /*
431 * Key attributes may have been returned by psa_get_key_attributes()
432 * thus reset them as required.
433 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200434 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100435
436 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200437 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100438}
439/* END_CASE */
440
441/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200442void import_large_key( int type_arg, int byte_size_arg,
443 int expected_status_arg )
444{
445 psa_key_type_t type = type_arg;
446 size_t byte_size = byte_size_arg;
447 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
448 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200449 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200450 psa_status_t status;
451 uint8_t *buffer = NULL;
452 size_t buffer_size = byte_size + 1;
453 size_t n;
454
Steven Cooreman69967ce2021-01-18 18:01:08 +0100455 /* Skip the test case if the target running the test cannot
456 * accomodate large keys due to heap size constraints */
457 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200458 memset( buffer, 'K', byte_size );
459
460 PSA_ASSERT( psa_crypto_init( ) );
461
462 /* Try importing the key */
463 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
464 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200465 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100466 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200467 TEST_EQUAL( status, expected_status );
468
469 if( status == PSA_SUCCESS )
470 {
Ronald Cron5425a212020-08-04 14:58:35 +0200471 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200472 TEST_EQUAL( psa_get_key_type( &attributes ), type );
473 TEST_EQUAL( psa_get_key_bits( &attributes ),
474 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200475 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200476 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200477 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200478 for( n = 0; n < byte_size; n++ )
479 TEST_EQUAL( buffer[n], 'K' );
480 for( n = byte_size; n < buffer_size; n++ )
481 TEST_EQUAL( buffer[n], 0 );
482 }
483
484exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100485 /*
486 * Key attributes may have been returned by psa_get_key_attributes()
487 * thus reset them as required.
488 */
489 psa_reset_key_attributes( &attributes );
490
Ronald Cron5425a212020-08-04 14:58:35 +0200491 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200492 PSA_DONE( );
493 mbedtls_free( buffer );
494}
495/* END_CASE */
496
497/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200498void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
499{
Ronald Cron5425a212020-08-04 14:58:35 +0200500 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200501 size_t bits = bits_arg;
502 psa_status_t expected_status = expected_status_arg;
503 psa_status_t status;
504 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200505 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200506 size_t buffer_size = /* Slight overapproximations */
507 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200508 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200509 unsigned char *p;
510 int ret;
511 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200512 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200513
Gilles Peskine8817f612018-12-18 00:18:46 +0100514 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200515 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200516
517 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
518 bits, keypair ) ) >= 0 );
519 length = ret;
520
521 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200522 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200523 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100524 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200525
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200526 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200527 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200528
529exit:
530 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200531 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200532}
533/* END_CASE */
534
535/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300536void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300537 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200538 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100539 int expected_bits,
540 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200541 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100542 int canonical_input )
543{
Ronald Cron5425a212020-08-04 14:58:35 +0200544 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100545 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200546 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200547 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100548 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100549 unsigned char *exported = NULL;
550 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100551 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100552 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100553 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200554 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200555 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100556
Moran Pekercb088e72018-07-17 17:36:59 +0300557 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200558 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100559 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200560 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100561 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100562
Gilles Peskine4747d192019-04-17 15:05:45 +0200563 psa_set_key_usage_flags( &attributes, usage_arg );
564 psa_set_key_algorithm( &attributes, alg );
565 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700566
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100567 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200568 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100569
570 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200571 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200572 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
573 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200574 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100575
576 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200577 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100578 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100579
580 /* The exported length must be set by psa_export_key() to a value between 0
581 * and export_size. On errors, the exported length must be 0. */
582 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
583 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
584 TEST_ASSERT( exported_length <= export_size );
585
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200586 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200587 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100588 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200589 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100590 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100591 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200592 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100593
Gilles Peskineea38a922021-02-13 00:05:16 +0100594 /* Run sanity checks on the exported key. For non-canonical inputs,
595 * this validates the canonical representations. For canonical inputs,
596 * this doesn't directly validate the implementation, but it still helps
597 * by cross-validating the test data with the sanity check code. */
598 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200599 goto exit;
600
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100601 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200602 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100603 else
604 {
Ronald Cron5425a212020-08-04 14:58:35 +0200605 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200606 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200607 &key2 ) );
608 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100609 reexported,
610 export_size,
611 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200612 ASSERT_COMPARE( exported, exported_length,
613 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200614 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100615 }
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100616 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100617
618destroy:
619 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200620 PSA_ASSERT( psa_destroy_key( key ) );
621 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100622
623exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100624 /*
625 * Key attributes may have been returned by psa_get_key_attributes()
626 * thus reset them as required.
627 */
628 psa_reset_key_attributes( &got_attributes );
629
itayzafrir3e02b3b2018-06-12 17:06:52 +0300630 mbedtls_free( exported );
631 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200632 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100633}
634/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100635
Moran Pekerf709f4a2018-06-06 17:26:04 +0300636/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300637void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200638 int type_arg,
639 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100640 int export_size_delta,
641 int expected_export_status_arg,
642 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300643{
Ronald Cron5425a212020-08-04 14:58:35 +0200644 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300645 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200646 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200647 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300648 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300649 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100650 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100651 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200652 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300653
Gilles Peskine8817f612018-12-18 00:18:46 +0100654 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300655
Gilles Peskine4747d192019-04-17 15:05:45 +0200656 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
657 psa_set_key_algorithm( &attributes, alg );
658 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300659
660 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200661 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300662
Gilles Peskine49c25912018-10-29 15:15:31 +0100663 /* Export the public key */
664 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200665 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200666 exported, export_size,
667 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100668 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100669 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100670 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200671 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100672 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200673 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200674 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100675 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100676 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +0100677 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
678 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100679 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300680
681exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100682 /*
683 * Key attributes may have been returned by psa_get_key_attributes()
684 * thus reset them as required.
685 */
686 psa_reset_key_attributes( &attributes );
687
itayzafrir3e02b3b2018-06-12 17:06:52 +0300688 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200689 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200690 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300691}
692/* END_CASE */
693
Gilles Peskine20035e32018-02-03 22:44:14 +0100694/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200695void import_and_exercise_key( data_t *data,
696 int type_arg,
697 int bits_arg,
698 int alg_arg )
699{
Ronald Cron5425a212020-08-04 14:58:35 +0200700 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200701 psa_key_type_t type = type_arg;
702 size_t bits = bits_arg;
703 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100704 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200705 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200706 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200707
Gilles Peskine8817f612018-12-18 00:18:46 +0100708 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200709
Gilles Peskine4747d192019-04-17 15:05:45 +0200710 psa_set_key_usage_flags( &attributes, usage );
711 psa_set_key_algorithm( &attributes, alg );
712 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200713
714 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200715 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200716
717 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200718 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200719 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
720 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200721
722 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100723 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200724 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200725
Ronald Cron5425a212020-08-04 14:58:35 +0200726 PSA_ASSERT( psa_destroy_key( key ) );
727 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200728
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200729exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100730 /*
731 * Key attributes may have been returned by psa_get_key_attributes()
732 * thus reset them as required.
733 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200734 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100735
736 psa_reset_key_attributes( &attributes );
737 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200738 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200739}
740/* END_CASE */
741
742/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100743void effective_key_attributes( int type_arg, int expected_type_arg,
744 int bits_arg, int expected_bits_arg,
745 int usage_arg, int expected_usage_arg,
746 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200747{
Ronald Cron5425a212020-08-04 14:58:35 +0200748 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100749 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100750 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100751 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100752 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200753 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100754 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200755 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100756 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200757 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200758
Gilles Peskine8817f612018-12-18 00:18:46 +0100759 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200760
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200761 psa_set_key_usage_flags( &attributes, usage );
762 psa_set_key_algorithm( &attributes, alg );
763 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +0100764 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +0200765
Ronald Cron5425a212020-08-04 14:58:35 +0200766 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +0100767 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +0200768
Ronald Cron5425a212020-08-04 14:58:35 +0200769 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +0100770 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
771 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
772 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
773 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200774
775exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100776 /*
777 * Key attributes may have been returned by psa_get_key_attributes()
778 * thus reset them as required.
779 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200780 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100781
782 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200783 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +0200784}
785/* END_CASE */
786
787/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100788void check_key_policy( int type_arg, int bits_arg,
789 int usage_arg, int alg_arg )
790{
791 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
792 usage_arg, usage_arg, alg_arg, alg_arg );
793 goto exit;
794}
795/* END_CASE */
796
797/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200798void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +0000799{
800 /* Test each valid way of initializing the object, except for `= {0}`, as
801 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
802 * though it's OK by the C standard. We could test for this, but we'd need
803 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200804 psa_key_attributes_t func = psa_key_attributes_init( );
805 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
806 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000807
808 memset( &zero, 0, sizeof( zero ) );
809
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200810 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
811 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
812 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000813
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200814 TEST_EQUAL( psa_get_key_type( &func ), 0 );
815 TEST_EQUAL( psa_get_key_type( &init ), 0 );
816 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
817
818 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
819 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
820 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
821
822 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
823 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
824 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
825
826 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
827 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
828 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +0000829}
830/* END_CASE */
831
832/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200833void mac_key_policy( int policy_usage,
834 int policy_alg,
835 int key_type,
836 data_t *key_data,
837 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200838{
Ronald Cron5425a212020-08-04 14:58:35 +0200839 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200840 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000841 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200842 psa_status_t status;
843 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200844
Gilles Peskine8817f612018-12-18 00:18:46 +0100845 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200846
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200847 psa_set_key_usage_flags( &attributes, policy_usage );
848 psa_set_key_algorithm( &attributes, policy_alg );
849 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200850
Gilles Peskine049c7532019-05-15 20:22:09 +0200851 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200852 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200853
Ronald Cron5425a212020-08-04 14:58:35 +0200854 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200855 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100856 ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100857 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200858 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100859 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200860 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200861
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200862 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200863 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200864 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100865 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100866 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200867 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100868 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200869
870exit:
871 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200872 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200873 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200874}
875/* END_CASE */
876
877/* BEGIN_CASE */
878void cipher_key_policy( int policy_usage,
879 int policy_alg,
880 int key_type,
881 data_t *key_data,
882 int exercise_alg )
883{
Ronald Cron5425a212020-08-04 14:58:35 +0200884 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200885 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000886 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200887 psa_status_t status;
888
Gilles Peskine8817f612018-12-18 00:18:46 +0100889 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200890
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200891 psa_set_key_usage_flags( &attributes, policy_usage );
892 psa_set_key_algorithm( &attributes, policy_alg );
893 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200894
Gilles Peskine049c7532019-05-15 20:22:09 +0200895 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200896 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200897
Ronald Cron5425a212020-08-04 14:58:35 +0200898 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200899 if( policy_alg == exercise_alg &&
900 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100901 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200902 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100903 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200904 psa_cipher_abort( &operation );
905
Ronald Cron5425a212020-08-04 14:58:35 +0200906 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200907 if( policy_alg == exercise_alg &&
908 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100909 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200910 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100911 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200912
913exit:
914 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200915 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200916 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200917}
918/* END_CASE */
919
920/* BEGIN_CASE */
921void aead_key_policy( int policy_usage,
922 int policy_alg,
923 int key_type,
924 data_t *key_data,
925 int nonce_length_arg,
926 int tag_length_arg,
927 int exercise_alg )
928{
Ronald Cron5425a212020-08-04 14:58:35 +0200929 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200930 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200931 psa_status_t status;
932 unsigned char nonce[16] = {0};
933 size_t nonce_length = nonce_length_arg;
934 unsigned char tag[16];
935 size_t tag_length = tag_length_arg;
936 size_t output_length;
937
938 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
939 TEST_ASSERT( tag_length <= sizeof( tag ) );
940
Gilles Peskine8817f612018-12-18 00:18:46 +0100941 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200942
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200943 psa_set_key_usage_flags( &attributes, policy_usage );
944 psa_set_key_algorithm( &attributes, policy_alg );
945 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200946
Gilles Peskine049c7532019-05-15 20:22:09 +0200947 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200948 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200949
Ronald Cron5425a212020-08-04 14:58:35 +0200950 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200951 nonce, nonce_length,
952 NULL, 0,
953 NULL, 0,
954 tag, tag_length,
955 &output_length );
956 if( policy_alg == exercise_alg &&
957 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100958 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200959 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100960 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200961
962 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200963 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200964 nonce, nonce_length,
965 NULL, 0,
966 tag, tag_length,
967 NULL, 0,
968 &output_length );
969 if( policy_alg == exercise_alg &&
970 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100971 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200972 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100973 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200974
975exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200976 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200977 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200978}
979/* END_CASE */
980
981/* BEGIN_CASE */
982void asymmetric_encryption_key_policy( int policy_usage,
983 int policy_alg,
984 int key_type,
985 data_t *key_data,
986 int exercise_alg )
987{
Ronald Cron5425a212020-08-04 14:58:35 +0200988 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200989 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200990 psa_status_t status;
991 size_t key_bits;
992 size_t buffer_length;
993 unsigned char *buffer = NULL;
994 size_t output_length;
995
Gilles Peskine8817f612018-12-18 00:18:46 +0100996 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200997
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200998 psa_set_key_usage_flags( &attributes, policy_usage );
999 psa_set_key_algorithm( &attributes, policy_alg );
1000 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001001
Gilles Peskine049c7532019-05-15 20:22:09 +02001002 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001003 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001004
Ronald Cron5425a212020-08-04 14:58:35 +02001005 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001006 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001007 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1008 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001009 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001010
Ronald Cron5425a212020-08-04 14:58:35 +02001011 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001012 NULL, 0,
1013 NULL, 0,
1014 buffer, buffer_length,
1015 &output_length );
1016 if( policy_alg == exercise_alg &&
1017 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001018 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001019 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001020 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001021
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001022 if( buffer_length != 0 )
1023 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001024 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001025 buffer, buffer_length,
1026 NULL, 0,
1027 buffer, buffer_length,
1028 &output_length );
1029 if( policy_alg == exercise_alg &&
1030 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001031 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001032 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001033 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001034
1035exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001036 /*
1037 * Key attributes may have been returned by psa_get_key_attributes()
1038 * thus reset them as required.
1039 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001040 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001041
1042 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001043 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001044 mbedtls_free( buffer );
1045}
1046/* END_CASE */
1047
1048/* BEGIN_CASE */
1049void asymmetric_signature_key_policy( int policy_usage,
1050 int policy_alg,
1051 int key_type,
1052 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001053 int exercise_alg,
1054 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001055{
Ronald Cron5425a212020-08-04 14:58:35 +02001056 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001057 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001058 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001059 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1060 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1061 * compatible with the policy and `payload_length_arg` is supposed to be
1062 * a valid input length to sign. If `payload_length_arg <= 0`,
1063 * `exercise_alg` is supposed to be forbidden by the policy. */
1064 int compatible_alg = payload_length_arg > 0;
1065 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001066 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001067 size_t signature_length;
1068
Gilles Peskine8817f612018-12-18 00:18:46 +01001069 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001070
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001071 psa_set_key_usage_flags( &attributes, policy_usage );
1072 psa_set_key_algorithm( &attributes, policy_alg );
1073 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001074
Gilles Peskine049c7532019-05-15 20:22:09 +02001075 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001076 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001077
Ronald Cron5425a212020-08-04 14:58:35 +02001078 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001079 payload, payload_length,
1080 signature, sizeof( signature ),
1081 &signature_length );
1082 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001083 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001084 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001085 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001086
1087 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001088 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001089 payload, payload_length,
1090 signature, sizeof( signature ) );
1091 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001092 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001093 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001094 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001095
1096exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001097 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001098 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001099}
1100/* END_CASE */
1101
Janos Follathba3fab92019-06-11 14:50:16 +01001102/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001103void derive_key_policy( int policy_usage,
1104 int policy_alg,
1105 int key_type,
1106 data_t *key_data,
1107 int exercise_alg )
1108{
Ronald Cron5425a212020-08-04 14:58:35 +02001109 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001110 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001111 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001112 psa_status_t status;
1113
Gilles Peskine8817f612018-12-18 00:18:46 +01001114 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001115
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001116 psa_set_key_usage_flags( &attributes, policy_usage );
1117 psa_set_key_algorithm( &attributes, policy_alg );
1118 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001119
Gilles Peskine049c7532019-05-15 20:22:09 +02001120 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001121 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001122
Janos Follathba3fab92019-06-11 14:50:16 +01001123 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1124
1125 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1126 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001127 {
Janos Follathba3fab92019-06-11 14:50:16 +01001128 PSA_ASSERT( psa_key_derivation_input_bytes(
1129 &operation,
1130 PSA_KEY_DERIVATION_INPUT_SEED,
1131 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001132 }
Janos Follathba3fab92019-06-11 14:50:16 +01001133
1134 status = psa_key_derivation_input_key( &operation,
1135 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001136 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001137
Gilles Peskineea0fb492018-07-12 17:17:20 +02001138 if( policy_alg == exercise_alg &&
1139 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001140 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001141 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001142 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001143
1144exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001145 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001146 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001147 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001148}
1149/* END_CASE */
1150
1151/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001152void agreement_key_policy( int policy_usage,
1153 int policy_alg,
1154 int key_type_arg,
1155 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001156 int exercise_alg,
1157 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001158{
Ronald Cron5425a212020-08-04 14:58:35 +02001159 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001160 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001161 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001162 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001163 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001164 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001165
Gilles Peskine8817f612018-12-18 00:18:46 +01001166 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001167
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001168 psa_set_key_usage_flags( &attributes, policy_usage );
1169 psa_set_key_algorithm( &attributes, policy_alg );
1170 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001171
Gilles Peskine049c7532019-05-15 20:22:09 +02001172 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001173 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001174
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001175 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001176 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001177
Steven Cooremance48e852020-10-05 16:02:45 +02001178 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001179
1180exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001181 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001182 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001183 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001184}
1185/* END_CASE */
1186
1187/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001188void key_policy_alg2( int key_type_arg, data_t *key_data,
1189 int usage_arg, int alg_arg, int alg2_arg )
1190{
Ronald Cron5425a212020-08-04 14:58:35 +02001191 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001192 psa_key_type_t key_type = key_type_arg;
1193 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1194 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1195 psa_key_usage_t usage = usage_arg;
1196 psa_algorithm_t alg = alg_arg;
1197 psa_algorithm_t alg2 = alg2_arg;
1198
1199 PSA_ASSERT( psa_crypto_init( ) );
1200
1201 psa_set_key_usage_flags( &attributes, usage );
1202 psa_set_key_algorithm( &attributes, alg );
1203 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1204 psa_set_key_type( &attributes, key_type );
1205 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001206 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001207
Ronald Cron5425a212020-08-04 14:58:35 +02001208 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001209 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1210 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1211 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1212
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001213 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001214 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001215 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001216 goto exit;
1217
1218exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001219 /*
1220 * Key attributes may have been returned by psa_get_key_attributes()
1221 * thus reset them as required.
1222 */
1223 psa_reset_key_attributes( &got_attributes );
1224
Ronald Cron5425a212020-08-04 14:58:35 +02001225 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001226 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001227}
1228/* END_CASE */
1229
1230/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001231void raw_agreement_key_policy( int policy_usage,
1232 int policy_alg,
1233 int key_type_arg,
1234 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001235 int exercise_alg,
1236 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001237{
Ronald Cron5425a212020-08-04 14:58:35 +02001238 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001239 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001240 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001241 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001242 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001243 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001244
1245 PSA_ASSERT( psa_crypto_init( ) );
1246
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001247 psa_set_key_usage_flags( &attributes, policy_usage );
1248 psa_set_key_algorithm( &attributes, policy_alg );
1249 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001250
Gilles Peskine049c7532019-05-15 20:22:09 +02001251 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001252 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001253
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001254 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001255
Steven Cooremance48e852020-10-05 16:02:45 +02001256 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001257
1258exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001259 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001260 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001261 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001262}
1263/* END_CASE */
1264
1265/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001266void copy_success( int source_usage_arg,
1267 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001268 int type_arg, data_t *material,
1269 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001270 int target_usage_arg,
1271 int target_alg_arg, int target_alg2_arg,
1272 int expected_usage_arg,
1273 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001274{
Gilles Peskineca25db92019-04-19 11:43:08 +02001275 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1276 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001277 psa_key_usage_t expected_usage = expected_usage_arg;
1278 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001279 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001280 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1281 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001282 uint8_t *export_buffer = NULL;
1283
Gilles Peskine57ab7212019-01-28 13:03:09 +01001284 PSA_ASSERT( psa_crypto_init( ) );
1285
Gilles Peskineca25db92019-04-19 11:43:08 +02001286 /* Prepare the source key. */
1287 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1288 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001289 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001290 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001291 PSA_ASSERT( psa_import_key( &source_attributes,
1292 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001293 &source_key ) );
1294 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001295
Gilles Peskineca25db92019-04-19 11:43:08 +02001296 /* Prepare the target attributes. */
1297 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001298 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001299 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001300 /* Set volatile lifetime to reset the key identifier to 0. */
1301 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1302 }
1303
Gilles Peskineca25db92019-04-19 11:43:08 +02001304 if( target_usage_arg != -1 )
1305 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1306 if( target_alg_arg != -1 )
1307 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001308 if( target_alg2_arg != -1 )
1309 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001310
1311 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001312 PSA_ASSERT( psa_copy_key( source_key,
1313 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001314
1315 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001316 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001317
1318 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001319 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001320 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1321 psa_get_key_type( &target_attributes ) );
1322 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1323 psa_get_key_bits( &target_attributes ) );
1324 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1325 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001326 TEST_EQUAL( expected_alg2,
1327 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001328 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1329 {
1330 size_t length;
1331 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001332 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001333 material->len, &length ) );
1334 ASSERT_COMPARE( material->x, material->len,
1335 export_buffer, length );
1336 }
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001337 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001338 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001339 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001340 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001341
Ronald Cron5425a212020-08-04 14:58:35 +02001342 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001343
1344exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001345 /*
1346 * Source and target key attributes may have been returned by
1347 * psa_get_key_attributes() thus reset them as required.
1348 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001349 psa_reset_key_attributes( &source_attributes );
1350 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001351
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001352 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001353 mbedtls_free( export_buffer );
1354}
1355/* END_CASE */
1356
1357/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001358void copy_fail( int source_usage_arg,
1359 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001360 int type_arg, data_t *material,
1361 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001362 int target_usage_arg,
1363 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001364 int expected_status_arg )
1365{
1366 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1367 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001368 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1369 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine4a644642019-05-03 17:14:08 +02001370
1371 PSA_ASSERT( psa_crypto_init( ) );
1372
1373 /* Prepare the source key. */
1374 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1375 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001376 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001377 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001378 PSA_ASSERT( psa_import_key( &source_attributes,
1379 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001380 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001381
1382 /* Prepare the target attributes. */
1383 psa_set_key_type( &target_attributes, target_type_arg );
1384 psa_set_key_bits( &target_attributes, target_bits_arg );
1385 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1386 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001387 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001388
1389 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001390 TEST_EQUAL( psa_copy_key( source_key,
1391 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001392 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001393
Ronald Cron5425a212020-08-04 14:58:35 +02001394 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001395
Gilles Peskine4a644642019-05-03 17:14:08 +02001396exit:
1397 psa_reset_key_attributes( &source_attributes );
1398 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001399 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001400}
1401/* END_CASE */
1402
1403/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001404void hash_operation_init( )
1405{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001406 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001407 /* Test each valid way of initializing the object, except for `= {0}`, as
1408 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1409 * though it's OK by the C standard. We could test for this, but we'd need
1410 * to supress the Clang warning for the test. */
1411 psa_hash_operation_t func = psa_hash_operation_init( );
1412 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1413 psa_hash_operation_t zero;
1414
1415 memset( &zero, 0, sizeof( zero ) );
1416
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001417 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001418 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1419 PSA_ERROR_BAD_STATE );
1420 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1421 PSA_ERROR_BAD_STATE );
1422 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1423 PSA_ERROR_BAD_STATE );
1424
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001425 /* A default hash operation should be abortable without error. */
1426 PSA_ASSERT( psa_hash_abort( &func ) );
1427 PSA_ASSERT( psa_hash_abort( &init ) );
1428 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001429}
1430/* END_CASE */
1431
1432/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001433void hash_setup( int alg_arg,
1434 int expected_status_arg )
1435{
1436 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001437 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001438 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001439 psa_status_t status;
1440
Gilles Peskine8817f612018-12-18 00:18:46 +01001441 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001442
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001443 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001444 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001445
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001446 /* Whether setup succeeded or failed, abort must succeed. */
1447 PSA_ASSERT( psa_hash_abort( &operation ) );
1448
1449 /* If setup failed, reproduce the failure, so as to
1450 * test the resulting state of the operation object. */
1451 if( status != PSA_SUCCESS )
1452 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1453
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001454 /* Now the operation object should be reusable. */
1455#if defined(KNOWN_SUPPORTED_HASH_ALG)
1456 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1457 PSA_ASSERT( psa_hash_abort( &operation ) );
1458#endif
1459
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001460exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001461 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001462}
1463/* END_CASE */
1464
1465/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001466void hash_compute_fail( int alg_arg, data_t *input,
1467 int output_size_arg, int expected_status_arg )
1468{
1469 psa_algorithm_t alg = alg_arg;
1470 uint8_t *output = NULL;
1471 size_t output_size = output_size_arg;
1472 size_t output_length = INVALID_EXPORT_LENGTH;
1473 psa_status_t expected_status = expected_status_arg;
1474 psa_status_t status;
1475
1476 ASSERT_ALLOC( output, output_size );
1477
1478 PSA_ASSERT( psa_crypto_init( ) );
1479
1480 status = psa_hash_compute( alg, input->x, input->len,
1481 output, output_size, &output_length );
1482 TEST_EQUAL( status, expected_status );
1483 TEST_ASSERT( output_length <= output_size );
1484
1485exit:
1486 mbedtls_free( output );
1487 PSA_DONE( );
1488}
1489/* END_CASE */
1490
1491/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001492void hash_compare_fail( int alg_arg, data_t *input,
1493 data_t *reference_hash,
1494 int expected_status_arg )
1495{
1496 psa_algorithm_t alg = alg_arg;
1497 psa_status_t expected_status = expected_status_arg;
1498 psa_status_t status;
1499
1500 PSA_ASSERT( psa_crypto_init( ) );
1501
1502 status = psa_hash_compare( alg, input->x, input->len,
1503 reference_hash->x, reference_hash->len );
1504 TEST_EQUAL( status, expected_status );
1505
1506exit:
1507 PSA_DONE( );
1508}
1509/* END_CASE */
1510
1511/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001512void hash_compute_compare( int alg_arg, data_t *input,
1513 data_t *expected_output )
1514{
1515 psa_algorithm_t alg = alg_arg;
1516 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1517 size_t output_length = INVALID_EXPORT_LENGTH;
1518 size_t i;
1519
1520 PSA_ASSERT( psa_crypto_init( ) );
1521
1522 /* Compute with tight buffer */
1523 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001524 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001525 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001526 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001527 ASSERT_COMPARE( output, output_length,
1528 expected_output->x, expected_output->len );
1529
1530 /* Compute with larger buffer */
1531 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1532 output, sizeof( output ),
1533 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001534 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001535 ASSERT_COMPARE( output, output_length,
1536 expected_output->x, expected_output->len );
1537
1538 /* Compare with correct hash */
1539 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1540 output, output_length ) );
1541
1542 /* Compare with trailing garbage */
1543 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1544 output, output_length + 1 ),
1545 PSA_ERROR_INVALID_SIGNATURE );
1546
1547 /* Compare with truncated hash */
1548 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1549 output, output_length - 1 ),
1550 PSA_ERROR_INVALID_SIGNATURE );
1551
1552 /* Compare with corrupted value */
1553 for( i = 0; i < output_length; i++ )
1554 {
Chris Jones9634bb12021-01-20 15:56:42 +00001555 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001556 output[i] ^= 1;
1557 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1558 output, output_length ),
1559 PSA_ERROR_INVALID_SIGNATURE );
1560 output[i] ^= 1;
1561 }
1562
1563exit:
1564 PSA_DONE( );
1565}
1566/* END_CASE */
1567
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001568/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001569void hash_bad_order( )
1570{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001571 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001572 unsigned char input[] = "";
1573 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001574 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001575 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1576 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1577 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001578 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001579 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001580 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001581
Gilles Peskine8817f612018-12-18 00:18:46 +01001582 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001583
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001584 /* Call setup twice in a row. */
1585 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1586 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1587 PSA_ERROR_BAD_STATE );
1588 PSA_ASSERT( psa_hash_abort( &operation ) );
1589
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001590 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001591 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001592 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001593 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001594
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001595 /* Call update after finish. */
1596 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1597 PSA_ASSERT( psa_hash_finish( &operation,
1598 hash, sizeof( hash ), &hash_len ) );
1599 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001600 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001601 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001602
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001603 /* Call verify without calling setup beforehand. */
1604 TEST_EQUAL( psa_hash_verify( &operation,
1605 valid_hash, sizeof( valid_hash ) ),
1606 PSA_ERROR_BAD_STATE );
1607 PSA_ASSERT( psa_hash_abort( &operation ) );
1608
1609 /* Call verify after finish. */
1610 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1611 PSA_ASSERT( psa_hash_finish( &operation,
1612 hash, sizeof( hash ), &hash_len ) );
1613 TEST_EQUAL( psa_hash_verify( &operation,
1614 valid_hash, sizeof( valid_hash ) ),
1615 PSA_ERROR_BAD_STATE );
1616 PSA_ASSERT( psa_hash_abort( &operation ) );
1617
1618 /* Call verify twice in a row. */
1619 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1620 PSA_ASSERT( psa_hash_verify( &operation,
1621 valid_hash, sizeof( valid_hash ) ) );
1622 TEST_EQUAL( psa_hash_verify( &operation,
1623 valid_hash, sizeof( valid_hash ) ),
1624 PSA_ERROR_BAD_STATE );
1625 PSA_ASSERT( psa_hash_abort( &operation ) );
1626
1627 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001628 TEST_EQUAL( psa_hash_finish( &operation,
1629 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001630 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001631 PSA_ASSERT( psa_hash_abort( &operation ) );
1632
1633 /* Call finish twice in a row. */
1634 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1635 PSA_ASSERT( psa_hash_finish( &operation,
1636 hash, sizeof( hash ), &hash_len ) );
1637 TEST_EQUAL( psa_hash_finish( &operation,
1638 hash, sizeof( hash ), &hash_len ),
1639 PSA_ERROR_BAD_STATE );
1640 PSA_ASSERT( psa_hash_abort( &operation ) );
1641
1642 /* Call finish after calling verify. */
1643 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1644 PSA_ASSERT( psa_hash_verify( &operation,
1645 valid_hash, sizeof( valid_hash ) ) );
1646 TEST_EQUAL( psa_hash_finish( &operation,
1647 hash, sizeof( hash ), &hash_len ),
1648 PSA_ERROR_BAD_STATE );
1649 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001650
1651exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001652 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001653}
1654/* END_CASE */
1655
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001656/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001657void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001658{
1659 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001660 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1661 * appended to it */
1662 unsigned char hash[] = {
1663 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1664 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1665 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001666 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001667 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001668
Gilles Peskine8817f612018-12-18 00:18:46 +01001669 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001670
itayzafrir27e69452018-11-01 14:26:34 +02001671 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001672 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001673 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001674 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001675
itayzafrir27e69452018-11-01 14:26:34 +02001676 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001677 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001678 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001679 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001680
itayzafrir27e69452018-11-01 14:26:34 +02001681 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001682 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001683 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001684 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001685
itayzafrirec93d302018-10-18 18:01:10 +03001686exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001687 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001688}
1689/* END_CASE */
1690
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001691/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1692void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001693{
1694 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001695 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001696 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001697 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001698 size_t hash_len;
1699
Gilles Peskine8817f612018-12-18 00:18:46 +01001700 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001701
itayzafrir58028322018-10-25 10:22:01 +03001702 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001703 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001704 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001705 hash, expected_size - 1, &hash_len ),
1706 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001707
1708exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001709 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001710}
1711/* END_CASE */
1712
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001713/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1714void hash_clone_source_state( )
1715{
1716 psa_algorithm_t alg = PSA_ALG_SHA_256;
1717 unsigned char hash[PSA_HASH_MAX_SIZE];
1718 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1719 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1720 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1721 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1722 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1723 size_t hash_len;
1724
1725 PSA_ASSERT( psa_crypto_init( ) );
1726 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1727
1728 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1729 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1730 PSA_ASSERT( psa_hash_finish( &op_finished,
1731 hash, sizeof( hash ), &hash_len ) );
1732 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1733 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1734
1735 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1736 PSA_ERROR_BAD_STATE );
1737
1738 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1739 PSA_ASSERT( psa_hash_finish( &op_init,
1740 hash, sizeof( hash ), &hash_len ) );
1741 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1742 PSA_ASSERT( psa_hash_finish( &op_finished,
1743 hash, sizeof( hash ), &hash_len ) );
1744 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1745 PSA_ASSERT( psa_hash_finish( &op_aborted,
1746 hash, sizeof( hash ), &hash_len ) );
1747
1748exit:
1749 psa_hash_abort( &op_source );
1750 psa_hash_abort( &op_init );
1751 psa_hash_abort( &op_setup );
1752 psa_hash_abort( &op_finished );
1753 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001754 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001755}
1756/* END_CASE */
1757
1758/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1759void hash_clone_target_state( )
1760{
1761 psa_algorithm_t alg = PSA_ALG_SHA_256;
1762 unsigned char hash[PSA_HASH_MAX_SIZE];
1763 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1764 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1765 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1766 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1767 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1768 size_t hash_len;
1769
1770 PSA_ASSERT( psa_crypto_init( ) );
1771
1772 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1773 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1774 PSA_ASSERT( psa_hash_finish( &op_finished,
1775 hash, sizeof( hash ), &hash_len ) );
1776 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1777 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1778
1779 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1780 PSA_ASSERT( psa_hash_finish( &op_target,
1781 hash, sizeof( hash ), &hash_len ) );
1782
1783 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1784 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1785 PSA_ERROR_BAD_STATE );
1786 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1787 PSA_ERROR_BAD_STATE );
1788
1789exit:
1790 psa_hash_abort( &op_target );
1791 psa_hash_abort( &op_init );
1792 psa_hash_abort( &op_setup );
1793 psa_hash_abort( &op_finished );
1794 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001795 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001796}
1797/* END_CASE */
1798
itayzafrir58028322018-10-25 10:22:01 +03001799/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001800void mac_operation_init( )
1801{
Jaeden Amero252ef282019-02-15 14:05:35 +00001802 const uint8_t input[1] = { 0 };
1803
Jaeden Amero769ce272019-01-04 11:48:03 +00001804 /* Test each valid way of initializing the object, except for `= {0}`, as
1805 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1806 * though it's OK by the C standard. We could test for this, but we'd need
1807 * to supress the Clang warning for the test. */
1808 psa_mac_operation_t func = psa_mac_operation_init( );
1809 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1810 psa_mac_operation_t zero;
1811
1812 memset( &zero, 0, sizeof( zero ) );
1813
Jaeden Amero252ef282019-02-15 14:05:35 +00001814 /* A freshly-initialized MAC operation should not be usable. */
1815 TEST_EQUAL( psa_mac_update( &func,
1816 input, sizeof( input ) ),
1817 PSA_ERROR_BAD_STATE );
1818 TEST_EQUAL( psa_mac_update( &init,
1819 input, sizeof( input ) ),
1820 PSA_ERROR_BAD_STATE );
1821 TEST_EQUAL( psa_mac_update( &zero,
1822 input, sizeof( input ) ),
1823 PSA_ERROR_BAD_STATE );
1824
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001825 /* A default MAC operation should be abortable without error. */
1826 PSA_ASSERT( psa_mac_abort( &func ) );
1827 PSA_ASSERT( psa_mac_abort( &init ) );
1828 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001829}
1830/* END_CASE */
1831
1832/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001833void mac_setup( int key_type_arg,
1834 data_t *key,
1835 int alg_arg,
1836 int expected_status_arg )
1837{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001838 psa_key_type_t key_type = key_type_arg;
1839 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001840 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001841 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001842 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1843#if defined(KNOWN_SUPPORTED_MAC_ALG)
1844 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1845#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001846
Gilles Peskine8817f612018-12-18 00:18:46 +01001847 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001848
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001849 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1850 &operation, &status ) )
1851 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001852 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001853
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001854 /* The operation object should be reusable. */
1855#if defined(KNOWN_SUPPORTED_MAC_ALG)
1856 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1857 smoke_test_key_data,
1858 sizeof( smoke_test_key_data ),
1859 KNOWN_SUPPORTED_MAC_ALG,
1860 &operation, &status ) )
1861 goto exit;
1862 TEST_EQUAL( status, PSA_SUCCESS );
1863#endif
1864
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001865exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001866 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001867}
1868/* END_CASE */
1869
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001870/* 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 +00001871void mac_bad_order( )
1872{
Ronald Cron5425a212020-08-04 14:58:35 +02001873 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001874 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1875 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001876 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001877 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1878 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1879 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001880 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001881 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1882 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1883 size_t sign_mac_length = 0;
1884 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1885 const uint8_t verify_mac[] = {
1886 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1887 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1888 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1889
1890 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001891 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001892 psa_set_key_algorithm( &attributes, alg );
1893 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001894
Ronald Cron5425a212020-08-04 14:58:35 +02001895 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1896 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001897
Jaeden Amero252ef282019-02-15 14:05:35 +00001898 /* Call update without calling setup beforehand. */
1899 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1900 PSA_ERROR_BAD_STATE );
1901 PSA_ASSERT( psa_mac_abort( &operation ) );
1902
1903 /* Call sign finish without calling setup beforehand. */
1904 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1905 &sign_mac_length),
1906 PSA_ERROR_BAD_STATE );
1907 PSA_ASSERT( psa_mac_abort( &operation ) );
1908
1909 /* Call verify finish without calling setup beforehand. */
1910 TEST_EQUAL( psa_mac_verify_finish( &operation,
1911 verify_mac, sizeof( verify_mac ) ),
1912 PSA_ERROR_BAD_STATE );
1913 PSA_ASSERT( psa_mac_abort( &operation ) );
1914
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001915 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001916 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
1917 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001918 PSA_ERROR_BAD_STATE );
1919 PSA_ASSERT( psa_mac_abort( &operation ) );
1920
Jaeden Amero252ef282019-02-15 14:05:35 +00001921 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001922 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001923 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1924 PSA_ASSERT( psa_mac_sign_finish( &operation,
1925 sign_mac, sizeof( sign_mac ),
1926 &sign_mac_length ) );
1927 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1928 PSA_ERROR_BAD_STATE );
1929 PSA_ASSERT( psa_mac_abort( &operation ) );
1930
1931 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001932 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001933 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1934 PSA_ASSERT( psa_mac_verify_finish( &operation,
1935 verify_mac, sizeof( verify_mac ) ) );
1936 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1937 PSA_ERROR_BAD_STATE );
1938 PSA_ASSERT( psa_mac_abort( &operation ) );
1939
1940 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001941 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001942 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1943 PSA_ASSERT( psa_mac_sign_finish( &operation,
1944 sign_mac, sizeof( sign_mac ),
1945 &sign_mac_length ) );
1946 TEST_EQUAL( psa_mac_sign_finish( &operation,
1947 sign_mac, sizeof( sign_mac ),
1948 &sign_mac_length ),
1949 PSA_ERROR_BAD_STATE );
1950 PSA_ASSERT( psa_mac_abort( &operation ) );
1951
1952 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001953 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001954 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1955 PSA_ASSERT( psa_mac_verify_finish( &operation,
1956 verify_mac, sizeof( verify_mac ) ) );
1957 TEST_EQUAL( psa_mac_verify_finish( &operation,
1958 verify_mac, sizeof( verify_mac ) ),
1959 PSA_ERROR_BAD_STATE );
1960 PSA_ASSERT( psa_mac_abort( &operation ) );
1961
1962 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02001963 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001964 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1965 TEST_EQUAL( psa_mac_verify_finish( &operation,
1966 verify_mac, sizeof( verify_mac ) ),
1967 PSA_ERROR_BAD_STATE );
1968 PSA_ASSERT( psa_mac_abort( &operation ) );
1969
1970 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02001971 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001972 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1973 TEST_EQUAL( psa_mac_sign_finish( &operation,
1974 sign_mac, sizeof( sign_mac ),
1975 &sign_mac_length ),
1976 PSA_ERROR_BAD_STATE );
1977 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001978
Ronald Cron5425a212020-08-04 14:58:35 +02001979 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001980
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001981exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001982 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001983}
1984/* END_CASE */
1985
1986/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001987void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02001988 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001989 int alg_arg,
1990 data_t *input,
1991 data_t *expected_mac )
1992{
Ronald Cron5425a212020-08-04 14:58:35 +02001993 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001994 psa_key_type_t key_type = key_type_arg;
1995 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001996 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001997 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02001998 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001999 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002000 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002001 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002002 const size_t output_sizes_to_test[] = {
2003 0,
2004 1,
2005 expected_mac->len - 1,
2006 expected_mac->len,
2007 expected_mac->len + 1,
2008 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002009
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002010 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002011 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002012 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002013
Gilles Peskine8817f612018-12-18 00:18:46 +01002014 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002015
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002016 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002017 psa_set_key_algorithm( &attributes, alg );
2018 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002019
Ronald Cron5425a212020-08-04 14:58:35 +02002020 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2021 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002022
Gilles Peskine8b356b52020-08-25 23:44:59 +02002023 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2024 {
2025 const size_t output_size = output_sizes_to_test[i];
2026 psa_status_t expected_status =
2027 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2028 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002029
Chris Jones9634bb12021-01-20 15:56:42 +00002030 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002031 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002032
Gilles Peskine8b356b52020-08-25 23:44:59 +02002033 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002034 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002035 PSA_ASSERT( psa_mac_update( &operation,
2036 input->x, input->len ) );
2037 TEST_EQUAL( psa_mac_sign_finish( &operation,
2038 actual_mac, output_size,
2039 &mac_length ),
2040 expected_status );
2041 PSA_ASSERT( psa_mac_abort( &operation ) );
2042
2043 if( expected_status == PSA_SUCCESS )
2044 {
2045 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2046 actual_mac, mac_length );
2047 }
2048 mbedtls_free( actual_mac );
2049 actual_mac = NULL;
2050 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002051
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002052exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002053 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002054 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002055 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002056 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002057}
2058/* END_CASE */
2059
2060/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002061void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002062 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002063 int alg_arg,
2064 data_t *input,
2065 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002066{
Ronald Cron5425a212020-08-04 14:58:35 +02002067 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002068 psa_key_type_t key_type = key_type_arg;
2069 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002070 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002071 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002072 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002073
Gilles Peskine69c12672018-06-28 00:07:19 +02002074 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2075
Gilles Peskine8817f612018-12-18 00:18:46 +01002076 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002077
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002078 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002079 psa_set_key_algorithm( &attributes, alg );
2080 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002081
Ronald Cron5425a212020-08-04 14:58:35 +02002082 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2083 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002084
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002085 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002086 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002087 PSA_ASSERT( psa_mac_update( &operation,
2088 input->x, input->len ) );
2089 PSA_ASSERT( psa_mac_verify_finish( &operation,
2090 expected_mac->x,
2091 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002092
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002093 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002094 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002095 PSA_ASSERT( psa_mac_update( &operation,
2096 input->x, input->len ) );
2097 TEST_EQUAL( psa_mac_verify_finish( &operation,
2098 expected_mac->x,
2099 expected_mac->len - 1 ),
2100 PSA_ERROR_INVALID_SIGNATURE );
2101
2102 /* Test a MAC that's too long. */
2103 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2104 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002105 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002106 PSA_ASSERT( psa_mac_update( &operation,
2107 input->x, input->len ) );
2108 TEST_EQUAL( psa_mac_verify_finish( &operation,
2109 perturbed_mac,
2110 expected_mac->len + 1 ),
2111 PSA_ERROR_INVALID_SIGNATURE );
2112
2113 /* Test changing one byte. */
2114 for( size_t i = 0; i < expected_mac->len; i++ )
2115 {
Chris Jones9634bb12021-01-20 15:56:42 +00002116 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002117 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002118 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002119 PSA_ASSERT( psa_mac_update( &operation,
2120 input->x, input->len ) );
2121 TEST_EQUAL( psa_mac_verify_finish( &operation,
2122 perturbed_mac,
2123 expected_mac->len ),
2124 PSA_ERROR_INVALID_SIGNATURE );
2125 perturbed_mac[i] ^= 1;
2126 }
2127
Gilles Peskine8c9def32018-02-08 10:02:12 +01002128exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002129 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002130 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002131 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002132 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002133}
2134/* END_CASE */
2135
2136/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002137void cipher_operation_init( )
2138{
Jaeden Ameroab439972019-02-15 14:12:05 +00002139 const uint8_t input[1] = { 0 };
2140 unsigned char output[1] = { 0 };
2141 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002142 /* Test each valid way of initializing the object, except for `= {0}`, as
2143 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2144 * though it's OK by the C standard. We could test for this, but we'd need
2145 * to supress the Clang warning for the test. */
2146 psa_cipher_operation_t func = psa_cipher_operation_init( );
2147 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2148 psa_cipher_operation_t zero;
2149
2150 memset( &zero, 0, sizeof( zero ) );
2151
Jaeden Ameroab439972019-02-15 14:12:05 +00002152 /* A freshly-initialized cipher operation should not be usable. */
2153 TEST_EQUAL( psa_cipher_update( &func,
2154 input, sizeof( input ),
2155 output, sizeof( output ),
2156 &output_length ),
2157 PSA_ERROR_BAD_STATE );
2158 TEST_EQUAL( psa_cipher_update( &init,
2159 input, sizeof( input ),
2160 output, sizeof( output ),
2161 &output_length ),
2162 PSA_ERROR_BAD_STATE );
2163 TEST_EQUAL( psa_cipher_update( &zero,
2164 input, sizeof( input ),
2165 output, sizeof( output ),
2166 &output_length ),
2167 PSA_ERROR_BAD_STATE );
2168
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002169 /* A default cipher operation should be abortable without error. */
2170 PSA_ASSERT( psa_cipher_abort( &func ) );
2171 PSA_ASSERT( psa_cipher_abort( &init ) );
2172 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002173}
2174/* END_CASE */
2175
2176/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002177void cipher_setup( int key_type_arg,
2178 data_t *key,
2179 int alg_arg,
2180 int expected_status_arg )
2181{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002182 psa_key_type_t key_type = key_type_arg;
2183 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002184 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002185 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002186 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002187#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002188 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2189#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002190
Gilles Peskine8817f612018-12-18 00:18:46 +01002191 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002192
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002193 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2194 &operation, &status ) )
2195 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002196 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002197
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002198 /* The operation object should be reusable. */
2199#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2200 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2201 smoke_test_key_data,
2202 sizeof( smoke_test_key_data ),
2203 KNOWN_SUPPORTED_CIPHER_ALG,
2204 &operation, &status ) )
2205 goto exit;
2206 TEST_EQUAL( status, PSA_SUCCESS );
2207#endif
2208
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002209exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002210 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002211 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002212}
2213/* END_CASE */
2214
Steven Cooreman29eecbf2021-01-28 19:41:25 +01002215/* BEGIN_CASE depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002216void cipher_bad_order( )
2217{
Ronald Cron5425a212020-08-04 14:58:35 +02002218 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002219 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2220 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002221 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002222 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002223 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002224 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002225 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2226 0xaa, 0xaa, 0xaa, 0xaa };
2227 const uint8_t text[] = {
2228 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2229 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002230 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002231 size_t length = 0;
2232
2233 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002234 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2235 psa_set_key_algorithm( &attributes, alg );
2236 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002237 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2238 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002239
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002240 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002241 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2242 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002243 PSA_ERROR_BAD_STATE );
2244 PSA_ASSERT( psa_cipher_abort( &operation ) );
2245
2246 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002247 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2248 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002249 PSA_ERROR_BAD_STATE );
2250 PSA_ASSERT( psa_cipher_abort( &operation ) );
2251
Jaeden Ameroab439972019-02-15 14:12:05 +00002252 /* Generate an IV without calling setup beforehand. */
2253 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2254 buffer, sizeof( buffer ),
2255 &length ),
2256 PSA_ERROR_BAD_STATE );
2257 PSA_ASSERT( psa_cipher_abort( &operation ) );
2258
2259 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002260 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002261 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2262 buffer, sizeof( buffer ),
2263 &length ) );
2264 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2265 buffer, sizeof( buffer ),
2266 &length ),
2267 PSA_ERROR_BAD_STATE );
2268 PSA_ASSERT( psa_cipher_abort( &operation ) );
2269
2270 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002271 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002272 PSA_ASSERT( psa_cipher_set_iv( &operation,
2273 iv, sizeof( iv ) ) );
2274 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2275 buffer, sizeof( buffer ),
2276 &length ),
2277 PSA_ERROR_BAD_STATE );
2278 PSA_ASSERT( psa_cipher_abort( &operation ) );
2279
2280 /* Set an IV without calling setup beforehand. */
2281 TEST_EQUAL( psa_cipher_set_iv( &operation,
2282 iv, sizeof( iv ) ),
2283 PSA_ERROR_BAD_STATE );
2284 PSA_ASSERT( psa_cipher_abort( &operation ) );
2285
2286 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002287 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002288 PSA_ASSERT( psa_cipher_set_iv( &operation,
2289 iv, sizeof( iv ) ) );
2290 TEST_EQUAL( psa_cipher_set_iv( &operation,
2291 iv, sizeof( iv ) ),
2292 PSA_ERROR_BAD_STATE );
2293 PSA_ASSERT( psa_cipher_abort( &operation ) );
2294
2295 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002296 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002297 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2298 buffer, sizeof( buffer ),
2299 &length ) );
2300 TEST_EQUAL( psa_cipher_set_iv( &operation,
2301 iv, sizeof( iv ) ),
2302 PSA_ERROR_BAD_STATE );
2303 PSA_ASSERT( psa_cipher_abort( &operation ) );
2304
2305 /* Call update without calling setup beforehand. */
2306 TEST_EQUAL( psa_cipher_update( &operation,
2307 text, sizeof( text ),
2308 buffer, sizeof( buffer ),
2309 &length ),
2310 PSA_ERROR_BAD_STATE );
2311 PSA_ASSERT( psa_cipher_abort( &operation ) );
2312
2313 /* Call update without an IV where an IV is required. */
2314 TEST_EQUAL( psa_cipher_update( &operation,
2315 text, sizeof( text ),
2316 buffer, sizeof( buffer ),
2317 &length ),
2318 PSA_ERROR_BAD_STATE );
2319 PSA_ASSERT( psa_cipher_abort( &operation ) );
2320
2321 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002322 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002323 PSA_ASSERT( psa_cipher_set_iv( &operation,
2324 iv, sizeof( iv ) ) );
2325 PSA_ASSERT( psa_cipher_finish( &operation,
2326 buffer, sizeof( buffer ), &length ) );
2327 TEST_EQUAL( psa_cipher_update( &operation,
2328 text, sizeof( text ),
2329 buffer, sizeof( buffer ),
2330 &length ),
2331 PSA_ERROR_BAD_STATE );
2332 PSA_ASSERT( psa_cipher_abort( &operation ) );
2333
2334 /* Call finish without calling setup beforehand. */
2335 TEST_EQUAL( psa_cipher_finish( &operation,
2336 buffer, sizeof( buffer ), &length ),
2337 PSA_ERROR_BAD_STATE );
2338 PSA_ASSERT( psa_cipher_abort( &operation ) );
2339
2340 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002341 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002342 /* Not calling update means we are encrypting an empty buffer, which is OK
2343 * for cipher modes with padding. */
2344 TEST_EQUAL( psa_cipher_finish( &operation,
2345 buffer, sizeof( buffer ), &length ),
2346 PSA_ERROR_BAD_STATE );
2347 PSA_ASSERT( psa_cipher_abort( &operation ) );
2348
2349 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002350 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002351 PSA_ASSERT( psa_cipher_set_iv( &operation,
2352 iv, sizeof( iv ) ) );
2353 PSA_ASSERT( psa_cipher_finish( &operation,
2354 buffer, sizeof( buffer ), &length ) );
2355 TEST_EQUAL( psa_cipher_finish( &operation,
2356 buffer, sizeof( buffer ), &length ),
2357 PSA_ERROR_BAD_STATE );
2358 PSA_ASSERT( psa_cipher_abort( &operation ) );
2359
Ronald Cron5425a212020-08-04 14:58:35 +02002360 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002361
Jaeden Ameroab439972019-02-15 14:12:05 +00002362exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002363 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002364 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002365}
2366/* END_CASE */
2367
2368/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002369void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002370 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002371 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002372 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002373{
Ronald Cron5425a212020-08-04 14:58:35 +02002374 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002375 psa_status_t status;
2376 psa_key_type_t key_type = key_type_arg;
2377 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002378 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002379 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002380 size_t output_buffer_size = 0;
2381 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002382 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002383 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002384 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002385
Gilles Peskine8817f612018-12-18 00:18:46 +01002386 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002387
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002388 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2389 psa_set_key_algorithm( &attributes, alg );
2390 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002391
Ronald Cron5425a212020-08-04 14:58:35 +02002392 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2393 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002394
Ronald Cron5425a212020-08-04 14:58:35 +02002395 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002396
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002397 if( iv->len > 0 )
2398 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002399 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002400 }
2401
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002402 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002403 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002404 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002405
Gilles Peskine8817f612018-12-18 00:18:46 +01002406 PSA_ASSERT( psa_cipher_update( &operation,
2407 input->x, input->len,
2408 output, output_buffer_size,
2409 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002410 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002411 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002412 output + total_output_length,
2413 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002414 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002415 total_output_length += function_output_length;
2416
Gilles Peskinefe11b722018-12-18 00:24:04 +01002417 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002418 if( expected_status == PSA_SUCCESS )
2419 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002420 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002421 ASSERT_COMPARE( expected_output->x, expected_output->len,
2422 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002423 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002424
Gilles Peskine50e586b2018-06-08 14:28:46 +02002425exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002426 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002427 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002428 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002429 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002430}
2431/* END_CASE */
2432
2433/* BEGIN_CASE */
2434void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002435 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002436 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002437 int first_part_size_arg,
2438 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002439 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002440{
Ronald Cron5425a212020-08-04 14:58:35 +02002441 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002442 psa_key_type_t key_type = key_type_arg;
2443 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002444 size_t first_part_size = first_part_size_arg;
2445 size_t output1_length = output1_length_arg;
2446 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002447 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002448 size_t output_buffer_size = 0;
2449 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002450 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002451 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002452 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002453
Gilles Peskine8817f612018-12-18 00:18:46 +01002454 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002455
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002456 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2457 psa_set_key_algorithm( &attributes, alg );
2458 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002459
Ronald Cron5425a212020-08-04 14:58:35 +02002460 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2461 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002462
Ronald Cron5425a212020-08-04 14:58:35 +02002463 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002464
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002465 if( iv->len > 0 )
2466 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002467 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002468 }
2469
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002470 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002471 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002472 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002473
Gilles Peskinee0866522019-02-19 19:44:00 +01002474 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002475 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2476 output, output_buffer_size,
2477 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002478 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002479 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002480 PSA_ASSERT( psa_cipher_update( &operation,
2481 input->x + first_part_size,
2482 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002483 output + total_output_length,
2484 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002485 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002486 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002487 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002488 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002489 output + total_output_length,
2490 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002491 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002492 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002493 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002494
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002495 ASSERT_COMPARE( expected_output->x, expected_output->len,
2496 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002497
2498exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002499 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002500 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002501 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002502 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002503}
2504/* END_CASE */
2505
2506/* BEGIN_CASE */
2507void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002508 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002509 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002510 int first_part_size_arg,
2511 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002512 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002513{
Ronald Cron5425a212020-08-04 14:58:35 +02002514 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002515 psa_key_type_t key_type = key_type_arg;
2516 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002517 size_t first_part_size = first_part_size_arg;
2518 size_t output1_length = output1_length_arg;
2519 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002520 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002521 size_t output_buffer_size = 0;
2522 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002523 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002524 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002525 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002526
Gilles Peskine8817f612018-12-18 00:18:46 +01002527 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002528
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002529 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2530 psa_set_key_algorithm( &attributes, alg );
2531 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002532
Ronald Cron5425a212020-08-04 14:58:35 +02002533 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2534 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002535
Ronald Cron5425a212020-08-04 14:58:35 +02002536 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002537
Steven Cooreman177deba2020-09-07 17:14:14 +02002538 if( iv->len > 0 )
2539 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002540 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002541 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002542
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002543 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002544 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002545 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002546
Gilles Peskinee0866522019-02-19 19:44:00 +01002547 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002548 PSA_ASSERT( psa_cipher_update( &operation,
2549 input->x, first_part_size,
2550 output, output_buffer_size,
2551 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002552 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002553 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002554 PSA_ASSERT( psa_cipher_update( &operation,
2555 input->x + first_part_size,
2556 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002557 output + total_output_length,
2558 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002559 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002560 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002561 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002562 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002563 output + total_output_length,
2564 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002565 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002566 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002567 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002568
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002569 ASSERT_COMPARE( expected_output->x, expected_output->len,
2570 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002571
2572exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002573 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002574 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002575 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002576 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002577}
2578/* END_CASE */
2579
Gilles Peskine50e586b2018-06-08 14:28:46 +02002580/* BEGIN_CASE */
2581void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002582 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002583 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002584 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002585{
Ronald Cron5425a212020-08-04 14:58:35 +02002586 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002587 psa_status_t status;
2588 psa_key_type_t key_type = key_type_arg;
2589 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002590 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002591 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002592 size_t output_buffer_size = 0;
2593 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002594 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002595 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002596 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002597
Gilles Peskine8817f612018-12-18 00:18:46 +01002598 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002599
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002600 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2601 psa_set_key_algorithm( &attributes, alg );
2602 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002603
Ronald Cron5425a212020-08-04 14:58:35 +02002604 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2605 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002606
Ronald Cron5425a212020-08-04 14:58:35 +02002607 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002608
Steven Cooreman177deba2020-09-07 17:14:14 +02002609 if( iv->len > 0 )
2610 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002611 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002612 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002613
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002614 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002615 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002616 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002617
Gilles Peskine8817f612018-12-18 00:18:46 +01002618 PSA_ASSERT( psa_cipher_update( &operation,
2619 input->x, input->len,
2620 output, output_buffer_size,
2621 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002622 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002623 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002624 output + total_output_length,
2625 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002626 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002627 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002628 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002629
2630 if( expected_status == PSA_SUCCESS )
2631 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002632 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002633 ASSERT_COMPARE( expected_output->x, expected_output->len,
2634 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002635 }
2636
Gilles Peskine50e586b2018-06-08 14:28:46 +02002637exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002638 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002639 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002640 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002641 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002642}
2643/* END_CASE */
2644
Gilles Peskine50e586b2018-06-08 14:28:46 +02002645/* BEGIN_CASE */
2646void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002647 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002648 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002649{
Ronald Cron5425a212020-08-04 14:58:35 +02002650 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002651 psa_key_type_t key_type = key_type_arg;
2652 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002653 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002654 size_t iv_size = 16;
2655 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002656 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002657 size_t output1_size = 0;
2658 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002659 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002660 size_t output2_size = 0;
2661 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002662 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002663 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2664 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002665 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002666
Gilles Peskine8817f612018-12-18 00:18:46 +01002667 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002668
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002669 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2670 psa_set_key_algorithm( &attributes, alg );
2671 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002672
Ronald Cron5425a212020-08-04 14:58:35 +02002673 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2674 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002675
Ronald Cron5425a212020-08-04 14:58:35 +02002676 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2677 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002678
Steven Cooreman177deba2020-09-07 17:14:14 +02002679 if( alg != PSA_ALG_ECB_NO_PADDING )
2680 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002681 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2682 iv, iv_size,
2683 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002684 }
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002685 output1_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002686 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002687 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002688
Gilles Peskine8817f612018-12-18 00:18:46 +01002689 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2690 output1, output1_size,
2691 &output1_length ) );
2692 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002693 output1 + output1_length,
2694 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002695 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002696
Gilles Peskine048b7f02018-06-08 14:20:49 +02002697 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002698
Gilles Peskine8817f612018-12-18 00:18:46 +01002699 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002700
2701 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002702 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002703
Steven Cooreman177deba2020-09-07 17:14:14 +02002704 if( iv_length > 0 )
2705 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002706 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2707 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002708 }
2709
Gilles Peskine8817f612018-12-18 00:18:46 +01002710 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2711 output2, output2_size,
2712 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002713 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002714 PSA_ASSERT( psa_cipher_finish( &operation2,
2715 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002716 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002717 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002718
Gilles Peskine048b7f02018-06-08 14:20:49 +02002719 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002720
Gilles Peskine8817f612018-12-18 00:18:46 +01002721 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002722
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002723 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002724
2725exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002726 psa_cipher_abort( &operation1 );
2727 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002728 mbedtls_free( output1 );
2729 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002730 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002731 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03002732}
2733/* END_CASE */
2734
2735/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002736void cipher_verify_output_multipart( int alg_arg,
2737 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002738 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002739 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002740 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03002741{
Ronald Cron5425a212020-08-04 14:58:35 +02002742 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002743 psa_key_type_t key_type = key_type_arg;
2744 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002745 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002746 unsigned char iv[16] = {0};
2747 size_t iv_size = 16;
2748 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002749 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002750 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002751 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002752 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002753 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002754 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002755 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002756 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2757 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002758 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002759
Gilles Peskine8817f612018-12-18 00:18:46 +01002760 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002761
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002762 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2763 psa_set_key_algorithm( &attributes, alg );
2764 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002765
Ronald Cron5425a212020-08-04 14:58:35 +02002766 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2767 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03002768
Ronald Cron5425a212020-08-04 14:58:35 +02002769 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2770 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002771
Steven Cooreman177deba2020-09-07 17:14:14 +02002772 if( alg != PSA_ALG_ECB_NO_PADDING )
2773 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002774 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2775 iv, iv_size,
2776 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002777 }
2778
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002779 output1_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002780 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002781 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002782
Gilles Peskinee0866522019-02-19 19:44:00 +01002783 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002784
Gilles Peskine8817f612018-12-18 00:18:46 +01002785 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2786 output1, output1_buffer_size,
2787 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002788 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002789
Gilles Peskine8817f612018-12-18 00:18:46 +01002790 PSA_ASSERT( psa_cipher_update( &operation1,
2791 input->x + first_part_size,
2792 input->len - first_part_size,
2793 output1, output1_buffer_size,
2794 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002795 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002796
Gilles Peskine8817f612018-12-18 00:18:46 +01002797 PSA_ASSERT( psa_cipher_finish( &operation1,
2798 output1 + output1_length,
2799 output1_buffer_size - output1_length,
2800 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002801 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002802
Gilles Peskine8817f612018-12-18 00:18:46 +01002803 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002804
Gilles Peskine048b7f02018-06-08 14:20:49 +02002805 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002806 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002807
Steven Cooreman177deba2020-09-07 17:14:14 +02002808 if( iv_length > 0 )
2809 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002810 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2811 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002812 }
Moran Pekerded84402018-06-06 16:36:50 +03002813
Gilles Peskine8817f612018-12-18 00:18:46 +01002814 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2815 output2, output2_buffer_size,
2816 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002817 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002818
Gilles Peskine8817f612018-12-18 00:18:46 +01002819 PSA_ASSERT( psa_cipher_update( &operation2,
2820 output1 + first_part_size,
2821 output1_length - first_part_size,
2822 output2, output2_buffer_size,
2823 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002824 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002825
Gilles Peskine8817f612018-12-18 00:18:46 +01002826 PSA_ASSERT( psa_cipher_finish( &operation2,
2827 output2 + output2_length,
2828 output2_buffer_size - output2_length,
2829 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002830 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002831
Gilles Peskine8817f612018-12-18 00:18:46 +01002832 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002833
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002834 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002835
2836exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002837 psa_cipher_abort( &operation1 );
2838 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002839 mbedtls_free( output1 );
2840 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002841 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002842 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002843}
2844/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002845
Gilles Peskine20035e32018-02-03 22:44:14 +01002846/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002847void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002848 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002849 data_t *nonce,
2850 data_t *additional_data,
2851 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002852 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002853{
Ronald Cron5425a212020-08-04 14:58:35 +02002854 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002855 psa_key_type_t key_type = key_type_arg;
2856 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002857 unsigned char *output_data = NULL;
2858 size_t output_size = 0;
2859 size_t output_length = 0;
2860 unsigned char *output_data2 = NULL;
2861 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02002862 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002863 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002864 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002865
Gilles Peskine4abf7412018-06-18 16:35:34 +02002866 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02002867 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
2868 * should be exact. */
2869 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
2870 TEST_EQUAL( output_size,
2871 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002872 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002873
Gilles Peskine8817f612018-12-18 00:18:46 +01002874 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002875
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002876 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2877 psa_set_key_algorithm( &attributes, alg );
2878 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002879
Gilles Peskine049c7532019-05-15 20:22:09 +02002880 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002881 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002882
Ronald Cron5425a212020-08-04 14:58:35 +02002883 TEST_EQUAL( psa_aead_encrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01002884 nonce->x, nonce->len,
2885 additional_data->x,
2886 additional_data->len,
2887 input_data->x, input_data->len,
2888 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002889 &output_length ),
2890 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002891
2892 if( PSA_SUCCESS == expected_result )
2893 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002894 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002895
Gilles Peskine003a4a92019-05-14 16:09:40 +02002896 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
2897 * should be exact. */
2898 TEST_EQUAL( input_data->len,
2899 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
2900
Ronald Cron5425a212020-08-04 14:58:35 +02002901 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01002902 nonce->x, nonce->len,
2903 additional_data->x,
2904 additional_data->len,
2905 output_data, output_length,
2906 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002907 &output_length2 ),
2908 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002909
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002910 ASSERT_COMPARE( input_data->x, input_data->len,
2911 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002912 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002913
Gilles Peskinea1cac842018-06-11 19:33:02 +02002914exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002915 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002916 mbedtls_free( output_data );
2917 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002918 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002919}
2920/* END_CASE */
2921
2922/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002923void aead_encrypt( int key_type_arg, data_t *key_data,
2924 int alg_arg,
2925 data_t *nonce,
2926 data_t *additional_data,
2927 data_t *input_data,
2928 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002929{
Ronald Cron5425a212020-08-04 14:58:35 +02002930 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002931 psa_key_type_t key_type = key_type_arg;
2932 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002933 unsigned char *output_data = NULL;
2934 size_t output_size = 0;
2935 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02002936 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002937 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01002938 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002939
Gilles Peskine4abf7412018-06-18 16:35:34 +02002940 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02002941 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
2942 * should be exact. */
2943 TEST_EQUAL( output_size,
2944 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002945 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002946
Gilles Peskine8817f612018-12-18 00:18:46 +01002947 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002948
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002949 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2950 psa_set_key_algorithm( &attributes, alg );
2951 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002952
Gilles Peskine049c7532019-05-15 20:22:09 +02002953 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002954 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002955
Steven Cooremand588ea12021-01-11 19:36:04 +01002956 status = psa_aead_encrypt( key, alg,
2957 nonce->x, nonce->len,
2958 additional_data->x, additional_data->len,
2959 input_data->x, input_data->len,
2960 output_data, output_size,
2961 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002962
Ronald Cron28a45ed2021-02-09 20:35:42 +01002963 /* If the operation is not supported, just skip and not fail in case the
2964 * encryption involves a common limitation of cryptography hardwares and
2965 * an alternative implementation. */
2966 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01002967 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01002968 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
2969 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01002970 }
Steven Cooremand588ea12021-01-11 19:36:04 +01002971
2972 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002973 ASSERT_COMPARE( expected_result->x, expected_result->len,
2974 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002975
Gilles Peskinea1cac842018-06-11 19:33:02 +02002976exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002977 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002978 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002979 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002980}
2981/* END_CASE */
2982
2983/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002984void aead_decrypt( int key_type_arg, data_t *key_data,
2985 int alg_arg,
2986 data_t *nonce,
2987 data_t *additional_data,
2988 data_t *input_data,
2989 data_t *expected_data,
2990 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002991{
Ronald Cron5425a212020-08-04 14:58:35 +02002992 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002993 psa_key_type_t key_type = key_type_arg;
2994 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002995 unsigned char *output_data = NULL;
2996 size_t output_size = 0;
2997 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02002998 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002999 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003000 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003001 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003002
Gilles Peskine003a4a92019-05-14 16:09:40 +02003003 output_size = input_data->len - tag_length;
3004 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3005 * should be exact. */
3006 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3007 TEST_EQUAL( output_size,
3008 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003009 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003010
Gilles Peskine8817f612018-12-18 00:18:46 +01003011 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003012
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003013 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3014 psa_set_key_algorithm( &attributes, alg );
3015 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003016
Gilles Peskine049c7532019-05-15 20:22:09 +02003017 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003018 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003019
Steven Cooremand588ea12021-01-11 19:36:04 +01003020 status = psa_aead_decrypt( key, alg,
3021 nonce->x, nonce->len,
3022 additional_data->x,
3023 additional_data->len,
3024 input_data->x, input_data->len,
3025 output_data, output_size,
3026 &output_length );
3027
Ronald Cron28a45ed2021-02-09 20:35:42 +01003028 /* If the operation is not supported, just skip and not fail in case the
3029 * decryption involves a common limitation of cryptography hardwares and
3030 * an alternative implementation. */
3031 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003032 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003033 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3034 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003035 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003036
3037 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003038
Gilles Peskine2d277862018-06-18 15:41:12 +02003039 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003040 ASSERT_COMPARE( expected_data->x, expected_data->len,
3041 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003042
Gilles Peskinea1cac842018-06-11 19:33:02 +02003043exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003044 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003045 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003046 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003047}
3048/* END_CASE */
3049
3050/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003051void signature_size( int type_arg,
3052 int bits,
3053 int alg_arg,
3054 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003055{
3056 psa_key_type_t type = type_arg;
3057 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003058 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003059
Gilles Peskinefe11b722018-12-18 00:24:04 +01003060 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003061#if defined(MBEDTLS_TEST_DEPRECATED)
3062 TEST_EQUAL( actual_size,
3063 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3064#endif /* MBEDTLS_TEST_DEPRECATED */
3065
Gilles Peskinee59236f2018-01-27 23:32:46 +01003066exit:
3067 ;
3068}
3069/* END_CASE */
3070
3071/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003072void sign_deterministic( int key_type_arg, data_t *key_data,
3073 int alg_arg, data_t *input_data,
3074 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003075{
Ronald Cron5425a212020-08-04 14:58:35 +02003076 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003077 psa_key_type_t key_type = key_type_arg;
3078 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003079 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003080 unsigned char *signature = NULL;
3081 size_t signature_size;
3082 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003083 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003084
Gilles Peskine8817f612018-12-18 00:18:46 +01003085 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003086
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003087 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003088 psa_set_key_algorithm( &attributes, alg );
3089 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003090
Gilles Peskine049c7532019-05-15 20:22:09 +02003091 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003092 &key ) );
3093 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003094 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003095
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003096 /* Allocate a buffer which has the size advertized by the
3097 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003098 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003099 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003100 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003101 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003102 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003103
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003104 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003105 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003106 input_data->x, input_data->len,
3107 signature, signature_size,
3108 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003109 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003110 ASSERT_COMPARE( output_data->x, output_data->len,
3111 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003112
Gilles Peskine0627f982019-11-26 19:12:16 +01003113#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003114 memset( signature, 0, signature_size );
3115 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003116 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003117 input_data->x, input_data->len,
3118 signature, signature_size,
3119 &signature_length ) );
3120 ASSERT_COMPARE( output_data->x, output_data->len,
3121 signature, signature_length );
3122#endif /* MBEDTLS_TEST_DEPRECATED */
3123
Gilles Peskine20035e32018-02-03 22:44:14 +01003124exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003125 /*
3126 * Key attributes may have been returned by psa_get_key_attributes()
3127 * thus reset them as required.
3128 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003129 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003130
Ronald Cron5425a212020-08-04 14:58:35 +02003131 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003132 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003133 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003134}
3135/* END_CASE */
3136
3137/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003138void sign_fail( int key_type_arg, data_t *key_data,
3139 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003140 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003141{
Ronald Cron5425a212020-08-04 14:58:35 +02003142 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003143 psa_key_type_t key_type = key_type_arg;
3144 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003145 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003146 psa_status_t actual_status;
3147 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003148 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003149 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003150 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003151
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003152 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003153
Gilles Peskine8817f612018-12-18 00:18:46 +01003154 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003155
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003156 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003157 psa_set_key_algorithm( &attributes, alg );
3158 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003159
Gilles Peskine049c7532019-05-15 20:22:09 +02003160 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003161 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003162
Ronald Cron5425a212020-08-04 14:58:35 +02003163 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003164 input_data->x, input_data->len,
3165 signature, signature_size,
3166 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003167 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003168 /* The value of *signature_length is unspecified on error, but
3169 * whatever it is, it should be less than signature_size, so that
3170 * if the caller tries to read *signature_length bytes without
3171 * checking the error code then they don't overflow a buffer. */
3172 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003173
Gilles Peskine895242b2019-11-29 12:15:40 +01003174#if defined(MBEDTLS_TEST_DEPRECATED)
3175 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003176 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003177 input_data->x, input_data->len,
3178 signature, signature_size,
3179 &signature_length ),
3180 expected_status );
3181 TEST_ASSERT( signature_length <= signature_size );
3182#endif /* MBEDTLS_TEST_DEPRECATED */
3183
Gilles Peskine20035e32018-02-03 22:44:14 +01003184exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003185 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003186 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003187 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003188 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003189}
3190/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003191
3192/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003193void sign_verify( int key_type_arg, data_t *key_data,
3194 int alg_arg, data_t *input_data )
3195{
Ronald Cron5425a212020-08-04 14:58:35 +02003196 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003197 psa_key_type_t key_type = key_type_arg;
3198 psa_algorithm_t alg = alg_arg;
3199 size_t key_bits;
3200 unsigned char *signature = NULL;
3201 size_t signature_size;
3202 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003203 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003204
Gilles Peskine8817f612018-12-18 00:18:46 +01003205 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003206
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003207 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003208 psa_set_key_algorithm( &attributes, alg );
3209 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003210
Gilles Peskine049c7532019-05-15 20:22:09 +02003211 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003212 &key ) );
3213 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003214 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003215
3216 /* Allocate a buffer which has the size advertized by the
3217 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003218 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003219 key_bits, alg );
3220 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003221 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003222 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003223
3224 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003225 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003226 input_data->x, input_data->len,
3227 signature, signature_size,
3228 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003229 /* Check that the signature length looks sensible. */
3230 TEST_ASSERT( signature_length <= signature_size );
3231 TEST_ASSERT( signature_length > 0 );
3232
3233 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003234 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003235 input_data->x, input_data->len,
3236 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003237
3238 if( input_data->len != 0 )
3239 {
3240 /* Flip a bit in the input and verify that the signature is now
3241 * detected as invalid. Flip a bit at the beginning, not at the end,
3242 * because ECDSA may ignore the last few bits of the input. */
3243 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003244 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003245 input_data->x, input_data->len,
3246 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003247 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003248 }
3249
3250exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003251 /*
3252 * Key attributes may have been returned by psa_get_key_attributes()
3253 * thus reset them as required.
3254 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003255 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003256
Ronald Cron5425a212020-08-04 14:58:35 +02003257 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003258 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003259 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003260}
3261/* END_CASE */
3262
3263/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003264void asymmetric_verify( int key_type_arg, data_t *key_data,
3265 int alg_arg, data_t *hash_data,
3266 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003267{
Ronald Cron5425a212020-08-04 14:58:35 +02003268 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003269 psa_key_type_t key_type = key_type_arg;
3270 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003271 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003272
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003273 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003274
Gilles Peskine8817f612018-12-18 00:18:46 +01003275 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003276
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003277 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003278 psa_set_key_algorithm( &attributes, alg );
3279 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003280
Gilles Peskine049c7532019-05-15 20:22:09 +02003281 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003282 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003283
Ronald Cron5425a212020-08-04 14:58:35 +02003284 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003285 hash_data->x, hash_data->len,
3286 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003287
3288#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003289 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003290 hash_data->x, hash_data->len,
3291 signature_data->x,
3292 signature_data->len ) );
3293
3294#endif /* MBEDTLS_TEST_DEPRECATED */
3295
itayzafrir5c753392018-05-08 11:18:38 +03003296exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003297 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003298 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003299 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003300}
3301/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003302
3303/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003304void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3305 int alg_arg, data_t *hash_data,
3306 data_t *signature_data,
3307 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003308{
Ronald Cron5425a212020-08-04 14:58:35 +02003309 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003310 psa_key_type_t key_type = key_type_arg;
3311 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003312 psa_status_t actual_status;
3313 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003314 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003315
Gilles Peskine8817f612018-12-18 00:18:46 +01003316 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003317
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003318 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003319 psa_set_key_algorithm( &attributes, alg );
3320 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003321
Gilles Peskine049c7532019-05-15 20:22:09 +02003322 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003323 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003324
Ronald Cron5425a212020-08-04 14:58:35 +02003325 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003326 hash_data->x, hash_data->len,
3327 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003328 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003329
Gilles Peskine895242b2019-11-29 12:15:40 +01003330#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003331 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003332 hash_data->x, hash_data->len,
3333 signature_data->x, signature_data->len ),
3334 expected_status );
3335#endif /* MBEDTLS_TEST_DEPRECATED */
3336
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003337exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003338 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003339 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003340 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003341}
3342/* END_CASE */
3343
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003344/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003345void asymmetric_encrypt( int key_type_arg,
3346 data_t *key_data,
3347 int alg_arg,
3348 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003349 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003350 int expected_output_length_arg,
3351 int expected_status_arg )
3352{
Ronald Cron5425a212020-08-04 14:58:35 +02003353 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003354 psa_key_type_t key_type = key_type_arg;
3355 psa_algorithm_t alg = alg_arg;
3356 size_t expected_output_length = expected_output_length_arg;
3357 size_t key_bits;
3358 unsigned char *output = NULL;
3359 size_t output_size;
3360 size_t output_length = ~0;
3361 psa_status_t actual_status;
3362 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003363 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003364
Gilles Peskine8817f612018-12-18 00:18:46 +01003365 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003366
Gilles Peskine656896e2018-06-29 19:12:28 +02003367 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003368 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3369 psa_set_key_algorithm( &attributes, alg );
3370 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003371 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003372 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003373
3374 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003375 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003376 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02003377 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003378 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003379
3380 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003381 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003382 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003383 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003384 output, output_size,
3385 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003386 TEST_EQUAL( actual_status, expected_status );
3387 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003388
Gilles Peskine68428122018-06-30 18:42:41 +02003389 /* If the label is empty, the test framework puts a non-null pointer
3390 * in label->x. Test that a null pointer works as well. */
3391 if( label->len == 0 )
3392 {
3393 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003394 if( output_size != 0 )
3395 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003396 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003397 input_data->x, input_data->len,
3398 NULL, label->len,
3399 output, output_size,
3400 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003401 TEST_EQUAL( actual_status, expected_status );
3402 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003403 }
3404
Gilles Peskine656896e2018-06-29 19:12:28 +02003405exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003406 /*
3407 * Key attributes may have been returned by psa_get_key_attributes()
3408 * thus reset them as required.
3409 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003410 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003411
Ronald Cron5425a212020-08-04 14:58:35 +02003412 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02003413 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003414 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003415}
3416/* END_CASE */
3417
3418/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003419void asymmetric_encrypt_decrypt( int key_type_arg,
3420 data_t *key_data,
3421 int alg_arg,
3422 data_t *input_data,
3423 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003424{
Ronald Cron5425a212020-08-04 14:58:35 +02003425 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003426 psa_key_type_t key_type = key_type_arg;
3427 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003428 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003429 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003430 size_t output_size;
3431 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003432 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003433 size_t output2_size;
3434 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003435 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003436
Gilles Peskine8817f612018-12-18 00:18:46 +01003437 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003438
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003439 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3440 psa_set_key_algorithm( &attributes, alg );
3441 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003442
Gilles Peskine049c7532019-05-15 20:22:09 +02003443 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003444 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003445
3446 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02003447 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003448 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003449 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003450 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003451 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003452 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003453
Gilles Peskineeebd7382018-06-08 18:11:54 +02003454 /* We test encryption by checking that encrypt-then-decrypt gives back
3455 * the original plaintext because of the non-optional random
3456 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02003457 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003458 input_data->x, input_data->len,
3459 label->x, label->len,
3460 output, output_size,
3461 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003462 /* We don't know what ciphertext length to expect, but check that
3463 * it looks sensible. */
3464 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003465
Ronald Cron5425a212020-08-04 14:58:35 +02003466 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003467 output, output_length,
3468 label->x, label->len,
3469 output2, output2_size,
3470 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003471 ASSERT_COMPARE( input_data->x, input_data->len,
3472 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003473
3474exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003475 /*
3476 * Key attributes may have been returned by psa_get_key_attributes()
3477 * thus reset them as required.
3478 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003479 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003480
Ronald Cron5425a212020-08-04 14:58:35 +02003481 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003482 mbedtls_free( output );
3483 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003484 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003485}
3486/* END_CASE */
3487
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003488/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003489void asymmetric_decrypt( int key_type_arg,
3490 data_t *key_data,
3491 int alg_arg,
3492 data_t *input_data,
3493 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003494 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003495{
Ronald Cron5425a212020-08-04 14:58:35 +02003496 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003497 psa_key_type_t key_type = key_type_arg;
3498 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003499 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003500 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003501 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003502 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003503
Jaeden Amero412654a2019-02-06 12:57:46 +00003504 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003505 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003506
Gilles Peskine8817f612018-12-18 00:18:46 +01003507 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003508
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003509 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3510 psa_set_key_algorithm( &attributes, alg );
3511 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003512
Gilles Peskine049c7532019-05-15 20:22:09 +02003513 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003514 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003515
Ronald Cron5425a212020-08-04 14:58:35 +02003516 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003517 input_data->x, input_data->len,
3518 label->x, label->len,
3519 output,
3520 output_size,
3521 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003522 ASSERT_COMPARE( expected_data->x, expected_data->len,
3523 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003524
Gilles Peskine68428122018-06-30 18:42:41 +02003525 /* If the label is empty, the test framework puts a non-null pointer
3526 * in label->x. Test that a null pointer works as well. */
3527 if( label->len == 0 )
3528 {
3529 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003530 if( output_size != 0 )
3531 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003532 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003533 input_data->x, input_data->len,
3534 NULL, label->len,
3535 output,
3536 output_size,
3537 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003538 ASSERT_COMPARE( expected_data->x, expected_data->len,
3539 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003540 }
3541
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003542exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003543 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003544 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003545 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003546 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003547}
3548/* END_CASE */
3549
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003550/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003551void asymmetric_decrypt_fail( int key_type_arg,
3552 data_t *key_data,
3553 int alg_arg,
3554 data_t *input_data,
3555 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00003556 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02003557 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003558{
Ronald Cron5425a212020-08-04 14:58:35 +02003559 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003560 psa_key_type_t key_type = key_type_arg;
3561 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003562 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00003563 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003564 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003565 psa_status_t actual_status;
3566 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003567 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003568
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003569 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003570
Gilles Peskine8817f612018-12-18 00:18:46 +01003571 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003572
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003573 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3574 psa_set_key_algorithm( &attributes, alg );
3575 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003576
Gilles Peskine049c7532019-05-15 20:22:09 +02003577 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003578 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003579
Ronald Cron5425a212020-08-04 14:58:35 +02003580 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003581 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003582 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003583 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003584 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003585 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003586 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003587
Gilles Peskine68428122018-06-30 18:42:41 +02003588 /* If the label is empty, the test framework puts a non-null pointer
3589 * in label->x. Test that a null pointer works as well. */
3590 if( label->len == 0 )
3591 {
3592 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003593 if( output_size != 0 )
3594 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003595 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003596 input_data->x, input_data->len,
3597 NULL, label->len,
3598 output, output_size,
3599 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003600 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003601 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003602 }
3603
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003604exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003605 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003606 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02003607 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003608 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003609}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003610/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003611
3612/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003613void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00003614{
3615 /* Test each valid way of initializing the object, except for `= {0}`, as
3616 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3617 * though it's OK by the C standard. We could test for this, but we'd need
3618 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003619 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003620 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
3621 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
3622 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00003623
3624 memset( &zero, 0, sizeof( zero ) );
3625
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003626 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003627 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003628 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003629 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003630 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003631 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003632 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003633
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003634 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003635 PSA_ASSERT( psa_key_derivation_abort(&func) );
3636 PSA_ASSERT( psa_key_derivation_abort(&init) );
3637 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00003638}
3639/* END_CASE */
3640
Janos Follath16de4a42019-06-13 16:32:24 +01003641/* BEGIN_CASE */
3642void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02003643{
Gilles Peskineea0fb492018-07-12 17:17:20 +02003644 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003645 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003646 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003647
Gilles Peskine8817f612018-12-18 00:18:46 +01003648 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003649
Janos Follath16de4a42019-06-13 16:32:24 +01003650 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003651 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003652
3653exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003654 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003655 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003656}
3657/* END_CASE */
3658
Janos Follathaf3c2a02019-06-12 12:34:34 +01003659/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01003660void derive_set_capacity( int alg_arg, int capacity_arg,
3661 int expected_status_arg )
3662{
3663 psa_algorithm_t alg = alg_arg;
3664 size_t capacity = capacity_arg;
3665 psa_status_t expected_status = expected_status_arg;
3666 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
3667
3668 PSA_ASSERT( psa_crypto_init( ) );
3669
3670 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
3671
3672 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
3673 expected_status );
3674
3675exit:
3676 psa_key_derivation_abort( &operation );
3677 PSA_DONE( );
3678}
3679/* END_CASE */
3680
3681/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01003682void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02003683 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01003684 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02003685 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01003686 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02003687 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003688 int expected_status_arg3,
3689 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01003690{
3691 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02003692 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
3693 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01003694 psa_status_t expected_statuses[] = {expected_status_arg1,
3695 expected_status_arg2,
3696 expected_status_arg3};
3697 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02003698 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
3699 MBEDTLS_SVC_KEY_ID_INIT,
3700 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01003701 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
3702 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3703 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003704 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02003705 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003706 psa_status_t expected_output_status = expected_output_status_arg;
3707 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01003708
3709 PSA_ASSERT( psa_crypto_init( ) );
3710
3711 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
3712 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01003713
3714 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
3715
3716 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
3717 {
Gilles Peskineb8965192019-09-24 16:21:10 +02003718 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01003719 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02003720 psa_set_key_type( &attributes, key_types[i] );
3721 PSA_ASSERT( psa_import_key( &attributes,
3722 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003723 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02003724 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
3725 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
3726 {
3727 // When taking a private key as secret input, use key agreement
3728 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01003729 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
3730 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02003731 expected_statuses[i] );
3732 }
3733 else
3734 {
3735 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02003736 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02003737 expected_statuses[i] );
3738 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02003739 }
3740 else
3741 {
3742 TEST_EQUAL( psa_key_derivation_input_bytes(
3743 &operation, steps[i],
3744 inputs[i]->x, inputs[i]->len ),
3745 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01003746 }
3747 }
3748
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003749 if( output_key_type != PSA_KEY_TYPE_NONE )
3750 {
3751 psa_reset_key_attributes( &attributes );
3752 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
3753 psa_set_key_bits( &attributes, 8 );
3754 actual_output_status =
3755 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02003756 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003757 }
3758 else
3759 {
3760 uint8_t buffer[1];
3761 actual_output_status =
3762 psa_key_derivation_output_bytes( &operation,
3763 buffer, sizeof( buffer ) );
3764 }
3765 TEST_EQUAL( actual_output_status, expected_output_status );
3766
Janos Follathaf3c2a02019-06-12 12:34:34 +01003767exit:
3768 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003769 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
3770 psa_destroy_key( keys[i] );
3771 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01003772 PSA_DONE( );
3773}
3774/* END_CASE */
3775
Janos Follathd958bb72019-07-03 15:02:16 +01003776/* BEGIN_CASE */
3777void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003778{
Janos Follathd958bb72019-07-03 15:02:16 +01003779 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02003780 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003781 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003782 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01003783 unsigned char input1[] = "Input 1";
3784 size_t input1_length = sizeof( input1 );
3785 unsigned char input2[] = "Input 2";
3786 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003787 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003788 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003789 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3790 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3791 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003792 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003793
Gilles Peskine8817f612018-12-18 00:18:46 +01003794 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003795
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003796 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
3797 psa_set_key_algorithm( &attributes, alg );
3798 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003799
Gilles Peskine73676cb2019-05-15 20:15:10 +02003800 PSA_ASSERT( psa_import_key( &attributes,
3801 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02003802 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003803
3804 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01003805 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
3806 input1, input1_length,
3807 input2, input2_length,
3808 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01003809 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003810
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003811 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01003812 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003813 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003814
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003815 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003816
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003817 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02003818 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003819
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003820exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003821 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003822 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003823 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003824}
3825/* END_CASE */
3826
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003827/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003828void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003829{
3830 uint8_t output_buffer[16];
3831 size_t buffer_size = 16;
3832 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003833 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003834
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003835 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
3836 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003837 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003838
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003839 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003840 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003841
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003842 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003843
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003844 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
3845 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003846 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003847
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003848 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003849 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003850
3851exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003852 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003853}
3854/* END_CASE */
3855
3856/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003857void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02003858 int step1_arg, data_t *input1,
3859 int step2_arg, data_t *input2,
3860 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003861 int requested_capacity_arg,
3862 data_t *expected_output1,
3863 data_t *expected_output2 )
3864{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003865 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02003866 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
3867 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02003868 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
3869 MBEDTLS_SVC_KEY_ID_INIT,
3870 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003871 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003872 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003873 uint8_t *expected_outputs[2] =
3874 {expected_output1->x, expected_output2->x};
3875 size_t output_sizes[2] =
3876 {expected_output1->len, expected_output2->len};
3877 size_t output_buffer_size = 0;
3878 uint8_t *output_buffer = NULL;
3879 size_t expected_capacity;
3880 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02003881 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003882 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02003883 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003884
3885 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3886 {
3887 if( output_sizes[i] > output_buffer_size )
3888 output_buffer_size = output_sizes[i];
3889 if( output_sizes[i] == 0 )
3890 expected_outputs[i] = NULL;
3891 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003892 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003893 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003894
Gilles Peskineff5f0e72019-04-18 12:53:30 +02003895 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
3896 psa_set_key_algorithm( &attributes, alg );
3897 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003898
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003899 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02003900 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
3901 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
3902 requested_capacity ) );
3903 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003904 {
Gilles Peskine1468da72019-05-29 17:35:49 +02003905 switch( steps[i] )
3906 {
3907 case 0:
3908 break;
3909 case PSA_KEY_DERIVATION_INPUT_SECRET:
3910 PSA_ASSERT( psa_import_key( &attributes,
3911 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003912 &keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02003913 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02003914 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02003915 break;
3916 default:
3917 PSA_ASSERT( psa_key_derivation_input_bytes(
3918 &operation, steps[i],
3919 inputs[i]->x, inputs[i]->len ) );
3920 break;
3921 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003922 }
Gilles Peskine1468da72019-05-29 17:35:49 +02003923
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003924 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003925 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003926 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003927 expected_capacity = requested_capacity;
3928
3929 /* Expansion phase. */
3930 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3931 {
3932 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003933 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003934 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003935 if( expected_capacity == 0 && output_sizes[i] == 0 )
3936 {
3937 /* Reading 0 bytes when 0 bytes are available can go either way. */
3938 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02003939 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003940 continue;
3941 }
3942 else if( expected_capacity == 0 ||
3943 output_sizes[i] > expected_capacity )
3944 {
3945 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02003946 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003947 expected_capacity = 0;
3948 continue;
3949 }
3950 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003951 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003952 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003953 ASSERT_COMPARE( output_buffer, output_sizes[i],
3954 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003955 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003956 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003957 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003958 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003959 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003960 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003961 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003962
3963exit:
3964 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003965 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003966 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
3967 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003968 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003969}
3970/* END_CASE */
3971
3972/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003973void derive_full( int alg_arg,
3974 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01003975 data_t *input1,
3976 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02003977 int requested_capacity_arg )
3978{
Ronald Cron5425a212020-08-04 14:58:35 +02003979 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02003980 psa_algorithm_t alg = alg_arg;
3981 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003982 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02003983 unsigned char output_buffer[16];
3984 size_t expected_capacity = requested_capacity;
3985 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02003986 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02003987
Gilles Peskine8817f612018-12-18 00:18:46 +01003988 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003989
Gilles Peskineff5f0e72019-04-18 12:53:30 +02003990 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
3991 psa_set_key_algorithm( &attributes, alg );
3992 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02003993
Gilles Peskine049c7532019-05-15 20:22:09 +02003994 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003995 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003996
Gilles Peskinec18e25f2021-02-12 23:48:20 +01003997 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
3998 input1->x, input1->len,
3999 input2->x, input2->len,
4000 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004001 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004002
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004003 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004004 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004005 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004006
4007 /* Expansion phase. */
4008 while( current_capacity > 0 )
4009 {
4010 size_t read_size = sizeof( output_buffer );
4011 if( read_size > current_capacity )
4012 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004013 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004014 output_buffer,
4015 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004016 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004017 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004018 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004019 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004020 }
4021
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004022 /* Check that the operation refuses to go over capacity. */
4023 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004024 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004025
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004026 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004027
4028exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004029 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004030 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004031 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004032}
4033/* END_CASE */
4034
Janos Follathe60c9052019-07-03 13:51:30 +01004035/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004036void derive_key_exercise( int alg_arg,
4037 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004038 data_t *input1,
4039 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004040 int derived_type_arg,
4041 int derived_bits_arg,
4042 int derived_usage_arg,
4043 int derived_alg_arg )
4044{
Ronald Cron5425a212020-08-04 14:58:35 +02004045 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4046 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004047 psa_algorithm_t alg = alg_arg;
4048 psa_key_type_t derived_type = derived_type_arg;
4049 size_t derived_bits = derived_bits_arg;
4050 psa_key_usage_t derived_usage = derived_usage_arg;
4051 psa_algorithm_t derived_alg = derived_alg_arg;
4052 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004053 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004054 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004055 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004056
Gilles Peskine8817f612018-12-18 00:18:46 +01004057 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004058
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004059 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4060 psa_set_key_algorithm( &attributes, alg );
4061 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004062 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004063 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004064
4065 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004066 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4067 input1->x, input1->len,
4068 input2->x, input2->len,
4069 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004070 goto exit;
4071
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004072 psa_set_key_usage_flags( &attributes, derived_usage );
4073 psa_set_key_algorithm( &attributes, derived_alg );
4074 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004075 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004076 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004077 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004078
4079 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004080 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004081 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4082 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004083
4084 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004085 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004086 goto exit;
4087
4088exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004089 /*
4090 * Key attributes may have been returned by psa_get_key_attributes()
4091 * thus reset them as required.
4092 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004093 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004094
4095 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004096 psa_destroy_key( base_key );
4097 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004098 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004099}
4100/* END_CASE */
4101
Janos Follath42fd8882019-07-03 14:17:09 +01004102/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004103void derive_key_export( int alg_arg,
4104 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004105 data_t *input1,
4106 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004107 int bytes1_arg,
4108 int bytes2_arg )
4109{
Ronald Cron5425a212020-08-04 14:58:35 +02004110 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4111 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004112 psa_algorithm_t alg = alg_arg;
4113 size_t bytes1 = bytes1_arg;
4114 size_t bytes2 = bytes2_arg;
4115 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004116 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004117 uint8_t *output_buffer = NULL;
4118 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004119 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4120 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004121 size_t length;
4122
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004123 ASSERT_ALLOC( output_buffer, capacity );
4124 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004125 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004126
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004127 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4128 psa_set_key_algorithm( &base_attributes, alg );
4129 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004130 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004131 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004132
4133 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004134 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4135 input1->x, input1->len,
4136 input2->x, input2->len,
4137 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004138 goto exit;
4139
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004140 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004141 output_buffer,
4142 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004143 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004144
4145 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004146 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4147 input1->x, input1->len,
4148 input2->x, input2->len,
4149 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004150 goto exit;
4151
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004152 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4153 psa_set_key_algorithm( &derived_attributes, 0 );
4154 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004155 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004156 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004157 &derived_key ) );
4158 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004159 export_buffer, bytes1,
4160 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004161 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004162 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004163 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004164 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004165 &derived_key ) );
4166 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004167 export_buffer + bytes1, bytes2,
4168 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004169 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004170
4171 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004172 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4173 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004174
4175exit:
4176 mbedtls_free( output_buffer );
4177 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004178 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004179 psa_destroy_key( base_key );
4180 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004181 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004182}
4183/* END_CASE */
4184
4185/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004186void derive_key( int alg_arg,
4187 data_t *key_data, data_t *input1, data_t *input2,
4188 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004189 int expected_status_arg,
4190 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004191{
Ronald Cron5425a212020-08-04 14:58:35 +02004192 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4193 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004194 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004195 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004196 size_t bits = bits_arg;
4197 psa_status_t expected_status = expected_status_arg;
4198 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4199 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4200 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4201
4202 PSA_ASSERT( psa_crypto_init( ) );
4203
4204 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4205 psa_set_key_algorithm( &base_attributes, alg );
4206 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4207 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004208 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004209
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004210 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4211 input1->x, input1->len,
4212 input2->x, input2->len,
4213 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004214 goto exit;
4215
4216 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4217 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004218 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004219 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004220
4221 psa_status_t status =
4222 psa_key_derivation_output_key( &derived_attributes,
4223 &operation,
4224 &derived_key );
4225 if( is_large_output > 0 )
4226 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4227 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004228
4229exit:
4230 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004231 psa_destroy_key( base_key );
4232 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004233 PSA_DONE( );
4234}
4235/* END_CASE */
4236
4237/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004238void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004239 int our_key_type_arg, int our_key_alg_arg,
4240 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004241 int expected_status_arg )
4242{
Ronald Cron5425a212020-08-04 14:58:35 +02004243 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004244 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004245 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004246 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004247 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004248 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004249 psa_status_t expected_status = expected_status_arg;
4250 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004251
Gilles Peskine8817f612018-12-18 00:18:46 +01004252 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004253
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004254 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004255 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004256 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004257 PSA_ASSERT( psa_import_key( &attributes,
4258 our_key_data->x, our_key_data->len,
4259 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004260
Gilles Peskine77f40d82019-04-11 21:27:06 +02004261 /* The tests currently include inputs that should fail at either step.
4262 * Test cases that fail at the setup step should be changed to call
4263 * key_derivation_setup instead, and this function should be renamed
4264 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004265 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004266 if( status == PSA_SUCCESS )
4267 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004268 TEST_EQUAL( psa_key_derivation_key_agreement(
4269 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4270 our_key,
4271 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004272 expected_status );
4273 }
4274 else
4275 {
4276 TEST_ASSERT( status == expected_status );
4277 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004278
4279exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004280 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004281 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004282 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004283}
4284/* END_CASE */
4285
4286/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004287void raw_key_agreement( int alg_arg,
4288 int our_key_type_arg, data_t *our_key_data,
4289 data_t *peer_key_data,
4290 data_t *expected_output )
4291{
Ronald Cron5425a212020-08-04 14:58:35 +02004292 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004293 psa_algorithm_t alg = alg_arg;
4294 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004295 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004296 unsigned char *output = NULL;
4297 size_t output_length = ~0;
4298
4299 ASSERT_ALLOC( output, expected_output->len );
4300 PSA_ASSERT( psa_crypto_init( ) );
4301
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004302 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4303 psa_set_key_algorithm( &attributes, alg );
4304 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004305 PSA_ASSERT( psa_import_key( &attributes,
4306 our_key_data->x, our_key_data->len,
4307 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004308
Gilles Peskinebe697d82019-05-16 18:00:41 +02004309 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4310 peer_key_data->x, peer_key_data->len,
4311 output, expected_output->len,
4312 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004313 ASSERT_COMPARE( output, output_length,
4314 expected_output->x, expected_output->len );
4315
4316exit:
4317 mbedtls_free( output );
4318 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004319 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004320}
4321/* END_CASE */
4322
4323/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004324void key_agreement_capacity( int alg_arg,
4325 int our_key_type_arg, data_t *our_key_data,
4326 data_t *peer_key_data,
4327 int expected_capacity_arg )
4328{
Ronald Cron5425a212020-08-04 14:58:35 +02004329 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004330 psa_algorithm_t alg = alg_arg;
4331 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004332 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004333 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004334 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004335 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004336
Gilles Peskine8817f612018-12-18 00:18:46 +01004337 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004338
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004339 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4340 psa_set_key_algorithm( &attributes, alg );
4341 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004342 PSA_ASSERT( psa_import_key( &attributes,
4343 our_key_data->x, our_key_data->len,
4344 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004345
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004346 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004347 PSA_ASSERT( psa_key_derivation_key_agreement(
4348 &operation,
4349 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4350 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004351 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4352 {
4353 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004354 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004355 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004356 NULL, 0 ) );
4357 }
Gilles Peskine59685592018-09-18 12:11:34 +02004358
Gilles Peskinebf491972018-10-25 22:36:12 +02004359 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004360 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004361 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004362 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004363
Gilles Peskinebf491972018-10-25 22:36:12 +02004364 /* Test the actual capacity by reading the output. */
4365 while( actual_capacity > sizeof( output ) )
4366 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004367 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004368 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004369 actual_capacity -= sizeof( output );
4370 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004371 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004372 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004373 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004374 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004375
Gilles Peskine59685592018-09-18 12:11:34 +02004376exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004377 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004378 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004379 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004380}
4381/* END_CASE */
4382
4383/* BEGIN_CASE */
4384void key_agreement_output( int alg_arg,
4385 int our_key_type_arg, data_t *our_key_data,
4386 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004387 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004388{
Ronald Cron5425a212020-08-04 14:58:35 +02004389 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004390 psa_algorithm_t alg = alg_arg;
4391 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004392 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004393 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004394 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004395
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004396 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4397 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004398
Gilles Peskine8817f612018-12-18 00:18:46 +01004399 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004400
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004401 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4402 psa_set_key_algorithm( &attributes, alg );
4403 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004404 PSA_ASSERT( psa_import_key( &attributes,
4405 our_key_data->x, our_key_data->len,
4406 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004407
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004408 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004409 PSA_ASSERT( psa_key_derivation_key_agreement(
4410 &operation,
4411 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4412 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004413 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4414 {
4415 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004416 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004417 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004418 NULL, 0 ) );
4419 }
Gilles Peskine59685592018-09-18 12:11:34 +02004420
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004421 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004422 actual_output,
4423 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004424 ASSERT_COMPARE( actual_output, expected_output1->len,
4425 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004426 if( expected_output2->len != 0 )
4427 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004428 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004429 actual_output,
4430 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004431 ASSERT_COMPARE( actual_output, expected_output2->len,
4432 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004433 }
Gilles Peskine59685592018-09-18 12:11:34 +02004434
4435exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004436 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004437 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004438 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004439 mbedtls_free( actual_output );
4440}
4441/* END_CASE */
4442
4443/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004444void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004445{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004446 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004447 unsigned char *output = NULL;
4448 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004449 size_t i;
4450 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004451
Simon Butcher49f8e312020-03-03 15:51:50 +00004452 TEST_ASSERT( bytes_arg >= 0 );
4453
Gilles Peskine91892022021-02-08 19:50:26 +01004454 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004455 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02004456
Gilles Peskine8817f612018-12-18 00:18:46 +01004457 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004458
Gilles Peskinea50d7392018-06-21 10:22:13 +02004459 /* Run several times, to ensure that every output byte will be
4460 * nonzero at least once with overwhelming probability
4461 * (2^(-8*number_of_runs)). */
4462 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004463 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004464 if( bytes != 0 )
4465 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004466 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004467
Gilles Peskinea50d7392018-06-21 10:22:13 +02004468 for( i = 0; i < bytes; i++ )
4469 {
4470 if( output[i] != 0 )
4471 ++changed[i];
4472 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004473 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004474
4475 /* Check that every byte was changed to nonzero at least once. This
4476 * validates that psa_generate_random is overwriting every byte of
4477 * the output buffer. */
4478 for( i = 0; i < bytes; i++ )
4479 {
4480 TEST_ASSERT( changed[i] != 0 );
4481 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004482
4483exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004484 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004485 mbedtls_free( output );
4486 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004487}
4488/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004489
4490/* BEGIN_CASE */
4491void generate_key( int type_arg,
4492 int bits_arg,
4493 int usage_arg,
4494 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004495 int expected_status_arg,
4496 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004497{
Ronald Cron5425a212020-08-04 14:58:35 +02004498 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004499 psa_key_type_t type = type_arg;
4500 psa_key_usage_t usage = usage_arg;
4501 size_t bits = bits_arg;
4502 psa_algorithm_t alg = alg_arg;
4503 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004504 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004505 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004506
Gilles Peskine8817f612018-12-18 00:18:46 +01004507 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004508
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004509 psa_set_key_usage_flags( &attributes, usage );
4510 psa_set_key_algorithm( &attributes, alg );
4511 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004512 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004513
4514 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01004515 psa_status_t status = psa_generate_key( &attributes, &key );
4516
4517 if( is_large_key > 0 )
4518 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4519 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004520 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004521 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004522
4523 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004524 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004525 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
4526 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004527
Gilles Peskine818ca122018-06-20 18:16:48 +02004528 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004529 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004530 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004531
4532exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004533 /*
4534 * Key attributes may have been returned by psa_get_key_attributes()
4535 * thus reset them as required.
4536 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004537 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004538
Ronald Cron5425a212020-08-04 14:58:35 +02004539 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004540 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004541}
4542/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004543
Gilles Peskinee56e8782019-04-26 17:34:02 +02004544/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
4545void generate_key_rsa( int bits_arg,
4546 data_t *e_arg,
4547 int expected_status_arg )
4548{
Ronald Cron5425a212020-08-04 14:58:35 +02004549 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004550 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02004551 size_t bits = bits_arg;
4552 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
4553 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
4554 psa_status_t expected_status = expected_status_arg;
4555 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4556 uint8_t *exported = NULL;
4557 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004558 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004559 size_t exported_length = SIZE_MAX;
4560 uint8_t *e_read_buffer = NULL;
4561 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02004562 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004563 size_t e_read_length = SIZE_MAX;
4564
4565 if( e_arg->len == 0 ||
4566 ( e_arg->len == 3 &&
4567 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
4568 {
4569 is_default_public_exponent = 1;
4570 e_read_size = 0;
4571 }
4572 ASSERT_ALLOC( e_read_buffer, e_read_size );
4573 ASSERT_ALLOC( exported, exported_size );
4574
4575 PSA_ASSERT( psa_crypto_init( ) );
4576
4577 psa_set_key_usage_flags( &attributes, usage );
4578 psa_set_key_algorithm( &attributes, alg );
4579 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
4580 e_arg->x, e_arg->len ) );
4581 psa_set_key_bits( &attributes, bits );
4582
4583 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02004584 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004585 if( expected_status != PSA_SUCCESS )
4586 goto exit;
4587
4588 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004589 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004590 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4591 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4592 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
4593 e_read_buffer, e_read_size,
4594 &e_read_length ) );
4595 if( is_default_public_exponent )
4596 TEST_EQUAL( e_read_length, 0 );
4597 else
4598 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
4599
4600 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004601 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02004602 goto exit;
4603
4604 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02004605 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02004606 exported, exported_size,
4607 &exported_length ) );
4608 {
4609 uint8_t *p = exported;
4610 uint8_t *end = exported + exported_length;
4611 size_t len;
4612 /* RSAPublicKey ::= SEQUENCE {
4613 * modulus INTEGER, -- n
4614 * publicExponent INTEGER } -- e
4615 */
4616 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004617 MBEDTLS_ASN1_SEQUENCE |
4618 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01004619 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004620 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
4621 MBEDTLS_ASN1_INTEGER ) );
4622 if( len >= 1 && p[0] == 0 )
4623 {
4624 ++p;
4625 --len;
4626 }
4627 if( e_arg->len == 0 )
4628 {
4629 TEST_EQUAL( len, 3 );
4630 TEST_EQUAL( p[0], 1 );
4631 TEST_EQUAL( p[1], 0 );
4632 TEST_EQUAL( p[2], 1 );
4633 }
4634 else
4635 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
4636 }
4637
4638exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004639 /*
4640 * Key attributes may have been returned by psa_get_key_attributes() or
4641 * set by psa_set_key_domain_parameters() thus reset them as required.
4642 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02004643 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004644
Ronald Cron5425a212020-08-04 14:58:35 +02004645 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004646 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004647 mbedtls_free( e_read_buffer );
4648 mbedtls_free( exported );
4649}
4650/* END_CASE */
4651
Darryl Greend49a4992018-06-18 17:27:26 +01004652/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004653void persistent_key_load_key_from_storage( data_t *data,
4654 int type_arg, int bits_arg,
4655 int usage_flags_arg, int alg_arg,
4656 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01004657{
Ronald Cron71016a92020-08-28 19:01:50 +02004658 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004659 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02004660 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4661 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004662 psa_key_type_t type = type_arg;
4663 size_t bits = bits_arg;
4664 psa_key_usage_t usage_flags = usage_flags_arg;
4665 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004666 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004667 unsigned char *first_export = NULL;
4668 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004669 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004670 size_t first_exported_length;
4671 size_t second_exported_length;
4672
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004673 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4674 {
4675 ASSERT_ALLOC( first_export, export_size );
4676 ASSERT_ALLOC( second_export, export_size );
4677 }
Darryl Greend49a4992018-06-18 17:27:26 +01004678
Gilles Peskine8817f612018-12-18 00:18:46 +01004679 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004680
Gilles Peskinec87af662019-05-15 16:12:22 +02004681 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004682 psa_set_key_usage_flags( &attributes, usage_flags );
4683 psa_set_key_algorithm( &attributes, alg );
4684 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004685 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004686
Darryl Green0c6575a2018-11-07 16:05:30 +00004687 switch( generation_method )
4688 {
4689 case IMPORT_KEY:
4690 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02004691 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004692 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004693 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004694
Darryl Green0c6575a2018-11-07 16:05:30 +00004695 case GENERATE_KEY:
4696 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02004697 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004698 break;
4699
4700 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01004701#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004702 {
4703 /* Create base key */
4704 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
4705 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4706 psa_set_key_usage_flags( &base_attributes,
4707 PSA_KEY_USAGE_DERIVE );
4708 psa_set_key_algorithm( &base_attributes, derive_alg );
4709 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004710 PSA_ASSERT( psa_import_key( &base_attributes,
4711 data->x, data->len,
4712 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004713 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004714 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004715 PSA_ASSERT( psa_key_derivation_input_key(
4716 &operation,
4717 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004718 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004719 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004720 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004721 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
4722 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004723 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004724 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004725 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02004726 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004727 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01004728#else
4729 TEST_ASSUME( ! "KDF not supported in this configuration" );
4730#endif
4731 break;
4732
4733 default:
4734 TEST_ASSERT( ! "generation_method not implemented in test" );
4735 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00004736 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004737 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01004738
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004739 /* Export the key if permitted by the key policy. */
4740 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4741 {
Ronald Cron5425a212020-08-04 14:58:35 +02004742 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004743 first_export, export_size,
4744 &first_exported_length ) );
4745 if( generation_method == IMPORT_KEY )
4746 ASSERT_COMPARE( data->x, data->len,
4747 first_export, first_exported_length );
4748 }
Darryl Greend49a4992018-06-18 17:27:26 +01004749
4750 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02004751 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004752 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01004753 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004754
Darryl Greend49a4992018-06-18 17:27:26 +01004755 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02004756 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02004757 TEST_ASSERT( mbedtls_svc_key_id_equal(
4758 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004759 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
4760 PSA_KEY_LIFETIME_PERSISTENT );
4761 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4762 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4763 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
4764 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004765
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004766 /* Export the key again if permitted by the key policy. */
4767 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00004768 {
Ronald Cron5425a212020-08-04 14:58:35 +02004769 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004770 second_export, export_size,
4771 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004772 ASSERT_COMPARE( first_export, first_exported_length,
4773 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00004774 }
4775
4776 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004777 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004778 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004779
4780exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004781 /*
4782 * Key attributes may have been returned by psa_get_key_attributes()
4783 * thus reset them as required.
4784 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004785 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004786
Darryl Greend49a4992018-06-18 17:27:26 +01004787 mbedtls_free( first_export );
4788 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004789 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004790 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02004791 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004792 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01004793}
4794/* END_CASE */