blob: fca78cb5eec854a5ebb06176b8638071b1516014 [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,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100837 int exercise_alg,
838 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200839{
Ronald Cron5425a212020-08-04 14:58:35 +0200840 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200841 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000842 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200843 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100844 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200845 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200846
Gilles Peskine8817f612018-12-18 00:18:46 +0100847 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200848
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200849 psa_set_key_usage_flags( &attributes, policy_usage );
850 psa_set_key_algorithm( &attributes, policy_alg );
851 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200852
Gilles Peskine049c7532019-05-15 20:22:09 +0200853 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200854 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200855
Ronald Cron5425a212020-08-04 14:58:35 +0200856 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100857 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100858 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100859 else
860 TEST_EQUAL( status, expected_status );
861
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200862 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200863
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200864 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200865 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100866 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100867 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100868 else
869 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200870
871exit:
872 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200873 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200874 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200875}
876/* END_CASE */
877
878/* BEGIN_CASE */
879void cipher_key_policy( int policy_usage,
880 int policy_alg,
881 int key_type,
882 data_t *key_data,
883 int exercise_alg )
884{
Ronald Cron5425a212020-08-04 14:58:35 +0200885 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200886 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000887 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200888 psa_status_t status;
889
Gilles Peskine8817f612018-12-18 00:18:46 +0100890 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200891
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200892 psa_set_key_usage_flags( &attributes, policy_usage );
893 psa_set_key_algorithm( &attributes, policy_alg );
894 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200895
Gilles Peskine049c7532019-05-15 20:22:09 +0200896 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200897 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200898
Ronald Cron5425a212020-08-04 14:58:35 +0200899 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200900 if( policy_alg == exercise_alg &&
901 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100902 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200903 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100904 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200905 psa_cipher_abort( &operation );
906
Ronald Cron5425a212020-08-04 14:58:35 +0200907 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200908 if( policy_alg == exercise_alg &&
909 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100910 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200911 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100912 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200913
914exit:
915 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200916 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200917 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200918}
919/* END_CASE */
920
921/* BEGIN_CASE */
922void aead_key_policy( int policy_usage,
923 int policy_alg,
924 int key_type,
925 data_t *key_data,
926 int nonce_length_arg,
927 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100928 int exercise_alg,
929 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200930{
Ronald Cron5425a212020-08-04 14:58:35 +0200931 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200932 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200933 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100934 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200935 unsigned char nonce[16] = {0};
936 size_t nonce_length = nonce_length_arg;
937 unsigned char tag[16];
938 size_t tag_length = tag_length_arg;
939 size_t output_length;
940
941 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
942 TEST_ASSERT( tag_length <= sizeof( tag ) );
943
Gilles Peskine8817f612018-12-18 00:18:46 +0100944 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200945
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200946 psa_set_key_usage_flags( &attributes, policy_usage );
947 psa_set_key_algorithm( &attributes, policy_alg );
948 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200949
Gilles Peskine049c7532019-05-15 20:22:09 +0200950 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200951 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200952
Ronald Cron5425a212020-08-04 14:58:35 +0200953 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200954 nonce, nonce_length,
955 NULL, 0,
956 NULL, 0,
957 tag, tag_length,
958 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100959 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
960 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200961 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100962 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200963
964 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200965 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200966 nonce, nonce_length,
967 NULL, 0,
968 tag, tag_length,
969 NULL, 0,
970 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100971 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
972 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
973 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100974 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200975 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100976 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200977
978exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200979 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200980 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200981}
982/* END_CASE */
983
984/* BEGIN_CASE */
985void asymmetric_encryption_key_policy( int policy_usage,
986 int policy_alg,
987 int key_type,
988 data_t *key_data,
989 int exercise_alg )
990{
Ronald Cron5425a212020-08-04 14:58:35 +0200991 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200992 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200993 psa_status_t status;
994 size_t key_bits;
995 size_t buffer_length;
996 unsigned char *buffer = NULL;
997 size_t output_length;
998
Gilles Peskine8817f612018-12-18 00:18:46 +0100999 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001000
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001001 psa_set_key_usage_flags( &attributes, policy_usage );
1002 psa_set_key_algorithm( &attributes, policy_alg );
1003 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001004
Gilles Peskine049c7532019-05-15 20:22:09 +02001005 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001006 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001007
Ronald Cron5425a212020-08-04 14:58:35 +02001008 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001009 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001010 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1011 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001012 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001013
Ronald Cron5425a212020-08-04 14:58:35 +02001014 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001015 NULL, 0,
1016 NULL, 0,
1017 buffer, buffer_length,
1018 &output_length );
1019 if( policy_alg == exercise_alg &&
1020 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001021 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001022 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001023 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001024
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001025 if( buffer_length != 0 )
1026 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001027 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001028 buffer, buffer_length,
1029 NULL, 0,
1030 buffer, buffer_length,
1031 &output_length );
1032 if( policy_alg == exercise_alg &&
1033 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001034 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001035 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001036 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001037
1038exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001039 /*
1040 * Key attributes may have been returned by psa_get_key_attributes()
1041 * thus reset them as required.
1042 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001043 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001044
1045 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001046 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001047 mbedtls_free( buffer );
1048}
1049/* END_CASE */
1050
1051/* BEGIN_CASE */
1052void asymmetric_signature_key_policy( int policy_usage,
1053 int policy_alg,
1054 int key_type,
1055 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001056 int exercise_alg,
1057 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001058{
Ronald Cron5425a212020-08-04 14:58:35 +02001059 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001060 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001061 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001062 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1063 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1064 * compatible with the policy and `payload_length_arg` is supposed to be
1065 * a valid input length to sign. If `payload_length_arg <= 0`,
1066 * `exercise_alg` is supposed to be forbidden by the policy. */
1067 int compatible_alg = payload_length_arg > 0;
1068 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001069 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001070 size_t signature_length;
1071
Gilles Peskine8817f612018-12-18 00:18:46 +01001072 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001073
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001074 psa_set_key_usage_flags( &attributes, policy_usage );
1075 psa_set_key_algorithm( &attributes, policy_alg );
1076 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001077
Gilles Peskine049c7532019-05-15 20:22:09 +02001078 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001079 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001080
Ronald Cron5425a212020-08-04 14:58:35 +02001081 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001082 payload, payload_length,
1083 signature, sizeof( signature ),
1084 &signature_length );
1085 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001086 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001087 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001088 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001089
1090 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001091 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001092 payload, payload_length,
1093 signature, sizeof( signature ) );
1094 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001095 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001096 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001097 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001098
1099exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001100 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001101 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001102}
1103/* END_CASE */
1104
Janos Follathba3fab92019-06-11 14:50:16 +01001105/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001106void derive_key_policy( int policy_usage,
1107 int policy_alg,
1108 int key_type,
1109 data_t *key_data,
1110 int exercise_alg )
1111{
Ronald Cron5425a212020-08-04 14:58:35 +02001112 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001113 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001114 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001115 psa_status_t status;
1116
Gilles Peskine8817f612018-12-18 00:18:46 +01001117 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001118
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001119 psa_set_key_usage_flags( &attributes, policy_usage );
1120 psa_set_key_algorithm( &attributes, policy_alg );
1121 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001122
Gilles Peskine049c7532019-05-15 20:22:09 +02001123 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001124 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001125
Janos Follathba3fab92019-06-11 14:50:16 +01001126 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1127
1128 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1129 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001130 {
Janos Follathba3fab92019-06-11 14:50:16 +01001131 PSA_ASSERT( psa_key_derivation_input_bytes(
1132 &operation,
1133 PSA_KEY_DERIVATION_INPUT_SEED,
1134 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001135 }
Janos Follathba3fab92019-06-11 14:50:16 +01001136
1137 status = psa_key_derivation_input_key( &operation,
1138 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001139 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001140
Gilles Peskineea0fb492018-07-12 17:17:20 +02001141 if( policy_alg == exercise_alg &&
1142 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001143 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001144 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001145 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001146
1147exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001148 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001149 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001150 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001151}
1152/* END_CASE */
1153
1154/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001155void agreement_key_policy( int policy_usage,
1156 int policy_alg,
1157 int key_type_arg,
1158 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001159 int exercise_alg,
1160 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001161{
Ronald Cron5425a212020-08-04 14:58:35 +02001162 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001163 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001164 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001165 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001166 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001167 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001168
Gilles Peskine8817f612018-12-18 00:18:46 +01001169 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001170
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001171 psa_set_key_usage_flags( &attributes, policy_usage );
1172 psa_set_key_algorithm( &attributes, policy_alg );
1173 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001174
Gilles Peskine049c7532019-05-15 20:22:09 +02001175 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001176 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001177
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001178 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001179 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001180
Steven Cooremance48e852020-10-05 16:02:45 +02001181 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001182
1183exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001184 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001185 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001186 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001187}
1188/* END_CASE */
1189
1190/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001191void key_policy_alg2( int key_type_arg, data_t *key_data,
1192 int usage_arg, int alg_arg, int alg2_arg )
1193{
Ronald Cron5425a212020-08-04 14:58:35 +02001194 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001195 psa_key_type_t key_type = key_type_arg;
1196 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1197 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1198 psa_key_usage_t usage = usage_arg;
1199 psa_algorithm_t alg = alg_arg;
1200 psa_algorithm_t alg2 = alg2_arg;
1201
1202 PSA_ASSERT( psa_crypto_init( ) );
1203
1204 psa_set_key_usage_flags( &attributes, usage );
1205 psa_set_key_algorithm( &attributes, alg );
1206 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1207 psa_set_key_type( &attributes, key_type );
1208 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001209 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001210
Ronald Cron5425a212020-08-04 14:58:35 +02001211 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001212 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1213 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1214 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1215
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001216 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001217 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001218 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001219 goto exit;
1220
1221exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001222 /*
1223 * Key attributes may have been returned by psa_get_key_attributes()
1224 * thus reset them as required.
1225 */
1226 psa_reset_key_attributes( &got_attributes );
1227
Ronald Cron5425a212020-08-04 14:58:35 +02001228 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001229 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001230}
1231/* END_CASE */
1232
1233/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001234void raw_agreement_key_policy( int policy_usage,
1235 int policy_alg,
1236 int key_type_arg,
1237 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001238 int exercise_alg,
1239 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001240{
Ronald Cron5425a212020-08-04 14:58:35 +02001241 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001242 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001243 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001244 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001245 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001246 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001247
1248 PSA_ASSERT( psa_crypto_init( ) );
1249
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001250 psa_set_key_usage_flags( &attributes, policy_usage );
1251 psa_set_key_algorithm( &attributes, policy_alg );
1252 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001253
Gilles Peskine049c7532019-05-15 20:22:09 +02001254 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001255 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001256
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001257 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001258
Steven Cooremance48e852020-10-05 16:02:45 +02001259 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001260
1261exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001262 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001263 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001264 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001265}
1266/* END_CASE */
1267
1268/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001269void copy_success( int source_usage_arg,
1270 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001271 int type_arg, data_t *material,
1272 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001273 int target_usage_arg,
1274 int target_alg_arg, int target_alg2_arg,
1275 int expected_usage_arg,
1276 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001277{
Gilles Peskineca25db92019-04-19 11:43:08 +02001278 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1279 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001280 psa_key_usage_t expected_usage = expected_usage_arg;
1281 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001282 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001283 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1284 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001285 uint8_t *export_buffer = NULL;
1286
Gilles Peskine57ab7212019-01-28 13:03:09 +01001287 PSA_ASSERT( psa_crypto_init( ) );
1288
Gilles Peskineca25db92019-04-19 11:43:08 +02001289 /* Prepare the source key. */
1290 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1291 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001292 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001293 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001294 PSA_ASSERT( psa_import_key( &source_attributes,
1295 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001296 &source_key ) );
1297 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001298
Gilles Peskineca25db92019-04-19 11:43:08 +02001299 /* Prepare the target attributes. */
1300 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001301 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001302 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001303 /* Set volatile lifetime to reset the key identifier to 0. */
1304 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1305 }
1306
Gilles Peskineca25db92019-04-19 11:43:08 +02001307 if( target_usage_arg != -1 )
1308 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1309 if( target_alg_arg != -1 )
1310 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001311 if( target_alg2_arg != -1 )
1312 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001313
1314 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001315 PSA_ASSERT( psa_copy_key( source_key,
1316 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001317
1318 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001319 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001320
1321 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001322 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001323 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1324 psa_get_key_type( &target_attributes ) );
1325 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1326 psa_get_key_bits( &target_attributes ) );
1327 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1328 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001329 TEST_EQUAL( expected_alg2,
1330 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001331 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1332 {
1333 size_t length;
1334 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001335 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001336 material->len, &length ) );
1337 ASSERT_COMPARE( material->x, material->len,
1338 export_buffer, length );
1339 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001340
1341 /* Convert wildcard algorithm to exercisable algorithm */
1342 if( PSA_ALG_IS_WILDCARD( expected_alg ) )
1343 {
1344 if( PSA_ALG_IS_MAC( expected_alg ) )
1345 expected_alg = PSA_ALG_TRUNCATED_MAC(
1346 expected_alg,
1347 PSA_MAC_TRUNCATED_LENGTH( expected_alg ) );
1348 if( PSA_ALG_IS_AEAD( expected_alg ) )
1349 expected_alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG(
1350 expected_alg,
1351 PSA_ALG_AEAD_GET_TAG_LENGTH( expected_alg ) );
1352 }
1353
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001354 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001355 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001356 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001357 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001358
Ronald Cron5425a212020-08-04 14:58:35 +02001359 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001360
1361exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001362 /*
1363 * Source and target key attributes may have been returned by
1364 * psa_get_key_attributes() thus reset them as required.
1365 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001366 psa_reset_key_attributes( &source_attributes );
1367 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001368
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001369 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001370 mbedtls_free( export_buffer );
1371}
1372/* END_CASE */
1373
1374/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001375void copy_fail( int source_usage_arg,
1376 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001377 int type_arg, data_t *material,
1378 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001379 int target_usage_arg,
1380 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001381 int expected_status_arg )
1382{
1383 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1384 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001385 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1386 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine4a644642019-05-03 17:14:08 +02001387
1388 PSA_ASSERT( psa_crypto_init( ) );
1389
1390 /* Prepare the source key. */
1391 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1392 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001393 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001394 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001395 PSA_ASSERT( psa_import_key( &source_attributes,
1396 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001397 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001398
1399 /* Prepare the target attributes. */
1400 psa_set_key_type( &target_attributes, target_type_arg );
1401 psa_set_key_bits( &target_attributes, target_bits_arg );
1402 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1403 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001404 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001405
1406 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001407 TEST_EQUAL( psa_copy_key( source_key,
1408 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001409 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001410
Ronald Cron5425a212020-08-04 14:58:35 +02001411 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001412
Gilles Peskine4a644642019-05-03 17:14:08 +02001413exit:
1414 psa_reset_key_attributes( &source_attributes );
1415 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001416 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001417}
1418/* END_CASE */
1419
1420/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001421void hash_operation_init( )
1422{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001423 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001424 /* Test each valid way of initializing the object, except for `= {0}`, as
1425 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1426 * though it's OK by the C standard. We could test for this, but we'd need
1427 * to supress the Clang warning for the test. */
1428 psa_hash_operation_t func = psa_hash_operation_init( );
1429 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1430 psa_hash_operation_t zero;
1431
1432 memset( &zero, 0, sizeof( zero ) );
1433
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001434 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001435 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1436 PSA_ERROR_BAD_STATE );
1437 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1438 PSA_ERROR_BAD_STATE );
1439 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1440 PSA_ERROR_BAD_STATE );
1441
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001442 /* A default hash operation should be abortable without error. */
1443 PSA_ASSERT( psa_hash_abort( &func ) );
1444 PSA_ASSERT( psa_hash_abort( &init ) );
1445 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001446}
1447/* END_CASE */
1448
1449/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001450void hash_setup( int alg_arg,
1451 int expected_status_arg )
1452{
1453 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001454 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001455 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001456 psa_status_t status;
1457
Gilles Peskine8817f612018-12-18 00:18:46 +01001458 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001459
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001460 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001461 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001462
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001463 /* Whether setup succeeded or failed, abort must succeed. */
1464 PSA_ASSERT( psa_hash_abort( &operation ) );
1465
1466 /* If setup failed, reproduce the failure, so as to
1467 * test the resulting state of the operation object. */
1468 if( status != PSA_SUCCESS )
1469 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1470
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001471 /* Now the operation object should be reusable. */
1472#if defined(KNOWN_SUPPORTED_HASH_ALG)
1473 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1474 PSA_ASSERT( psa_hash_abort( &operation ) );
1475#endif
1476
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001477exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001478 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001479}
1480/* END_CASE */
1481
1482/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001483void hash_compute_fail( int alg_arg, data_t *input,
1484 int output_size_arg, int expected_status_arg )
1485{
1486 psa_algorithm_t alg = alg_arg;
1487 uint8_t *output = NULL;
1488 size_t output_size = output_size_arg;
1489 size_t output_length = INVALID_EXPORT_LENGTH;
1490 psa_status_t expected_status = expected_status_arg;
1491 psa_status_t status;
1492
1493 ASSERT_ALLOC( output, output_size );
1494
1495 PSA_ASSERT( psa_crypto_init( ) );
1496
1497 status = psa_hash_compute( alg, input->x, input->len,
1498 output, output_size, &output_length );
1499 TEST_EQUAL( status, expected_status );
1500 TEST_ASSERT( output_length <= output_size );
1501
1502exit:
1503 mbedtls_free( output );
1504 PSA_DONE( );
1505}
1506/* END_CASE */
1507
1508/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001509void hash_compare_fail( int alg_arg, data_t *input,
1510 data_t *reference_hash,
1511 int expected_status_arg )
1512{
1513 psa_algorithm_t alg = alg_arg;
1514 psa_status_t expected_status = expected_status_arg;
1515 psa_status_t status;
1516
1517 PSA_ASSERT( psa_crypto_init( ) );
1518
1519 status = psa_hash_compare( alg, input->x, input->len,
1520 reference_hash->x, reference_hash->len );
1521 TEST_EQUAL( status, expected_status );
1522
1523exit:
1524 PSA_DONE( );
1525}
1526/* END_CASE */
1527
1528/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001529void hash_compute_compare( int alg_arg, data_t *input,
1530 data_t *expected_output )
1531{
1532 psa_algorithm_t alg = alg_arg;
1533 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1534 size_t output_length = INVALID_EXPORT_LENGTH;
1535 size_t i;
1536
1537 PSA_ASSERT( psa_crypto_init( ) );
1538
1539 /* Compute with tight buffer */
1540 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001541 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001542 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001543 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001544 ASSERT_COMPARE( output, output_length,
1545 expected_output->x, expected_output->len );
1546
1547 /* Compute with larger buffer */
1548 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1549 output, sizeof( output ),
1550 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001551 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001552 ASSERT_COMPARE( output, output_length,
1553 expected_output->x, expected_output->len );
1554
1555 /* Compare with correct hash */
1556 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1557 output, output_length ) );
1558
1559 /* Compare with trailing garbage */
1560 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1561 output, output_length + 1 ),
1562 PSA_ERROR_INVALID_SIGNATURE );
1563
1564 /* Compare with truncated hash */
1565 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1566 output, output_length - 1 ),
1567 PSA_ERROR_INVALID_SIGNATURE );
1568
1569 /* Compare with corrupted value */
1570 for( i = 0; i < output_length; i++ )
1571 {
Chris Jones9634bb12021-01-20 15:56:42 +00001572 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001573 output[i] ^= 1;
1574 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1575 output, output_length ),
1576 PSA_ERROR_INVALID_SIGNATURE );
1577 output[i] ^= 1;
1578 }
1579
1580exit:
1581 PSA_DONE( );
1582}
1583/* END_CASE */
1584
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001585/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001586void hash_bad_order( )
1587{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001588 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001589 unsigned char input[] = "";
1590 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001591 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001592 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1593 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1594 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001595 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001596 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001597 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001598
Gilles Peskine8817f612018-12-18 00:18:46 +01001599 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001600
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001601 /* Call setup twice in a row. */
1602 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1603 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1604 PSA_ERROR_BAD_STATE );
1605 PSA_ASSERT( psa_hash_abort( &operation ) );
1606
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001607 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001608 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001609 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001610 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001611
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001612 /* Call update after finish. */
1613 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1614 PSA_ASSERT( psa_hash_finish( &operation,
1615 hash, sizeof( hash ), &hash_len ) );
1616 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001617 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001618 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001619
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001620 /* Call verify without calling setup beforehand. */
1621 TEST_EQUAL( psa_hash_verify( &operation,
1622 valid_hash, sizeof( valid_hash ) ),
1623 PSA_ERROR_BAD_STATE );
1624 PSA_ASSERT( psa_hash_abort( &operation ) );
1625
1626 /* Call verify after finish. */
1627 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1628 PSA_ASSERT( psa_hash_finish( &operation,
1629 hash, sizeof( hash ), &hash_len ) );
1630 TEST_EQUAL( psa_hash_verify( &operation,
1631 valid_hash, sizeof( valid_hash ) ),
1632 PSA_ERROR_BAD_STATE );
1633 PSA_ASSERT( psa_hash_abort( &operation ) );
1634
1635 /* Call verify twice in a row. */
1636 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1637 PSA_ASSERT( psa_hash_verify( &operation,
1638 valid_hash, sizeof( valid_hash ) ) );
1639 TEST_EQUAL( psa_hash_verify( &operation,
1640 valid_hash, sizeof( valid_hash ) ),
1641 PSA_ERROR_BAD_STATE );
1642 PSA_ASSERT( psa_hash_abort( &operation ) );
1643
1644 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001645 TEST_EQUAL( psa_hash_finish( &operation,
1646 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001647 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001648 PSA_ASSERT( psa_hash_abort( &operation ) );
1649
1650 /* Call finish twice in a row. */
1651 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1652 PSA_ASSERT( psa_hash_finish( &operation,
1653 hash, sizeof( hash ), &hash_len ) );
1654 TEST_EQUAL( psa_hash_finish( &operation,
1655 hash, sizeof( hash ), &hash_len ),
1656 PSA_ERROR_BAD_STATE );
1657 PSA_ASSERT( psa_hash_abort( &operation ) );
1658
1659 /* Call finish after calling verify. */
1660 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1661 PSA_ASSERT( psa_hash_verify( &operation,
1662 valid_hash, sizeof( valid_hash ) ) );
1663 TEST_EQUAL( psa_hash_finish( &operation,
1664 hash, sizeof( hash ), &hash_len ),
1665 PSA_ERROR_BAD_STATE );
1666 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001667
1668exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001669 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001670}
1671/* END_CASE */
1672
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001673/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001674void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001675{
1676 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001677 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1678 * appended to it */
1679 unsigned char hash[] = {
1680 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1681 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1682 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001683 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001684 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001685
Gilles Peskine8817f612018-12-18 00:18:46 +01001686 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001687
itayzafrir27e69452018-11-01 14:26:34 +02001688 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001689 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001690 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001691 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001692
itayzafrir27e69452018-11-01 14:26:34 +02001693 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001694 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001695 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001696 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001697
itayzafrir27e69452018-11-01 14:26:34 +02001698 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001699 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001700 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001701 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001702
itayzafrirec93d302018-10-18 18:01:10 +03001703exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001704 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001705}
1706/* END_CASE */
1707
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001708/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1709void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001710{
1711 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001712 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001713 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001714 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001715 size_t hash_len;
1716
Gilles Peskine8817f612018-12-18 00:18:46 +01001717 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001718
itayzafrir58028322018-10-25 10:22:01 +03001719 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001720 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001721 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001722 hash, expected_size - 1, &hash_len ),
1723 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001724
1725exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001726 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001727}
1728/* END_CASE */
1729
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001730/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1731void hash_clone_source_state( )
1732{
1733 psa_algorithm_t alg = PSA_ALG_SHA_256;
1734 unsigned char hash[PSA_HASH_MAX_SIZE];
1735 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1736 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1737 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1738 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1739 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1740 size_t hash_len;
1741
1742 PSA_ASSERT( psa_crypto_init( ) );
1743 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1744
1745 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1746 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1747 PSA_ASSERT( psa_hash_finish( &op_finished,
1748 hash, sizeof( hash ), &hash_len ) );
1749 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1750 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1751
1752 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1753 PSA_ERROR_BAD_STATE );
1754
1755 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1756 PSA_ASSERT( psa_hash_finish( &op_init,
1757 hash, sizeof( hash ), &hash_len ) );
1758 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1759 PSA_ASSERT( psa_hash_finish( &op_finished,
1760 hash, sizeof( hash ), &hash_len ) );
1761 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1762 PSA_ASSERT( psa_hash_finish( &op_aborted,
1763 hash, sizeof( hash ), &hash_len ) );
1764
1765exit:
1766 psa_hash_abort( &op_source );
1767 psa_hash_abort( &op_init );
1768 psa_hash_abort( &op_setup );
1769 psa_hash_abort( &op_finished );
1770 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001771 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001772}
1773/* END_CASE */
1774
1775/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1776void hash_clone_target_state( )
1777{
1778 psa_algorithm_t alg = PSA_ALG_SHA_256;
1779 unsigned char hash[PSA_HASH_MAX_SIZE];
1780 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1781 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1782 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1783 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1784 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1785 size_t hash_len;
1786
1787 PSA_ASSERT( psa_crypto_init( ) );
1788
1789 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1790 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1791 PSA_ASSERT( psa_hash_finish( &op_finished,
1792 hash, sizeof( hash ), &hash_len ) );
1793 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1794 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1795
1796 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1797 PSA_ASSERT( psa_hash_finish( &op_target,
1798 hash, sizeof( hash ), &hash_len ) );
1799
1800 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1801 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1802 PSA_ERROR_BAD_STATE );
1803 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1804 PSA_ERROR_BAD_STATE );
1805
1806exit:
1807 psa_hash_abort( &op_target );
1808 psa_hash_abort( &op_init );
1809 psa_hash_abort( &op_setup );
1810 psa_hash_abort( &op_finished );
1811 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001812 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001813}
1814/* END_CASE */
1815
itayzafrir58028322018-10-25 10:22:01 +03001816/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001817void mac_operation_init( )
1818{
Jaeden Amero252ef282019-02-15 14:05:35 +00001819 const uint8_t input[1] = { 0 };
1820
Jaeden Amero769ce272019-01-04 11:48:03 +00001821 /* Test each valid way of initializing the object, except for `= {0}`, as
1822 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1823 * though it's OK by the C standard. We could test for this, but we'd need
1824 * to supress the Clang warning for the test. */
1825 psa_mac_operation_t func = psa_mac_operation_init( );
1826 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1827 psa_mac_operation_t zero;
1828
1829 memset( &zero, 0, sizeof( zero ) );
1830
Jaeden Amero252ef282019-02-15 14:05:35 +00001831 /* A freshly-initialized MAC operation should not be usable. */
1832 TEST_EQUAL( psa_mac_update( &func,
1833 input, sizeof( input ) ),
1834 PSA_ERROR_BAD_STATE );
1835 TEST_EQUAL( psa_mac_update( &init,
1836 input, sizeof( input ) ),
1837 PSA_ERROR_BAD_STATE );
1838 TEST_EQUAL( psa_mac_update( &zero,
1839 input, sizeof( input ) ),
1840 PSA_ERROR_BAD_STATE );
1841
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001842 /* A default MAC operation should be abortable without error. */
1843 PSA_ASSERT( psa_mac_abort( &func ) );
1844 PSA_ASSERT( psa_mac_abort( &init ) );
1845 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001846}
1847/* END_CASE */
1848
1849/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001850void mac_setup( int key_type_arg,
1851 data_t *key,
1852 int alg_arg,
1853 int expected_status_arg )
1854{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001855 psa_key_type_t key_type = key_type_arg;
1856 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001857 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001858 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001859 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1860#if defined(KNOWN_SUPPORTED_MAC_ALG)
1861 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1862#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001863
Gilles Peskine8817f612018-12-18 00:18:46 +01001864 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001865
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001866 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1867 &operation, &status ) )
1868 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001869 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001870
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001871 /* The operation object should be reusable. */
1872#if defined(KNOWN_SUPPORTED_MAC_ALG)
1873 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1874 smoke_test_key_data,
1875 sizeof( smoke_test_key_data ),
1876 KNOWN_SUPPORTED_MAC_ALG,
1877 &operation, &status ) )
1878 goto exit;
1879 TEST_EQUAL( status, PSA_SUCCESS );
1880#endif
1881
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001882exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001883 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001884}
1885/* END_CASE */
1886
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001887/* 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 +00001888void mac_bad_order( )
1889{
Ronald Cron5425a212020-08-04 14:58:35 +02001890 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001891 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1892 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001893 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001894 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1895 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1896 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001897 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001898 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1899 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1900 size_t sign_mac_length = 0;
1901 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1902 const uint8_t verify_mac[] = {
1903 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1904 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1905 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1906
1907 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001908 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001909 psa_set_key_algorithm( &attributes, alg );
1910 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001911
Ronald Cron5425a212020-08-04 14:58:35 +02001912 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1913 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001914
Jaeden Amero252ef282019-02-15 14:05:35 +00001915 /* Call update without calling setup beforehand. */
1916 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1917 PSA_ERROR_BAD_STATE );
1918 PSA_ASSERT( psa_mac_abort( &operation ) );
1919
1920 /* Call sign finish without calling setup beforehand. */
1921 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1922 &sign_mac_length),
1923 PSA_ERROR_BAD_STATE );
1924 PSA_ASSERT( psa_mac_abort( &operation ) );
1925
1926 /* Call verify finish without calling setup beforehand. */
1927 TEST_EQUAL( psa_mac_verify_finish( &operation,
1928 verify_mac, sizeof( verify_mac ) ),
1929 PSA_ERROR_BAD_STATE );
1930 PSA_ASSERT( psa_mac_abort( &operation ) );
1931
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001932 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001933 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
1934 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001935 PSA_ERROR_BAD_STATE );
1936 PSA_ASSERT( psa_mac_abort( &operation ) );
1937
Jaeden Amero252ef282019-02-15 14:05:35 +00001938 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001939 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001940 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1941 PSA_ASSERT( psa_mac_sign_finish( &operation,
1942 sign_mac, sizeof( sign_mac ),
1943 &sign_mac_length ) );
1944 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1945 PSA_ERROR_BAD_STATE );
1946 PSA_ASSERT( psa_mac_abort( &operation ) );
1947
1948 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001949 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001950 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1951 PSA_ASSERT( psa_mac_verify_finish( &operation,
1952 verify_mac, sizeof( verify_mac ) ) );
1953 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1954 PSA_ERROR_BAD_STATE );
1955 PSA_ASSERT( psa_mac_abort( &operation ) );
1956
1957 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001958 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001959 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1960 PSA_ASSERT( psa_mac_sign_finish( &operation,
1961 sign_mac, sizeof( sign_mac ),
1962 &sign_mac_length ) );
1963 TEST_EQUAL( psa_mac_sign_finish( &operation,
1964 sign_mac, sizeof( sign_mac ),
1965 &sign_mac_length ),
1966 PSA_ERROR_BAD_STATE );
1967 PSA_ASSERT( psa_mac_abort( &operation ) );
1968
1969 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001970 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001971 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1972 PSA_ASSERT( psa_mac_verify_finish( &operation,
1973 verify_mac, sizeof( verify_mac ) ) );
1974 TEST_EQUAL( psa_mac_verify_finish( &operation,
1975 verify_mac, sizeof( verify_mac ) ),
1976 PSA_ERROR_BAD_STATE );
1977 PSA_ASSERT( psa_mac_abort( &operation ) );
1978
1979 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02001980 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001981 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1982 TEST_EQUAL( psa_mac_verify_finish( &operation,
1983 verify_mac, sizeof( verify_mac ) ),
1984 PSA_ERROR_BAD_STATE );
1985 PSA_ASSERT( psa_mac_abort( &operation ) );
1986
1987 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02001988 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001989 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1990 TEST_EQUAL( psa_mac_sign_finish( &operation,
1991 sign_mac, sizeof( sign_mac ),
1992 &sign_mac_length ),
1993 PSA_ERROR_BAD_STATE );
1994 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001995
Ronald Cron5425a212020-08-04 14:58:35 +02001996 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001997
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001998exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001999 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002000}
2001/* END_CASE */
2002
2003/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002004void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002005 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002006 int alg_arg,
2007 data_t *input,
2008 data_t *expected_mac )
2009{
Ronald Cron5425a212020-08-04 14:58:35 +02002010 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002011 psa_key_type_t key_type = key_type_arg;
2012 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002013 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002014 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002015 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002016 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002017 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002018 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002019 const size_t output_sizes_to_test[] = {
2020 0,
2021 1,
2022 expected_mac->len - 1,
2023 expected_mac->len,
2024 expected_mac->len + 1,
2025 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002026
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002027 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002028 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002029 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002030
Gilles Peskine8817f612018-12-18 00:18:46 +01002031 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002032
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002033 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002034 psa_set_key_algorithm( &attributes, alg );
2035 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002036
Ronald Cron5425a212020-08-04 14:58:35 +02002037 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2038 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002039
Gilles Peskine8b356b52020-08-25 23:44:59 +02002040 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2041 {
2042 const size_t output_size = output_sizes_to_test[i];
2043 psa_status_t expected_status =
2044 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2045 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002046
Chris Jones9634bb12021-01-20 15:56:42 +00002047 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002048 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002049
Gilles Peskine8b356b52020-08-25 23:44:59 +02002050 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002051 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002052 PSA_ASSERT( psa_mac_update( &operation,
2053 input->x, input->len ) );
2054 TEST_EQUAL( psa_mac_sign_finish( &operation,
2055 actual_mac, output_size,
2056 &mac_length ),
2057 expected_status );
2058 PSA_ASSERT( psa_mac_abort( &operation ) );
2059
2060 if( expected_status == PSA_SUCCESS )
2061 {
2062 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2063 actual_mac, mac_length );
2064 }
2065 mbedtls_free( actual_mac );
2066 actual_mac = NULL;
2067 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002068
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002069exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002070 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002071 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002072 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002073 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002074}
2075/* END_CASE */
2076
2077/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002078void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002079 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002080 int alg_arg,
2081 data_t *input,
2082 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002083{
Ronald Cron5425a212020-08-04 14:58:35 +02002084 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002085 psa_key_type_t key_type = key_type_arg;
2086 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002087 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002088 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002089 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002090
Gilles Peskine69c12672018-06-28 00:07:19 +02002091 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2092
Gilles Peskine8817f612018-12-18 00:18:46 +01002093 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002094
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002095 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002096 psa_set_key_algorithm( &attributes, alg );
2097 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002098
Ronald Cron5425a212020-08-04 14:58:35 +02002099 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2100 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002101
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002102 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002103 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002104 PSA_ASSERT( psa_mac_update( &operation,
2105 input->x, input->len ) );
2106 PSA_ASSERT( psa_mac_verify_finish( &operation,
2107 expected_mac->x,
2108 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002109
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002110 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002111 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002112 PSA_ASSERT( psa_mac_update( &operation,
2113 input->x, input->len ) );
2114 TEST_EQUAL( psa_mac_verify_finish( &operation,
2115 expected_mac->x,
2116 expected_mac->len - 1 ),
2117 PSA_ERROR_INVALID_SIGNATURE );
2118
2119 /* Test a MAC that's too long. */
2120 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2121 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002122 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002123 PSA_ASSERT( psa_mac_update( &operation,
2124 input->x, input->len ) );
2125 TEST_EQUAL( psa_mac_verify_finish( &operation,
2126 perturbed_mac,
2127 expected_mac->len + 1 ),
2128 PSA_ERROR_INVALID_SIGNATURE );
2129
2130 /* Test changing one byte. */
2131 for( size_t i = 0; i < expected_mac->len; i++ )
2132 {
Chris Jones9634bb12021-01-20 15:56:42 +00002133 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002134 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002135 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002136 PSA_ASSERT( psa_mac_update( &operation,
2137 input->x, input->len ) );
2138 TEST_EQUAL( psa_mac_verify_finish( &operation,
2139 perturbed_mac,
2140 expected_mac->len ),
2141 PSA_ERROR_INVALID_SIGNATURE );
2142 perturbed_mac[i] ^= 1;
2143 }
2144
Gilles Peskine8c9def32018-02-08 10:02:12 +01002145exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002146 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002147 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002148 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002149 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002150}
2151/* END_CASE */
2152
2153/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002154void cipher_operation_init( )
2155{
Jaeden Ameroab439972019-02-15 14:12:05 +00002156 const uint8_t input[1] = { 0 };
2157 unsigned char output[1] = { 0 };
2158 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002159 /* Test each valid way of initializing the object, except for `= {0}`, as
2160 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2161 * though it's OK by the C standard. We could test for this, but we'd need
2162 * to supress the Clang warning for the test. */
2163 psa_cipher_operation_t func = psa_cipher_operation_init( );
2164 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2165 psa_cipher_operation_t zero;
2166
2167 memset( &zero, 0, sizeof( zero ) );
2168
Jaeden Ameroab439972019-02-15 14:12:05 +00002169 /* A freshly-initialized cipher operation should not be usable. */
2170 TEST_EQUAL( psa_cipher_update( &func,
2171 input, sizeof( input ),
2172 output, sizeof( output ),
2173 &output_length ),
2174 PSA_ERROR_BAD_STATE );
2175 TEST_EQUAL( psa_cipher_update( &init,
2176 input, sizeof( input ),
2177 output, sizeof( output ),
2178 &output_length ),
2179 PSA_ERROR_BAD_STATE );
2180 TEST_EQUAL( psa_cipher_update( &zero,
2181 input, sizeof( input ),
2182 output, sizeof( output ),
2183 &output_length ),
2184 PSA_ERROR_BAD_STATE );
2185
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002186 /* A default cipher operation should be abortable without error. */
2187 PSA_ASSERT( psa_cipher_abort( &func ) );
2188 PSA_ASSERT( psa_cipher_abort( &init ) );
2189 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002190}
2191/* END_CASE */
2192
2193/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002194void cipher_setup( int key_type_arg,
2195 data_t *key,
2196 int alg_arg,
2197 int expected_status_arg )
2198{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002199 psa_key_type_t key_type = key_type_arg;
2200 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002201 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002202 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002203 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002204#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002205 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2206#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002207
Gilles Peskine8817f612018-12-18 00:18:46 +01002208 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002209
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002210 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2211 &operation, &status ) )
2212 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002213 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002214
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002215 /* The operation object should be reusable. */
2216#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2217 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2218 smoke_test_key_data,
2219 sizeof( smoke_test_key_data ),
2220 KNOWN_SUPPORTED_CIPHER_ALG,
2221 &operation, &status ) )
2222 goto exit;
2223 TEST_EQUAL( status, PSA_SUCCESS );
2224#endif
2225
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002226exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002227 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002228 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002229}
2230/* END_CASE */
2231
Steven Cooreman29eecbf2021-01-28 19:41:25 +01002232/* BEGIN_CASE depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002233void cipher_bad_order( )
2234{
Ronald Cron5425a212020-08-04 14:58:35 +02002235 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002236 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2237 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002238 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002239 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002240 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002241 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002242 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2243 0xaa, 0xaa, 0xaa, 0xaa };
2244 const uint8_t text[] = {
2245 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2246 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002247 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002248 size_t length = 0;
2249
2250 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002251 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2252 psa_set_key_algorithm( &attributes, alg );
2253 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002254 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2255 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002256
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002257 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002258 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2259 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002260 PSA_ERROR_BAD_STATE );
2261 PSA_ASSERT( psa_cipher_abort( &operation ) );
2262
2263 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002264 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2265 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002266 PSA_ERROR_BAD_STATE );
2267 PSA_ASSERT( psa_cipher_abort( &operation ) );
2268
Jaeden Ameroab439972019-02-15 14:12:05 +00002269 /* Generate an IV without calling setup beforehand. */
2270 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2271 buffer, sizeof( buffer ),
2272 &length ),
2273 PSA_ERROR_BAD_STATE );
2274 PSA_ASSERT( psa_cipher_abort( &operation ) );
2275
2276 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002277 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002278 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2279 buffer, sizeof( buffer ),
2280 &length ) );
2281 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2282 buffer, sizeof( buffer ),
2283 &length ),
2284 PSA_ERROR_BAD_STATE );
2285 PSA_ASSERT( psa_cipher_abort( &operation ) );
2286
2287 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002288 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002289 PSA_ASSERT( psa_cipher_set_iv( &operation,
2290 iv, sizeof( iv ) ) );
2291 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2292 buffer, sizeof( buffer ),
2293 &length ),
2294 PSA_ERROR_BAD_STATE );
2295 PSA_ASSERT( psa_cipher_abort( &operation ) );
2296
2297 /* Set an IV without calling setup beforehand. */
2298 TEST_EQUAL( psa_cipher_set_iv( &operation,
2299 iv, sizeof( iv ) ),
2300 PSA_ERROR_BAD_STATE );
2301 PSA_ASSERT( psa_cipher_abort( &operation ) );
2302
2303 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002304 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002305 PSA_ASSERT( psa_cipher_set_iv( &operation,
2306 iv, sizeof( iv ) ) );
2307 TEST_EQUAL( psa_cipher_set_iv( &operation,
2308 iv, sizeof( iv ) ),
2309 PSA_ERROR_BAD_STATE );
2310 PSA_ASSERT( psa_cipher_abort( &operation ) );
2311
2312 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002313 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002314 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2315 buffer, sizeof( buffer ),
2316 &length ) );
2317 TEST_EQUAL( psa_cipher_set_iv( &operation,
2318 iv, sizeof( iv ) ),
2319 PSA_ERROR_BAD_STATE );
2320 PSA_ASSERT( psa_cipher_abort( &operation ) );
2321
2322 /* Call update without calling setup beforehand. */
2323 TEST_EQUAL( psa_cipher_update( &operation,
2324 text, sizeof( text ),
2325 buffer, sizeof( buffer ),
2326 &length ),
2327 PSA_ERROR_BAD_STATE );
2328 PSA_ASSERT( psa_cipher_abort( &operation ) );
2329
2330 /* Call update without an IV where an IV is required. */
2331 TEST_EQUAL( psa_cipher_update( &operation,
2332 text, sizeof( text ),
2333 buffer, sizeof( buffer ),
2334 &length ),
2335 PSA_ERROR_BAD_STATE );
2336 PSA_ASSERT( psa_cipher_abort( &operation ) );
2337
2338 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002339 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002340 PSA_ASSERT( psa_cipher_set_iv( &operation,
2341 iv, sizeof( iv ) ) );
2342 PSA_ASSERT( psa_cipher_finish( &operation,
2343 buffer, sizeof( buffer ), &length ) );
2344 TEST_EQUAL( psa_cipher_update( &operation,
2345 text, sizeof( text ),
2346 buffer, sizeof( buffer ),
2347 &length ),
2348 PSA_ERROR_BAD_STATE );
2349 PSA_ASSERT( psa_cipher_abort( &operation ) );
2350
2351 /* Call finish without calling setup beforehand. */
2352 TEST_EQUAL( psa_cipher_finish( &operation,
2353 buffer, sizeof( buffer ), &length ),
2354 PSA_ERROR_BAD_STATE );
2355 PSA_ASSERT( psa_cipher_abort( &operation ) );
2356
2357 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002358 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002359 /* Not calling update means we are encrypting an empty buffer, which is OK
2360 * for cipher modes with padding. */
2361 TEST_EQUAL( psa_cipher_finish( &operation,
2362 buffer, sizeof( buffer ), &length ),
2363 PSA_ERROR_BAD_STATE );
2364 PSA_ASSERT( psa_cipher_abort( &operation ) );
2365
2366 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002367 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002368 PSA_ASSERT( psa_cipher_set_iv( &operation,
2369 iv, sizeof( iv ) ) );
2370 PSA_ASSERT( psa_cipher_finish( &operation,
2371 buffer, sizeof( buffer ), &length ) );
2372 TEST_EQUAL( psa_cipher_finish( &operation,
2373 buffer, sizeof( buffer ), &length ),
2374 PSA_ERROR_BAD_STATE );
2375 PSA_ASSERT( psa_cipher_abort( &operation ) );
2376
Ronald Cron5425a212020-08-04 14:58:35 +02002377 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002378
Jaeden Ameroab439972019-02-15 14:12:05 +00002379exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002380 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002381 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002382}
2383/* END_CASE */
2384
2385/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002386void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002387 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002388 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002389 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002390{
Ronald Cron5425a212020-08-04 14:58:35 +02002391 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002392 psa_status_t status;
2393 psa_key_type_t key_type = key_type_arg;
2394 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002395 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002396 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002397 size_t output_buffer_size = 0;
2398 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002399 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002400 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002401 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002402
Gilles Peskine8817f612018-12-18 00:18:46 +01002403 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002404
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002405 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2406 psa_set_key_algorithm( &attributes, alg );
2407 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002408
Ronald Cron5425a212020-08-04 14:58:35 +02002409 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2410 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002411
Ronald Cron5425a212020-08-04 14:58:35 +02002412 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002413
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002414 if( iv->len > 0 )
2415 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002416 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002417 }
2418
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002419 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002420 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002421 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002422
Gilles Peskine8817f612018-12-18 00:18:46 +01002423 PSA_ASSERT( psa_cipher_update( &operation,
2424 input->x, input->len,
2425 output, output_buffer_size,
2426 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002427 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002428 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002429 output + total_output_length,
2430 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002431 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002432 total_output_length += function_output_length;
2433
Gilles Peskinefe11b722018-12-18 00:24:04 +01002434 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002435 if( expected_status == PSA_SUCCESS )
2436 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002437 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002438 ASSERT_COMPARE( expected_output->x, expected_output->len,
2439 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002440 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002441
Gilles Peskine50e586b2018-06-08 14:28:46 +02002442exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002443 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002444 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002445 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002446 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002447}
2448/* END_CASE */
2449
2450/* BEGIN_CASE */
2451void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002452 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002453 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002454 int first_part_size_arg,
2455 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002456 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002457{
Ronald Cron5425a212020-08-04 14:58:35 +02002458 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002459 psa_key_type_t key_type = key_type_arg;
2460 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002461 size_t first_part_size = first_part_size_arg;
2462 size_t output1_length = output1_length_arg;
2463 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002464 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002465 size_t output_buffer_size = 0;
2466 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002467 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002468 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002469 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002470
Gilles Peskine8817f612018-12-18 00:18:46 +01002471 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002472
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002473 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2474 psa_set_key_algorithm( &attributes, alg );
2475 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002476
Ronald Cron5425a212020-08-04 14:58:35 +02002477 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2478 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002479
Ronald Cron5425a212020-08-04 14:58:35 +02002480 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002481
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002482 if( iv->len > 0 )
2483 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002484 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002485 }
2486
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002487 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002488 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002489 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002490
Gilles Peskinee0866522019-02-19 19:44:00 +01002491 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002492 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2493 output, output_buffer_size,
2494 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002495 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002496 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002497 PSA_ASSERT( psa_cipher_update( &operation,
2498 input->x + first_part_size,
2499 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002500 output + total_output_length,
2501 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002502 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002503 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002504 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002505 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002506 output + total_output_length,
2507 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002508 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002509 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002510 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002511
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002512 ASSERT_COMPARE( expected_output->x, expected_output->len,
2513 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002514
2515exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002516 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002517 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002518 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002519 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002520}
2521/* END_CASE */
2522
2523/* BEGIN_CASE */
2524void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002525 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002526 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002527 int first_part_size_arg,
2528 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002529 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002530{
Ronald Cron5425a212020-08-04 14:58:35 +02002531 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002532 psa_key_type_t key_type = key_type_arg;
2533 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002534 size_t first_part_size = first_part_size_arg;
2535 size_t output1_length = output1_length_arg;
2536 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002537 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002538 size_t output_buffer_size = 0;
2539 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002540 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002541 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002542 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002543
Gilles Peskine8817f612018-12-18 00:18:46 +01002544 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002545
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002546 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2547 psa_set_key_algorithm( &attributes, alg );
2548 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002549
Ronald Cron5425a212020-08-04 14:58:35 +02002550 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2551 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002552
Ronald Cron5425a212020-08-04 14:58:35 +02002553 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002554
Steven Cooreman177deba2020-09-07 17:14:14 +02002555 if( iv->len > 0 )
2556 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002557 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002558 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002559
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002560 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002561 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002562 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002563
Gilles Peskinee0866522019-02-19 19:44:00 +01002564 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002565 PSA_ASSERT( psa_cipher_update( &operation,
2566 input->x, first_part_size,
2567 output, output_buffer_size,
2568 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002569 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002570 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002571 PSA_ASSERT( psa_cipher_update( &operation,
2572 input->x + first_part_size,
2573 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002574 output + total_output_length,
2575 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002576 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002577 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002578 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002579 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002580 output + total_output_length,
2581 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002582 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002583 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002584 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002585
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002586 ASSERT_COMPARE( expected_output->x, expected_output->len,
2587 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002588
2589exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002590 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002591 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002592 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002593 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002594}
2595/* END_CASE */
2596
Gilles Peskine50e586b2018-06-08 14:28:46 +02002597/* BEGIN_CASE */
2598void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002599 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002600 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002601 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002602{
Ronald Cron5425a212020-08-04 14:58:35 +02002603 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002604 psa_status_t status;
2605 psa_key_type_t key_type = key_type_arg;
2606 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002607 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002608 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002609 size_t output_buffer_size = 0;
2610 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002611 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002612 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002613 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002614
Gilles Peskine8817f612018-12-18 00:18:46 +01002615 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002616
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002617 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2618 psa_set_key_algorithm( &attributes, alg );
2619 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002620
Ronald Cron5425a212020-08-04 14:58:35 +02002621 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2622 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002623
Ronald Cron5425a212020-08-04 14:58:35 +02002624 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002625
Steven Cooreman177deba2020-09-07 17:14:14 +02002626 if( iv->len > 0 )
2627 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002628 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002629 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002630
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002631 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002632 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002633 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002634
Gilles Peskine8817f612018-12-18 00:18:46 +01002635 PSA_ASSERT( psa_cipher_update( &operation,
2636 input->x, input->len,
2637 output, output_buffer_size,
2638 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002639 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002640 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002641 output + total_output_length,
2642 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002643 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002644 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002645 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002646
2647 if( expected_status == PSA_SUCCESS )
2648 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002649 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002650 ASSERT_COMPARE( expected_output->x, expected_output->len,
2651 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002652 }
2653
Gilles Peskine50e586b2018-06-08 14:28:46 +02002654exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002655 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002656 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002657 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002658 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002659}
2660/* END_CASE */
2661
Gilles Peskine50e586b2018-06-08 14:28:46 +02002662/* BEGIN_CASE */
2663void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002664 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002665 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002666{
Ronald Cron5425a212020-08-04 14:58:35 +02002667 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002668 psa_key_type_t key_type = key_type_arg;
2669 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002670 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002671 size_t iv_size = 16;
2672 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002673 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002674 size_t output1_size = 0;
2675 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002676 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002677 size_t output2_size = 0;
2678 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002679 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002680 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2681 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002682 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002683
Gilles Peskine8817f612018-12-18 00:18:46 +01002684 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002685
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002686 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2687 psa_set_key_algorithm( &attributes, alg );
2688 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002689
Ronald Cron5425a212020-08-04 14:58:35 +02002690 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2691 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002692
Ronald Cron5425a212020-08-04 14:58:35 +02002693 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2694 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002695
Steven Cooreman177deba2020-09-07 17:14:14 +02002696 if( alg != PSA_ALG_ECB_NO_PADDING )
2697 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002698 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2699 iv, iv_size,
2700 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002701 }
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002702 output1_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002703 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002704 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002705
Gilles Peskine8817f612018-12-18 00:18:46 +01002706 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2707 output1, output1_size,
2708 &output1_length ) );
2709 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002710 output1 + output1_length,
2711 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002712 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002713
Gilles Peskine048b7f02018-06-08 14:20:49 +02002714 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002715
Gilles Peskine8817f612018-12-18 00:18:46 +01002716 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002717
2718 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002719 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002720
Steven Cooreman177deba2020-09-07 17:14:14 +02002721 if( iv_length > 0 )
2722 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002723 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2724 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002725 }
2726
Gilles Peskine8817f612018-12-18 00:18:46 +01002727 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2728 output2, output2_size,
2729 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002730 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002731 PSA_ASSERT( psa_cipher_finish( &operation2,
2732 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002733 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002734 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002735
Gilles Peskine048b7f02018-06-08 14:20:49 +02002736 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002737
Gilles Peskine8817f612018-12-18 00:18:46 +01002738 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002739
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002740 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002741
2742exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002743 psa_cipher_abort( &operation1 );
2744 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002745 mbedtls_free( output1 );
2746 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002747 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002748 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03002749}
2750/* END_CASE */
2751
2752/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002753void cipher_verify_output_multipart( int alg_arg,
2754 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002755 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002756 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002757 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03002758{
Ronald Cron5425a212020-08-04 14:58:35 +02002759 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002760 psa_key_type_t key_type = key_type_arg;
2761 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002762 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002763 unsigned char iv[16] = {0};
2764 size_t iv_size = 16;
2765 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002766 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002767 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002768 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002769 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002770 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002771 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002772 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002773 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2774 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002775 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002776
Gilles Peskine8817f612018-12-18 00:18:46 +01002777 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002778
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002779 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2780 psa_set_key_algorithm( &attributes, alg );
2781 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002782
Ronald Cron5425a212020-08-04 14:58:35 +02002783 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2784 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03002785
Ronald Cron5425a212020-08-04 14:58:35 +02002786 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2787 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002788
Steven Cooreman177deba2020-09-07 17:14:14 +02002789 if( alg != PSA_ALG_ECB_NO_PADDING )
2790 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002791 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2792 iv, iv_size,
2793 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002794 }
2795
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002796 output1_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002797 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002798 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002799
Gilles Peskinee0866522019-02-19 19:44:00 +01002800 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002801
Gilles Peskine8817f612018-12-18 00:18:46 +01002802 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2803 output1, output1_buffer_size,
2804 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002805 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002806
Gilles Peskine8817f612018-12-18 00:18:46 +01002807 PSA_ASSERT( psa_cipher_update( &operation1,
2808 input->x + first_part_size,
2809 input->len - first_part_size,
2810 output1, output1_buffer_size,
2811 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002812 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002813
Gilles Peskine8817f612018-12-18 00:18:46 +01002814 PSA_ASSERT( psa_cipher_finish( &operation1,
2815 output1 + output1_length,
2816 output1_buffer_size - output1_length,
2817 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002818 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002819
Gilles Peskine8817f612018-12-18 00:18:46 +01002820 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002821
Gilles Peskine048b7f02018-06-08 14:20:49 +02002822 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002823 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002824
Steven Cooreman177deba2020-09-07 17:14:14 +02002825 if( iv_length > 0 )
2826 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002827 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2828 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002829 }
Moran Pekerded84402018-06-06 16:36:50 +03002830
Gilles Peskine8817f612018-12-18 00:18:46 +01002831 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2832 output2, output2_buffer_size,
2833 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002834 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002835
Gilles Peskine8817f612018-12-18 00:18:46 +01002836 PSA_ASSERT( psa_cipher_update( &operation2,
2837 output1 + first_part_size,
2838 output1_length - first_part_size,
2839 output2, output2_buffer_size,
2840 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002841 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002842
Gilles Peskine8817f612018-12-18 00:18:46 +01002843 PSA_ASSERT( psa_cipher_finish( &operation2,
2844 output2 + output2_length,
2845 output2_buffer_size - output2_length,
2846 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002847 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002848
Gilles Peskine8817f612018-12-18 00:18:46 +01002849 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002850
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002851 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002852
2853exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002854 psa_cipher_abort( &operation1 );
2855 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002856 mbedtls_free( output1 );
2857 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002858 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002859 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002860}
2861/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002862
Gilles Peskine20035e32018-02-03 22:44:14 +01002863/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002864void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002865 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002866 data_t *nonce,
2867 data_t *additional_data,
2868 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002869 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002870{
Ronald Cron5425a212020-08-04 14:58:35 +02002871 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002872 psa_key_type_t key_type = key_type_arg;
2873 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002874 unsigned char *output_data = NULL;
2875 size_t output_size = 0;
2876 size_t output_length = 0;
2877 unsigned char *output_data2 = NULL;
2878 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02002879 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002880 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002881 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002882
Gilles Peskine4abf7412018-06-18 16:35:34 +02002883 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02002884 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
2885 * should be exact. */
2886 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
2887 TEST_EQUAL( output_size,
2888 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002889 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002890
Gilles Peskine8817f612018-12-18 00:18:46 +01002891 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002892
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002893 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2894 psa_set_key_algorithm( &attributes, alg );
2895 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002896
Gilles Peskine049c7532019-05-15 20:22:09 +02002897 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002898 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002899
Ronald Cron5425a212020-08-04 14:58:35 +02002900 TEST_EQUAL( psa_aead_encrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01002901 nonce->x, nonce->len,
2902 additional_data->x,
2903 additional_data->len,
2904 input_data->x, input_data->len,
2905 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002906 &output_length ),
2907 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002908
2909 if( PSA_SUCCESS == expected_result )
2910 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002911 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002912
Gilles Peskine003a4a92019-05-14 16:09:40 +02002913 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
2914 * should be exact. */
2915 TEST_EQUAL( input_data->len,
2916 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
2917
Ronald Cron5425a212020-08-04 14:58:35 +02002918 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01002919 nonce->x, nonce->len,
2920 additional_data->x,
2921 additional_data->len,
2922 output_data, output_length,
2923 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002924 &output_length2 ),
2925 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002926
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002927 ASSERT_COMPARE( input_data->x, input_data->len,
2928 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002929 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002930
Gilles Peskinea1cac842018-06-11 19:33:02 +02002931exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002932 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002933 mbedtls_free( output_data );
2934 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002935 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002936}
2937/* END_CASE */
2938
2939/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002940void aead_encrypt( int key_type_arg, data_t *key_data,
2941 int alg_arg,
2942 data_t *nonce,
2943 data_t *additional_data,
2944 data_t *input_data,
2945 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002946{
Ronald Cron5425a212020-08-04 14:58:35 +02002947 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002948 psa_key_type_t key_type = key_type_arg;
2949 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002950 unsigned char *output_data = NULL;
2951 size_t output_size = 0;
2952 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02002953 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002954 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01002955 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002956
Gilles Peskine4abf7412018-06-18 16:35:34 +02002957 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02002958 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
2959 * should be exact. */
2960 TEST_EQUAL( output_size,
2961 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002962 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002963
Gilles Peskine8817f612018-12-18 00:18:46 +01002964 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002965
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002966 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2967 psa_set_key_algorithm( &attributes, alg );
2968 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002969
Gilles Peskine049c7532019-05-15 20:22:09 +02002970 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002971 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002972
Steven Cooremand588ea12021-01-11 19:36:04 +01002973 status = psa_aead_encrypt( key, alg,
2974 nonce->x, nonce->len,
2975 additional_data->x, additional_data->len,
2976 input_data->x, input_data->len,
2977 output_data, output_size,
2978 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002979
Ronald Cron28a45ed2021-02-09 20:35:42 +01002980 /* If the operation is not supported, just skip and not fail in case the
2981 * encryption involves a common limitation of cryptography hardwares and
2982 * an alternative implementation. */
2983 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01002984 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01002985 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
2986 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01002987 }
Steven Cooremand588ea12021-01-11 19:36:04 +01002988
2989 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002990 ASSERT_COMPARE( expected_result->x, expected_result->len,
2991 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002992
Gilles Peskinea1cac842018-06-11 19:33:02 +02002993exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002994 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002995 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002996 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002997}
2998/* END_CASE */
2999
3000/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003001void aead_decrypt( int key_type_arg, data_t *key_data,
3002 int alg_arg,
3003 data_t *nonce,
3004 data_t *additional_data,
3005 data_t *input_data,
3006 data_t *expected_data,
3007 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003008{
Ronald Cron5425a212020-08-04 14:58:35 +02003009 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003010 psa_key_type_t key_type = key_type_arg;
3011 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003012 unsigned char *output_data = NULL;
3013 size_t output_size = 0;
3014 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003015 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003016 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003017 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003018 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003019
Gilles Peskine003a4a92019-05-14 16:09:40 +02003020 output_size = input_data->len - tag_length;
3021 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3022 * should be exact. */
3023 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3024 TEST_EQUAL( output_size,
3025 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003026 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003027
Gilles Peskine8817f612018-12-18 00:18:46 +01003028 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003029
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003030 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3031 psa_set_key_algorithm( &attributes, alg );
3032 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003033
Gilles Peskine049c7532019-05-15 20:22:09 +02003034 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003035 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003036
Steven Cooremand588ea12021-01-11 19:36:04 +01003037 status = psa_aead_decrypt( key, alg,
3038 nonce->x, nonce->len,
3039 additional_data->x,
3040 additional_data->len,
3041 input_data->x, input_data->len,
3042 output_data, output_size,
3043 &output_length );
3044
Ronald Cron28a45ed2021-02-09 20:35:42 +01003045 /* If the operation is not supported, just skip and not fail in case the
3046 * decryption involves a common limitation of cryptography hardwares and
3047 * an alternative implementation. */
3048 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003049 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003050 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3051 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003052 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003053
3054 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003055
Gilles Peskine2d277862018-06-18 15:41:12 +02003056 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003057 ASSERT_COMPARE( expected_data->x, expected_data->len,
3058 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003059
Gilles Peskinea1cac842018-06-11 19:33:02 +02003060exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003061 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003062 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003063 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003064}
3065/* END_CASE */
3066
3067/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003068void signature_size( int type_arg,
3069 int bits,
3070 int alg_arg,
3071 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003072{
3073 psa_key_type_t type = type_arg;
3074 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003075 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003076
Gilles Peskinefe11b722018-12-18 00:24:04 +01003077 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003078#if defined(MBEDTLS_TEST_DEPRECATED)
3079 TEST_EQUAL( actual_size,
3080 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3081#endif /* MBEDTLS_TEST_DEPRECATED */
3082
Gilles Peskinee59236f2018-01-27 23:32:46 +01003083exit:
3084 ;
3085}
3086/* END_CASE */
3087
3088/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003089void sign_deterministic( int key_type_arg, data_t *key_data,
3090 int alg_arg, data_t *input_data,
3091 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003092{
Ronald Cron5425a212020-08-04 14:58:35 +02003093 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003094 psa_key_type_t key_type = key_type_arg;
3095 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003096 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003097 unsigned char *signature = NULL;
3098 size_t signature_size;
3099 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003100 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003101
Gilles Peskine8817f612018-12-18 00:18:46 +01003102 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003103
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003104 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003105 psa_set_key_algorithm( &attributes, alg );
3106 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003107
Gilles Peskine049c7532019-05-15 20:22:09 +02003108 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003109 &key ) );
3110 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003111 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003112
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003113 /* Allocate a buffer which has the size advertized by the
3114 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003115 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003116 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003117 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003118 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003119 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003120
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003121 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003122 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003123 input_data->x, input_data->len,
3124 signature, signature_size,
3125 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003126 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003127 ASSERT_COMPARE( output_data->x, output_data->len,
3128 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003129
Gilles Peskine0627f982019-11-26 19:12:16 +01003130#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003131 memset( signature, 0, signature_size );
3132 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003133 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003134 input_data->x, input_data->len,
3135 signature, signature_size,
3136 &signature_length ) );
3137 ASSERT_COMPARE( output_data->x, output_data->len,
3138 signature, signature_length );
3139#endif /* MBEDTLS_TEST_DEPRECATED */
3140
Gilles Peskine20035e32018-02-03 22:44:14 +01003141exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003142 /*
3143 * Key attributes may have been returned by psa_get_key_attributes()
3144 * thus reset them as required.
3145 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003146 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003147
Ronald Cron5425a212020-08-04 14:58:35 +02003148 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003149 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003150 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003151}
3152/* END_CASE */
3153
3154/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003155void sign_fail( int key_type_arg, data_t *key_data,
3156 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003157 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003158{
Ronald Cron5425a212020-08-04 14:58:35 +02003159 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003160 psa_key_type_t key_type = key_type_arg;
3161 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003162 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003163 psa_status_t actual_status;
3164 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003165 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003166 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003167 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003168
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003169 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003170
Gilles Peskine8817f612018-12-18 00:18:46 +01003171 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003172
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003173 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003174 psa_set_key_algorithm( &attributes, alg );
3175 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003176
Gilles Peskine049c7532019-05-15 20:22:09 +02003177 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003178 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003179
Ronald Cron5425a212020-08-04 14:58:35 +02003180 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003181 input_data->x, input_data->len,
3182 signature, signature_size,
3183 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003184 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003185 /* The value of *signature_length is unspecified on error, but
3186 * whatever it is, it should be less than signature_size, so that
3187 * if the caller tries to read *signature_length bytes without
3188 * checking the error code then they don't overflow a buffer. */
3189 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003190
Gilles Peskine895242b2019-11-29 12:15:40 +01003191#if defined(MBEDTLS_TEST_DEPRECATED)
3192 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003193 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003194 input_data->x, input_data->len,
3195 signature, signature_size,
3196 &signature_length ),
3197 expected_status );
3198 TEST_ASSERT( signature_length <= signature_size );
3199#endif /* MBEDTLS_TEST_DEPRECATED */
3200
Gilles Peskine20035e32018-02-03 22:44:14 +01003201exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003202 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003203 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003204 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003205 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003206}
3207/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003208
3209/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003210void sign_verify( int key_type_arg, data_t *key_data,
3211 int alg_arg, data_t *input_data )
3212{
Ronald Cron5425a212020-08-04 14:58:35 +02003213 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003214 psa_key_type_t key_type = key_type_arg;
3215 psa_algorithm_t alg = alg_arg;
3216 size_t key_bits;
3217 unsigned char *signature = NULL;
3218 size_t signature_size;
3219 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003220 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003221
Gilles Peskine8817f612018-12-18 00:18:46 +01003222 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003223
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003224 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003225 psa_set_key_algorithm( &attributes, alg );
3226 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003227
Gilles Peskine049c7532019-05-15 20:22:09 +02003228 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003229 &key ) );
3230 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003231 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003232
3233 /* Allocate a buffer which has the size advertized by the
3234 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003235 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003236 key_bits, alg );
3237 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003238 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003239 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003240
3241 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003242 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003243 input_data->x, input_data->len,
3244 signature, signature_size,
3245 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003246 /* Check that the signature length looks sensible. */
3247 TEST_ASSERT( signature_length <= signature_size );
3248 TEST_ASSERT( signature_length > 0 );
3249
3250 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003251 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003252 input_data->x, input_data->len,
3253 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003254
3255 if( input_data->len != 0 )
3256 {
3257 /* Flip a bit in the input and verify that the signature is now
3258 * detected as invalid. Flip a bit at the beginning, not at the end,
3259 * because ECDSA may ignore the last few bits of the input. */
3260 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003261 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003262 input_data->x, input_data->len,
3263 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003264 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003265 }
3266
3267exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003268 /*
3269 * Key attributes may have been returned by psa_get_key_attributes()
3270 * thus reset them as required.
3271 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003272 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003273
Ronald Cron5425a212020-08-04 14:58:35 +02003274 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003275 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003276 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003277}
3278/* END_CASE */
3279
3280/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003281void asymmetric_verify( int key_type_arg, data_t *key_data,
3282 int alg_arg, data_t *hash_data,
3283 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003284{
Ronald Cron5425a212020-08-04 14:58:35 +02003285 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003286 psa_key_type_t key_type = key_type_arg;
3287 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003288 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003289
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003290 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003291
Gilles Peskine8817f612018-12-18 00:18:46 +01003292 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003293
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003294 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003295 psa_set_key_algorithm( &attributes, alg );
3296 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003297
Gilles Peskine049c7532019-05-15 20:22:09 +02003298 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003299 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003300
Ronald Cron5425a212020-08-04 14:58:35 +02003301 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003302 hash_data->x, hash_data->len,
3303 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003304
3305#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003306 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003307 hash_data->x, hash_data->len,
3308 signature_data->x,
3309 signature_data->len ) );
3310
3311#endif /* MBEDTLS_TEST_DEPRECATED */
3312
itayzafrir5c753392018-05-08 11:18:38 +03003313exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003314 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003315 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003316 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003317}
3318/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003319
3320/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003321void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3322 int alg_arg, data_t *hash_data,
3323 data_t *signature_data,
3324 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003325{
Ronald Cron5425a212020-08-04 14:58:35 +02003326 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003327 psa_key_type_t key_type = key_type_arg;
3328 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003329 psa_status_t actual_status;
3330 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003331 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003332
Gilles Peskine8817f612018-12-18 00:18:46 +01003333 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003334
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003335 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003336 psa_set_key_algorithm( &attributes, alg );
3337 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003338
Gilles Peskine049c7532019-05-15 20:22:09 +02003339 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003340 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003341
Ronald Cron5425a212020-08-04 14:58:35 +02003342 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003343 hash_data->x, hash_data->len,
3344 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003345 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003346
Gilles Peskine895242b2019-11-29 12:15:40 +01003347#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003348 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003349 hash_data->x, hash_data->len,
3350 signature_data->x, signature_data->len ),
3351 expected_status );
3352#endif /* MBEDTLS_TEST_DEPRECATED */
3353
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003354exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003355 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003356 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003357 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003358}
3359/* END_CASE */
3360
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003361/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003362void asymmetric_encrypt( int key_type_arg,
3363 data_t *key_data,
3364 int alg_arg,
3365 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003366 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003367 int expected_output_length_arg,
3368 int expected_status_arg )
3369{
Ronald Cron5425a212020-08-04 14:58:35 +02003370 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003371 psa_key_type_t key_type = key_type_arg;
3372 psa_algorithm_t alg = alg_arg;
3373 size_t expected_output_length = expected_output_length_arg;
3374 size_t key_bits;
3375 unsigned char *output = NULL;
3376 size_t output_size;
3377 size_t output_length = ~0;
3378 psa_status_t actual_status;
3379 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003380 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003381
Gilles Peskine8817f612018-12-18 00:18:46 +01003382 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003383
Gilles Peskine656896e2018-06-29 19:12:28 +02003384 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003385 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3386 psa_set_key_algorithm( &attributes, alg );
3387 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003388 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003389 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003390
3391 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003392 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003393 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02003394 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003395 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003396
3397 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003398 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003399 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003400 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003401 output, output_size,
3402 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003403 TEST_EQUAL( actual_status, expected_status );
3404 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003405
Gilles Peskine68428122018-06-30 18:42:41 +02003406 /* If the label is empty, the test framework puts a non-null pointer
3407 * in label->x. Test that a null pointer works as well. */
3408 if( label->len == 0 )
3409 {
3410 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003411 if( output_size != 0 )
3412 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003413 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003414 input_data->x, input_data->len,
3415 NULL, label->len,
3416 output, output_size,
3417 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003418 TEST_EQUAL( actual_status, expected_status );
3419 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003420 }
3421
Gilles Peskine656896e2018-06-29 19:12:28 +02003422exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003423 /*
3424 * Key attributes may have been returned by psa_get_key_attributes()
3425 * thus reset them as required.
3426 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003427 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003428
Ronald Cron5425a212020-08-04 14:58:35 +02003429 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02003430 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003431 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003432}
3433/* END_CASE */
3434
3435/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003436void asymmetric_encrypt_decrypt( int key_type_arg,
3437 data_t *key_data,
3438 int alg_arg,
3439 data_t *input_data,
3440 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003441{
Ronald Cron5425a212020-08-04 14:58:35 +02003442 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003443 psa_key_type_t key_type = key_type_arg;
3444 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003445 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003446 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003447 size_t output_size;
3448 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003449 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003450 size_t output2_size;
3451 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003452 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003453
Gilles Peskine8817f612018-12-18 00:18:46 +01003454 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003455
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003456 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3457 psa_set_key_algorithm( &attributes, alg );
3458 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003459
Gilles Peskine049c7532019-05-15 20:22:09 +02003460 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003461 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003462
3463 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02003464 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003465 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003466 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003467 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003468 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003469 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003470
Gilles Peskineeebd7382018-06-08 18:11:54 +02003471 /* We test encryption by checking that encrypt-then-decrypt gives back
3472 * the original plaintext because of the non-optional random
3473 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02003474 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003475 input_data->x, input_data->len,
3476 label->x, label->len,
3477 output, output_size,
3478 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003479 /* We don't know what ciphertext length to expect, but check that
3480 * it looks sensible. */
3481 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003482
Ronald Cron5425a212020-08-04 14:58:35 +02003483 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003484 output, output_length,
3485 label->x, label->len,
3486 output2, output2_size,
3487 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003488 ASSERT_COMPARE( input_data->x, input_data->len,
3489 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003490
3491exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003492 /*
3493 * Key attributes may have been returned by psa_get_key_attributes()
3494 * thus reset them as required.
3495 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003496 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003497
Ronald Cron5425a212020-08-04 14:58:35 +02003498 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003499 mbedtls_free( output );
3500 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003501 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003502}
3503/* END_CASE */
3504
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003505/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003506void asymmetric_decrypt( int key_type_arg,
3507 data_t *key_data,
3508 int alg_arg,
3509 data_t *input_data,
3510 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003511 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003512{
Ronald Cron5425a212020-08-04 14:58:35 +02003513 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003514 psa_key_type_t key_type = key_type_arg;
3515 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003516 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003517 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003518 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003519 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003520
Jaeden Amero412654a2019-02-06 12:57:46 +00003521 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003522 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003523
Gilles Peskine8817f612018-12-18 00:18:46 +01003524 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003525
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003526 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3527 psa_set_key_algorithm( &attributes, alg );
3528 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003529
Gilles Peskine049c7532019-05-15 20:22:09 +02003530 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003531 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003532
Ronald Cron5425a212020-08-04 14:58:35 +02003533 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003534 input_data->x, input_data->len,
3535 label->x, label->len,
3536 output,
3537 output_size,
3538 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003539 ASSERT_COMPARE( expected_data->x, expected_data->len,
3540 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003541
Gilles Peskine68428122018-06-30 18:42:41 +02003542 /* If the label is empty, the test framework puts a non-null pointer
3543 * in label->x. Test that a null pointer works as well. */
3544 if( label->len == 0 )
3545 {
3546 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003547 if( output_size != 0 )
3548 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003549 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003550 input_data->x, input_data->len,
3551 NULL, label->len,
3552 output,
3553 output_size,
3554 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003555 ASSERT_COMPARE( expected_data->x, expected_data->len,
3556 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003557 }
3558
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003559exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003560 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003561 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003562 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003563 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003564}
3565/* END_CASE */
3566
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003567/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003568void asymmetric_decrypt_fail( int key_type_arg,
3569 data_t *key_data,
3570 int alg_arg,
3571 data_t *input_data,
3572 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00003573 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02003574 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003575{
Ronald Cron5425a212020-08-04 14:58:35 +02003576 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003577 psa_key_type_t key_type = key_type_arg;
3578 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003579 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00003580 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003581 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003582 psa_status_t actual_status;
3583 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003584 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003585
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003586 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003587
Gilles Peskine8817f612018-12-18 00:18:46 +01003588 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003589
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003590 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3591 psa_set_key_algorithm( &attributes, alg );
3592 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003593
Gilles Peskine049c7532019-05-15 20:22:09 +02003594 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003595 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003596
Ronald Cron5425a212020-08-04 14:58:35 +02003597 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003598 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003599 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003600 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003601 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003602 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003603 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003604
Gilles Peskine68428122018-06-30 18:42:41 +02003605 /* If the label is empty, the test framework puts a non-null pointer
3606 * in label->x. Test that a null pointer works as well. */
3607 if( label->len == 0 )
3608 {
3609 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003610 if( output_size != 0 )
3611 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003612 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003613 input_data->x, input_data->len,
3614 NULL, label->len,
3615 output, output_size,
3616 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003617 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003618 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003619 }
3620
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003621exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003622 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003623 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02003624 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003625 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003626}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003627/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003628
3629/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003630void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00003631{
3632 /* Test each valid way of initializing the object, except for `= {0}`, as
3633 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3634 * though it's OK by the C standard. We could test for this, but we'd need
3635 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003636 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003637 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
3638 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
3639 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00003640
3641 memset( &zero, 0, sizeof( zero ) );
3642
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003643 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003644 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003645 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003646 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003647 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003648 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003649 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003650
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003651 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003652 PSA_ASSERT( psa_key_derivation_abort(&func) );
3653 PSA_ASSERT( psa_key_derivation_abort(&init) );
3654 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00003655}
3656/* END_CASE */
3657
Janos Follath16de4a42019-06-13 16:32:24 +01003658/* BEGIN_CASE */
3659void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02003660{
Gilles Peskineea0fb492018-07-12 17:17:20 +02003661 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003662 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003663 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003664
Gilles Peskine8817f612018-12-18 00:18:46 +01003665 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003666
Janos Follath16de4a42019-06-13 16:32:24 +01003667 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003668 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003669
3670exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003671 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003672 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003673}
3674/* END_CASE */
3675
Janos Follathaf3c2a02019-06-12 12:34:34 +01003676/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01003677void derive_set_capacity( int alg_arg, int capacity_arg,
3678 int expected_status_arg )
3679{
3680 psa_algorithm_t alg = alg_arg;
3681 size_t capacity = capacity_arg;
3682 psa_status_t expected_status = expected_status_arg;
3683 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
3684
3685 PSA_ASSERT( psa_crypto_init( ) );
3686
3687 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
3688
3689 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
3690 expected_status );
3691
3692exit:
3693 psa_key_derivation_abort( &operation );
3694 PSA_DONE( );
3695}
3696/* END_CASE */
3697
3698/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01003699void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02003700 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01003701 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02003702 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01003703 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02003704 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003705 int expected_status_arg3,
3706 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01003707{
3708 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02003709 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
3710 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01003711 psa_status_t expected_statuses[] = {expected_status_arg1,
3712 expected_status_arg2,
3713 expected_status_arg3};
3714 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02003715 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
3716 MBEDTLS_SVC_KEY_ID_INIT,
3717 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01003718 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
3719 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3720 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003721 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02003722 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003723 psa_status_t expected_output_status = expected_output_status_arg;
3724 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01003725
3726 PSA_ASSERT( psa_crypto_init( ) );
3727
3728 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
3729 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01003730
3731 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
3732
3733 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
3734 {
Gilles Peskineb8965192019-09-24 16:21:10 +02003735 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01003736 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02003737 psa_set_key_type( &attributes, key_types[i] );
3738 PSA_ASSERT( psa_import_key( &attributes,
3739 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003740 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02003741 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
3742 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
3743 {
3744 // When taking a private key as secret input, use key agreement
3745 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01003746 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
3747 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02003748 expected_statuses[i] );
3749 }
3750 else
3751 {
3752 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02003753 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02003754 expected_statuses[i] );
3755 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02003756 }
3757 else
3758 {
3759 TEST_EQUAL( psa_key_derivation_input_bytes(
3760 &operation, steps[i],
3761 inputs[i]->x, inputs[i]->len ),
3762 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01003763 }
3764 }
3765
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003766 if( output_key_type != PSA_KEY_TYPE_NONE )
3767 {
3768 psa_reset_key_attributes( &attributes );
3769 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
3770 psa_set_key_bits( &attributes, 8 );
3771 actual_output_status =
3772 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02003773 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003774 }
3775 else
3776 {
3777 uint8_t buffer[1];
3778 actual_output_status =
3779 psa_key_derivation_output_bytes( &operation,
3780 buffer, sizeof( buffer ) );
3781 }
3782 TEST_EQUAL( actual_output_status, expected_output_status );
3783
Janos Follathaf3c2a02019-06-12 12:34:34 +01003784exit:
3785 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003786 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
3787 psa_destroy_key( keys[i] );
3788 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01003789 PSA_DONE( );
3790}
3791/* END_CASE */
3792
Janos Follathd958bb72019-07-03 15:02:16 +01003793/* BEGIN_CASE */
3794void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003795{
Janos Follathd958bb72019-07-03 15:02:16 +01003796 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02003797 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003798 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003799 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01003800 unsigned char input1[] = "Input 1";
3801 size_t input1_length = sizeof( input1 );
3802 unsigned char input2[] = "Input 2";
3803 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003804 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003805 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003806 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3807 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3808 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003809 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003810
Gilles Peskine8817f612018-12-18 00:18:46 +01003811 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003812
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003813 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
3814 psa_set_key_algorithm( &attributes, alg );
3815 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003816
Gilles Peskine73676cb2019-05-15 20:15:10 +02003817 PSA_ASSERT( psa_import_key( &attributes,
3818 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02003819 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003820
3821 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01003822 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
3823 input1, input1_length,
3824 input2, input2_length,
3825 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01003826 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003827
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003828 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01003829 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003830 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003831
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003832 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003833
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003834 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02003835 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003836
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003837exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003838 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003839 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003840 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003841}
3842/* END_CASE */
3843
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003844/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003845void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003846{
3847 uint8_t output_buffer[16];
3848 size_t buffer_size = 16;
3849 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003850 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003851
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003852 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
3853 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003854 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003855
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003856 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003857 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003858
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003859 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003860
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003861 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
3862 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003863 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003864
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003865 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003866 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003867
3868exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003869 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003870}
3871/* END_CASE */
3872
3873/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003874void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02003875 int step1_arg, data_t *input1,
3876 int step2_arg, data_t *input2,
3877 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003878 int requested_capacity_arg,
3879 data_t *expected_output1,
3880 data_t *expected_output2 )
3881{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003882 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02003883 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
3884 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02003885 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
3886 MBEDTLS_SVC_KEY_ID_INIT,
3887 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003888 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003889 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003890 uint8_t *expected_outputs[2] =
3891 {expected_output1->x, expected_output2->x};
3892 size_t output_sizes[2] =
3893 {expected_output1->len, expected_output2->len};
3894 size_t output_buffer_size = 0;
3895 uint8_t *output_buffer = NULL;
3896 size_t expected_capacity;
3897 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02003898 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003899 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02003900 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003901
3902 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3903 {
3904 if( output_sizes[i] > output_buffer_size )
3905 output_buffer_size = output_sizes[i];
3906 if( output_sizes[i] == 0 )
3907 expected_outputs[i] = NULL;
3908 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003909 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003910 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003911
Gilles Peskineff5f0e72019-04-18 12:53:30 +02003912 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
3913 psa_set_key_algorithm( &attributes, alg );
3914 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003915
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003916 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02003917 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
3918 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
3919 requested_capacity ) );
3920 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003921 {
Gilles Peskine1468da72019-05-29 17:35:49 +02003922 switch( steps[i] )
3923 {
3924 case 0:
3925 break;
3926 case PSA_KEY_DERIVATION_INPUT_SECRET:
3927 PSA_ASSERT( psa_import_key( &attributes,
3928 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003929 &keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02003930 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02003931 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02003932 break;
3933 default:
3934 PSA_ASSERT( psa_key_derivation_input_bytes(
3935 &operation, steps[i],
3936 inputs[i]->x, inputs[i]->len ) );
3937 break;
3938 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003939 }
Gilles Peskine1468da72019-05-29 17:35:49 +02003940
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003941 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003942 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003943 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003944 expected_capacity = requested_capacity;
3945
3946 /* Expansion phase. */
3947 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3948 {
3949 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003950 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003951 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003952 if( expected_capacity == 0 && output_sizes[i] == 0 )
3953 {
3954 /* Reading 0 bytes when 0 bytes are available can go either way. */
3955 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02003956 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003957 continue;
3958 }
3959 else if( expected_capacity == 0 ||
3960 output_sizes[i] > expected_capacity )
3961 {
3962 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02003963 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003964 expected_capacity = 0;
3965 continue;
3966 }
3967 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003968 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003969 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003970 ASSERT_COMPARE( output_buffer, output_sizes[i],
3971 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003972 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003973 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003974 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003975 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003976 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003977 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003978 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003979
3980exit:
3981 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003982 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003983 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
3984 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003985 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003986}
3987/* END_CASE */
3988
3989/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003990void derive_full( int alg_arg,
3991 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01003992 data_t *input1,
3993 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02003994 int requested_capacity_arg )
3995{
Ronald Cron5425a212020-08-04 14:58:35 +02003996 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02003997 psa_algorithm_t alg = alg_arg;
3998 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003999 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004000 unsigned char output_buffer[16];
4001 size_t expected_capacity = requested_capacity;
4002 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004003 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004004
Gilles Peskine8817f612018-12-18 00:18:46 +01004005 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004006
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004007 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4008 psa_set_key_algorithm( &attributes, alg );
4009 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004010
Gilles Peskine049c7532019-05-15 20:22:09 +02004011 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004012 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004013
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004014 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4015 input1->x, input1->len,
4016 input2->x, input2->len,
4017 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004018 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004019
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004020 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004021 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004022 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004023
4024 /* Expansion phase. */
4025 while( current_capacity > 0 )
4026 {
4027 size_t read_size = sizeof( output_buffer );
4028 if( read_size > current_capacity )
4029 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004030 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004031 output_buffer,
4032 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004033 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004034 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004035 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004036 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004037 }
4038
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004039 /* Check that the operation refuses to go over capacity. */
4040 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004041 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004042
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004043 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004044
4045exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004046 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004047 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004048 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004049}
4050/* END_CASE */
4051
Janos Follathe60c9052019-07-03 13:51:30 +01004052/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004053void derive_key_exercise( int alg_arg,
4054 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004055 data_t *input1,
4056 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004057 int derived_type_arg,
4058 int derived_bits_arg,
4059 int derived_usage_arg,
4060 int derived_alg_arg )
4061{
Ronald Cron5425a212020-08-04 14:58:35 +02004062 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4063 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004064 psa_algorithm_t alg = alg_arg;
4065 psa_key_type_t derived_type = derived_type_arg;
4066 size_t derived_bits = derived_bits_arg;
4067 psa_key_usage_t derived_usage = derived_usage_arg;
4068 psa_algorithm_t derived_alg = derived_alg_arg;
4069 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004070 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004071 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004072 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004073
Gilles Peskine8817f612018-12-18 00:18:46 +01004074 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004075
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004076 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4077 psa_set_key_algorithm( &attributes, alg );
4078 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004079 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004080 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004081
4082 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004083 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4084 input1->x, input1->len,
4085 input2->x, input2->len,
4086 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004087 goto exit;
4088
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004089 psa_set_key_usage_flags( &attributes, derived_usage );
4090 psa_set_key_algorithm( &attributes, derived_alg );
4091 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004092 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004093 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004094 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004095
4096 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004097 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004098 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4099 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004100
4101 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004102 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004103 goto exit;
4104
4105exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004106 /*
4107 * Key attributes may have been returned by psa_get_key_attributes()
4108 * thus reset them as required.
4109 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004110 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004111
4112 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004113 psa_destroy_key( base_key );
4114 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004115 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004116}
4117/* END_CASE */
4118
Janos Follath42fd8882019-07-03 14:17:09 +01004119/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004120void derive_key_export( int alg_arg,
4121 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004122 data_t *input1,
4123 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004124 int bytes1_arg,
4125 int bytes2_arg )
4126{
Ronald Cron5425a212020-08-04 14:58:35 +02004127 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4128 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004129 psa_algorithm_t alg = alg_arg;
4130 size_t bytes1 = bytes1_arg;
4131 size_t bytes2 = bytes2_arg;
4132 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004133 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004134 uint8_t *output_buffer = NULL;
4135 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004136 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4137 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004138 size_t length;
4139
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004140 ASSERT_ALLOC( output_buffer, capacity );
4141 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004142 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004143
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004144 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4145 psa_set_key_algorithm( &base_attributes, alg );
4146 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004147 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004148 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004149
4150 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004151 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4152 input1->x, input1->len,
4153 input2->x, input2->len,
4154 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004155 goto exit;
4156
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004157 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004158 output_buffer,
4159 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004160 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004161
4162 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004163 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4164 input1->x, input1->len,
4165 input2->x, input2->len,
4166 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004167 goto exit;
4168
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004169 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4170 psa_set_key_algorithm( &derived_attributes, 0 );
4171 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004172 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004173 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004174 &derived_key ) );
4175 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004176 export_buffer, bytes1,
4177 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004178 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004179 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004180 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004181 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004182 &derived_key ) );
4183 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004184 export_buffer + bytes1, bytes2,
4185 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004186 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004187
4188 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004189 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4190 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004191
4192exit:
4193 mbedtls_free( output_buffer );
4194 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004195 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004196 psa_destroy_key( base_key );
4197 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004198 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004199}
4200/* END_CASE */
4201
4202/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004203void derive_key( int alg_arg,
4204 data_t *key_data, data_t *input1, data_t *input2,
4205 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004206 int expected_status_arg,
4207 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004208{
Ronald Cron5425a212020-08-04 14:58:35 +02004209 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4210 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004211 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004212 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004213 size_t bits = bits_arg;
4214 psa_status_t expected_status = expected_status_arg;
4215 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4216 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4217 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4218
4219 PSA_ASSERT( psa_crypto_init( ) );
4220
4221 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4222 psa_set_key_algorithm( &base_attributes, alg );
4223 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4224 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004225 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004226
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004227 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4228 input1->x, input1->len,
4229 input2->x, input2->len,
4230 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004231 goto exit;
4232
4233 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4234 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004235 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004236 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004237
4238 psa_status_t status =
4239 psa_key_derivation_output_key( &derived_attributes,
4240 &operation,
4241 &derived_key );
4242 if( is_large_output > 0 )
4243 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4244 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004245
4246exit:
4247 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004248 psa_destroy_key( base_key );
4249 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004250 PSA_DONE( );
4251}
4252/* END_CASE */
4253
4254/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004255void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004256 int our_key_type_arg, int our_key_alg_arg,
4257 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004258 int expected_status_arg )
4259{
Ronald Cron5425a212020-08-04 14:58:35 +02004260 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004261 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004262 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004263 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004264 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004265 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004266 psa_status_t expected_status = expected_status_arg;
4267 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004268
Gilles Peskine8817f612018-12-18 00:18:46 +01004269 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004270
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004271 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004272 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004273 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004274 PSA_ASSERT( psa_import_key( &attributes,
4275 our_key_data->x, our_key_data->len,
4276 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004277
Gilles Peskine77f40d82019-04-11 21:27:06 +02004278 /* The tests currently include inputs that should fail at either step.
4279 * Test cases that fail at the setup step should be changed to call
4280 * key_derivation_setup instead, and this function should be renamed
4281 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004282 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004283 if( status == PSA_SUCCESS )
4284 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004285 TEST_EQUAL( psa_key_derivation_key_agreement(
4286 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4287 our_key,
4288 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004289 expected_status );
4290 }
4291 else
4292 {
4293 TEST_ASSERT( status == expected_status );
4294 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004295
4296exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004297 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004298 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004299 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004300}
4301/* END_CASE */
4302
4303/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004304void raw_key_agreement( int alg_arg,
4305 int our_key_type_arg, data_t *our_key_data,
4306 data_t *peer_key_data,
4307 data_t *expected_output )
4308{
Ronald Cron5425a212020-08-04 14:58:35 +02004309 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004310 psa_algorithm_t alg = alg_arg;
4311 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004312 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004313 unsigned char *output = NULL;
4314 size_t output_length = ~0;
4315
4316 ASSERT_ALLOC( output, expected_output->len );
4317 PSA_ASSERT( psa_crypto_init( ) );
4318
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004319 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4320 psa_set_key_algorithm( &attributes, alg );
4321 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004322 PSA_ASSERT( psa_import_key( &attributes,
4323 our_key_data->x, our_key_data->len,
4324 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004325
Gilles Peskinebe697d82019-05-16 18:00:41 +02004326 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4327 peer_key_data->x, peer_key_data->len,
4328 output, expected_output->len,
4329 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004330 ASSERT_COMPARE( output, output_length,
4331 expected_output->x, expected_output->len );
4332
4333exit:
4334 mbedtls_free( output );
4335 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004336 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004337}
4338/* END_CASE */
4339
4340/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004341void key_agreement_capacity( int alg_arg,
4342 int our_key_type_arg, data_t *our_key_data,
4343 data_t *peer_key_data,
4344 int expected_capacity_arg )
4345{
Ronald Cron5425a212020-08-04 14:58:35 +02004346 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004347 psa_algorithm_t alg = alg_arg;
4348 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004349 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004350 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004351 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004352 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004353
Gilles Peskine8817f612018-12-18 00:18:46 +01004354 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004355
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004356 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4357 psa_set_key_algorithm( &attributes, alg );
4358 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004359 PSA_ASSERT( psa_import_key( &attributes,
4360 our_key_data->x, our_key_data->len,
4361 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004362
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004363 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004364 PSA_ASSERT( psa_key_derivation_key_agreement(
4365 &operation,
4366 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4367 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004368 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4369 {
4370 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004371 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004372 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004373 NULL, 0 ) );
4374 }
Gilles Peskine59685592018-09-18 12:11:34 +02004375
Gilles Peskinebf491972018-10-25 22:36:12 +02004376 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004377 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004378 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004379 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004380
Gilles Peskinebf491972018-10-25 22:36:12 +02004381 /* Test the actual capacity by reading the output. */
4382 while( actual_capacity > sizeof( output ) )
4383 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004384 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004385 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004386 actual_capacity -= sizeof( output );
4387 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004388 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004389 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004390 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004391 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004392
Gilles Peskine59685592018-09-18 12:11:34 +02004393exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004394 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004395 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004396 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004397}
4398/* END_CASE */
4399
4400/* BEGIN_CASE */
4401void key_agreement_output( int alg_arg,
4402 int our_key_type_arg, data_t *our_key_data,
4403 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004404 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004405{
Ronald Cron5425a212020-08-04 14:58:35 +02004406 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004407 psa_algorithm_t alg = alg_arg;
4408 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004409 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004410 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004411 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004412
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004413 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4414 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004415
Gilles Peskine8817f612018-12-18 00:18:46 +01004416 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004417
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004418 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4419 psa_set_key_algorithm( &attributes, alg );
4420 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004421 PSA_ASSERT( psa_import_key( &attributes,
4422 our_key_data->x, our_key_data->len,
4423 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004424
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004425 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004426 PSA_ASSERT( psa_key_derivation_key_agreement(
4427 &operation,
4428 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4429 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004430 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4431 {
4432 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004433 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004434 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004435 NULL, 0 ) );
4436 }
Gilles Peskine59685592018-09-18 12:11:34 +02004437
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004438 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004439 actual_output,
4440 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004441 ASSERT_COMPARE( actual_output, expected_output1->len,
4442 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004443 if( expected_output2->len != 0 )
4444 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004445 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004446 actual_output,
4447 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004448 ASSERT_COMPARE( actual_output, expected_output2->len,
4449 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004450 }
Gilles Peskine59685592018-09-18 12:11:34 +02004451
4452exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004453 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004454 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004455 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004456 mbedtls_free( actual_output );
4457}
4458/* END_CASE */
4459
4460/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004461void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004462{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004463 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004464 unsigned char *output = NULL;
4465 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004466 size_t i;
4467 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004468
Simon Butcher49f8e312020-03-03 15:51:50 +00004469 TEST_ASSERT( bytes_arg >= 0 );
4470
Gilles Peskine91892022021-02-08 19:50:26 +01004471 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004472 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02004473
Gilles Peskine8817f612018-12-18 00:18:46 +01004474 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004475
Gilles Peskinea50d7392018-06-21 10:22:13 +02004476 /* Run several times, to ensure that every output byte will be
4477 * nonzero at least once with overwhelming probability
4478 * (2^(-8*number_of_runs)). */
4479 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004480 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004481 if( bytes != 0 )
4482 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004483 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004484
Gilles Peskinea50d7392018-06-21 10:22:13 +02004485 for( i = 0; i < bytes; i++ )
4486 {
4487 if( output[i] != 0 )
4488 ++changed[i];
4489 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004490 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004491
4492 /* Check that every byte was changed to nonzero at least once. This
4493 * validates that psa_generate_random is overwriting every byte of
4494 * the output buffer. */
4495 for( i = 0; i < bytes; i++ )
4496 {
4497 TEST_ASSERT( changed[i] != 0 );
4498 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004499
4500exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004501 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004502 mbedtls_free( output );
4503 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004504}
4505/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004506
4507/* BEGIN_CASE */
4508void generate_key( int type_arg,
4509 int bits_arg,
4510 int usage_arg,
4511 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004512 int expected_status_arg,
4513 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004514{
Ronald Cron5425a212020-08-04 14:58:35 +02004515 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004516 psa_key_type_t type = type_arg;
4517 psa_key_usage_t usage = usage_arg;
4518 size_t bits = bits_arg;
4519 psa_algorithm_t alg = alg_arg;
4520 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004521 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004522 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004523
Gilles Peskine8817f612018-12-18 00:18:46 +01004524 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004525
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004526 psa_set_key_usage_flags( &attributes, usage );
4527 psa_set_key_algorithm( &attributes, alg );
4528 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004529 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004530
4531 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01004532 psa_status_t status = psa_generate_key( &attributes, &key );
4533
4534 if( is_large_key > 0 )
4535 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4536 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004537 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004538 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004539
4540 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004541 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004542 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
4543 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004544
Gilles Peskine818ca122018-06-20 18:16:48 +02004545 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004546 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004547 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004548
4549exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004550 /*
4551 * Key attributes may have been returned by psa_get_key_attributes()
4552 * thus reset them as required.
4553 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004554 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004555
Ronald Cron5425a212020-08-04 14:58:35 +02004556 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004557 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004558}
4559/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004560
Gilles Peskinee56e8782019-04-26 17:34:02 +02004561/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
4562void generate_key_rsa( int bits_arg,
4563 data_t *e_arg,
4564 int expected_status_arg )
4565{
Ronald Cron5425a212020-08-04 14:58:35 +02004566 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004567 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02004568 size_t bits = bits_arg;
4569 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
4570 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
4571 psa_status_t expected_status = expected_status_arg;
4572 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4573 uint8_t *exported = NULL;
4574 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004575 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004576 size_t exported_length = SIZE_MAX;
4577 uint8_t *e_read_buffer = NULL;
4578 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02004579 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004580 size_t e_read_length = SIZE_MAX;
4581
4582 if( e_arg->len == 0 ||
4583 ( e_arg->len == 3 &&
4584 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
4585 {
4586 is_default_public_exponent = 1;
4587 e_read_size = 0;
4588 }
4589 ASSERT_ALLOC( e_read_buffer, e_read_size );
4590 ASSERT_ALLOC( exported, exported_size );
4591
4592 PSA_ASSERT( psa_crypto_init( ) );
4593
4594 psa_set_key_usage_flags( &attributes, usage );
4595 psa_set_key_algorithm( &attributes, alg );
4596 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
4597 e_arg->x, e_arg->len ) );
4598 psa_set_key_bits( &attributes, bits );
4599
4600 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02004601 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004602 if( expected_status != PSA_SUCCESS )
4603 goto exit;
4604
4605 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004606 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004607 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4608 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4609 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
4610 e_read_buffer, e_read_size,
4611 &e_read_length ) );
4612 if( is_default_public_exponent )
4613 TEST_EQUAL( e_read_length, 0 );
4614 else
4615 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
4616
4617 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004618 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02004619 goto exit;
4620
4621 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02004622 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02004623 exported, exported_size,
4624 &exported_length ) );
4625 {
4626 uint8_t *p = exported;
4627 uint8_t *end = exported + exported_length;
4628 size_t len;
4629 /* RSAPublicKey ::= SEQUENCE {
4630 * modulus INTEGER, -- n
4631 * publicExponent INTEGER } -- e
4632 */
4633 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004634 MBEDTLS_ASN1_SEQUENCE |
4635 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01004636 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004637 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
4638 MBEDTLS_ASN1_INTEGER ) );
4639 if( len >= 1 && p[0] == 0 )
4640 {
4641 ++p;
4642 --len;
4643 }
4644 if( e_arg->len == 0 )
4645 {
4646 TEST_EQUAL( len, 3 );
4647 TEST_EQUAL( p[0], 1 );
4648 TEST_EQUAL( p[1], 0 );
4649 TEST_EQUAL( p[2], 1 );
4650 }
4651 else
4652 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
4653 }
4654
4655exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004656 /*
4657 * Key attributes may have been returned by psa_get_key_attributes() or
4658 * set by psa_set_key_domain_parameters() thus reset them as required.
4659 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02004660 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004661
Ronald Cron5425a212020-08-04 14:58:35 +02004662 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004663 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004664 mbedtls_free( e_read_buffer );
4665 mbedtls_free( exported );
4666}
4667/* END_CASE */
4668
Darryl Greend49a4992018-06-18 17:27:26 +01004669/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004670void persistent_key_load_key_from_storage( data_t *data,
4671 int type_arg, int bits_arg,
4672 int usage_flags_arg, int alg_arg,
4673 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01004674{
Ronald Cron71016a92020-08-28 19:01:50 +02004675 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004676 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02004677 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4678 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004679 psa_key_type_t type = type_arg;
4680 size_t bits = bits_arg;
4681 psa_key_usage_t usage_flags = usage_flags_arg;
4682 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004683 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004684 unsigned char *first_export = NULL;
4685 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004686 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004687 size_t first_exported_length;
4688 size_t second_exported_length;
4689
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004690 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4691 {
4692 ASSERT_ALLOC( first_export, export_size );
4693 ASSERT_ALLOC( second_export, export_size );
4694 }
Darryl Greend49a4992018-06-18 17:27:26 +01004695
Gilles Peskine8817f612018-12-18 00:18:46 +01004696 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004697
Gilles Peskinec87af662019-05-15 16:12:22 +02004698 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004699 psa_set_key_usage_flags( &attributes, usage_flags );
4700 psa_set_key_algorithm( &attributes, alg );
4701 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004702 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004703
Darryl Green0c6575a2018-11-07 16:05:30 +00004704 switch( generation_method )
4705 {
4706 case IMPORT_KEY:
4707 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02004708 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004709 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004710 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004711
Darryl Green0c6575a2018-11-07 16:05:30 +00004712 case GENERATE_KEY:
4713 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02004714 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004715 break;
4716
4717 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01004718#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004719 {
4720 /* Create base key */
4721 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
4722 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4723 psa_set_key_usage_flags( &base_attributes,
4724 PSA_KEY_USAGE_DERIVE );
4725 psa_set_key_algorithm( &base_attributes, derive_alg );
4726 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004727 PSA_ASSERT( psa_import_key( &base_attributes,
4728 data->x, data->len,
4729 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004730 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004731 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004732 PSA_ASSERT( psa_key_derivation_input_key(
4733 &operation,
4734 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004735 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004736 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004737 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004738 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
4739 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004740 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004741 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004742 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02004743 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004744 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01004745#else
4746 TEST_ASSUME( ! "KDF not supported in this configuration" );
4747#endif
4748 break;
4749
4750 default:
4751 TEST_ASSERT( ! "generation_method not implemented in test" );
4752 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00004753 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004754 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01004755
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004756 /* Export the key if permitted by the key policy. */
4757 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4758 {
Ronald Cron5425a212020-08-04 14:58:35 +02004759 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004760 first_export, export_size,
4761 &first_exported_length ) );
4762 if( generation_method == IMPORT_KEY )
4763 ASSERT_COMPARE( data->x, data->len,
4764 first_export, first_exported_length );
4765 }
Darryl Greend49a4992018-06-18 17:27:26 +01004766
4767 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02004768 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004769 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01004770 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004771
Darryl Greend49a4992018-06-18 17:27:26 +01004772 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02004773 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02004774 TEST_ASSERT( mbedtls_svc_key_id_equal(
4775 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004776 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
4777 PSA_KEY_LIFETIME_PERSISTENT );
4778 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4779 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4780 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
4781 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004782
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004783 /* Export the key again if permitted by the key policy. */
4784 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00004785 {
Ronald Cron5425a212020-08-04 14:58:35 +02004786 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004787 second_export, export_size,
4788 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004789 ASSERT_COMPARE( first_export, first_exported_length,
4790 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00004791 }
4792
4793 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004794 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004795 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004796
4797exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004798 /*
4799 * Key attributes may have been returned by psa_get_key_attributes()
4800 * thus reset them as required.
4801 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004802 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004803
Darryl Greend49a4992018-06-18 17:27:26 +01004804 mbedtls_free( first_export );
4805 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004806 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004807 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02004808 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004809 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01004810}
4811/* END_CASE */