blob: a6b0b84e850726c0564966f4961a8212653023f4 [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
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001341 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001342 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001343 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001344 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001345
Ronald Cron5425a212020-08-04 14:58:35 +02001346 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001347
1348exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001349 /*
1350 * Source and target key attributes may have been returned by
1351 * psa_get_key_attributes() thus reset them as required.
1352 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001353 psa_reset_key_attributes( &source_attributes );
1354 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001355
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001356 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001357 mbedtls_free( export_buffer );
1358}
1359/* END_CASE */
1360
1361/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001362void copy_fail( int source_usage_arg,
1363 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001364 int type_arg, data_t *material,
1365 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001366 int target_usage_arg,
1367 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001368 int expected_status_arg )
1369{
1370 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1371 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001372 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1373 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine4a644642019-05-03 17:14:08 +02001374
1375 PSA_ASSERT( psa_crypto_init( ) );
1376
1377 /* Prepare the source key. */
1378 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1379 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001380 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001381 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001382 PSA_ASSERT( psa_import_key( &source_attributes,
1383 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001384 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001385
1386 /* Prepare the target attributes. */
1387 psa_set_key_type( &target_attributes, target_type_arg );
1388 psa_set_key_bits( &target_attributes, target_bits_arg );
1389 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1390 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001391 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001392
1393 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001394 TEST_EQUAL( psa_copy_key( source_key,
1395 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001396 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001397
Ronald Cron5425a212020-08-04 14:58:35 +02001398 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001399
Gilles Peskine4a644642019-05-03 17:14:08 +02001400exit:
1401 psa_reset_key_attributes( &source_attributes );
1402 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001403 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001404}
1405/* END_CASE */
1406
1407/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001408void hash_operation_init( )
1409{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001410 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001411 /* Test each valid way of initializing the object, except for `= {0}`, as
1412 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1413 * though it's OK by the C standard. We could test for this, but we'd need
1414 * to supress the Clang warning for the test. */
1415 psa_hash_operation_t func = psa_hash_operation_init( );
1416 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1417 psa_hash_operation_t zero;
1418
1419 memset( &zero, 0, sizeof( zero ) );
1420
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001421 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001422 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1423 PSA_ERROR_BAD_STATE );
1424 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1425 PSA_ERROR_BAD_STATE );
1426 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1427 PSA_ERROR_BAD_STATE );
1428
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001429 /* A default hash operation should be abortable without error. */
1430 PSA_ASSERT( psa_hash_abort( &func ) );
1431 PSA_ASSERT( psa_hash_abort( &init ) );
1432 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001433}
1434/* END_CASE */
1435
1436/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001437void hash_setup( int alg_arg,
1438 int expected_status_arg )
1439{
1440 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001441 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001442 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001443 psa_status_t status;
1444
Gilles Peskine8817f612018-12-18 00:18:46 +01001445 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001446
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001447 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001448 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001449
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001450 /* Whether setup succeeded or failed, abort must succeed. */
1451 PSA_ASSERT( psa_hash_abort( &operation ) );
1452
1453 /* If setup failed, reproduce the failure, so as to
1454 * test the resulting state of the operation object. */
1455 if( status != PSA_SUCCESS )
1456 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1457
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001458 /* Now the operation object should be reusable. */
1459#if defined(KNOWN_SUPPORTED_HASH_ALG)
1460 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1461 PSA_ASSERT( psa_hash_abort( &operation ) );
1462#endif
1463
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001464exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001465 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001466}
1467/* END_CASE */
1468
1469/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001470void hash_compute_fail( int alg_arg, data_t *input,
1471 int output_size_arg, int expected_status_arg )
1472{
1473 psa_algorithm_t alg = alg_arg;
1474 uint8_t *output = NULL;
1475 size_t output_size = output_size_arg;
1476 size_t output_length = INVALID_EXPORT_LENGTH;
1477 psa_status_t expected_status = expected_status_arg;
1478 psa_status_t status;
1479
1480 ASSERT_ALLOC( output, output_size );
1481
1482 PSA_ASSERT( psa_crypto_init( ) );
1483
1484 status = psa_hash_compute( alg, input->x, input->len,
1485 output, output_size, &output_length );
1486 TEST_EQUAL( status, expected_status );
1487 TEST_ASSERT( output_length <= output_size );
1488
1489exit:
1490 mbedtls_free( output );
1491 PSA_DONE( );
1492}
1493/* END_CASE */
1494
1495/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001496void hash_compare_fail( int alg_arg, data_t *input,
1497 data_t *reference_hash,
1498 int expected_status_arg )
1499{
1500 psa_algorithm_t alg = alg_arg;
1501 psa_status_t expected_status = expected_status_arg;
1502 psa_status_t status;
1503
1504 PSA_ASSERT( psa_crypto_init( ) );
1505
1506 status = psa_hash_compare( alg, input->x, input->len,
1507 reference_hash->x, reference_hash->len );
1508 TEST_EQUAL( status, expected_status );
1509
1510exit:
1511 PSA_DONE( );
1512}
1513/* END_CASE */
1514
1515/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001516void hash_compute_compare( int alg_arg, data_t *input,
1517 data_t *expected_output )
1518{
1519 psa_algorithm_t alg = alg_arg;
1520 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1521 size_t output_length = INVALID_EXPORT_LENGTH;
1522 size_t i;
1523
1524 PSA_ASSERT( psa_crypto_init( ) );
1525
1526 /* Compute with tight buffer */
1527 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001528 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001529 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001530 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001531 ASSERT_COMPARE( output, output_length,
1532 expected_output->x, expected_output->len );
1533
1534 /* Compute with larger buffer */
1535 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1536 output, sizeof( output ),
1537 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001538 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001539 ASSERT_COMPARE( output, output_length,
1540 expected_output->x, expected_output->len );
1541
1542 /* Compare with correct hash */
1543 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1544 output, output_length ) );
1545
1546 /* Compare with trailing garbage */
1547 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1548 output, output_length + 1 ),
1549 PSA_ERROR_INVALID_SIGNATURE );
1550
1551 /* Compare with truncated hash */
1552 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1553 output, output_length - 1 ),
1554 PSA_ERROR_INVALID_SIGNATURE );
1555
1556 /* Compare with corrupted value */
1557 for( i = 0; i < output_length; i++ )
1558 {
Chris Jones9634bb12021-01-20 15:56:42 +00001559 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001560 output[i] ^= 1;
1561 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1562 output, output_length ),
1563 PSA_ERROR_INVALID_SIGNATURE );
1564 output[i] ^= 1;
1565 }
1566
1567exit:
1568 PSA_DONE( );
1569}
1570/* END_CASE */
1571
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001572/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001573void hash_bad_order( )
1574{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001575 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001576 unsigned char input[] = "";
1577 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001578 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001579 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1580 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1581 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001582 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001583 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001584 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001585
Gilles Peskine8817f612018-12-18 00:18:46 +01001586 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001587
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001588 /* Call setup twice in a row. */
1589 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1590 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1591 PSA_ERROR_BAD_STATE );
1592 PSA_ASSERT( psa_hash_abort( &operation ) );
1593
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001594 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001595 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001596 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001597 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001598
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001599 /* Call update after finish. */
1600 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1601 PSA_ASSERT( psa_hash_finish( &operation,
1602 hash, sizeof( hash ), &hash_len ) );
1603 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001604 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001605 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001606
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001607 /* Call verify without calling setup beforehand. */
1608 TEST_EQUAL( psa_hash_verify( &operation,
1609 valid_hash, sizeof( valid_hash ) ),
1610 PSA_ERROR_BAD_STATE );
1611 PSA_ASSERT( psa_hash_abort( &operation ) );
1612
1613 /* Call verify after finish. */
1614 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1615 PSA_ASSERT( psa_hash_finish( &operation,
1616 hash, sizeof( hash ), &hash_len ) );
1617 TEST_EQUAL( psa_hash_verify( &operation,
1618 valid_hash, sizeof( valid_hash ) ),
1619 PSA_ERROR_BAD_STATE );
1620 PSA_ASSERT( psa_hash_abort( &operation ) );
1621
1622 /* Call verify twice in a row. */
1623 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1624 PSA_ASSERT( psa_hash_verify( &operation,
1625 valid_hash, sizeof( valid_hash ) ) );
1626 TEST_EQUAL( psa_hash_verify( &operation,
1627 valid_hash, sizeof( valid_hash ) ),
1628 PSA_ERROR_BAD_STATE );
1629 PSA_ASSERT( psa_hash_abort( &operation ) );
1630
1631 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001632 TEST_EQUAL( psa_hash_finish( &operation,
1633 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001634 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001635 PSA_ASSERT( psa_hash_abort( &operation ) );
1636
1637 /* Call finish twice in a row. */
1638 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1639 PSA_ASSERT( psa_hash_finish( &operation,
1640 hash, sizeof( hash ), &hash_len ) );
1641 TEST_EQUAL( psa_hash_finish( &operation,
1642 hash, sizeof( hash ), &hash_len ),
1643 PSA_ERROR_BAD_STATE );
1644 PSA_ASSERT( psa_hash_abort( &operation ) );
1645
1646 /* Call finish after calling verify. */
1647 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1648 PSA_ASSERT( psa_hash_verify( &operation,
1649 valid_hash, sizeof( valid_hash ) ) );
1650 TEST_EQUAL( psa_hash_finish( &operation,
1651 hash, sizeof( hash ), &hash_len ),
1652 PSA_ERROR_BAD_STATE );
1653 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001654
1655exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001656 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001657}
1658/* END_CASE */
1659
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001660/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001661void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001662{
1663 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001664 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1665 * appended to it */
1666 unsigned char hash[] = {
1667 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1668 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1669 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001670 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001671 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001672
Gilles Peskine8817f612018-12-18 00:18:46 +01001673 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001674
itayzafrir27e69452018-11-01 14:26:34 +02001675 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001676 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001677 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001678 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001679
itayzafrir27e69452018-11-01 14:26:34 +02001680 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001681 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001682 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001683 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001684
itayzafrir27e69452018-11-01 14:26:34 +02001685 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001686 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001687 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001688 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001689
itayzafrirec93d302018-10-18 18:01:10 +03001690exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001691 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001692}
1693/* END_CASE */
1694
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001695/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1696void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001697{
1698 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001699 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001700 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001701 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001702 size_t hash_len;
1703
Gilles Peskine8817f612018-12-18 00:18:46 +01001704 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001705
itayzafrir58028322018-10-25 10:22:01 +03001706 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001707 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001708 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001709 hash, expected_size - 1, &hash_len ),
1710 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001711
1712exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001713 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001714}
1715/* END_CASE */
1716
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001717/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1718void hash_clone_source_state( )
1719{
1720 psa_algorithm_t alg = PSA_ALG_SHA_256;
1721 unsigned char hash[PSA_HASH_MAX_SIZE];
1722 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1723 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1724 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1725 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1726 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1727 size_t hash_len;
1728
1729 PSA_ASSERT( psa_crypto_init( ) );
1730 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1731
1732 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1733 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1734 PSA_ASSERT( psa_hash_finish( &op_finished,
1735 hash, sizeof( hash ), &hash_len ) );
1736 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1737 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1738
1739 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1740 PSA_ERROR_BAD_STATE );
1741
1742 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1743 PSA_ASSERT( psa_hash_finish( &op_init,
1744 hash, sizeof( hash ), &hash_len ) );
1745 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1746 PSA_ASSERT( psa_hash_finish( &op_finished,
1747 hash, sizeof( hash ), &hash_len ) );
1748 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1749 PSA_ASSERT( psa_hash_finish( &op_aborted,
1750 hash, sizeof( hash ), &hash_len ) );
1751
1752exit:
1753 psa_hash_abort( &op_source );
1754 psa_hash_abort( &op_init );
1755 psa_hash_abort( &op_setup );
1756 psa_hash_abort( &op_finished );
1757 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001758 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001759}
1760/* END_CASE */
1761
1762/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1763void hash_clone_target_state( )
1764{
1765 psa_algorithm_t alg = PSA_ALG_SHA_256;
1766 unsigned char hash[PSA_HASH_MAX_SIZE];
1767 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1768 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1769 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1770 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1771 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1772 size_t hash_len;
1773
1774 PSA_ASSERT( psa_crypto_init( ) );
1775
1776 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1777 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1778 PSA_ASSERT( psa_hash_finish( &op_finished,
1779 hash, sizeof( hash ), &hash_len ) );
1780 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1781 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1782
1783 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1784 PSA_ASSERT( psa_hash_finish( &op_target,
1785 hash, sizeof( hash ), &hash_len ) );
1786
1787 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1788 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1789 PSA_ERROR_BAD_STATE );
1790 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1791 PSA_ERROR_BAD_STATE );
1792
1793exit:
1794 psa_hash_abort( &op_target );
1795 psa_hash_abort( &op_init );
1796 psa_hash_abort( &op_setup );
1797 psa_hash_abort( &op_finished );
1798 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001799 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001800}
1801/* END_CASE */
1802
itayzafrir58028322018-10-25 10:22:01 +03001803/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001804void mac_operation_init( )
1805{
Jaeden Amero252ef282019-02-15 14:05:35 +00001806 const uint8_t input[1] = { 0 };
1807
Jaeden Amero769ce272019-01-04 11:48:03 +00001808 /* Test each valid way of initializing the object, except for `= {0}`, as
1809 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1810 * though it's OK by the C standard. We could test for this, but we'd need
1811 * to supress the Clang warning for the test. */
1812 psa_mac_operation_t func = psa_mac_operation_init( );
1813 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1814 psa_mac_operation_t zero;
1815
1816 memset( &zero, 0, sizeof( zero ) );
1817
Jaeden Amero252ef282019-02-15 14:05:35 +00001818 /* A freshly-initialized MAC operation should not be usable. */
1819 TEST_EQUAL( psa_mac_update( &func,
1820 input, sizeof( input ) ),
1821 PSA_ERROR_BAD_STATE );
1822 TEST_EQUAL( psa_mac_update( &init,
1823 input, sizeof( input ) ),
1824 PSA_ERROR_BAD_STATE );
1825 TEST_EQUAL( psa_mac_update( &zero,
1826 input, sizeof( input ) ),
1827 PSA_ERROR_BAD_STATE );
1828
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001829 /* A default MAC operation should be abortable without error. */
1830 PSA_ASSERT( psa_mac_abort( &func ) );
1831 PSA_ASSERT( psa_mac_abort( &init ) );
1832 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001833}
1834/* END_CASE */
1835
1836/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001837void mac_setup( int key_type_arg,
1838 data_t *key,
1839 int alg_arg,
1840 int expected_status_arg )
1841{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001842 psa_key_type_t key_type = key_type_arg;
1843 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001844 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001845 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001846 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1847#if defined(KNOWN_SUPPORTED_MAC_ALG)
1848 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1849#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001850
Gilles Peskine8817f612018-12-18 00:18:46 +01001851 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001852
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001853 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1854 &operation, &status ) )
1855 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001856 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001857
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001858 /* The operation object should be reusable. */
1859#if defined(KNOWN_SUPPORTED_MAC_ALG)
1860 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1861 smoke_test_key_data,
1862 sizeof( smoke_test_key_data ),
1863 KNOWN_SUPPORTED_MAC_ALG,
1864 &operation, &status ) )
1865 goto exit;
1866 TEST_EQUAL( status, PSA_SUCCESS );
1867#endif
1868
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001869exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001870 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001871}
1872/* END_CASE */
1873
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001874/* 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 +00001875void mac_bad_order( )
1876{
Ronald Cron5425a212020-08-04 14:58:35 +02001877 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001878 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1879 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001880 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001881 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1882 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1883 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001884 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001885 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1886 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1887 size_t sign_mac_length = 0;
1888 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1889 const uint8_t verify_mac[] = {
1890 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1891 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1892 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1893
1894 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001895 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001896 psa_set_key_algorithm( &attributes, alg );
1897 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001898
Ronald Cron5425a212020-08-04 14:58:35 +02001899 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1900 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001901
Jaeden Amero252ef282019-02-15 14:05:35 +00001902 /* Call update without calling setup beforehand. */
1903 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1904 PSA_ERROR_BAD_STATE );
1905 PSA_ASSERT( psa_mac_abort( &operation ) );
1906
1907 /* Call sign finish without calling setup beforehand. */
1908 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1909 &sign_mac_length),
1910 PSA_ERROR_BAD_STATE );
1911 PSA_ASSERT( psa_mac_abort( &operation ) );
1912
1913 /* Call verify finish without calling setup beforehand. */
1914 TEST_EQUAL( psa_mac_verify_finish( &operation,
1915 verify_mac, sizeof( verify_mac ) ),
1916 PSA_ERROR_BAD_STATE );
1917 PSA_ASSERT( psa_mac_abort( &operation ) );
1918
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001919 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001920 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
1921 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001922 PSA_ERROR_BAD_STATE );
1923 PSA_ASSERT( psa_mac_abort( &operation ) );
1924
Jaeden Amero252ef282019-02-15 14:05:35 +00001925 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001926 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001927 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1928 PSA_ASSERT( psa_mac_sign_finish( &operation,
1929 sign_mac, sizeof( sign_mac ),
1930 &sign_mac_length ) );
1931 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1932 PSA_ERROR_BAD_STATE );
1933 PSA_ASSERT( psa_mac_abort( &operation ) );
1934
1935 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001936 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001937 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1938 PSA_ASSERT( psa_mac_verify_finish( &operation,
1939 verify_mac, sizeof( verify_mac ) ) );
1940 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1941 PSA_ERROR_BAD_STATE );
1942 PSA_ASSERT( psa_mac_abort( &operation ) );
1943
1944 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001945 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001946 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1947 PSA_ASSERT( psa_mac_sign_finish( &operation,
1948 sign_mac, sizeof( sign_mac ),
1949 &sign_mac_length ) );
1950 TEST_EQUAL( psa_mac_sign_finish( &operation,
1951 sign_mac, sizeof( sign_mac ),
1952 &sign_mac_length ),
1953 PSA_ERROR_BAD_STATE );
1954 PSA_ASSERT( psa_mac_abort( &operation ) );
1955
1956 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001957 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001958 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1959 PSA_ASSERT( psa_mac_verify_finish( &operation,
1960 verify_mac, sizeof( verify_mac ) ) );
1961 TEST_EQUAL( psa_mac_verify_finish( &operation,
1962 verify_mac, sizeof( verify_mac ) ),
1963 PSA_ERROR_BAD_STATE );
1964 PSA_ASSERT( psa_mac_abort( &operation ) );
1965
1966 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02001967 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001968 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1969 TEST_EQUAL( psa_mac_verify_finish( &operation,
1970 verify_mac, sizeof( verify_mac ) ),
1971 PSA_ERROR_BAD_STATE );
1972 PSA_ASSERT( psa_mac_abort( &operation ) );
1973
1974 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02001975 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001976 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1977 TEST_EQUAL( psa_mac_sign_finish( &operation,
1978 sign_mac, sizeof( sign_mac ),
1979 &sign_mac_length ),
1980 PSA_ERROR_BAD_STATE );
1981 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001982
Ronald Cron5425a212020-08-04 14:58:35 +02001983 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001984
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001985exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001986 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001987}
1988/* END_CASE */
1989
1990/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001991void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02001992 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001993 int alg_arg,
1994 data_t *input,
1995 data_t *expected_mac )
1996{
Ronald Cron5425a212020-08-04 14:58:35 +02001997 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001998 psa_key_type_t key_type = key_type_arg;
1999 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002000 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002001 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002002 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002003 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002004 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002005 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002006 const size_t output_sizes_to_test[] = {
2007 0,
2008 1,
2009 expected_mac->len - 1,
2010 expected_mac->len,
2011 expected_mac->len + 1,
2012 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002013
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002014 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002015 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002016 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002017
Gilles Peskine8817f612018-12-18 00:18:46 +01002018 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002019
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002020 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002021 psa_set_key_algorithm( &attributes, alg );
2022 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002023
Ronald Cron5425a212020-08-04 14:58:35 +02002024 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2025 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002026
Gilles Peskine8b356b52020-08-25 23:44:59 +02002027 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2028 {
2029 const size_t output_size = output_sizes_to_test[i];
2030 psa_status_t expected_status =
2031 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2032 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002033
Chris Jones9634bb12021-01-20 15:56:42 +00002034 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002035 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002036
Gilles Peskine8b356b52020-08-25 23:44:59 +02002037 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002038 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002039 PSA_ASSERT( psa_mac_update( &operation,
2040 input->x, input->len ) );
2041 TEST_EQUAL( psa_mac_sign_finish( &operation,
2042 actual_mac, output_size,
2043 &mac_length ),
2044 expected_status );
2045 PSA_ASSERT( psa_mac_abort( &operation ) );
2046
2047 if( expected_status == PSA_SUCCESS )
2048 {
2049 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2050 actual_mac, mac_length );
2051 }
2052 mbedtls_free( actual_mac );
2053 actual_mac = NULL;
2054 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002055
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002056exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002057 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002058 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002059 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002060 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002061}
2062/* END_CASE */
2063
2064/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002065void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002066 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002067 int alg_arg,
2068 data_t *input,
2069 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002070{
Ronald Cron5425a212020-08-04 14:58:35 +02002071 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002072 psa_key_type_t key_type = key_type_arg;
2073 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002074 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002075 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002076 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002077
Gilles Peskine69c12672018-06-28 00:07:19 +02002078 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2079
Gilles Peskine8817f612018-12-18 00:18:46 +01002080 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002081
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002082 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002083 psa_set_key_algorithm( &attributes, alg );
2084 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002085
Ronald Cron5425a212020-08-04 14:58:35 +02002086 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2087 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002088
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002089 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002090 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002091 PSA_ASSERT( psa_mac_update( &operation,
2092 input->x, input->len ) );
2093 PSA_ASSERT( psa_mac_verify_finish( &operation,
2094 expected_mac->x,
2095 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002096
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002097 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002098 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002099 PSA_ASSERT( psa_mac_update( &operation,
2100 input->x, input->len ) );
2101 TEST_EQUAL( psa_mac_verify_finish( &operation,
2102 expected_mac->x,
2103 expected_mac->len - 1 ),
2104 PSA_ERROR_INVALID_SIGNATURE );
2105
2106 /* Test a MAC that's too long. */
2107 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2108 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002109 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002110 PSA_ASSERT( psa_mac_update( &operation,
2111 input->x, input->len ) );
2112 TEST_EQUAL( psa_mac_verify_finish( &operation,
2113 perturbed_mac,
2114 expected_mac->len + 1 ),
2115 PSA_ERROR_INVALID_SIGNATURE );
2116
2117 /* Test changing one byte. */
2118 for( size_t i = 0; i < expected_mac->len; i++ )
2119 {
Chris Jones9634bb12021-01-20 15:56:42 +00002120 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002121 perturbed_mac[i] ^= 1;
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 ),
2128 PSA_ERROR_INVALID_SIGNATURE );
2129 perturbed_mac[i] ^= 1;
2130 }
2131
Gilles Peskine8c9def32018-02-08 10:02:12 +01002132exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002133 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002134 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002135 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002136 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002137}
2138/* END_CASE */
2139
2140/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002141void cipher_operation_init( )
2142{
Jaeden Ameroab439972019-02-15 14:12:05 +00002143 const uint8_t input[1] = { 0 };
2144 unsigned char output[1] = { 0 };
2145 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002146 /* Test each valid way of initializing the object, except for `= {0}`, as
2147 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2148 * though it's OK by the C standard. We could test for this, but we'd need
2149 * to supress the Clang warning for the test. */
2150 psa_cipher_operation_t func = psa_cipher_operation_init( );
2151 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2152 psa_cipher_operation_t zero;
2153
2154 memset( &zero, 0, sizeof( zero ) );
2155
Jaeden Ameroab439972019-02-15 14:12:05 +00002156 /* A freshly-initialized cipher operation should not be usable. */
2157 TEST_EQUAL( psa_cipher_update( &func,
2158 input, sizeof( input ),
2159 output, sizeof( output ),
2160 &output_length ),
2161 PSA_ERROR_BAD_STATE );
2162 TEST_EQUAL( psa_cipher_update( &init,
2163 input, sizeof( input ),
2164 output, sizeof( output ),
2165 &output_length ),
2166 PSA_ERROR_BAD_STATE );
2167 TEST_EQUAL( psa_cipher_update( &zero,
2168 input, sizeof( input ),
2169 output, sizeof( output ),
2170 &output_length ),
2171 PSA_ERROR_BAD_STATE );
2172
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002173 /* A default cipher operation should be abortable without error. */
2174 PSA_ASSERT( psa_cipher_abort( &func ) );
2175 PSA_ASSERT( psa_cipher_abort( &init ) );
2176 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002177}
2178/* END_CASE */
2179
2180/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002181void cipher_setup( int key_type_arg,
2182 data_t *key,
2183 int alg_arg,
2184 int expected_status_arg )
2185{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002186 psa_key_type_t key_type = key_type_arg;
2187 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002188 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002189 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002190 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002191#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002192 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2193#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002194
Gilles Peskine8817f612018-12-18 00:18:46 +01002195 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002196
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002197 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2198 &operation, &status ) )
2199 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002200 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002201
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002202 /* The operation object should be reusable. */
2203#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2204 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2205 smoke_test_key_data,
2206 sizeof( smoke_test_key_data ),
2207 KNOWN_SUPPORTED_CIPHER_ALG,
2208 &operation, &status ) )
2209 goto exit;
2210 TEST_EQUAL( status, PSA_SUCCESS );
2211#endif
2212
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002213exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002214 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002215 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002216}
2217/* END_CASE */
2218
Steven Cooreman29eecbf2021-01-28 19:41:25 +01002219/* BEGIN_CASE depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002220void cipher_bad_order( )
2221{
Ronald Cron5425a212020-08-04 14:58:35 +02002222 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002223 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2224 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002225 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002226 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002227 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002228 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002229 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2230 0xaa, 0xaa, 0xaa, 0xaa };
2231 const uint8_t text[] = {
2232 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2233 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002234 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002235 size_t length = 0;
2236
2237 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002238 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2239 psa_set_key_algorithm( &attributes, alg );
2240 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002241 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2242 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002243
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002244 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002245 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2246 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002247 PSA_ERROR_BAD_STATE );
2248 PSA_ASSERT( psa_cipher_abort( &operation ) );
2249
2250 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002251 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2252 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002253 PSA_ERROR_BAD_STATE );
2254 PSA_ASSERT( psa_cipher_abort( &operation ) );
2255
Jaeden Ameroab439972019-02-15 14:12:05 +00002256 /* Generate an IV without calling setup beforehand. */
2257 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2258 buffer, sizeof( buffer ),
2259 &length ),
2260 PSA_ERROR_BAD_STATE );
2261 PSA_ASSERT( psa_cipher_abort( &operation ) );
2262
2263 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002264 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002265 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2266 buffer, sizeof( buffer ),
2267 &length ) );
2268 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2269 buffer, sizeof( buffer ),
2270 &length ),
2271 PSA_ERROR_BAD_STATE );
2272 PSA_ASSERT( psa_cipher_abort( &operation ) );
2273
2274 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002275 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002276 PSA_ASSERT( psa_cipher_set_iv( &operation,
2277 iv, sizeof( iv ) ) );
2278 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2279 buffer, sizeof( buffer ),
2280 &length ),
2281 PSA_ERROR_BAD_STATE );
2282 PSA_ASSERT( psa_cipher_abort( &operation ) );
2283
2284 /* Set an IV without calling setup beforehand. */
2285 TEST_EQUAL( psa_cipher_set_iv( &operation,
2286 iv, sizeof( iv ) ),
2287 PSA_ERROR_BAD_STATE );
2288 PSA_ASSERT( psa_cipher_abort( &operation ) );
2289
2290 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002291 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002292 PSA_ASSERT( psa_cipher_set_iv( &operation,
2293 iv, sizeof( iv ) ) );
2294 TEST_EQUAL( psa_cipher_set_iv( &operation,
2295 iv, sizeof( iv ) ),
2296 PSA_ERROR_BAD_STATE );
2297 PSA_ASSERT( psa_cipher_abort( &operation ) );
2298
2299 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002300 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002301 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2302 buffer, sizeof( buffer ),
2303 &length ) );
2304 TEST_EQUAL( psa_cipher_set_iv( &operation,
2305 iv, sizeof( iv ) ),
2306 PSA_ERROR_BAD_STATE );
2307 PSA_ASSERT( psa_cipher_abort( &operation ) );
2308
2309 /* Call update without calling setup beforehand. */
2310 TEST_EQUAL( psa_cipher_update( &operation,
2311 text, sizeof( text ),
2312 buffer, sizeof( buffer ),
2313 &length ),
2314 PSA_ERROR_BAD_STATE );
2315 PSA_ASSERT( psa_cipher_abort( &operation ) );
2316
2317 /* Call update without an IV where an IV is required. */
2318 TEST_EQUAL( psa_cipher_update( &operation,
2319 text, sizeof( text ),
2320 buffer, sizeof( buffer ),
2321 &length ),
2322 PSA_ERROR_BAD_STATE );
2323 PSA_ASSERT( psa_cipher_abort( &operation ) );
2324
2325 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002326 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002327 PSA_ASSERT( psa_cipher_set_iv( &operation,
2328 iv, sizeof( iv ) ) );
2329 PSA_ASSERT( psa_cipher_finish( &operation,
2330 buffer, sizeof( buffer ), &length ) );
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 finish without calling setup beforehand. */
2339 TEST_EQUAL( psa_cipher_finish( &operation,
2340 buffer, sizeof( buffer ), &length ),
2341 PSA_ERROR_BAD_STATE );
2342 PSA_ASSERT( psa_cipher_abort( &operation ) );
2343
2344 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002345 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002346 /* Not calling update means we are encrypting an empty buffer, which is OK
2347 * for cipher modes with padding. */
2348 TEST_EQUAL( psa_cipher_finish( &operation,
2349 buffer, sizeof( buffer ), &length ),
2350 PSA_ERROR_BAD_STATE );
2351 PSA_ASSERT( psa_cipher_abort( &operation ) );
2352
2353 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002354 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002355 PSA_ASSERT( psa_cipher_set_iv( &operation,
2356 iv, sizeof( iv ) ) );
2357 PSA_ASSERT( psa_cipher_finish( &operation,
2358 buffer, sizeof( buffer ), &length ) );
2359 TEST_EQUAL( psa_cipher_finish( &operation,
2360 buffer, sizeof( buffer ), &length ),
2361 PSA_ERROR_BAD_STATE );
2362 PSA_ASSERT( psa_cipher_abort( &operation ) );
2363
Ronald Cron5425a212020-08-04 14:58:35 +02002364 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002365
Jaeden Ameroab439972019-02-15 14:12:05 +00002366exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002367 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002368 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002369}
2370/* END_CASE */
2371
2372/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002373void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002374 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002375 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002376 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002377{
Ronald Cron5425a212020-08-04 14:58:35 +02002378 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002379 psa_status_t status;
2380 psa_key_type_t key_type = key_type_arg;
2381 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002382 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002383 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002384 size_t output_buffer_size = 0;
2385 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002386 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002387 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002388 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002389
Gilles Peskine8817f612018-12-18 00:18:46 +01002390 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002391
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002392 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2393 psa_set_key_algorithm( &attributes, alg );
2394 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002395
Ronald Cron5425a212020-08-04 14:58:35 +02002396 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2397 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002398
Ronald Cron5425a212020-08-04 14:58:35 +02002399 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002400
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002401 if( iv->len > 0 )
2402 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002403 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002404 }
2405
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002406 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002407 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002408 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002409
Gilles Peskine8817f612018-12-18 00:18:46 +01002410 PSA_ASSERT( psa_cipher_update( &operation,
2411 input->x, input->len,
2412 output, output_buffer_size,
2413 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002414 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002415 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002416 output + total_output_length,
2417 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002418 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002419 total_output_length += function_output_length;
2420
Gilles Peskinefe11b722018-12-18 00:24:04 +01002421 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002422 if( expected_status == PSA_SUCCESS )
2423 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002424 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002425 ASSERT_COMPARE( expected_output->x, expected_output->len,
2426 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002427 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002428
Gilles Peskine50e586b2018-06-08 14:28:46 +02002429exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002430 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002431 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002432 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002433 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002434}
2435/* END_CASE */
2436
2437/* BEGIN_CASE */
2438void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002439 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002440 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002441 int first_part_size_arg,
2442 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002443 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002444{
Ronald Cron5425a212020-08-04 14:58:35 +02002445 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002446 psa_key_type_t key_type = key_type_arg;
2447 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002448 size_t first_part_size = first_part_size_arg;
2449 size_t output1_length = output1_length_arg;
2450 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002451 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002452 size_t output_buffer_size = 0;
2453 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002454 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002455 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002456 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002457
Gilles Peskine8817f612018-12-18 00:18:46 +01002458 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002459
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002460 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2461 psa_set_key_algorithm( &attributes, alg );
2462 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002463
Ronald Cron5425a212020-08-04 14:58:35 +02002464 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2465 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002466
Ronald Cron5425a212020-08-04 14:58:35 +02002467 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002468
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002469 if( iv->len > 0 )
2470 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002471 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002472 }
2473
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002474 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002475 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002476 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002477
Gilles Peskinee0866522019-02-19 19:44:00 +01002478 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002479 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2480 output, output_buffer_size,
2481 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002482 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002483 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002484 PSA_ASSERT( psa_cipher_update( &operation,
2485 input->x + first_part_size,
2486 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002487 output + total_output_length,
2488 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002489 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002490 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002491 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002492 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002493 output + total_output_length,
2494 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002495 &function_output_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_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002498
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002499 ASSERT_COMPARE( expected_output->x, expected_output->len,
2500 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002501
2502exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002503 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002504 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002505 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002506 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002507}
2508/* END_CASE */
2509
2510/* BEGIN_CASE */
2511void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002512 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002513 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002514 int first_part_size_arg,
2515 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002516 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002517{
Ronald Cron5425a212020-08-04 14:58:35 +02002518 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002519 psa_key_type_t key_type = key_type_arg;
2520 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002521 size_t first_part_size = first_part_size_arg;
2522 size_t output1_length = output1_length_arg;
2523 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002524 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002525 size_t output_buffer_size = 0;
2526 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002527 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002528 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002529 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002530
Gilles Peskine8817f612018-12-18 00:18:46 +01002531 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002532
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002533 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2534 psa_set_key_algorithm( &attributes, alg );
2535 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002536
Ronald Cron5425a212020-08-04 14:58:35 +02002537 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2538 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002539
Ronald Cron5425a212020-08-04 14:58:35 +02002540 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002541
Steven Cooreman177deba2020-09-07 17:14:14 +02002542 if( iv->len > 0 )
2543 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002544 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002545 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002546
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002547 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002548 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002549 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002550
Gilles Peskinee0866522019-02-19 19:44:00 +01002551 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002552 PSA_ASSERT( psa_cipher_update( &operation,
2553 input->x, first_part_size,
2554 output, output_buffer_size,
2555 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002556 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002557 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002558 PSA_ASSERT( psa_cipher_update( &operation,
2559 input->x + first_part_size,
2560 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002561 output + total_output_length,
2562 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002563 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002564 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002565 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002566 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002567 output + total_output_length,
2568 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002569 &function_output_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_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002572
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002573 ASSERT_COMPARE( expected_output->x, expected_output->len,
2574 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002575
2576exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002577 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002578 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002579 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002580 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002581}
2582/* END_CASE */
2583
Gilles Peskine50e586b2018-06-08 14:28:46 +02002584/* BEGIN_CASE */
2585void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002586 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002587 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002588 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002589{
Ronald Cron5425a212020-08-04 14:58:35 +02002590 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002591 psa_status_t status;
2592 psa_key_type_t key_type = key_type_arg;
2593 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002594 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002595 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002596 size_t output_buffer_size = 0;
2597 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002598 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002599 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002600 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002601
Gilles Peskine8817f612018-12-18 00:18:46 +01002602 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002603
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002604 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2605 psa_set_key_algorithm( &attributes, alg );
2606 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002607
Ronald Cron5425a212020-08-04 14:58:35 +02002608 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2609 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002610
Ronald Cron5425a212020-08-04 14:58:35 +02002611 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002612
Steven Cooreman177deba2020-09-07 17:14:14 +02002613 if( iv->len > 0 )
2614 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002615 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002616 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002617
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002618 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002619 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002620 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002621
Gilles Peskine8817f612018-12-18 00:18:46 +01002622 PSA_ASSERT( psa_cipher_update( &operation,
2623 input->x, input->len,
2624 output, output_buffer_size,
2625 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002626 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002627 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002628 output + total_output_length,
2629 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002630 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002631 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002632 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002633
2634 if( expected_status == PSA_SUCCESS )
2635 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002636 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002637 ASSERT_COMPARE( expected_output->x, expected_output->len,
2638 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002639 }
2640
Gilles Peskine50e586b2018-06-08 14:28:46 +02002641exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002642 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002643 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002644 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002645 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002646}
2647/* END_CASE */
2648
Gilles Peskine50e586b2018-06-08 14:28:46 +02002649/* BEGIN_CASE */
2650void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002651 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002652 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002653{
Ronald Cron5425a212020-08-04 14:58:35 +02002654 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002655 psa_key_type_t key_type = key_type_arg;
2656 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002657 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002658 size_t iv_size = 16;
2659 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002660 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002661 size_t output1_size = 0;
2662 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002663 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002664 size_t output2_size = 0;
2665 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002666 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002667 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2668 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002669 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002670
Gilles Peskine8817f612018-12-18 00:18:46 +01002671 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002672
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002673 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2674 psa_set_key_algorithm( &attributes, alg );
2675 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002676
Ronald Cron5425a212020-08-04 14:58:35 +02002677 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2678 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002679
Ronald Cron5425a212020-08-04 14:58:35 +02002680 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2681 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002682
Steven Cooreman177deba2020-09-07 17:14:14 +02002683 if( alg != PSA_ALG_ECB_NO_PADDING )
2684 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002685 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2686 iv, iv_size,
2687 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002688 }
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002689 output1_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002690 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002691 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002692
Gilles Peskine8817f612018-12-18 00:18:46 +01002693 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2694 output1, output1_size,
2695 &output1_length ) );
2696 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002697 output1 + output1_length,
2698 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002699 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002700
Gilles Peskine048b7f02018-06-08 14:20:49 +02002701 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002702
Gilles Peskine8817f612018-12-18 00:18:46 +01002703 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002704
2705 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002706 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002707
Steven Cooreman177deba2020-09-07 17:14:14 +02002708 if( iv_length > 0 )
2709 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002710 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2711 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002712 }
2713
Gilles Peskine8817f612018-12-18 00:18:46 +01002714 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2715 output2, output2_size,
2716 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002717 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002718 PSA_ASSERT( psa_cipher_finish( &operation2,
2719 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002720 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002721 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002722
Gilles Peskine048b7f02018-06-08 14:20:49 +02002723 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002724
Gilles Peskine8817f612018-12-18 00:18:46 +01002725 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002726
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002727 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002728
2729exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002730 psa_cipher_abort( &operation1 );
2731 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002732 mbedtls_free( output1 );
2733 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002734 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002735 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03002736}
2737/* END_CASE */
2738
2739/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002740void cipher_verify_output_multipart( int alg_arg,
2741 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002742 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002743 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002744 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03002745{
Ronald Cron5425a212020-08-04 14:58:35 +02002746 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002747 psa_key_type_t key_type = key_type_arg;
2748 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002749 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002750 unsigned char iv[16] = {0};
2751 size_t iv_size = 16;
2752 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002753 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002754 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002755 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002756 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002757 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002758 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002759 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002760 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2761 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002762 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002763
Gilles Peskine8817f612018-12-18 00:18:46 +01002764 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002765
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002766 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2767 psa_set_key_algorithm( &attributes, alg );
2768 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002769
Ronald Cron5425a212020-08-04 14:58:35 +02002770 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2771 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03002772
Ronald Cron5425a212020-08-04 14:58:35 +02002773 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2774 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002775
Steven Cooreman177deba2020-09-07 17:14:14 +02002776 if( alg != PSA_ALG_ECB_NO_PADDING )
2777 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002778 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2779 iv, iv_size,
2780 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002781 }
2782
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002783 output1_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002784 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002785 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002786
Gilles Peskinee0866522019-02-19 19:44:00 +01002787 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002788
Gilles Peskine8817f612018-12-18 00:18:46 +01002789 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2790 output1, output1_buffer_size,
2791 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002792 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002793
Gilles Peskine8817f612018-12-18 00:18:46 +01002794 PSA_ASSERT( psa_cipher_update( &operation1,
2795 input->x + first_part_size,
2796 input->len - first_part_size,
2797 output1, output1_buffer_size,
2798 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002799 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002800
Gilles Peskine8817f612018-12-18 00:18:46 +01002801 PSA_ASSERT( psa_cipher_finish( &operation1,
2802 output1 + output1_length,
2803 output1_buffer_size - output1_length,
2804 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002805 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002806
Gilles Peskine8817f612018-12-18 00:18:46 +01002807 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002808
Gilles Peskine048b7f02018-06-08 14:20:49 +02002809 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002810 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002811
Steven Cooreman177deba2020-09-07 17:14:14 +02002812 if( iv_length > 0 )
2813 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002814 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2815 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002816 }
Moran Pekerded84402018-06-06 16:36:50 +03002817
Gilles Peskine8817f612018-12-18 00:18:46 +01002818 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2819 output2, output2_buffer_size,
2820 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002821 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002822
Gilles Peskine8817f612018-12-18 00:18:46 +01002823 PSA_ASSERT( psa_cipher_update( &operation2,
2824 output1 + first_part_size,
2825 output1_length - first_part_size,
2826 output2, output2_buffer_size,
2827 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002828 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002829
Gilles Peskine8817f612018-12-18 00:18:46 +01002830 PSA_ASSERT( psa_cipher_finish( &operation2,
2831 output2 + output2_length,
2832 output2_buffer_size - output2_length,
2833 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002834 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002835
Gilles Peskine8817f612018-12-18 00:18:46 +01002836 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002837
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002838 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002839
2840exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002841 psa_cipher_abort( &operation1 );
2842 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002843 mbedtls_free( output1 );
2844 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002845 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002846 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002847}
2848/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002849
Gilles Peskine20035e32018-02-03 22:44:14 +01002850/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002851void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002852 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002853 data_t *nonce,
2854 data_t *additional_data,
2855 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002856 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002857{
Ronald Cron5425a212020-08-04 14:58:35 +02002858 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002859 psa_key_type_t key_type = key_type_arg;
2860 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002861 unsigned char *output_data = NULL;
2862 size_t output_size = 0;
2863 size_t output_length = 0;
2864 unsigned char *output_data2 = NULL;
2865 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02002866 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002867 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002868 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002869
Gilles Peskine4abf7412018-06-18 16:35:34 +02002870 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02002871 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
2872 * should be exact. */
2873 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
2874 TEST_EQUAL( output_size,
2875 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002876 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002877
Gilles Peskine8817f612018-12-18 00:18:46 +01002878 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002879
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002880 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2881 psa_set_key_algorithm( &attributes, alg );
2882 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002883
Gilles Peskine049c7532019-05-15 20:22:09 +02002884 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002885 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002886
Ronald Cron5425a212020-08-04 14:58:35 +02002887 TEST_EQUAL( psa_aead_encrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01002888 nonce->x, nonce->len,
2889 additional_data->x,
2890 additional_data->len,
2891 input_data->x, input_data->len,
2892 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002893 &output_length ),
2894 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002895
2896 if( PSA_SUCCESS == expected_result )
2897 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002898 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002899
Gilles Peskine003a4a92019-05-14 16:09:40 +02002900 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
2901 * should be exact. */
2902 TEST_EQUAL( input_data->len,
2903 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
2904
Ronald Cron5425a212020-08-04 14:58:35 +02002905 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01002906 nonce->x, nonce->len,
2907 additional_data->x,
2908 additional_data->len,
2909 output_data, output_length,
2910 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002911 &output_length2 ),
2912 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002913
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002914 ASSERT_COMPARE( input_data->x, input_data->len,
2915 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002916 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002917
Gilles Peskinea1cac842018-06-11 19:33:02 +02002918exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002919 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002920 mbedtls_free( output_data );
2921 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002922 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002923}
2924/* END_CASE */
2925
2926/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002927void aead_encrypt( int key_type_arg, data_t *key_data,
2928 int alg_arg,
2929 data_t *nonce,
2930 data_t *additional_data,
2931 data_t *input_data,
2932 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002933{
Ronald Cron5425a212020-08-04 14:58:35 +02002934 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002935 psa_key_type_t key_type = key_type_arg;
2936 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002937 unsigned char *output_data = NULL;
2938 size_t output_size = 0;
2939 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02002940 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002941 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01002942 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002943
Gilles Peskine4abf7412018-06-18 16:35:34 +02002944 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02002945 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
2946 * should be exact. */
2947 TEST_EQUAL( output_size,
2948 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002949 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002950
Gilles Peskine8817f612018-12-18 00:18:46 +01002951 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002952
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002953 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2954 psa_set_key_algorithm( &attributes, alg );
2955 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002956
Gilles Peskine049c7532019-05-15 20:22:09 +02002957 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002958 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002959
Steven Cooremand588ea12021-01-11 19:36:04 +01002960 status = psa_aead_encrypt( key, alg,
2961 nonce->x, nonce->len,
2962 additional_data->x, additional_data->len,
2963 input_data->x, input_data->len,
2964 output_data, output_size,
2965 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002966
Ronald Cron28a45ed2021-02-09 20:35:42 +01002967 /* If the operation is not supported, just skip and not fail in case the
2968 * encryption involves a common limitation of cryptography hardwares and
2969 * an alternative implementation. */
2970 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01002971 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01002972 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
2973 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01002974 }
Steven Cooremand588ea12021-01-11 19:36:04 +01002975
2976 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002977 ASSERT_COMPARE( expected_result->x, expected_result->len,
2978 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002979
Gilles Peskinea1cac842018-06-11 19:33:02 +02002980exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002981 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002982 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002983 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002984}
2985/* END_CASE */
2986
2987/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002988void aead_decrypt( int key_type_arg, data_t *key_data,
2989 int alg_arg,
2990 data_t *nonce,
2991 data_t *additional_data,
2992 data_t *input_data,
2993 data_t *expected_data,
2994 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002995{
Ronald Cron5425a212020-08-04 14:58:35 +02002996 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002997 psa_key_type_t key_type = key_type_arg;
2998 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002999 unsigned char *output_data = NULL;
3000 size_t output_size = 0;
3001 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003002 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003003 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003004 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003005 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003006
Gilles Peskine003a4a92019-05-14 16:09:40 +02003007 output_size = input_data->len - tag_length;
3008 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3009 * should be exact. */
3010 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3011 TEST_EQUAL( output_size,
3012 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003013 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003014
Gilles Peskine8817f612018-12-18 00:18:46 +01003015 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003016
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003017 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3018 psa_set_key_algorithm( &attributes, alg );
3019 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003020
Gilles Peskine049c7532019-05-15 20:22:09 +02003021 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003022 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003023
Steven Cooremand588ea12021-01-11 19:36:04 +01003024 status = psa_aead_decrypt( key, alg,
3025 nonce->x, nonce->len,
3026 additional_data->x,
3027 additional_data->len,
3028 input_data->x, input_data->len,
3029 output_data, output_size,
3030 &output_length );
3031
Ronald Cron28a45ed2021-02-09 20:35:42 +01003032 /* If the operation is not supported, just skip and not fail in case the
3033 * decryption involves a common limitation of cryptography hardwares and
3034 * an alternative implementation. */
3035 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003036 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003037 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3038 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003039 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003040
3041 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003042
Gilles Peskine2d277862018-06-18 15:41:12 +02003043 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003044 ASSERT_COMPARE( expected_data->x, expected_data->len,
3045 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003046
Gilles Peskinea1cac842018-06-11 19:33:02 +02003047exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003048 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003049 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003050 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003051}
3052/* END_CASE */
3053
3054/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003055void signature_size( int type_arg,
3056 int bits,
3057 int alg_arg,
3058 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003059{
3060 psa_key_type_t type = type_arg;
3061 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003062 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003063
Gilles Peskinefe11b722018-12-18 00:24:04 +01003064 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003065#if defined(MBEDTLS_TEST_DEPRECATED)
3066 TEST_EQUAL( actual_size,
3067 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3068#endif /* MBEDTLS_TEST_DEPRECATED */
3069
Gilles Peskinee59236f2018-01-27 23:32:46 +01003070exit:
3071 ;
3072}
3073/* END_CASE */
3074
3075/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003076void sign_deterministic( int key_type_arg, data_t *key_data,
3077 int alg_arg, data_t *input_data,
3078 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003079{
Ronald Cron5425a212020-08-04 14:58:35 +02003080 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003081 psa_key_type_t key_type = key_type_arg;
3082 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003083 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003084 unsigned char *signature = NULL;
3085 size_t signature_size;
3086 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003087 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003088
Gilles Peskine8817f612018-12-18 00:18:46 +01003089 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003090
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003091 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003092 psa_set_key_algorithm( &attributes, alg );
3093 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003094
Gilles Peskine049c7532019-05-15 20:22:09 +02003095 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003096 &key ) );
3097 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003098 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003099
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003100 /* Allocate a buffer which has the size advertized by the
3101 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003102 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003103 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003104 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003105 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003106 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003107
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003108 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003109 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003110 input_data->x, input_data->len,
3111 signature, signature_size,
3112 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003113 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003114 ASSERT_COMPARE( output_data->x, output_data->len,
3115 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003116
Gilles Peskine0627f982019-11-26 19:12:16 +01003117#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003118 memset( signature, 0, signature_size );
3119 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003120 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003121 input_data->x, input_data->len,
3122 signature, signature_size,
3123 &signature_length ) );
3124 ASSERT_COMPARE( output_data->x, output_data->len,
3125 signature, signature_length );
3126#endif /* MBEDTLS_TEST_DEPRECATED */
3127
Gilles Peskine20035e32018-02-03 22:44:14 +01003128exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003129 /*
3130 * Key attributes may have been returned by psa_get_key_attributes()
3131 * thus reset them as required.
3132 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003133 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003134
Ronald Cron5425a212020-08-04 14:58:35 +02003135 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003136 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003137 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003138}
3139/* END_CASE */
3140
3141/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003142void sign_fail( int key_type_arg, data_t *key_data,
3143 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003144 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003145{
Ronald Cron5425a212020-08-04 14:58:35 +02003146 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003147 psa_key_type_t key_type = key_type_arg;
3148 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003149 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003150 psa_status_t actual_status;
3151 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003152 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003153 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003154 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003155
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003156 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003157
Gilles Peskine8817f612018-12-18 00:18:46 +01003158 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003159
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003160 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003161 psa_set_key_algorithm( &attributes, alg );
3162 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003163
Gilles Peskine049c7532019-05-15 20:22:09 +02003164 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003165 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003166
Ronald Cron5425a212020-08-04 14:58:35 +02003167 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003168 input_data->x, input_data->len,
3169 signature, signature_size,
3170 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003171 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003172 /* The value of *signature_length is unspecified on error, but
3173 * whatever it is, it should be less than signature_size, so that
3174 * if the caller tries to read *signature_length bytes without
3175 * checking the error code then they don't overflow a buffer. */
3176 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003177
Gilles Peskine895242b2019-11-29 12:15:40 +01003178#if defined(MBEDTLS_TEST_DEPRECATED)
3179 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003180 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003181 input_data->x, input_data->len,
3182 signature, signature_size,
3183 &signature_length ),
3184 expected_status );
3185 TEST_ASSERT( signature_length <= signature_size );
3186#endif /* MBEDTLS_TEST_DEPRECATED */
3187
Gilles Peskine20035e32018-02-03 22:44:14 +01003188exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003189 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003190 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003191 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003192 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003193}
3194/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003195
3196/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003197void sign_verify( int key_type_arg, data_t *key_data,
3198 int alg_arg, data_t *input_data )
3199{
Ronald Cron5425a212020-08-04 14:58:35 +02003200 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003201 psa_key_type_t key_type = key_type_arg;
3202 psa_algorithm_t alg = alg_arg;
3203 size_t key_bits;
3204 unsigned char *signature = NULL;
3205 size_t signature_size;
3206 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003207 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003208
Gilles Peskine8817f612018-12-18 00:18:46 +01003209 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003210
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003211 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003212 psa_set_key_algorithm( &attributes, alg );
3213 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003214
Gilles Peskine049c7532019-05-15 20:22:09 +02003215 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003216 &key ) );
3217 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003218 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003219
3220 /* Allocate a buffer which has the size advertized by the
3221 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003222 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003223 key_bits, alg );
3224 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003225 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003226 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003227
3228 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003229 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003230 input_data->x, input_data->len,
3231 signature, signature_size,
3232 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003233 /* Check that the signature length looks sensible. */
3234 TEST_ASSERT( signature_length <= signature_size );
3235 TEST_ASSERT( signature_length > 0 );
3236
3237 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003238 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003239 input_data->x, input_data->len,
3240 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003241
3242 if( input_data->len != 0 )
3243 {
3244 /* Flip a bit in the input and verify that the signature is now
3245 * detected as invalid. Flip a bit at the beginning, not at the end,
3246 * because ECDSA may ignore the last few bits of the input. */
3247 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003248 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003249 input_data->x, input_data->len,
3250 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003251 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003252 }
3253
3254exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003255 /*
3256 * Key attributes may have been returned by psa_get_key_attributes()
3257 * thus reset them as required.
3258 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003259 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003260
Ronald Cron5425a212020-08-04 14:58:35 +02003261 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003262 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003263 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003264}
3265/* END_CASE */
3266
3267/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003268void asymmetric_verify( int key_type_arg, data_t *key_data,
3269 int alg_arg, data_t *hash_data,
3270 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003271{
Ronald Cron5425a212020-08-04 14:58:35 +02003272 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003273 psa_key_type_t key_type = key_type_arg;
3274 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003275 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003276
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003277 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003278
Gilles Peskine8817f612018-12-18 00:18:46 +01003279 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003280
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003281 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003282 psa_set_key_algorithm( &attributes, alg );
3283 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003284
Gilles Peskine049c7532019-05-15 20:22:09 +02003285 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003286 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003287
Ronald Cron5425a212020-08-04 14:58:35 +02003288 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003289 hash_data->x, hash_data->len,
3290 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003291
3292#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003293 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003294 hash_data->x, hash_data->len,
3295 signature_data->x,
3296 signature_data->len ) );
3297
3298#endif /* MBEDTLS_TEST_DEPRECATED */
3299
itayzafrir5c753392018-05-08 11:18:38 +03003300exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003301 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003302 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003303 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003304}
3305/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003306
3307/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003308void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3309 int alg_arg, data_t *hash_data,
3310 data_t *signature_data,
3311 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003312{
Ronald Cron5425a212020-08-04 14:58:35 +02003313 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003314 psa_key_type_t key_type = key_type_arg;
3315 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003316 psa_status_t actual_status;
3317 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003318 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003319
Gilles Peskine8817f612018-12-18 00:18:46 +01003320 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003321
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003322 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003323 psa_set_key_algorithm( &attributes, alg );
3324 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003325
Gilles Peskine049c7532019-05-15 20:22:09 +02003326 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003327 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003328
Ronald Cron5425a212020-08-04 14:58:35 +02003329 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003330 hash_data->x, hash_data->len,
3331 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003332 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003333
Gilles Peskine895242b2019-11-29 12:15:40 +01003334#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003335 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003336 hash_data->x, hash_data->len,
3337 signature_data->x, signature_data->len ),
3338 expected_status );
3339#endif /* MBEDTLS_TEST_DEPRECATED */
3340
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003341exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003342 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003343 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003344 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003345}
3346/* END_CASE */
3347
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003348/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003349void asymmetric_encrypt( int key_type_arg,
3350 data_t *key_data,
3351 int alg_arg,
3352 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003353 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003354 int expected_output_length_arg,
3355 int expected_status_arg )
3356{
Ronald Cron5425a212020-08-04 14:58:35 +02003357 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003358 psa_key_type_t key_type = key_type_arg;
3359 psa_algorithm_t alg = alg_arg;
3360 size_t expected_output_length = expected_output_length_arg;
3361 size_t key_bits;
3362 unsigned char *output = NULL;
3363 size_t output_size;
3364 size_t output_length = ~0;
3365 psa_status_t actual_status;
3366 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003367 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003368
Gilles Peskine8817f612018-12-18 00:18:46 +01003369 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003370
Gilles Peskine656896e2018-06-29 19:12:28 +02003371 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003372 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3373 psa_set_key_algorithm( &attributes, alg );
3374 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003375 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003376 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003377
3378 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003379 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003380 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02003381 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003382 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003383
3384 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003385 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003386 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003387 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003388 output, output_size,
3389 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003390 TEST_EQUAL( actual_status, expected_status );
3391 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003392
Gilles Peskine68428122018-06-30 18:42:41 +02003393 /* If the label is empty, the test framework puts a non-null pointer
3394 * in label->x. Test that a null pointer works as well. */
3395 if( label->len == 0 )
3396 {
3397 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003398 if( output_size != 0 )
3399 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003400 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003401 input_data->x, input_data->len,
3402 NULL, label->len,
3403 output, output_size,
3404 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003405 TEST_EQUAL( actual_status, expected_status );
3406 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003407 }
3408
Gilles Peskine656896e2018-06-29 19:12:28 +02003409exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003410 /*
3411 * Key attributes may have been returned by psa_get_key_attributes()
3412 * thus reset them as required.
3413 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003414 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003415
Ronald Cron5425a212020-08-04 14:58:35 +02003416 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02003417 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003418 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003419}
3420/* END_CASE */
3421
3422/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003423void asymmetric_encrypt_decrypt( int key_type_arg,
3424 data_t *key_data,
3425 int alg_arg,
3426 data_t *input_data,
3427 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003428{
Ronald Cron5425a212020-08-04 14:58:35 +02003429 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003430 psa_key_type_t key_type = key_type_arg;
3431 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003432 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003433 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003434 size_t output_size;
3435 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003436 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003437 size_t output2_size;
3438 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003439 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003440
Gilles Peskine8817f612018-12-18 00:18:46 +01003441 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003442
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003443 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3444 psa_set_key_algorithm( &attributes, alg );
3445 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003446
Gilles Peskine049c7532019-05-15 20:22:09 +02003447 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003448 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003449
3450 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02003451 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003452 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003453 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003454 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003455 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003456 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003457
Gilles Peskineeebd7382018-06-08 18:11:54 +02003458 /* We test encryption by checking that encrypt-then-decrypt gives back
3459 * the original plaintext because of the non-optional random
3460 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02003461 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003462 input_data->x, input_data->len,
3463 label->x, label->len,
3464 output, output_size,
3465 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003466 /* We don't know what ciphertext length to expect, but check that
3467 * it looks sensible. */
3468 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003469
Ronald Cron5425a212020-08-04 14:58:35 +02003470 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003471 output, output_length,
3472 label->x, label->len,
3473 output2, output2_size,
3474 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003475 ASSERT_COMPARE( input_data->x, input_data->len,
3476 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003477
3478exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003479 /*
3480 * Key attributes may have been returned by psa_get_key_attributes()
3481 * thus reset them as required.
3482 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003483 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003484
Ronald Cron5425a212020-08-04 14:58:35 +02003485 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003486 mbedtls_free( output );
3487 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003488 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003489}
3490/* END_CASE */
3491
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003492/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003493void asymmetric_decrypt( int key_type_arg,
3494 data_t *key_data,
3495 int alg_arg,
3496 data_t *input_data,
3497 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003498 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003499{
Ronald Cron5425a212020-08-04 14:58:35 +02003500 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003501 psa_key_type_t key_type = key_type_arg;
3502 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003503 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003504 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003505 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003506 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003507
Jaeden Amero412654a2019-02-06 12:57:46 +00003508 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003509 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003510
Gilles Peskine8817f612018-12-18 00:18:46 +01003511 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003512
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003513 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3514 psa_set_key_algorithm( &attributes, alg );
3515 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003516
Gilles Peskine049c7532019-05-15 20:22:09 +02003517 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003518 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003519
Ronald Cron5425a212020-08-04 14:58:35 +02003520 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003521 input_data->x, input_data->len,
3522 label->x, label->len,
3523 output,
3524 output_size,
3525 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003526 ASSERT_COMPARE( expected_data->x, expected_data->len,
3527 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003528
Gilles Peskine68428122018-06-30 18:42:41 +02003529 /* If the label is empty, the test framework puts a non-null pointer
3530 * in label->x. Test that a null pointer works as well. */
3531 if( label->len == 0 )
3532 {
3533 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003534 if( output_size != 0 )
3535 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003536 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003537 input_data->x, input_data->len,
3538 NULL, label->len,
3539 output,
3540 output_size,
3541 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003542 ASSERT_COMPARE( expected_data->x, expected_data->len,
3543 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003544 }
3545
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003546exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003547 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003548 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003549 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003550 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003551}
3552/* END_CASE */
3553
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003554/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003555void asymmetric_decrypt_fail( int key_type_arg,
3556 data_t *key_data,
3557 int alg_arg,
3558 data_t *input_data,
3559 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00003560 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02003561 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003562{
Ronald Cron5425a212020-08-04 14:58:35 +02003563 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003564 psa_key_type_t key_type = key_type_arg;
3565 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003566 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00003567 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003568 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003569 psa_status_t actual_status;
3570 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003571 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003572
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003573 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003574
Gilles Peskine8817f612018-12-18 00:18:46 +01003575 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003576
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003577 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3578 psa_set_key_algorithm( &attributes, alg );
3579 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003580
Gilles Peskine049c7532019-05-15 20:22:09 +02003581 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003582 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003583
Ronald Cron5425a212020-08-04 14:58:35 +02003584 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003585 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003586 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003587 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003588 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003589 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003590 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003591
Gilles Peskine68428122018-06-30 18:42:41 +02003592 /* If the label is empty, the test framework puts a non-null pointer
3593 * in label->x. Test that a null pointer works as well. */
3594 if( label->len == 0 )
3595 {
3596 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003597 if( output_size != 0 )
3598 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003599 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003600 input_data->x, input_data->len,
3601 NULL, label->len,
3602 output, output_size,
3603 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003604 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003605 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003606 }
3607
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003608exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003609 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003610 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02003611 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003612 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003613}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003614/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003615
3616/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003617void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00003618{
3619 /* Test each valid way of initializing the object, except for `= {0}`, as
3620 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3621 * though it's OK by the C standard. We could test for this, but we'd need
3622 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003623 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003624 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
3625 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
3626 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00003627
3628 memset( &zero, 0, sizeof( zero ) );
3629
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003630 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003631 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003632 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003633 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003634 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003635 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003636 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003637
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003638 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003639 PSA_ASSERT( psa_key_derivation_abort(&func) );
3640 PSA_ASSERT( psa_key_derivation_abort(&init) );
3641 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00003642}
3643/* END_CASE */
3644
Janos Follath16de4a42019-06-13 16:32:24 +01003645/* BEGIN_CASE */
3646void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02003647{
Gilles Peskineea0fb492018-07-12 17:17:20 +02003648 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003649 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003650 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003651
Gilles Peskine8817f612018-12-18 00:18:46 +01003652 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003653
Janos Follath16de4a42019-06-13 16:32:24 +01003654 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003655 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003656
3657exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003658 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003659 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003660}
3661/* END_CASE */
3662
Janos Follathaf3c2a02019-06-12 12:34:34 +01003663/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01003664void derive_set_capacity( int alg_arg, int capacity_arg,
3665 int expected_status_arg )
3666{
3667 psa_algorithm_t alg = alg_arg;
3668 size_t capacity = capacity_arg;
3669 psa_status_t expected_status = expected_status_arg;
3670 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
3671
3672 PSA_ASSERT( psa_crypto_init( ) );
3673
3674 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
3675
3676 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
3677 expected_status );
3678
3679exit:
3680 psa_key_derivation_abort( &operation );
3681 PSA_DONE( );
3682}
3683/* END_CASE */
3684
3685/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01003686void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02003687 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01003688 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02003689 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01003690 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02003691 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003692 int expected_status_arg3,
3693 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01003694{
3695 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02003696 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
3697 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01003698 psa_status_t expected_statuses[] = {expected_status_arg1,
3699 expected_status_arg2,
3700 expected_status_arg3};
3701 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02003702 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
3703 MBEDTLS_SVC_KEY_ID_INIT,
3704 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01003705 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
3706 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3707 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003708 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02003709 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003710 psa_status_t expected_output_status = expected_output_status_arg;
3711 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01003712
3713 PSA_ASSERT( psa_crypto_init( ) );
3714
3715 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
3716 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01003717
3718 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
3719
3720 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
3721 {
Gilles Peskineb8965192019-09-24 16:21:10 +02003722 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01003723 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02003724 psa_set_key_type( &attributes, key_types[i] );
3725 PSA_ASSERT( psa_import_key( &attributes,
3726 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003727 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02003728 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
3729 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
3730 {
3731 // When taking a private key as secret input, use key agreement
3732 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01003733 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
3734 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02003735 expected_statuses[i] );
3736 }
3737 else
3738 {
3739 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02003740 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02003741 expected_statuses[i] );
3742 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02003743 }
3744 else
3745 {
3746 TEST_EQUAL( psa_key_derivation_input_bytes(
3747 &operation, steps[i],
3748 inputs[i]->x, inputs[i]->len ),
3749 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01003750 }
3751 }
3752
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003753 if( output_key_type != PSA_KEY_TYPE_NONE )
3754 {
3755 psa_reset_key_attributes( &attributes );
3756 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
3757 psa_set_key_bits( &attributes, 8 );
3758 actual_output_status =
3759 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02003760 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003761 }
3762 else
3763 {
3764 uint8_t buffer[1];
3765 actual_output_status =
3766 psa_key_derivation_output_bytes( &operation,
3767 buffer, sizeof( buffer ) );
3768 }
3769 TEST_EQUAL( actual_output_status, expected_output_status );
3770
Janos Follathaf3c2a02019-06-12 12:34:34 +01003771exit:
3772 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003773 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
3774 psa_destroy_key( keys[i] );
3775 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01003776 PSA_DONE( );
3777}
3778/* END_CASE */
3779
Janos Follathd958bb72019-07-03 15:02:16 +01003780/* BEGIN_CASE */
3781void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003782{
Janos Follathd958bb72019-07-03 15:02:16 +01003783 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02003784 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003785 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003786 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01003787 unsigned char input1[] = "Input 1";
3788 size_t input1_length = sizeof( input1 );
3789 unsigned char input2[] = "Input 2";
3790 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003791 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003792 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003793 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3794 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3795 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003796 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003797
Gilles Peskine8817f612018-12-18 00:18:46 +01003798 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003799
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003800 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
3801 psa_set_key_algorithm( &attributes, alg );
3802 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003803
Gilles Peskine73676cb2019-05-15 20:15:10 +02003804 PSA_ASSERT( psa_import_key( &attributes,
3805 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02003806 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003807
3808 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01003809 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
3810 input1, input1_length,
3811 input2, input2_length,
3812 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01003813 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003814
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003815 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01003816 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003817 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003818
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003819 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003820
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003821 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02003822 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003823
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003824exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003825 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003826 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003827 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003828}
3829/* END_CASE */
3830
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003831/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003832void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003833{
3834 uint8_t output_buffer[16];
3835 size_t buffer_size = 16;
3836 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003837 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003838
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003839 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
3840 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003841 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003842
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003843 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003844 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003845
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003846 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003847
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003848 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
3849 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003850 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003851
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003852 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003853 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003854
3855exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003856 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003857}
3858/* END_CASE */
3859
3860/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003861void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02003862 int step1_arg, data_t *input1,
3863 int step2_arg, data_t *input2,
3864 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003865 int requested_capacity_arg,
3866 data_t *expected_output1,
3867 data_t *expected_output2 )
3868{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003869 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02003870 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
3871 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02003872 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
3873 MBEDTLS_SVC_KEY_ID_INIT,
3874 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003875 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003876 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003877 uint8_t *expected_outputs[2] =
3878 {expected_output1->x, expected_output2->x};
3879 size_t output_sizes[2] =
3880 {expected_output1->len, expected_output2->len};
3881 size_t output_buffer_size = 0;
3882 uint8_t *output_buffer = NULL;
3883 size_t expected_capacity;
3884 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02003885 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003886 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02003887 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003888
3889 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3890 {
3891 if( output_sizes[i] > output_buffer_size )
3892 output_buffer_size = output_sizes[i];
3893 if( output_sizes[i] == 0 )
3894 expected_outputs[i] = NULL;
3895 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003896 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003897 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003898
Gilles Peskineff5f0e72019-04-18 12:53:30 +02003899 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
3900 psa_set_key_algorithm( &attributes, alg );
3901 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003902
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003903 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02003904 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
3905 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
3906 requested_capacity ) );
3907 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003908 {
Gilles Peskine1468da72019-05-29 17:35:49 +02003909 switch( steps[i] )
3910 {
3911 case 0:
3912 break;
3913 case PSA_KEY_DERIVATION_INPUT_SECRET:
3914 PSA_ASSERT( psa_import_key( &attributes,
3915 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003916 &keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02003917 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02003918 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02003919 break;
3920 default:
3921 PSA_ASSERT( psa_key_derivation_input_bytes(
3922 &operation, steps[i],
3923 inputs[i]->x, inputs[i]->len ) );
3924 break;
3925 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003926 }
Gilles Peskine1468da72019-05-29 17:35:49 +02003927
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003928 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003929 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003930 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003931 expected_capacity = requested_capacity;
3932
3933 /* Expansion phase. */
3934 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3935 {
3936 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003937 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003938 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003939 if( expected_capacity == 0 && output_sizes[i] == 0 )
3940 {
3941 /* Reading 0 bytes when 0 bytes are available can go either way. */
3942 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02003943 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003944 continue;
3945 }
3946 else if( expected_capacity == 0 ||
3947 output_sizes[i] > expected_capacity )
3948 {
3949 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02003950 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003951 expected_capacity = 0;
3952 continue;
3953 }
3954 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003955 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003956 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003957 ASSERT_COMPARE( output_buffer, output_sizes[i],
3958 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003959 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003960 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003961 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003962 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003963 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003964 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003965 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003966
3967exit:
3968 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003969 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003970 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
3971 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003972 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003973}
3974/* END_CASE */
3975
3976/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003977void derive_full( int alg_arg,
3978 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01003979 data_t *input1,
3980 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02003981 int requested_capacity_arg )
3982{
Ronald Cron5425a212020-08-04 14:58:35 +02003983 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02003984 psa_algorithm_t alg = alg_arg;
3985 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003986 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02003987 unsigned char output_buffer[16];
3988 size_t expected_capacity = requested_capacity;
3989 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02003990 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02003991
Gilles Peskine8817f612018-12-18 00:18:46 +01003992 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003993
Gilles Peskineff5f0e72019-04-18 12:53:30 +02003994 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
3995 psa_set_key_algorithm( &attributes, alg );
3996 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02003997
Gilles Peskine049c7532019-05-15 20:22:09 +02003998 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003999 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004000
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004001 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4002 input1->x, input1->len,
4003 input2->x, input2->len,
4004 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004005 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004006
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004007 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004008 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004009 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004010
4011 /* Expansion phase. */
4012 while( current_capacity > 0 )
4013 {
4014 size_t read_size = sizeof( output_buffer );
4015 if( read_size > current_capacity )
4016 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004017 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004018 output_buffer,
4019 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004020 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004021 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004022 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004023 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004024 }
4025
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004026 /* Check that the operation refuses to go over capacity. */
4027 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004028 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004029
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004030 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004031
4032exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004033 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004034 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004035 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004036}
4037/* END_CASE */
4038
Janos Follathe60c9052019-07-03 13:51:30 +01004039/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004040void derive_key_exercise( int alg_arg,
4041 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004042 data_t *input1,
4043 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004044 int derived_type_arg,
4045 int derived_bits_arg,
4046 int derived_usage_arg,
4047 int derived_alg_arg )
4048{
Ronald Cron5425a212020-08-04 14:58:35 +02004049 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4050 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004051 psa_algorithm_t alg = alg_arg;
4052 psa_key_type_t derived_type = derived_type_arg;
4053 size_t derived_bits = derived_bits_arg;
4054 psa_key_usage_t derived_usage = derived_usage_arg;
4055 psa_algorithm_t derived_alg = derived_alg_arg;
4056 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004057 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004058 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004059 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004060
Gilles Peskine8817f612018-12-18 00:18:46 +01004061 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004062
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004063 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4064 psa_set_key_algorithm( &attributes, alg );
4065 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004066 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004067 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004068
4069 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004070 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4071 input1->x, input1->len,
4072 input2->x, input2->len,
4073 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004074 goto exit;
4075
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004076 psa_set_key_usage_flags( &attributes, derived_usage );
4077 psa_set_key_algorithm( &attributes, derived_alg );
4078 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004079 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004080 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004081 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004082
4083 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004084 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004085 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4086 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004087
4088 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004089 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004090 goto exit;
4091
4092exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004093 /*
4094 * Key attributes may have been returned by psa_get_key_attributes()
4095 * thus reset them as required.
4096 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004097 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004098
4099 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004100 psa_destroy_key( base_key );
4101 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004102 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004103}
4104/* END_CASE */
4105
Janos Follath42fd8882019-07-03 14:17:09 +01004106/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004107void derive_key_export( int alg_arg,
4108 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004109 data_t *input1,
4110 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004111 int bytes1_arg,
4112 int bytes2_arg )
4113{
Ronald Cron5425a212020-08-04 14:58:35 +02004114 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4115 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004116 psa_algorithm_t alg = alg_arg;
4117 size_t bytes1 = bytes1_arg;
4118 size_t bytes2 = bytes2_arg;
4119 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004120 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004121 uint8_t *output_buffer = NULL;
4122 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004123 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4124 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004125 size_t length;
4126
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004127 ASSERT_ALLOC( output_buffer, capacity );
4128 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004129 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004130
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004131 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4132 psa_set_key_algorithm( &base_attributes, alg );
4133 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004134 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004135 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004136
4137 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004138 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4139 input1->x, input1->len,
4140 input2->x, input2->len,
4141 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004142 goto exit;
4143
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004144 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004145 output_buffer,
4146 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004147 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004148
4149 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004150 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4151 input1->x, input1->len,
4152 input2->x, input2->len,
4153 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004154 goto exit;
4155
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004156 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4157 psa_set_key_algorithm( &derived_attributes, 0 );
4158 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004159 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004160 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004161 &derived_key ) );
4162 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004163 export_buffer, bytes1,
4164 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004165 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004166 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004167 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004168 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004169 &derived_key ) );
4170 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004171 export_buffer + bytes1, bytes2,
4172 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004173 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004174
4175 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004176 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4177 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004178
4179exit:
4180 mbedtls_free( output_buffer );
4181 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004182 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004183 psa_destroy_key( base_key );
4184 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004185 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004186}
4187/* END_CASE */
4188
4189/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004190void derive_key( int alg_arg,
4191 data_t *key_data, data_t *input1, data_t *input2,
4192 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004193 int expected_status_arg,
4194 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004195{
Ronald Cron5425a212020-08-04 14:58:35 +02004196 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4197 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004198 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004199 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004200 size_t bits = bits_arg;
4201 psa_status_t expected_status = expected_status_arg;
4202 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4203 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4204 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4205
4206 PSA_ASSERT( psa_crypto_init( ) );
4207
4208 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4209 psa_set_key_algorithm( &base_attributes, alg );
4210 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4211 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004212 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004213
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004214 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4215 input1->x, input1->len,
4216 input2->x, input2->len,
4217 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004218 goto exit;
4219
4220 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4221 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004222 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004223 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004224
4225 psa_status_t status =
4226 psa_key_derivation_output_key( &derived_attributes,
4227 &operation,
4228 &derived_key );
4229 if( is_large_output > 0 )
4230 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4231 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004232
4233exit:
4234 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004235 psa_destroy_key( base_key );
4236 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004237 PSA_DONE( );
4238}
4239/* END_CASE */
4240
4241/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004242void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004243 int our_key_type_arg, int our_key_alg_arg,
4244 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004245 int expected_status_arg )
4246{
Ronald Cron5425a212020-08-04 14:58:35 +02004247 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004248 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004249 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004250 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004251 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004252 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004253 psa_status_t expected_status = expected_status_arg;
4254 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004255
Gilles Peskine8817f612018-12-18 00:18:46 +01004256 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004257
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004258 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004259 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004260 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004261 PSA_ASSERT( psa_import_key( &attributes,
4262 our_key_data->x, our_key_data->len,
4263 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004264
Gilles Peskine77f40d82019-04-11 21:27:06 +02004265 /* The tests currently include inputs that should fail at either step.
4266 * Test cases that fail at the setup step should be changed to call
4267 * key_derivation_setup instead, and this function should be renamed
4268 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004269 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004270 if( status == PSA_SUCCESS )
4271 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004272 TEST_EQUAL( psa_key_derivation_key_agreement(
4273 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4274 our_key,
4275 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004276 expected_status );
4277 }
4278 else
4279 {
4280 TEST_ASSERT( status == expected_status );
4281 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004282
4283exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004284 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004285 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004286 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004287}
4288/* END_CASE */
4289
4290/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004291void raw_key_agreement( int alg_arg,
4292 int our_key_type_arg, data_t *our_key_data,
4293 data_t *peer_key_data,
4294 data_t *expected_output )
4295{
Ronald Cron5425a212020-08-04 14:58:35 +02004296 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004297 psa_algorithm_t alg = alg_arg;
4298 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004299 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004300 unsigned char *output = NULL;
4301 size_t output_length = ~0;
4302
4303 ASSERT_ALLOC( output, expected_output->len );
4304 PSA_ASSERT( psa_crypto_init( ) );
4305
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004306 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4307 psa_set_key_algorithm( &attributes, alg );
4308 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004309 PSA_ASSERT( psa_import_key( &attributes,
4310 our_key_data->x, our_key_data->len,
4311 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004312
Gilles Peskinebe697d82019-05-16 18:00:41 +02004313 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4314 peer_key_data->x, peer_key_data->len,
4315 output, expected_output->len,
4316 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004317 ASSERT_COMPARE( output, output_length,
4318 expected_output->x, expected_output->len );
4319
4320exit:
4321 mbedtls_free( output );
4322 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004323 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004324}
4325/* END_CASE */
4326
4327/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004328void key_agreement_capacity( int alg_arg,
4329 int our_key_type_arg, data_t *our_key_data,
4330 data_t *peer_key_data,
4331 int expected_capacity_arg )
4332{
Ronald Cron5425a212020-08-04 14:58:35 +02004333 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004334 psa_algorithm_t alg = alg_arg;
4335 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004336 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004337 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004338 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004339 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004340
Gilles Peskine8817f612018-12-18 00:18:46 +01004341 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004342
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004343 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4344 psa_set_key_algorithm( &attributes, alg );
4345 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004346 PSA_ASSERT( psa_import_key( &attributes,
4347 our_key_data->x, our_key_data->len,
4348 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004349
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004350 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004351 PSA_ASSERT( psa_key_derivation_key_agreement(
4352 &operation,
4353 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4354 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004355 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4356 {
4357 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004358 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004359 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004360 NULL, 0 ) );
4361 }
Gilles Peskine59685592018-09-18 12:11:34 +02004362
Gilles Peskinebf491972018-10-25 22:36:12 +02004363 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004364 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004365 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004366 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004367
Gilles Peskinebf491972018-10-25 22:36:12 +02004368 /* Test the actual capacity by reading the output. */
4369 while( actual_capacity > sizeof( output ) )
4370 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004371 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004372 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004373 actual_capacity -= sizeof( output );
4374 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004375 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004376 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004377 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004378 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004379
Gilles Peskine59685592018-09-18 12:11:34 +02004380exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004381 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004382 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004383 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004384}
4385/* END_CASE */
4386
4387/* BEGIN_CASE */
4388void key_agreement_output( int alg_arg,
4389 int our_key_type_arg, data_t *our_key_data,
4390 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004391 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004392{
Ronald Cron5425a212020-08-04 14:58:35 +02004393 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004394 psa_algorithm_t alg = alg_arg;
4395 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004396 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004397 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004398 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004399
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004400 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4401 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004402
Gilles Peskine8817f612018-12-18 00:18:46 +01004403 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004404
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004405 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4406 psa_set_key_algorithm( &attributes, alg );
4407 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004408 PSA_ASSERT( psa_import_key( &attributes,
4409 our_key_data->x, our_key_data->len,
4410 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004411
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004412 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004413 PSA_ASSERT( psa_key_derivation_key_agreement(
4414 &operation,
4415 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4416 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004417 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4418 {
4419 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004420 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004421 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004422 NULL, 0 ) );
4423 }
Gilles Peskine59685592018-09-18 12:11:34 +02004424
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004425 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004426 actual_output,
4427 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004428 ASSERT_COMPARE( actual_output, expected_output1->len,
4429 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004430 if( expected_output2->len != 0 )
4431 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004432 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004433 actual_output,
4434 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004435 ASSERT_COMPARE( actual_output, expected_output2->len,
4436 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004437 }
Gilles Peskine59685592018-09-18 12:11:34 +02004438
4439exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004440 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004441 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004442 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004443 mbedtls_free( actual_output );
4444}
4445/* END_CASE */
4446
4447/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004448void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004449{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004450 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004451 unsigned char *output = NULL;
4452 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004453 size_t i;
4454 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004455
Simon Butcher49f8e312020-03-03 15:51:50 +00004456 TEST_ASSERT( bytes_arg >= 0 );
4457
Gilles Peskine91892022021-02-08 19:50:26 +01004458 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004459 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02004460
Gilles Peskine8817f612018-12-18 00:18:46 +01004461 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004462
Gilles Peskinea50d7392018-06-21 10:22:13 +02004463 /* Run several times, to ensure that every output byte will be
4464 * nonzero at least once with overwhelming probability
4465 * (2^(-8*number_of_runs)). */
4466 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004467 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004468 if( bytes != 0 )
4469 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004470 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004471
Gilles Peskinea50d7392018-06-21 10:22:13 +02004472 for( i = 0; i < bytes; i++ )
4473 {
4474 if( output[i] != 0 )
4475 ++changed[i];
4476 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004477 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004478
4479 /* Check that every byte was changed to nonzero at least once. This
4480 * validates that psa_generate_random is overwriting every byte of
4481 * the output buffer. */
4482 for( i = 0; i < bytes; i++ )
4483 {
4484 TEST_ASSERT( changed[i] != 0 );
4485 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004486
4487exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004488 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004489 mbedtls_free( output );
4490 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004491}
4492/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004493
4494/* BEGIN_CASE */
4495void generate_key( int type_arg,
4496 int bits_arg,
4497 int usage_arg,
4498 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004499 int expected_status_arg,
4500 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004501{
Ronald Cron5425a212020-08-04 14:58:35 +02004502 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004503 psa_key_type_t type = type_arg;
4504 psa_key_usage_t usage = usage_arg;
4505 size_t bits = bits_arg;
4506 psa_algorithm_t alg = alg_arg;
4507 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004508 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004509 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004510
Gilles Peskine8817f612018-12-18 00:18:46 +01004511 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004512
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004513 psa_set_key_usage_flags( &attributes, usage );
4514 psa_set_key_algorithm( &attributes, alg );
4515 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004516 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004517
4518 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01004519 psa_status_t status = psa_generate_key( &attributes, &key );
4520
4521 if( is_large_key > 0 )
4522 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4523 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004524 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004525 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004526
4527 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004528 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004529 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
4530 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004531
Gilles Peskine818ca122018-06-20 18:16:48 +02004532 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004533 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004534 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004535
4536exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004537 /*
4538 * Key attributes may have been returned by psa_get_key_attributes()
4539 * thus reset them as required.
4540 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004541 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004542
Ronald Cron5425a212020-08-04 14:58:35 +02004543 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004544 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004545}
4546/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004547
Gilles Peskinee56e8782019-04-26 17:34:02 +02004548/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
4549void generate_key_rsa( int bits_arg,
4550 data_t *e_arg,
4551 int expected_status_arg )
4552{
Ronald Cron5425a212020-08-04 14:58:35 +02004553 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004554 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02004555 size_t bits = bits_arg;
4556 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
4557 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
4558 psa_status_t expected_status = expected_status_arg;
4559 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4560 uint8_t *exported = NULL;
4561 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004562 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004563 size_t exported_length = SIZE_MAX;
4564 uint8_t *e_read_buffer = NULL;
4565 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02004566 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004567 size_t e_read_length = SIZE_MAX;
4568
4569 if( e_arg->len == 0 ||
4570 ( e_arg->len == 3 &&
4571 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
4572 {
4573 is_default_public_exponent = 1;
4574 e_read_size = 0;
4575 }
4576 ASSERT_ALLOC( e_read_buffer, e_read_size );
4577 ASSERT_ALLOC( exported, exported_size );
4578
4579 PSA_ASSERT( psa_crypto_init( ) );
4580
4581 psa_set_key_usage_flags( &attributes, usage );
4582 psa_set_key_algorithm( &attributes, alg );
4583 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
4584 e_arg->x, e_arg->len ) );
4585 psa_set_key_bits( &attributes, bits );
4586
4587 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02004588 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004589 if( expected_status != PSA_SUCCESS )
4590 goto exit;
4591
4592 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004593 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004594 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4595 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4596 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
4597 e_read_buffer, e_read_size,
4598 &e_read_length ) );
4599 if( is_default_public_exponent )
4600 TEST_EQUAL( e_read_length, 0 );
4601 else
4602 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
4603
4604 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004605 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02004606 goto exit;
4607
4608 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02004609 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02004610 exported, exported_size,
4611 &exported_length ) );
4612 {
4613 uint8_t *p = exported;
4614 uint8_t *end = exported + exported_length;
4615 size_t len;
4616 /* RSAPublicKey ::= SEQUENCE {
4617 * modulus INTEGER, -- n
4618 * publicExponent INTEGER } -- e
4619 */
4620 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004621 MBEDTLS_ASN1_SEQUENCE |
4622 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01004623 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004624 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
4625 MBEDTLS_ASN1_INTEGER ) );
4626 if( len >= 1 && p[0] == 0 )
4627 {
4628 ++p;
4629 --len;
4630 }
4631 if( e_arg->len == 0 )
4632 {
4633 TEST_EQUAL( len, 3 );
4634 TEST_EQUAL( p[0], 1 );
4635 TEST_EQUAL( p[1], 0 );
4636 TEST_EQUAL( p[2], 1 );
4637 }
4638 else
4639 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
4640 }
4641
4642exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004643 /*
4644 * Key attributes may have been returned by psa_get_key_attributes() or
4645 * set by psa_set_key_domain_parameters() thus reset them as required.
4646 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02004647 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004648
Ronald Cron5425a212020-08-04 14:58:35 +02004649 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004650 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004651 mbedtls_free( e_read_buffer );
4652 mbedtls_free( exported );
4653}
4654/* END_CASE */
4655
Darryl Greend49a4992018-06-18 17:27:26 +01004656/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004657void persistent_key_load_key_from_storage( data_t *data,
4658 int type_arg, int bits_arg,
4659 int usage_flags_arg, int alg_arg,
4660 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01004661{
Ronald Cron71016a92020-08-28 19:01:50 +02004662 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004663 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02004664 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4665 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004666 psa_key_type_t type = type_arg;
4667 size_t bits = bits_arg;
4668 psa_key_usage_t usage_flags = usage_flags_arg;
4669 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004670 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004671 unsigned char *first_export = NULL;
4672 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004673 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004674 size_t first_exported_length;
4675 size_t second_exported_length;
4676
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004677 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4678 {
4679 ASSERT_ALLOC( first_export, export_size );
4680 ASSERT_ALLOC( second_export, export_size );
4681 }
Darryl Greend49a4992018-06-18 17:27:26 +01004682
Gilles Peskine8817f612018-12-18 00:18:46 +01004683 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004684
Gilles Peskinec87af662019-05-15 16:12:22 +02004685 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004686 psa_set_key_usage_flags( &attributes, usage_flags );
4687 psa_set_key_algorithm( &attributes, alg );
4688 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004689 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004690
Darryl Green0c6575a2018-11-07 16:05:30 +00004691 switch( generation_method )
4692 {
4693 case IMPORT_KEY:
4694 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02004695 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004696 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004697 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004698
Darryl Green0c6575a2018-11-07 16:05:30 +00004699 case GENERATE_KEY:
4700 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02004701 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004702 break;
4703
4704 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01004705#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004706 {
4707 /* Create base key */
4708 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
4709 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4710 psa_set_key_usage_flags( &base_attributes,
4711 PSA_KEY_USAGE_DERIVE );
4712 psa_set_key_algorithm( &base_attributes, derive_alg );
4713 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004714 PSA_ASSERT( psa_import_key( &base_attributes,
4715 data->x, data->len,
4716 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004717 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004718 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004719 PSA_ASSERT( psa_key_derivation_input_key(
4720 &operation,
4721 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004722 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004723 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004724 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004725 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
4726 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004727 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004728 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004729 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02004730 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004731 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01004732#else
4733 TEST_ASSUME( ! "KDF not supported in this configuration" );
4734#endif
4735 break;
4736
4737 default:
4738 TEST_ASSERT( ! "generation_method not implemented in test" );
4739 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00004740 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004741 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01004742
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004743 /* Export the key if permitted by the key policy. */
4744 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4745 {
Ronald Cron5425a212020-08-04 14:58:35 +02004746 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004747 first_export, export_size,
4748 &first_exported_length ) );
4749 if( generation_method == IMPORT_KEY )
4750 ASSERT_COMPARE( data->x, data->len,
4751 first_export, first_exported_length );
4752 }
Darryl Greend49a4992018-06-18 17:27:26 +01004753
4754 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02004755 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004756 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01004757 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004758
Darryl Greend49a4992018-06-18 17:27:26 +01004759 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02004760 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02004761 TEST_ASSERT( mbedtls_svc_key_id_equal(
4762 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004763 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
4764 PSA_KEY_LIFETIME_PERSISTENT );
4765 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4766 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4767 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
4768 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004769
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004770 /* Export the key again if permitted by the key policy. */
4771 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00004772 {
Ronald Cron5425a212020-08-04 14:58:35 +02004773 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004774 second_export, export_size,
4775 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004776 ASSERT_COMPARE( first_export, first_exported_length,
4777 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00004778 }
4779
4780 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004781 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004782 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004783
4784exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004785 /*
4786 * Key attributes may have been returned by psa_get_key_attributes()
4787 * thus reset them as required.
4788 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004789 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004790
Darryl Greend49a4992018-06-18 17:27:26 +01004791 mbedtls_free( first_export );
4792 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004793 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004794 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02004795 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004796 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01004797}
4798/* END_CASE */