blob: fde4a9b0b617abfaf6aa84f731804a40be19f44d [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010018
Jaeden Amerof24c7f82018-06-27 17:20:43 +010019/** An invalid export length that will never be set by psa_export_key(). */
20static const size_t INVALID_EXPORT_LENGTH = ~0U;
21
Gilles Peskinea7aa4422018-08-14 15:17:54 +020022/** Test if a buffer contains a constant byte value.
23 *
24 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020025 *
26 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020027 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020028 * \param size Size of the buffer in bytes.
29 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020030 * \return 1 if the buffer is all-bits-zero.
31 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020032 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020033static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020034{
35 size_t i;
36 for( i = 0; i < size; i++ )
37 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020038 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020039 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020040 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020041 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042}
Gilles Peskine818ca122018-06-20 18:16:48 +020043
Gilles Peskine0b352bc2018-06-28 00:16:11 +020044/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
45static int asn1_write_10x( unsigned char **p,
46 unsigned char *start,
47 size_t bits,
48 unsigned char x )
49{
50 int ret;
51 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020052 if( bits == 0 )
53 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
54 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020055 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030056 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020057 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
58 *p -= len;
59 ( *p )[len-1] = x;
60 if( bits % 8 == 0 )
61 ( *p )[1] |= 1;
62 else
63 ( *p )[0] |= 1 << ( bits % 8 );
64 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
65 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
66 MBEDTLS_ASN1_INTEGER ) );
67 return( len );
68}
69
70static int construct_fake_rsa_key( unsigned char *buffer,
71 size_t buffer_size,
72 unsigned char **p,
73 size_t bits,
74 int keypair )
75{
76 size_t half_bits = ( bits + 1 ) / 2;
77 int ret;
78 int len = 0;
79 /* Construct something that looks like a DER encoding of
80 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
81 * RSAPrivateKey ::= SEQUENCE {
82 * version Version,
83 * modulus INTEGER, -- n
84 * publicExponent INTEGER, -- e
85 * privateExponent INTEGER, -- d
86 * prime1 INTEGER, -- p
87 * prime2 INTEGER, -- q
88 * exponent1 INTEGER, -- d mod (p-1)
89 * exponent2 INTEGER, -- d mod (q-1)
90 * coefficient INTEGER, -- (inverse of q) mod p
91 * otherPrimeInfos OtherPrimeInfos OPTIONAL
92 * }
93 * Or, for a public key, the same structure with only
94 * version, modulus and publicExponent.
95 */
96 *p = buffer + buffer_size;
97 if( keypair )
98 {
99 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
100 asn1_write_10x( p, buffer, half_bits, 1 ) );
101 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
102 asn1_write_10x( p, buffer, half_bits, 1 ) );
103 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
104 asn1_write_10x( p, buffer, half_bits, 1 ) );
105 MBEDTLS_ASN1_CHK_ADD( len, /* q */
106 asn1_write_10x( p, buffer, half_bits, 1 ) );
107 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
108 asn1_write_10x( p, buffer, half_bits, 3 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* d */
110 asn1_write_10x( p, buffer, bits, 1 ) );
111 }
112 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
113 asn1_write_10x( p, buffer, 17, 1 ) );
114 MBEDTLS_ASN1_CHK_ADD( len, /* n */
115 asn1_write_10x( p, buffer, bits, 1 ) );
116 if( keypair )
117 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
118 mbedtls_asn1_write_int( p, buffer, 0 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
120 {
121 const unsigned char tag =
122 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
123 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
124 }
125 return( len );
126}
127
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100128int exercise_mac_setup( psa_key_type_t key_type,
129 const unsigned char *key_bytes,
130 size_t key_length,
131 psa_algorithm_t alg,
132 psa_mac_operation_t *operation,
133 psa_status_t *status )
134{
Ronald Cron5425a212020-08-04 14:58:35 +0200135 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200136 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100137
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100138 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200139 psa_set_key_algorithm( &attributes, alg );
140 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200141 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100142
Ronald Cron5425a212020-08-04 14:58:35 +0200143 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100144 /* Whether setup succeeded or failed, abort must succeed. */
145 PSA_ASSERT( psa_mac_abort( operation ) );
146 /* If setup failed, reproduce the failure, so that the caller can
147 * test the resulting state of the operation object. */
148 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100149 {
Ronald Cron5425a212020-08-04 14:58:35 +0200150 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100151 }
152
Ronald Cron5425a212020-08-04 14:58:35 +0200153 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100154 return( 1 );
155
156exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200157 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100158 return( 0 );
159}
160
161int exercise_cipher_setup( psa_key_type_t key_type,
162 const unsigned char *key_bytes,
163 size_t key_length,
164 psa_algorithm_t alg,
165 psa_cipher_operation_t *operation,
166 psa_status_t *status )
167{
Ronald Cron5425a212020-08-04 14:58:35 +0200168 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200169 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100170
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200171 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
172 psa_set_key_algorithm( &attributes, alg );
173 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200174 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100175
Ronald Cron5425a212020-08-04 14:58:35 +0200176 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100177 /* Whether setup succeeded or failed, abort must succeed. */
178 PSA_ASSERT( psa_cipher_abort( operation ) );
179 /* If setup failed, reproduce the failure, so that the caller can
180 * test the resulting state of the operation object. */
181 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100182 {
Ronald Cron5425a212020-08-04 14:58:35 +0200183 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100184 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185 }
186
Ronald Cron5425a212020-08-04 14:58:35 +0200187 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100188 return( 1 );
189
190exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200191 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100192 return( 0 );
193}
194
Ronald Cron5425a212020-08-04 14:58:35 +0200195static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200196{
197 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200198 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200199 uint8_t buffer[1];
200 size_t length;
201 int ok = 0;
202
Ronald Cronecfb2372020-07-23 17:13:42 +0200203 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200204 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
205 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
206 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200207 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Ronald Cron432e19c2020-09-17 14:12:30 +0200208 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cronecfb2372020-07-23 17:13:42 +0200209 TEST_EQUAL(
210 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
211 TEST_EQUAL(
212 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200213 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200214 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
215 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
216 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
217 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
218
Ronald Cron5425a212020-08-04 14:58:35 +0200219 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +0200220 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cron5425a212020-08-04 14:58:35 +0200221 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200222 buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +0200223 PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200224
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200225 ok = 1;
226
227exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100228 /*
229 * Key attributes may have been returned by psa_get_key_attributes()
230 * thus reset them as required.
231 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200232 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100233
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200234 return( ok );
235}
236
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200237/* Assert that a key isn't reported as having a slot number. */
238#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
239#define ASSERT_NO_SLOT_NUMBER( attributes ) \
240 do \
241 { \
242 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
243 TEST_EQUAL( psa_get_key_slot_number( \
244 attributes, \
245 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
246 PSA_ERROR_INVALID_ARGUMENT ); \
247 } \
248 while( 0 )
249#else /* MBEDTLS_PSA_CRYPTO_SE_C */
250#define ASSERT_NO_SLOT_NUMBER( attributes ) \
251 ( (void) 0 )
252#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
253
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100254/* An overapproximation of the amount of storage needed for a key of the
255 * given type and with the given content. The API doesn't make it easy
256 * to find a good value for the size. The current implementation doesn't
257 * care about the value anyway. */
258#define KEY_BITS_FROM_DATA( type, data ) \
259 ( data )->len
260
Darryl Green0c6575a2018-11-07 16:05:30 +0000261typedef enum {
262 IMPORT_KEY = 0,
263 GENERATE_KEY = 1,
264 DERIVE_KEY = 2
265} generate_method;
266
Gilles Peskinee59236f2018-01-27 23:32:46 +0100267/* END_HEADER */
268
269/* BEGIN_DEPENDENCIES
270 * depends_on:MBEDTLS_PSA_CRYPTO_C
271 * END_DEPENDENCIES
272 */
273
274/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200275void static_checks( )
276{
277 size_t max_truncated_mac_size =
278 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
279
280 /* Check that the length for a truncated MAC always fits in the algorithm
281 * encoding. The shifted mask is the maximum truncated value. The
282 * untruncated algorithm may be one byte larger. */
283 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +0100284
285#if defined(MBEDTLS_TEST_DEPRECATED)
286 /* Check deprecated constants. */
287 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
288 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
289 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
290 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
291 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
292 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
293 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
294 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100295
Paul Elliott8ff510a2020-06-02 17:19:28 +0100296 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
297 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
298 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
299 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
300 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
301 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
302 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
303 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
304 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
305 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
306 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
307 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
308 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
309 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
310 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
311 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
312 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
313 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
314 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
315 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
316 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
317 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
318 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
319 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
320 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
321 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
322 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
323 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
324 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
325 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
326
327 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
328 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
329 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
330 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
331 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
332 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
333 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
334 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100335
Paul Elliott75e27032020-06-03 15:17:39 +0100336 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
337 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
338 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
339 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
340 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
341
342 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
343 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100344#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200345}
346/* END_CASE */
347
348/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200349void import_with_policy( int type_arg,
350 int usage_arg, int alg_arg,
351 int expected_status_arg )
352{
353 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
354 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200355 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200356 psa_key_type_t type = type_arg;
357 psa_key_usage_t usage = usage_arg;
358 psa_algorithm_t alg = alg_arg;
359 psa_status_t expected_status = expected_status_arg;
360 const uint8_t key_material[16] = {0};
361 psa_status_t status;
362
363 PSA_ASSERT( psa_crypto_init( ) );
364
365 psa_set_key_type( &attributes, type );
366 psa_set_key_usage_flags( &attributes, usage );
367 psa_set_key_algorithm( &attributes, alg );
368
369 status = psa_import_key( &attributes,
370 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200371 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200372 TEST_EQUAL( status, expected_status );
373 if( status != PSA_SUCCESS )
374 goto exit;
375
Ronald Cron5425a212020-08-04 14:58:35 +0200376 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200377 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
378 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
379 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200380 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200381
Ronald Cron5425a212020-08-04 14:58:35 +0200382 PSA_ASSERT( psa_destroy_key( key ) );
383 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200384
385exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100386 /*
387 * Key attributes may have been returned by psa_get_key_attributes()
388 * thus reset them as required.
389 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200390 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100391
392 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200393 PSA_DONE( );
394}
395/* END_CASE */
396
397/* BEGIN_CASE */
398void import_with_data( data_t *data, int type_arg,
399 int attr_bits_arg,
400 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200401{
402 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
403 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200404 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200405 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200406 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200407 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100408 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100409
Gilles Peskine8817f612018-12-18 00:18:46 +0100410 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100411
Gilles Peskine4747d192019-04-17 15:05:45 +0200412 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200413 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200414
Ronald Cron5425a212020-08-04 14:58:35 +0200415 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100416 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200417 if( status != PSA_SUCCESS )
418 goto exit;
419
Ronald Cron5425a212020-08-04 14:58:35 +0200420 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200421 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200422 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200423 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200424 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200425
Ronald Cron5425a212020-08-04 14:58:35 +0200426 PSA_ASSERT( psa_destroy_key( key ) );
427 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100428
429exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100430 /*
431 * Key attributes may have been returned by psa_get_key_attributes()
432 * thus reset them as required.
433 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200434 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100435
436 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200437 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100438}
439/* END_CASE */
440
441/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200442void import_large_key( int type_arg, int byte_size_arg,
443 int expected_status_arg )
444{
445 psa_key_type_t type = type_arg;
446 size_t byte_size = byte_size_arg;
447 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
448 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200449 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200450 psa_status_t status;
451 uint8_t *buffer = NULL;
452 size_t buffer_size = byte_size + 1;
453 size_t n;
454
Steven Cooreman69967ce2021-01-18 18:01:08 +0100455 /* Skip the test case if the target running the test cannot
456 * accomodate large keys due to heap size constraints */
457 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200458 memset( buffer, 'K', byte_size );
459
460 PSA_ASSERT( psa_crypto_init( ) );
461
462 /* Try importing the key */
463 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
464 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200465 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100466 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200467 TEST_EQUAL( status, expected_status );
468
469 if( status == PSA_SUCCESS )
470 {
Ronald Cron5425a212020-08-04 14:58:35 +0200471 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200472 TEST_EQUAL( psa_get_key_type( &attributes ), type );
473 TEST_EQUAL( psa_get_key_bits( &attributes ),
474 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200475 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200476 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200477 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200478 for( n = 0; n < byte_size; n++ )
479 TEST_EQUAL( buffer[n], 'K' );
480 for( n = byte_size; n < buffer_size; n++ )
481 TEST_EQUAL( buffer[n], 0 );
482 }
483
484exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100485 /*
486 * Key attributes may have been returned by psa_get_key_attributes()
487 * thus reset them as required.
488 */
489 psa_reset_key_attributes( &attributes );
490
Ronald Cron5425a212020-08-04 14:58:35 +0200491 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200492 PSA_DONE( );
493 mbedtls_free( buffer );
494}
495/* END_CASE */
496
497/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200498void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
499{
Ronald Cron5425a212020-08-04 14:58:35 +0200500 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200501 size_t bits = bits_arg;
502 psa_status_t expected_status = expected_status_arg;
503 psa_status_t status;
504 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200505 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200506 size_t buffer_size = /* Slight overapproximations */
507 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200508 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200509 unsigned char *p;
510 int ret;
511 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200512 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200513
Gilles Peskine8817f612018-12-18 00:18:46 +0100514 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200515 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200516
517 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
518 bits, keypair ) ) >= 0 );
519 length = ret;
520
521 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200522 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200523 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100524 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200525
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200526 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200527 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200528
529exit:
530 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200531 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200532}
533/* END_CASE */
534
535/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300536void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300537 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200538 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100539 int expected_bits,
540 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200541 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100542 int canonical_input )
543{
Ronald Cron5425a212020-08-04 14:58:35 +0200544 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100545 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200546 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200547 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100548 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100549 unsigned char *exported = NULL;
550 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100551 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100552 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100553 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200554 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200555 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100556
Moran Pekercb088e72018-07-17 17:36:59 +0300557 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200558 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100559 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200560 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100561 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100562
Gilles Peskine4747d192019-04-17 15:05:45 +0200563 psa_set_key_usage_flags( &attributes, usage_arg );
564 psa_set_key_algorithm( &attributes, alg );
565 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700566
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100567 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200568 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100569
570 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200571 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200572 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
573 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200574 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100575
576 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200577 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100578 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100579
580 /* The exported length must be set by psa_export_key() to a value between 0
581 * and export_size. On errors, the exported length must be 0. */
582 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
583 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
584 TEST_ASSERT( exported_length <= export_size );
585
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200586 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200587 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100588 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200589 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100590 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100591 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200592 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100593
Gilles Peskineea38a922021-02-13 00:05:16 +0100594 /* Run sanity checks on the exported key. For non-canonical inputs,
595 * this validates the canonical representations. For canonical inputs,
596 * this doesn't directly validate the implementation, but it still helps
597 * by cross-validating the test data with the sanity check code. */
598 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200599 goto exit;
600
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100601 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200602 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100603 else
604 {
Ronald Cron5425a212020-08-04 14:58:35 +0200605 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200606 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200607 &key2 ) );
608 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100609 reexported,
610 export_size,
611 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200612 ASSERT_COMPARE( exported, exported_length,
613 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200614 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100615 }
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100616 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100617
618destroy:
619 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200620 PSA_ASSERT( psa_destroy_key( key ) );
621 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100622
623exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100624 /*
625 * Key attributes may have been returned by psa_get_key_attributes()
626 * thus reset them as required.
627 */
628 psa_reset_key_attributes( &got_attributes );
629
itayzafrir3e02b3b2018-06-12 17:06:52 +0300630 mbedtls_free( exported );
631 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200632 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100633}
634/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100635
Moran Pekerf709f4a2018-06-06 17:26:04 +0300636/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300637void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200638 int type_arg,
639 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100640 int export_size_delta,
641 int expected_export_status_arg,
642 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300643{
Ronald Cron5425a212020-08-04 14:58:35 +0200644 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300645 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200646 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200647 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300648 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300649 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100650 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100651 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200652 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300653
Gilles Peskine8817f612018-12-18 00:18:46 +0100654 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300655
Gilles Peskine4747d192019-04-17 15:05:45 +0200656 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
657 psa_set_key_algorithm( &attributes, alg );
658 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300659
660 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200661 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300662
Gilles Peskine49c25912018-10-29 15:15:31 +0100663 /* Export the public key */
664 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200665 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200666 exported, export_size,
667 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100668 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100669 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100670 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200671 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100672 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200673 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200674 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100675 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100676 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +0100677 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
678 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100679 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300680
681exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100682 /*
683 * Key attributes may have been returned by psa_get_key_attributes()
684 * thus reset them as required.
685 */
686 psa_reset_key_attributes( &attributes );
687
itayzafrir3e02b3b2018-06-12 17:06:52 +0300688 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200689 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200690 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300691}
692/* END_CASE */
693
Gilles Peskine20035e32018-02-03 22:44:14 +0100694/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200695void import_and_exercise_key( data_t *data,
696 int type_arg,
697 int bits_arg,
698 int alg_arg )
699{
Ronald Cron5425a212020-08-04 14:58:35 +0200700 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200701 psa_key_type_t type = type_arg;
702 size_t bits = bits_arg;
703 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100704 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200705 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200706 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200707
Gilles Peskine8817f612018-12-18 00:18:46 +0100708 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200709
Gilles Peskine4747d192019-04-17 15:05:45 +0200710 psa_set_key_usage_flags( &attributes, usage );
711 psa_set_key_algorithm( &attributes, alg );
712 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200713
714 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200715 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200716
717 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200718 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200719 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
720 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200721
722 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100723 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200724 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200725
Ronald Cron5425a212020-08-04 14:58:35 +0200726 PSA_ASSERT( psa_destroy_key( key ) );
727 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200728
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200729exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100730 /*
731 * Key attributes may have been returned by psa_get_key_attributes()
732 * thus reset them as required.
733 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200734 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100735
736 psa_reset_key_attributes( &attributes );
737 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200738 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200739}
740/* END_CASE */
741
742/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100743void effective_key_attributes( int type_arg, int expected_type_arg,
744 int bits_arg, int expected_bits_arg,
745 int usage_arg, int expected_usage_arg,
746 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200747{
Ronald Cron5425a212020-08-04 14:58:35 +0200748 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100749 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100750 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100751 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100752 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200753 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100754 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200755 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100756 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200757 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200758
Gilles Peskine8817f612018-12-18 00:18:46 +0100759 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200760
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200761 psa_set_key_usage_flags( &attributes, usage );
762 psa_set_key_algorithm( &attributes, alg );
763 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +0100764 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +0200765
Ronald Cron5425a212020-08-04 14:58:35 +0200766 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +0100767 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +0200768
Ronald Cron5425a212020-08-04 14:58:35 +0200769 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +0100770 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
771 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
772 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
773 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200774
775exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100776 /*
777 * Key attributes may have been returned by psa_get_key_attributes()
778 * thus reset them as required.
779 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200780 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100781
782 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200783 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +0200784}
785/* END_CASE */
786
787/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100788void check_key_policy( int type_arg, int bits_arg,
789 int usage_arg, int alg_arg )
790{
791 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
792 usage_arg, usage_arg, alg_arg, alg_arg );
793 goto exit;
794}
795/* END_CASE */
796
797/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200798void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +0000799{
800 /* Test each valid way of initializing the object, except for `= {0}`, as
801 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
802 * though it's OK by the C standard. We could test for this, but we'd need
803 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200804 psa_key_attributes_t func = psa_key_attributes_init( );
805 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
806 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000807
808 memset( &zero, 0, sizeof( zero ) );
809
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200810 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
811 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
812 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000813
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200814 TEST_EQUAL( psa_get_key_type( &func ), 0 );
815 TEST_EQUAL( psa_get_key_type( &init ), 0 );
816 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
817
818 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
819 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
820 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
821
822 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
823 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
824 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
825
826 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
827 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
828 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +0000829}
830/* END_CASE */
831
832/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200833void mac_key_policy( int policy_usage,
834 int policy_alg,
835 int key_type,
836 data_t *key_data,
837 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200838{
Ronald Cron5425a212020-08-04 14:58:35 +0200839 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200840 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000841 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200842 psa_status_t status;
843 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200844
Gilles Peskine8817f612018-12-18 00:18:46 +0100845 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200846
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200847 psa_set_key_usage_flags( &attributes, policy_usage );
848 psa_set_key_algorithm( &attributes, policy_alg );
849 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200850
Gilles Peskine049c7532019-05-15 20:22:09 +0200851 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200852 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200853
Ronald Cron5425a212020-08-04 14:58:35 +0200854 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200855 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100856 ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100857 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200858 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100859 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200860 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200861
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200862 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200863 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200864 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100865 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100866 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200867 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100868 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200869
870exit:
871 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200872 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200873 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200874}
875/* END_CASE */
876
877/* BEGIN_CASE */
878void cipher_key_policy( int policy_usage,
879 int policy_alg,
880 int key_type,
881 data_t *key_data,
882 int exercise_alg )
883{
Ronald Cron5425a212020-08-04 14:58:35 +0200884 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200885 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000886 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200887 psa_status_t status;
888
Gilles Peskine8817f612018-12-18 00:18:46 +0100889 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200890
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200891 psa_set_key_usage_flags( &attributes, policy_usage );
892 psa_set_key_algorithm( &attributes, policy_alg );
893 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200894
Gilles Peskine049c7532019-05-15 20:22:09 +0200895 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200896 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200897
Ronald Cron5425a212020-08-04 14:58:35 +0200898 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200899 if( policy_alg == exercise_alg &&
900 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100901 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200902 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100903 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200904 psa_cipher_abort( &operation );
905
Ronald Cron5425a212020-08-04 14:58:35 +0200906 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200907 if( policy_alg == exercise_alg &&
908 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100909 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200910 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100911 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200912
913exit:
914 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200915 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200916 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200917}
918/* END_CASE */
919
920/* BEGIN_CASE */
921void aead_key_policy( int policy_usage,
922 int policy_alg,
923 int key_type,
924 data_t *key_data,
925 int nonce_length_arg,
926 int tag_length_arg,
927 int exercise_alg )
928{
Ronald Cron5425a212020-08-04 14:58:35 +0200929 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200930 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200931 psa_status_t status;
932 unsigned char nonce[16] = {0};
933 size_t nonce_length = nonce_length_arg;
934 unsigned char tag[16];
935 size_t tag_length = tag_length_arg;
936 size_t output_length;
937
938 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
939 TEST_ASSERT( tag_length <= sizeof( tag ) );
940
Gilles Peskine8817f612018-12-18 00:18:46 +0100941 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200942
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200943 psa_set_key_usage_flags( &attributes, policy_usage );
944 psa_set_key_algorithm( &attributes, policy_alg );
945 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200946
Gilles Peskine049c7532019-05-15 20:22:09 +0200947 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200948 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200949
Ronald Cron5425a212020-08-04 14:58:35 +0200950 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200951 nonce, nonce_length,
952 NULL, 0,
953 NULL, 0,
954 tag, tag_length,
955 &output_length );
956 if( policy_alg == exercise_alg &&
957 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100958 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200959 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100960 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200961
962 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200963 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200964 nonce, nonce_length,
965 NULL, 0,
966 tag, tag_length,
967 NULL, 0,
968 &output_length );
969 if( policy_alg == exercise_alg &&
970 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100971 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200972 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100973 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200974
975exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200976 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200977 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200978}
979/* END_CASE */
980
981/* BEGIN_CASE */
982void asymmetric_encryption_key_policy( int policy_usage,
983 int policy_alg,
984 int key_type,
985 data_t *key_data,
986 int exercise_alg )
987{
Ronald Cron5425a212020-08-04 14:58:35 +0200988 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200989 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200990 psa_status_t status;
991 size_t key_bits;
992 size_t buffer_length;
993 unsigned char *buffer = NULL;
994 size_t output_length;
995
Gilles Peskine8817f612018-12-18 00:18:46 +0100996 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200997
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200998 psa_set_key_usage_flags( &attributes, policy_usage );
999 psa_set_key_algorithm( &attributes, policy_alg );
1000 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001001
Gilles Peskine049c7532019-05-15 20:22:09 +02001002 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001003 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001004
Ronald Cron5425a212020-08-04 14:58:35 +02001005 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001006 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001007 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1008 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001009 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001010
Ronald Cron5425a212020-08-04 14:58:35 +02001011 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001012 NULL, 0,
1013 NULL, 0,
1014 buffer, buffer_length,
1015 &output_length );
1016 if( policy_alg == exercise_alg &&
1017 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001018 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001019 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001020 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001021
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001022 if( buffer_length != 0 )
1023 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001024 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001025 buffer, buffer_length,
1026 NULL, 0,
1027 buffer, buffer_length,
1028 &output_length );
1029 if( policy_alg == exercise_alg &&
1030 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001031 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001032 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001033 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001034
1035exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001036 /*
1037 * Key attributes may have been returned by psa_get_key_attributes()
1038 * thus reset them as required.
1039 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001040 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001041
1042 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001043 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001044 mbedtls_free( buffer );
1045}
1046/* END_CASE */
1047
1048/* BEGIN_CASE */
1049void asymmetric_signature_key_policy( int policy_usage,
1050 int policy_alg,
1051 int key_type,
1052 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001053 int exercise_alg,
1054 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001055{
Ronald Cron5425a212020-08-04 14:58:35 +02001056 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001057 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001058 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001059 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1060 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1061 * compatible with the policy and `payload_length_arg` is supposed to be
1062 * a valid input length to sign. If `payload_length_arg <= 0`,
1063 * `exercise_alg` is supposed to be forbidden by the policy. */
1064 int compatible_alg = payload_length_arg > 0;
1065 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001066 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001067 size_t signature_length;
1068
Gilles Peskine8817f612018-12-18 00:18:46 +01001069 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001070
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001071 psa_set_key_usage_flags( &attributes, policy_usage );
1072 psa_set_key_algorithm( &attributes, policy_alg );
1073 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001074
Gilles Peskine049c7532019-05-15 20:22:09 +02001075 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001076 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001077
Ronald Cron5425a212020-08-04 14:58:35 +02001078 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001079 payload, payload_length,
1080 signature, sizeof( signature ),
1081 &signature_length );
1082 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001083 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001084 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001085 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001086
1087 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001088 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001089 payload, payload_length,
1090 signature, sizeof( signature ) );
1091 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001092 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001093 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001094 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001095
1096exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001097 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001098 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001099}
1100/* END_CASE */
1101
Janos Follathba3fab92019-06-11 14:50:16 +01001102/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001103void derive_key_policy( int policy_usage,
1104 int policy_alg,
1105 int key_type,
1106 data_t *key_data,
1107 int exercise_alg )
1108{
Ronald Cron5425a212020-08-04 14:58:35 +02001109 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001110 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001111 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001112 psa_status_t status;
1113
Gilles Peskine8817f612018-12-18 00:18:46 +01001114 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001115
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001116 psa_set_key_usage_flags( &attributes, policy_usage );
1117 psa_set_key_algorithm( &attributes, policy_alg );
1118 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001119
Gilles Peskine049c7532019-05-15 20:22:09 +02001120 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001121 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001122
Janos Follathba3fab92019-06-11 14:50:16 +01001123 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1124
1125 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1126 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001127 {
Janos Follathba3fab92019-06-11 14:50:16 +01001128 PSA_ASSERT( psa_key_derivation_input_bytes(
1129 &operation,
1130 PSA_KEY_DERIVATION_INPUT_SEED,
1131 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001132 }
Janos Follathba3fab92019-06-11 14:50:16 +01001133
1134 status = psa_key_derivation_input_key( &operation,
1135 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001136 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001137
Gilles Peskineea0fb492018-07-12 17:17:20 +02001138 if( policy_alg == exercise_alg &&
1139 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001140 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001141 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001142 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001143
1144exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001145 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001146 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001147 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001148}
1149/* END_CASE */
1150
1151/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001152void agreement_key_policy( int policy_usage,
1153 int policy_alg,
1154 int key_type_arg,
1155 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001156 int exercise_alg,
1157 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001158{
Ronald Cron5425a212020-08-04 14:58:35 +02001159 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001160 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001161 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001162 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001163 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001164 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001165
Gilles Peskine8817f612018-12-18 00:18:46 +01001166 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001167
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001168 psa_set_key_usage_flags( &attributes, policy_usage );
1169 psa_set_key_algorithm( &attributes, policy_alg );
1170 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001171
Gilles Peskine049c7532019-05-15 20:22:09 +02001172 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001173 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001174
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001175 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001176 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001177
Steven Cooremance48e852020-10-05 16:02:45 +02001178 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001179
1180exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001181 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001182 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001183 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001184}
1185/* END_CASE */
1186
1187/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001188void key_policy_alg2( int key_type_arg, data_t *key_data,
1189 int usage_arg, int alg_arg, int alg2_arg )
1190{
Ronald Cron5425a212020-08-04 14:58:35 +02001191 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001192 psa_key_type_t key_type = key_type_arg;
1193 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1194 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1195 psa_key_usage_t usage = usage_arg;
1196 psa_algorithm_t alg = alg_arg;
1197 psa_algorithm_t alg2 = alg2_arg;
1198
1199 PSA_ASSERT( psa_crypto_init( ) );
1200
1201 psa_set_key_usage_flags( &attributes, usage );
1202 psa_set_key_algorithm( &attributes, alg );
1203 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1204 psa_set_key_type( &attributes, key_type );
1205 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001206 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001207
Ronald Cron5425a212020-08-04 14:58:35 +02001208 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001209 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1210 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1211 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1212
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001213 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001214 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001215 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001216 goto exit;
1217
1218exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001219 /*
1220 * Key attributes may have been returned by psa_get_key_attributes()
1221 * thus reset them as required.
1222 */
1223 psa_reset_key_attributes( &got_attributes );
1224
Ronald Cron5425a212020-08-04 14:58:35 +02001225 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001226 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001227}
1228/* END_CASE */
1229
1230/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001231void raw_agreement_key_policy( int policy_usage,
1232 int policy_alg,
1233 int key_type_arg,
1234 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001235 int exercise_alg,
1236 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001237{
Ronald Cron5425a212020-08-04 14:58:35 +02001238 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001239 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001240 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001241 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001242 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001243 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001244
1245 PSA_ASSERT( psa_crypto_init( ) );
1246
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001247 psa_set_key_usage_flags( &attributes, policy_usage );
1248 psa_set_key_algorithm( &attributes, policy_alg );
1249 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001250
Gilles Peskine049c7532019-05-15 20:22:09 +02001251 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001252 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001253
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001254 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001255
Steven Cooremance48e852020-10-05 16:02:45 +02001256 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001257
1258exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001259 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001260 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001261 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001262}
1263/* END_CASE */
1264
1265/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001266void copy_success( int source_usage_arg,
1267 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001268 int type_arg, data_t *material,
1269 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001270 int target_usage_arg,
1271 int target_alg_arg, int target_alg2_arg,
1272 int expected_usage_arg,
1273 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001274{
Gilles Peskineca25db92019-04-19 11:43:08 +02001275 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1276 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001277 psa_key_usage_t expected_usage = expected_usage_arg;
1278 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001279 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001280 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1281 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001282 uint8_t *export_buffer = NULL;
1283
Gilles Peskine57ab7212019-01-28 13:03:09 +01001284 PSA_ASSERT( psa_crypto_init( ) );
1285
Gilles Peskineca25db92019-04-19 11:43:08 +02001286 /* Prepare the source key. */
1287 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1288 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001289 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001290 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001291 PSA_ASSERT( psa_import_key( &source_attributes,
1292 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001293 &source_key ) );
1294 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001295
Gilles Peskineca25db92019-04-19 11:43:08 +02001296 /* Prepare the target attributes. */
1297 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001298 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001299 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001300 /* Set volatile lifetime to reset the key identifier to 0. */
1301 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1302 }
1303
Gilles Peskineca25db92019-04-19 11:43:08 +02001304 if( target_usage_arg != -1 )
1305 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1306 if( target_alg_arg != -1 )
1307 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001308 if( target_alg2_arg != -1 )
1309 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001310
1311 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001312 PSA_ASSERT( psa_copy_key( source_key,
1313 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001314
1315 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001316 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001317
1318 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001319 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001320 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1321 psa_get_key_type( &target_attributes ) );
1322 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1323 psa_get_key_bits( &target_attributes ) );
1324 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1325 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001326 TEST_EQUAL( expected_alg2,
1327 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001328 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1329 {
1330 size_t length;
1331 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001332 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001333 material->len, &length ) );
1334 ASSERT_COMPARE( material->x, material->len,
1335 export_buffer, length );
1336 }
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001337 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001338 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001339 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001340 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001341
Ronald Cron5425a212020-08-04 14:58:35 +02001342 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001343
1344exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001345 /*
1346 * Source and target key attributes may have been returned by
1347 * psa_get_key_attributes() thus reset them as required.
1348 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001349 psa_reset_key_attributes( &source_attributes );
1350 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001351
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001352 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001353 mbedtls_free( export_buffer );
1354}
1355/* END_CASE */
1356
1357/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001358void copy_fail( int source_usage_arg,
1359 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001360 int type_arg, data_t *material,
1361 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001362 int target_usage_arg,
1363 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001364 int expected_status_arg )
1365{
1366 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1367 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001368 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1369 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine4a644642019-05-03 17:14:08 +02001370
1371 PSA_ASSERT( psa_crypto_init( ) );
1372
1373 /* Prepare the source key. */
1374 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1375 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001376 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001377 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001378 PSA_ASSERT( psa_import_key( &source_attributes,
1379 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001380 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001381
1382 /* Prepare the target attributes. */
1383 psa_set_key_type( &target_attributes, target_type_arg );
1384 psa_set_key_bits( &target_attributes, target_bits_arg );
1385 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1386 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001387 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001388
1389 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001390 TEST_EQUAL( psa_copy_key( source_key,
1391 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001392 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001393
Ronald Cron5425a212020-08-04 14:58:35 +02001394 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001395
Gilles Peskine4a644642019-05-03 17:14:08 +02001396exit:
1397 psa_reset_key_attributes( &source_attributes );
1398 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001399 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001400}
1401/* END_CASE */
1402
1403/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001404void hash_operation_init( )
1405{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001406 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001407 /* Test each valid way of initializing the object, except for `= {0}`, as
1408 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1409 * though it's OK by the C standard. We could test for this, but we'd need
1410 * to supress the Clang warning for the test. */
1411 psa_hash_operation_t func = psa_hash_operation_init( );
1412 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1413 psa_hash_operation_t zero;
1414
1415 memset( &zero, 0, sizeof( zero ) );
1416
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001417 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001418 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1419 PSA_ERROR_BAD_STATE );
1420 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1421 PSA_ERROR_BAD_STATE );
1422 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1423 PSA_ERROR_BAD_STATE );
1424
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001425 /* A default hash operation should be abortable without error. */
1426 PSA_ASSERT( psa_hash_abort( &func ) );
1427 PSA_ASSERT( psa_hash_abort( &init ) );
1428 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001429}
1430/* END_CASE */
1431
1432/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001433void hash_setup( int alg_arg,
1434 int expected_status_arg )
1435{
1436 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001437 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001438 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001439 psa_status_t status;
1440
Gilles Peskine8817f612018-12-18 00:18:46 +01001441 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001442
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001443 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001444 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001445
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001446 /* Whether setup succeeded or failed, abort must succeed. */
1447 PSA_ASSERT( psa_hash_abort( &operation ) );
1448
1449 /* If setup failed, reproduce the failure, so as to
1450 * test the resulting state of the operation object. */
1451 if( status != PSA_SUCCESS )
1452 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1453
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001454 /* Now the operation object should be reusable. */
1455#if defined(KNOWN_SUPPORTED_HASH_ALG)
1456 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1457 PSA_ASSERT( psa_hash_abort( &operation ) );
1458#endif
1459
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001460exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001461 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001462}
1463/* END_CASE */
1464
1465/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001466void hash_compute_fail( int alg_arg, data_t *input,
1467 int output_size_arg, int expected_status_arg )
1468{
1469 psa_algorithm_t alg = alg_arg;
1470 uint8_t *output = NULL;
1471 size_t output_size = output_size_arg;
1472 size_t output_length = INVALID_EXPORT_LENGTH;
1473 psa_status_t expected_status = expected_status_arg;
1474 psa_status_t status;
1475
1476 ASSERT_ALLOC( output, output_size );
1477
1478 PSA_ASSERT( psa_crypto_init( ) );
1479
1480 status = psa_hash_compute( alg, input->x, input->len,
1481 output, output_size, &output_length );
1482 TEST_EQUAL( status, expected_status );
1483 TEST_ASSERT( output_length <= output_size );
1484
1485exit:
1486 mbedtls_free( output );
1487 PSA_DONE( );
1488}
1489/* END_CASE */
1490
1491/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001492void hash_compare_fail( int alg_arg, data_t *input,
1493 data_t *reference_hash,
1494 int expected_status_arg )
1495{
1496 psa_algorithm_t alg = alg_arg;
1497 psa_status_t expected_status = expected_status_arg;
1498 psa_status_t status;
1499
1500 PSA_ASSERT( psa_crypto_init( ) );
1501
1502 status = psa_hash_compare( alg, input->x, input->len,
1503 reference_hash->x, reference_hash->len );
1504 TEST_EQUAL( status, expected_status );
1505
1506exit:
1507 PSA_DONE( );
1508}
1509/* END_CASE */
1510
1511/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001512void hash_compute_compare( int alg_arg, data_t *input,
1513 data_t *expected_output )
1514{
1515 psa_algorithm_t alg = alg_arg;
1516 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1517 size_t output_length = INVALID_EXPORT_LENGTH;
1518 size_t i;
1519
1520 PSA_ASSERT( psa_crypto_init( ) );
1521
1522 /* Compute with tight buffer */
1523 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001524 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001525 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001526 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001527 ASSERT_COMPARE( output, output_length,
1528 expected_output->x, expected_output->len );
1529
1530 /* Compute with larger buffer */
1531 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1532 output, sizeof( output ),
1533 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001534 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001535 ASSERT_COMPARE( output, output_length,
1536 expected_output->x, expected_output->len );
1537
1538 /* Compare with correct hash */
1539 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1540 output, output_length ) );
1541
1542 /* Compare with trailing garbage */
1543 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1544 output, output_length + 1 ),
1545 PSA_ERROR_INVALID_SIGNATURE );
1546
1547 /* Compare with truncated hash */
1548 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1549 output, output_length - 1 ),
1550 PSA_ERROR_INVALID_SIGNATURE );
1551
1552 /* Compare with corrupted value */
1553 for( i = 0; i < output_length; i++ )
1554 {
Chris Jones9634bb12021-01-20 15:56:42 +00001555 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001556 output[i] ^= 1;
1557 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1558 output, output_length ),
1559 PSA_ERROR_INVALID_SIGNATURE );
1560 output[i] ^= 1;
1561 }
1562
1563exit:
1564 PSA_DONE( );
1565}
1566/* END_CASE */
1567
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001568/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001569void hash_bad_order( )
1570{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001571 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001572 unsigned char input[] = "";
1573 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001574 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001575 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1576 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1577 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001578 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001579 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001580 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001581
Gilles Peskine8817f612018-12-18 00:18:46 +01001582 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001583
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001584 /* Call setup twice in a row. */
1585 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1586 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1587 PSA_ERROR_BAD_STATE );
1588 PSA_ASSERT( psa_hash_abort( &operation ) );
1589
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001590 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001591 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001592 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001593 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001594
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001595 /* Call update after finish. */
1596 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1597 PSA_ASSERT( psa_hash_finish( &operation,
1598 hash, sizeof( hash ), &hash_len ) );
1599 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001600 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001601 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001602
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001603 /* Call verify without calling setup beforehand. */
1604 TEST_EQUAL( psa_hash_verify( &operation,
1605 valid_hash, sizeof( valid_hash ) ),
1606 PSA_ERROR_BAD_STATE );
1607 PSA_ASSERT( psa_hash_abort( &operation ) );
1608
1609 /* Call verify after finish. */
1610 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1611 PSA_ASSERT( psa_hash_finish( &operation,
1612 hash, sizeof( hash ), &hash_len ) );
1613 TEST_EQUAL( psa_hash_verify( &operation,
1614 valid_hash, sizeof( valid_hash ) ),
1615 PSA_ERROR_BAD_STATE );
1616 PSA_ASSERT( psa_hash_abort( &operation ) );
1617
1618 /* Call verify twice in a row. */
1619 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1620 PSA_ASSERT( psa_hash_verify( &operation,
1621 valid_hash, sizeof( valid_hash ) ) );
1622 TEST_EQUAL( psa_hash_verify( &operation,
1623 valid_hash, sizeof( valid_hash ) ),
1624 PSA_ERROR_BAD_STATE );
1625 PSA_ASSERT( psa_hash_abort( &operation ) );
1626
1627 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001628 TEST_EQUAL( psa_hash_finish( &operation,
1629 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001630 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001631 PSA_ASSERT( psa_hash_abort( &operation ) );
1632
1633 /* Call finish twice in a row. */
1634 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1635 PSA_ASSERT( psa_hash_finish( &operation,
1636 hash, sizeof( hash ), &hash_len ) );
1637 TEST_EQUAL( psa_hash_finish( &operation,
1638 hash, sizeof( hash ), &hash_len ),
1639 PSA_ERROR_BAD_STATE );
1640 PSA_ASSERT( psa_hash_abort( &operation ) );
1641
1642 /* Call finish after calling verify. */
1643 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1644 PSA_ASSERT( psa_hash_verify( &operation,
1645 valid_hash, sizeof( valid_hash ) ) );
1646 TEST_EQUAL( psa_hash_finish( &operation,
1647 hash, sizeof( hash ), &hash_len ),
1648 PSA_ERROR_BAD_STATE );
1649 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001650
1651exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001652 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001653}
1654/* END_CASE */
1655
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001656/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001657void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001658{
1659 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001660 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1661 * appended to it */
1662 unsigned char hash[] = {
1663 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1664 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1665 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001666 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001667 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001668
Gilles Peskine8817f612018-12-18 00:18:46 +01001669 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001670
itayzafrir27e69452018-11-01 14:26:34 +02001671 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001672 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001673 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001674 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001675
itayzafrir27e69452018-11-01 14:26:34 +02001676 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001677 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001678 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001679 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001680
itayzafrir27e69452018-11-01 14:26:34 +02001681 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001682 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001683 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001684 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001685
itayzafrirec93d302018-10-18 18:01:10 +03001686exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001687 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001688}
1689/* END_CASE */
1690
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001691/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1692void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001693{
1694 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001695 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001696 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001697 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001698 size_t hash_len;
1699
Gilles Peskine8817f612018-12-18 00:18:46 +01001700 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001701
itayzafrir58028322018-10-25 10:22:01 +03001702 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001703 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001704 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001705 hash, expected_size - 1, &hash_len ),
1706 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001707
1708exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001709 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001710}
1711/* END_CASE */
1712
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001713/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1714void hash_clone_source_state( )
1715{
1716 psa_algorithm_t alg = PSA_ALG_SHA_256;
1717 unsigned char hash[PSA_HASH_MAX_SIZE];
1718 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1719 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1720 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1721 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1722 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1723 size_t hash_len;
1724
1725 PSA_ASSERT( psa_crypto_init( ) );
1726 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1727
1728 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1729 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1730 PSA_ASSERT( psa_hash_finish( &op_finished,
1731 hash, sizeof( hash ), &hash_len ) );
1732 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1733 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1734
1735 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1736 PSA_ERROR_BAD_STATE );
1737
1738 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1739 PSA_ASSERT( psa_hash_finish( &op_init,
1740 hash, sizeof( hash ), &hash_len ) );
1741 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1742 PSA_ASSERT( psa_hash_finish( &op_finished,
1743 hash, sizeof( hash ), &hash_len ) );
1744 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1745 PSA_ASSERT( psa_hash_finish( &op_aborted,
1746 hash, sizeof( hash ), &hash_len ) );
1747
1748exit:
1749 psa_hash_abort( &op_source );
1750 psa_hash_abort( &op_init );
1751 psa_hash_abort( &op_setup );
1752 psa_hash_abort( &op_finished );
1753 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001754 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001755}
1756/* END_CASE */
1757
1758/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1759void hash_clone_target_state( )
1760{
1761 psa_algorithm_t alg = PSA_ALG_SHA_256;
1762 unsigned char hash[PSA_HASH_MAX_SIZE];
1763 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1764 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1765 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1766 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1767 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1768 size_t hash_len;
1769
1770 PSA_ASSERT( psa_crypto_init( ) );
1771
1772 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1773 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1774 PSA_ASSERT( psa_hash_finish( &op_finished,
1775 hash, sizeof( hash ), &hash_len ) );
1776 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1777 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1778
1779 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1780 PSA_ASSERT( psa_hash_finish( &op_target,
1781 hash, sizeof( hash ), &hash_len ) );
1782
1783 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1784 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1785 PSA_ERROR_BAD_STATE );
1786 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1787 PSA_ERROR_BAD_STATE );
1788
1789exit:
1790 psa_hash_abort( &op_target );
1791 psa_hash_abort( &op_init );
1792 psa_hash_abort( &op_setup );
1793 psa_hash_abort( &op_finished );
1794 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001795 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001796}
1797/* END_CASE */
1798
itayzafrir58028322018-10-25 10:22:01 +03001799/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001800void mac_operation_init( )
1801{
Jaeden Amero252ef282019-02-15 14:05:35 +00001802 const uint8_t input[1] = { 0 };
1803
Jaeden Amero769ce272019-01-04 11:48:03 +00001804 /* Test each valid way of initializing the object, except for `= {0}`, as
1805 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1806 * though it's OK by the C standard. We could test for this, but we'd need
1807 * to supress the Clang warning for the test. */
1808 psa_mac_operation_t func = psa_mac_operation_init( );
1809 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1810 psa_mac_operation_t zero;
1811
1812 memset( &zero, 0, sizeof( zero ) );
1813
Jaeden Amero252ef282019-02-15 14:05:35 +00001814 /* A freshly-initialized MAC operation should not be usable. */
1815 TEST_EQUAL( psa_mac_update( &func,
1816 input, sizeof( input ) ),
1817 PSA_ERROR_BAD_STATE );
1818 TEST_EQUAL( psa_mac_update( &init,
1819 input, sizeof( input ) ),
1820 PSA_ERROR_BAD_STATE );
1821 TEST_EQUAL( psa_mac_update( &zero,
1822 input, sizeof( input ) ),
1823 PSA_ERROR_BAD_STATE );
1824
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001825 /* A default MAC operation should be abortable without error. */
1826 PSA_ASSERT( psa_mac_abort( &func ) );
1827 PSA_ASSERT( psa_mac_abort( &init ) );
1828 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001829}
1830/* END_CASE */
1831
1832/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001833void mac_setup( int key_type_arg,
1834 data_t *key,
1835 int alg_arg,
1836 int expected_status_arg )
1837{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001838 psa_key_type_t key_type = key_type_arg;
1839 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001840 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001841 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001842 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1843#if defined(KNOWN_SUPPORTED_MAC_ALG)
1844 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1845#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001846
Gilles Peskine8817f612018-12-18 00:18:46 +01001847 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001848
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001849 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1850 &operation, &status ) )
1851 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001852 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001853
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001854 /* The operation object should be reusable. */
1855#if defined(KNOWN_SUPPORTED_MAC_ALG)
1856 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1857 smoke_test_key_data,
1858 sizeof( smoke_test_key_data ),
1859 KNOWN_SUPPORTED_MAC_ALG,
1860 &operation, &status ) )
1861 goto exit;
1862 TEST_EQUAL( status, PSA_SUCCESS );
1863#endif
1864
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001865exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001866 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001867}
1868/* END_CASE */
1869
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001870/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_HMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256 */
Jaeden Amero252ef282019-02-15 14:05:35 +00001871void mac_bad_order( )
1872{
Ronald Cron5425a212020-08-04 14:58:35 +02001873 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001874 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1875 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001876 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001877 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1878 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1879 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001880 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001881 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1882 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1883 size_t sign_mac_length = 0;
1884 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1885 const uint8_t verify_mac[] = {
1886 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1887 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1888 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1889
1890 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001891 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001892 psa_set_key_algorithm( &attributes, alg );
1893 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001894
Ronald Cron5425a212020-08-04 14:58:35 +02001895 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1896 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001897
Jaeden Amero252ef282019-02-15 14:05:35 +00001898 /* Call update without calling setup beforehand. */
1899 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1900 PSA_ERROR_BAD_STATE );
1901 PSA_ASSERT( psa_mac_abort( &operation ) );
1902
1903 /* Call sign finish without calling setup beforehand. */
1904 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1905 &sign_mac_length),
1906 PSA_ERROR_BAD_STATE );
1907 PSA_ASSERT( psa_mac_abort( &operation ) );
1908
1909 /* Call verify finish without calling setup beforehand. */
1910 TEST_EQUAL( psa_mac_verify_finish( &operation,
1911 verify_mac, sizeof( verify_mac ) ),
1912 PSA_ERROR_BAD_STATE );
1913 PSA_ASSERT( psa_mac_abort( &operation ) );
1914
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001915 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001916 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
1917 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001918 PSA_ERROR_BAD_STATE );
1919 PSA_ASSERT( psa_mac_abort( &operation ) );
1920
Jaeden Amero252ef282019-02-15 14:05:35 +00001921 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001922 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001923 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1924 PSA_ASSERT( psa_mac_sign_finish( &operation,
1925 sign_mac, sizeof( sign_mac ),
1926 &sign_mac_length ) );
1927 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1928 PSA_ERROR_BAD_STATE );
1929 PSA_ASSERT( psa_mac_abort( &operation ) );
1930
1931 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001932 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001933 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1934 PSA_ASSERT( psa_mac_verify_finish( &operation,
1935 verify_mac, sizeof( verify_mac ) ) );
1936 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1937 PSA_ERROR_BAD_STATE );
1938 PSA_ASSERT( psa_mac_abort( &operation ) );
1939
1940 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001941 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001942 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1943 PSA_ASSERT( psa_mac_sign_finish( &operation,
1944 sign_mac, sizeof( sign_mac ),
1945 &sign_mac_length ) );
1946 TEST_EQUAL( psa_mac_sign_finish( &operation,
1947 sign_mac, sizeof( sign_mac ),
1948 &sign_mac_length ),
1949 PSA_ERROR_BAD_STATE );
1950 PSA_ASSERT( psa_mac_abort( &operation ) );
1951
1952 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001953 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001954 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1955 PSA_ASSERT( psa_mac_verify_finish( &operation,
1956 verify_mac, sizeof( verify_mac ) ) );
1957 TEST_EQUAL( psa_mac_verify_finish( &operation,
1958 verify_mac, sizeof( verify_mac ) ),
1959 PSA_ERROR_BAD_STATE );
1960 PSA_ASSERT( psa_mac_abort( &operation ) );
1961
1962 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02001963 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001964 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1965 TEST_EQUAL( psa_mac_verify_finish( &operation,
1966 verify_mac, sizeof( verify_mac ) ),
1967 PSA_ERROR_BAD_STATE );
1968 PSA_ASSERT( psa_mac_abort( &operation ) );
1969
1970 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02001971 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001972 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1973 TEST_EQUAL( psa_mac_sign_finish( &operation,
1974 sign_mac, sizeof( sign_mac ),
1975 &sign_mac_length ),
1976 PSA_ERROR_BAD_STATE );
1977 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001978
Ronald Cron5425a212020-08-04 14:58:35 +02001979 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001980
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001981exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001982 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001983}
1984/* END_CASE */
1985
1986/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001987void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02001988 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001989 int alg_arg,
1990 data_t *input,
1991 data_t *expected_mac )
1992{
Ronald Cron5425a212020-08-04 14:58:35 +02001993 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001994 psa_key_type_t key_type = key_type_arg;
1995 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001996 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001997 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02001998 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001999 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002000 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002001 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002002 const size_t output_sizes_to_test[] = {
2003 0,
2004 1,
2005 expected_mac->len - 1,
2006 expected_mac->len,
2007 expected_mac->len + 1,
2008 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002009
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002010 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002011 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002012 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002013
Gilles Peskine8817f612018-12-18 00:18:46 +01002014 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002015
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002016 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002017 psa_set_key_algorithm( &attributes, alg );
2018 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002019
Ronald Cron5425a212020-08-04 14:58:35 +02002020 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2021 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002022
Gilles Peskine8b356b52020-08-25 23:44:59 +02002023 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2024 {
2025 const size_t output_size = output_sizes_to_test[i];
2026 psa_status_t expected_status =
2027 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2028 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002029
Chris Jones9634bb12021-01-20 15:56:42 +00002030 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002031 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002032
Gilles Peskine8b356b52020-08-25 23:44:59 +02002033 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002034 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002035 PSA_ASSERT( psa_mac_update( &operation,
2036 input->x, input->len ) );
2037 TEST_EQUAL( psa_mac_sign_finish( &operation,
2038 actual_mac, output_size,
2039 &mac_length ),
2040 expected_status );
2041 PSA_ASSERT( psa_mac_abort( &operation ) );
2042
2043 if( expected_status == PSA_SUCCESS )
2044 {
2045 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2046 actual_mac, mac_length );
2047 }
2048 mbedtls_free( actual_mac );
2049 actual_mac = NULL;
2050 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002051
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002052exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002053 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002054 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002055 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002056 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002057}
2058/* END_CASE */
2059
2060/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002061void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002062 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002063 int alg_arg,
2064 data_t *input,
2065 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002066{
Ronald Cron5425a212020-08-04 14:58:35 +02002067 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002068 psa_key_type_t key_type = key_type_arg;
2069 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002070 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002071 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002072 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002073
Gilles Peskine69c12672018-06-28 00:07:19 +02002074 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2075
Gilles Peskine8817f612018-12-18 00:18:46 +01002076 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002077
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002078 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002079 psa_set_key_algorithm( &attributes, alg );
2080 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002081
Ronald Cron5425a212020-08-04 14:58:35 +02002082 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2083 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002084
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002085 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002086 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002087 PSA_ASSERT( psa_mac_update( &operation,
2088 input->x, input->len ) );
2089 PSA_ASSERT( psa_mac_verify_finish( &operation,
2090 expected_mac->x,
2091 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002092
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002093 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002094 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002095 PSA_ASSERT( psa_mac_update( &operation,
2096 input->x, input->len ) );
2097 TEST_EQUAL( psa_mac_verify_finish( &operation,
2098 expected_mac->x,
2099 expected_mac->len - 1 ),
2100 PSA_ERROR_INVALID_SIGNATURE );
2101
2102 /* Test a MAC that's too long. */
2103 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2104 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002105 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002106 PSA_ASSERT( psa_mac_update( &operation,
2107 input->x, input->len ) );
2108 TEST_EQUAL( psa_mac_verify_finish( &operation,
2109 perturbed_mac,
2110 expected_mac->len + 1 ),
2111 PSA_ERROR_INVALID_SIGNATURE );
2112
2113 /* Test changing one byte. */
2114 for( size_t i = 0; i < expected_mac->len; i++ )
2115 {
Chris Jones9634bb12021-01-20 15:56:42 +00002116 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002117 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002118 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002119 PSA_ASSERT( psa_mac_update( &operation,
2120 input->x, input->len ) );
2121 TEST_EQUAL( psa_mac_verify_finish( &operation,
2122 perturbed_mac,
2123 expected_mac->len ),
2124 PSA_ERROR_INVALID_SIGNATURE );
2125 perturbed_mac[i] ^= 1;
2126 }
2127
Gilles Peskine8c9def32018-02-08 10:02:12 +01002128exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002129 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002130 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002131 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002132 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002133}
2134/* END_CASE */
2135
2136/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002137void cipher_operation_init( )
2138{
Jaeden Ameroab439972019-02-15 14:12:05 +00002139 const uint8_t input[1] = { 0 };
2140 unsigned char output[1] = { 0 };
2141 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002142 /* Test each valid way of initializing the object, except for `= {0}`, as
2143 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2144 * though it's OK by the C standard. We could test for this, but we'd need
2145 * to supress the Clang warning for the test. */
2146 psa_cipher_operation_t func = psa_cipher_operation_init( );
2147 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2148 psa_cipher_operation_t zero;
2149
2150 memset( &zero, 0, sizeof( zero ) );
2151
Jaeden Ameroab439972019-02-15 14:12:05 +00002152 /* A freshly-initialized cipher operation should not be usable. */
2153 TEST_EQUAL( psa_cipher_update( &func,
2154 input, sizeof( input ),
2155 output, sizeof( output ),
2156 &output_length ),
2157 PSA_ERROR_BAD_STATE );
2158 TEST_EQUAL( psa_cipher_update( &init,
2159 input, sizeof( input ),
2160 output, sizeof( output ),
2161 &output_length ),
2162 PSA_ERROR_BAD_STATE );
2163 TEST_EQUAL( psa_cipher_update( &zero,
2164 input, sizeof( input ),
2165 output, sizeof( output ),
2166 &output_length ),
2167 PSA_ERROR_BAD_STATE );
2168
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002169 /* A default cipher operation should be abortable without error. */
2170 PSA_ASSERT( psa_cipher_abort( &func ) );
2171 PSA_ASSERT( psa_cipher_abort( &init ) );
2172 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002173}
2174/* END_CASE */
2175
2176/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002177void cipher_setup( int key_type_arg,
2178 data_t *key,
2179 int alg_arg,
2180 int expected_status_arg )
2181{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002182 psa_key_type_t key_type = key_type_arg;
2183 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002184 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002185 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002186 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002187#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002188 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2189#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002190
Gilles Peskine8817f612018-12-18 00:18:46 +01002191 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002192
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002193 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2194 &operation, &status ) )
2195 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002196 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002197
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002198 /* The operation object should be reusable. */
2199#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2200 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2201 smoke_test_key_data,
2202 sizeof( smoke_test_key_data ),
2203 KNOWN_SUPPORTED_CIPHER_ALG,
2204 &operation, &status ) )
2205 goto exit;
2206 TEST_EQUAL( status, PSA_SUCCESS );
2207#endif
2208
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002209exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002210 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002211 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002212}
2213/* END_CASE */
2214
Steven Cooreman29eecbf2021-01-28 19:41:25 +01002215/* BEGIN_CASE depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002216void cipher_bad_order( )
2217{
Ronald Cron5425a212020-08-04 14:58:35 +02002218 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002219 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2220 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002221 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002222 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002223 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002224 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002225 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2226 0xaa, 0xaa, 0xaa, 0xaa };
2227 const uint8_t text[] = {
2228 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2229 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002230 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002231 size_t length = 0;
2232
2233 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002234 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2235 psa_set_key_algorithm( &attributes, alg );
2236 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002237 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2238 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002239
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002240 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002241 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2242 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002243 PSA_ERROR_BAD_STATE );
2244 PSA_ASSERT( psa_cipher_abort( &operation ) );
2245
2246 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002247 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2248 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002249 PSA_ERROR_BAD_STATE );
2250 PSA_ASSERT( psa_cipher_abort( &operation ) );
2251
Jaeden Ameroab439972019-02-15 14:12:05 +00002252 /* Generate an IV without calling setup beforehand. */
2253 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2254 buffer, sizeof( buffer ),
2255 &length ),
2256 PSA_ERROR_BAD_STATE );
2257 PSA_ASSERT( psa_cipher_abort( &operation ) );
2258
2259 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002260 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002261 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2262 buffer, sizeof( buffer ),
2263 &length ) );
2264 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2265 buffer, sizeof( buffer ),
2266 &length ),
2267 PSA_ERROR_BAD_STATE );
2268 PSA_ASSERT( psa_cipher_abort( &operation ) );
2269
2270 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002271 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002272 PSA_ASSERT( psa_cipher_set_iv( &operation,
2273 iv, sizeof( iv ) ) );
2274 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2275 buffer, sizeof( buffer ),
2276 &length ),
2277 PSA_ERROR_BAD_STATE );
2278 PSA_ASSERT( psa_cipher_abort( &operation ) );
2279
2280 /* Set an IV without calling setup beforehand. */
2281 TEST_EQUAL( psa_cipher_set_iv( &operation,
2282 iv, sizeof( iv ) ),
2283 PSA_ERROR_BAD_STATE );
2284 PSA_ASSERT( psa_cipher_abort( &operation ) );
2285
2286 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002287 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002288 PSA_ASSERT( psa_cipher_set_iv( &operation,
2289 iv, sizeof( iv ) ) );
2290 TEST_EQUAL( psa_cipher_set_iv( &operation,
2291 iv, sizeof( iv ) ),
2292 PSA_ERROR_BAD_STATE );
2293 PSA_ASSERT( psa_cipher_abort( &operation ) );
2294
2295 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002296 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002297 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2298 buffer, sizeof( buffer ),
2299 &length ) );
2300 TEST_EQUAL( psa_cipher_set_iv( &operation,
2301 iv, sizeof( iv ) ),
2302 PSA_ERROR_BAD_STATE );
2303 PSA_ASSERT( psa_cipher_abort( &operation ) );
2304
2305 /* Call update without calling setup beforehand. */
2306 TEST_EQUAL( psa_cipher_update( &operation,
2307 text, sizeof( text ),
2308 buffer, sizeof( buffer ),
2309 &length ),
2310 PSA_ERROR_BAD_STATE );
2311 PSA_ASSERT( psa_cipher_abort( &operation ) );
2312
2313 /* Call update without an IV where an IV is required. */
2314 TEST_EQUAL( psa_cipher_update( &operation,
2315 text, sizeof( text ),
2316 buffer, sizeof( buffer ),
2317 &length ),
2318 PSA_ERROR_BAD_STATE );
2319 PSA_ASSERT( psa_cipher_abort( &operation ) );
2320
2321 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002322 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002323 PSA_ASSERT( psa_cipher_set_iv( &operation,
2324 iv, sizeof( iv ) ) );
2325 PSA_ASSERT( psa_cipher_finish( &operation,
2326 buffer, sizeof( buffer ), &length ) );
2327 TEST_EQUAL( psa_cipher_update( &operation,
2328 text, sizeof( text ),
2329 buffer, sizeof( buffer ),
2330 &length ),
2331 PSA_ERROR_BAD_STATE );
2332 PSA_ASSERT( psa_cipher_abort( &operation ) );
2333
2334 /* Call finish without calling setup beforehand. */
2335 TEST_EQUAL( psa_cipher_finish( &operation,
2336 buffer, sizeof( buffer ), &length ),
2337 PSA_ERROR_BAD_STATE );
2338 PSA_ASSERT( psa_cipher_abort( &operation ) );
2339
2340 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002341 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002342 /* Not calling update means we are encrypting an empty buffer, which is OK
2343 * for cipher modes with padding. */
2344 TEST_EQUAL( psa_cipher_finish( &operation,
2345 buffer, sizeof( buffer ), &length ),
2346 PSA_ERROR_BAD_STATE );
2347 PSA_ASSERT( psa_cipher_abort( &operation ) );
2348
2349 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002350 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002351 PSA_ASSERT( psa_cipher_set_iv( &operation,
2352 iv, sizeof( iv ) ) );
2353 PSA_ASSERT( psa_cipher_finish( &operation,
2354 buffer, sizeof( buffer ), &length ) );
2355 TEST_EQUAL( psa_cipher_finish( &operation,
2356 buffer, sizeof( buffer ), &length ),
2357 PSA_ERROR_BAD_STATE );
2358 PSA_ASSERT( psa_cipher_abort( &operation ) );
2359
Ronald Cron5425a212020-08-04 14:58:35 +02002360 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002361
Jaeden Ameroab439972019-02-15 14:12:05 +00002362exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002363 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002364 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002365}
2366/* END_CASE */
2367
2368/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002369void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002370 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002371 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002372 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002373{
Ronald Cron5425a212020-08-04 14:58:35 +02002374 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002375 psa_status_t status;
2376 psa_key_type_t key_type = key_type_arg;
2377 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002378 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002379 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002380 size_t output_buffer_size = 0;
2381 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002382 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002383 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002384 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002385
Gilles Peskine8817f612018-12-18 00:18:46 +01002386 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002387
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002388 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2389 psa_set_key_algorithm( &attributes, alg );
2390 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002391
Ronald Cron5425a212020-08-04 14:58:35 +02002392 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2393 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002394
Ronald Cron5425a212020-08-04 14:58:35 +02002395 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002396
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002397 if( iv->len > 0 )
2398 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002399 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002400 }
2401
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002402 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002403 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002404 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002405
Gilles Peskine8817f612018-12-18 00:18:46 +01002406 PSA_ASSERT( psa_cipher_update( &operation,
2407 input->x, input->len,
2408 output, output_buffer_size,
2409 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002410 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002411 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002412 output + total_output_length,
2413 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002414 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002415 total_output_length += function_output_length;
2416
Gilles Peskinefe11b722018-12-18 00:24:04 +01002417 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002418 if( expected_status == PSA_SUCCESS )
2419 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002420 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002421 ASSERT_COMPARE( expected_output->x, expected_output->len,
2422 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002423 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002424
Gilles Peskine50e586b2018-06-08 14:28:46 +02002425exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002426 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002427 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002428 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002429 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002430}
2431/* END_CASE */
2432
2433/* BEGIN_CASE */
2434void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002435 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002436 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002437 int first_part_size_arg,
2438 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002439 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002440{
Ronald Cron5425a212020-08-04 14:58:35 +02002441 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002442 psa_key_type_t key_type = key_type_arg;
2443 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002444 size_t first_part_size = first_part_size_arg;
2445 size_t output1_length = output1_length_arg;
2446 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002447 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002448 size_t output_buffer_size = 0;
2449 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002450 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002451 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002452 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002453
Gilles Peskine8817f612018-12-18 00:18:46 +01002454 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002455
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002456 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2457 psa_set_key_algorithm( &attributes, alg );
2458 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002459
Ronald Cron5425a212020-08-04 14:58:35 +02002460 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2461 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002462
Ronald Cron5425a212020-08-04 14:58:35 +02002463 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002464
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002465 if( iv->len > 0 )
2466 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002467 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002468 }
2469
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002470 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002471 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002472 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002473
Gilles Peskinee0866522019-02-19 19:44:00 +01002474 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002475 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2476 output, output_buffer_size,
2477 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002478 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002479 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002480 PSA_ASSERT( psa_cipher_update( &operation,
2481 input->x + first_part_size,
2482 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002483 output + total_output_length,
2484 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002485 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002486 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002487 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002488 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002489 output + total_output_length,
2490 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002491 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002492 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002493 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002494
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002495 ASSERT_COMPARE( expected_output->x, expected_output->len,
2496 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002497
2498exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002499 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002500 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002501 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002502 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002503}
2504/* END_CASE */
2505
2506/* BEGIN_CASE */
2507void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002508 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002509 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002510 int first_part_size_arg,
2511 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002512 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002513{
Ronald Cron5425a212020-08-04 14:58:35 +02002514 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002515 psa_key_type_t key_type = key_type_arg;
2516 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002517 size_t first_part_size = first_part_size_arg;
2518 size_t output1_length = output1_length_arg;
2519 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002520 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002521 size_t output_buffer_size = 0;
2522 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002523 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002524 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002525 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002526
Gilles Peskine8817f612018-12-18 00:18:46 +01002527 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002528
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002529 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2530 psa_set_key_algorithm( &attributes, alg );
2531 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002532
Ronald Cron5425a212020-08-04 14:58:35 +02002533 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2534 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002535
Ronald Cron5425a212020-08-04 14:58:35 +02002536 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002537
Steven Cooreman177deba2020-09-07 17:14:14 +02002538 if( iv->len > 0 )
2539 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002540 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002541 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002542
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002543 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002544 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002545 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002546
Gilles Peskinee0866522019-02-19 19:44:00 +01002547 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002548 PSA_ASSERT( psa_cipher_update( &operation,
2549 input->x, first_part_size,
2550 output, output_buffer_size,
2551 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002552 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002553 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002554 PSA_ASSERT( psa_cipher_update( &operation,
2555 input->x + first_part_size,
2556 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002557 output + total_output_length,
2558 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002559 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002560 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002561 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002562 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002563 output + total_output_length,
2564 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002565 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002566 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002567 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002568
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002569 ASSERT_COMPARE( expected_output->x, expected_output->len,
2570 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002571
2572exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002573 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002574 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002575 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002576 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002577}
2578/* END_CASE */
2579
Gilles Peskine50e586b2018-06-08 14:28:46 +02002580/* BEGIN_CASE */
2581void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002582 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002583 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002584 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002585{
Ronald Cron5425a212020-08-04 14:58:35 +02002586 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002587 psa_status_t status;
2588 psa_key_type_t key_type = key_type_arg;
2589 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002590 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002591 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002592 size_t output_buffer_size = 0;
2593 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002594 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002595 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002596 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002597
Gilles Peskine8817f612018-12-18 00:18:46 +01002598 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002599
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002600 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2601 psa_set_key_algorithm( &attributes, alg );
2602 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002603
Ronald Cron5425a212020-08-04 14:58:35 +02002604 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2605 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002606
Ronald Cron5425a212020-08-04 14:58:35 +02002607 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002608
Steven Cooreman177deba2020-09-07 17:14:14 +02002609 if( iv->len > 0 )
2610 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002611 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002612 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002613
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002614 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002615 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002616 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002617
Gilles Peskine8817f612018-12-18 00:18:46 +01002618 PSA_ASSERT( psa_cipher_update( &operation,
2619 input->x, input->len,
2620 output, output_buffer_size,
2621 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002622 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002623 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002624 output + total_output_length,
2625 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002626 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002627 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002628 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002629
2630 if( expected_status == PSA_SUCCESS )
2631 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002632 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002633 ASSERT_COMPARE( expected_output->x, expected_output->len,
2634 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002635 }
2636
Gilles Peskine50e586b2018-06-08 14:28:46 +02002637exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002638 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002639 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002640 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002641 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002642}
2643/* END_CASE */
2644
Gilles Peskine50e586b2018-06-08 14:28:46 +02002645/* BEGIN_CASE */
2646void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002647 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002648 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002649{
Ronald Cron5425a212020-08-04 14:58:35 +02002650 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002651 psa_key_type_t key_type = key_type_arg;
2652 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002653 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002654 size_t iv_size = 16;
2655 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002656 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002657 size_t output1_size = 0;
2658 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002659 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002660 size_t output2_size = 0;
2661 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002662 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002663 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2664 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002665 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002666
Gilles Peskine8817f612018-12-18 00:18:46 +01002667 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002668
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002669 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2670 psa_set_key_algorithm( &attributes, alg );
2671 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002672
Ronald Cron5425a212020-08-04 14:58:35 +02002673 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2674 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002675
Ronald Cron5425a212020-08-04 14:58:35 +02002676 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2677 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002678
Steven Cooreman177deba2020-09-07 17:14:14 +02002679 if( alg != PSA_ALG_ECB_NO_PADDING )
2680 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002681 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2682 iv, iv_size,
2683 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002684 }
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002685 output1_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002686 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002687 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002688
Gilles Peskine8817f612018-12-18 00:18:46 +01002689 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2690 output1, output1_size,
2691 &output1_length ) );
2692 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002693 output1 + output1_length,
2694 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002695 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002696
Gilles Peskine048b7f02018-06-08 14:20:49 +02002697 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002698
Gilles Peskine8817f612018-12-18 00:18:46 +01002699 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002700
2701 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002702 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002703
Steven Cooreman177deba2020-09-07 17:14:14 +02002704 if( iv_length > 0 )
2705 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002706 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2707 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002708 }
2709
Gilles Peskine8817f612018-12-18 00:18:46 +01002710 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2711 output2, output2_size,
2712 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002713 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002714 PSA_ASSERT( psa_cipher_finish( &operation2,
2715 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002716 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002717 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002718
Gilles Peskine048b7f02018-06-08 14:20:49 +02002719 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002720
Gilles Peskine8817f612018-12-18 00:18:46 +01002721 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002722
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002723 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002724
2725exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002726 psa_cipher_abort( &operation1 );
2727 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002728 mbedtls_free( output1 );
2729 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002730 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002731 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03002732}
2733/* END_CASE */
2734
2735/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002736void cipher_verify_output_multipart( int alg_arg,
2737 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002738 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002739 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002740 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03002741{
Ronald Cron5425a212020-08-04 14:58:35 +02002742 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002743 psa_key_type_t key_type = key_type_arg;
2744 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002745 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002746 unsigned char iv[16] = {0};
2747 size_t iv_size = 16;
2748 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002749 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002750 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002751 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002752 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002753 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002754 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002755 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002756 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2757 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002758 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002759
Gilles Peskine8817f612018-12-18 00:18:46 +01002760 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002761
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002762 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2763 psa_set_key_algorithm( &attributes, alg );
2764 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002765
Ronald Cron5425a212020-08-04 14:58:35 +02002766 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2767 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03002768
Ronald Cron5425a212020-08-04 14:58:35 +02002769 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2770 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002771
Steven Cooreman177deba2020-09-07 17:14:14 +02002772 if( alg != PSA_ALG_ECB_NO_PADDING )
2773 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002774 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2775 iv, iv_size,
2776 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002777 }
2778
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002779 output1_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002780 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002781 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002782
Gilles Peskinee0866522019-02-19 19:44:00 +01002783 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002784
Gilles Peskine8817f612018-12-18 00:18:46 +01002785 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2786 output1, output1_buffer_size,
2787 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002788 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002789
Gilles Peskine8817f612018-12-18 00:18:46 +01002790 PSA_ASSERT( psa_cipher_update( &operation1,
2791 input->x + first_part_size,
2792 input->len - first_part_size,
2793 output1, output1_buffer_size,
2794 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002795 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002796
Gilles Peskine8817f612018-12-18 00:18:46 +01002797 PSA_ASSERT( psa_cipher_finish( &operation1,
2798 output1 + output1_length,
2799 output1_buffer_size - output1_length,
2800 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002801 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002802
Gilles Peskine8817f612018-12-18 00:18:46 +01002803 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002804
Gilles Peskine048b7f02018-06-08 14:20:49 +02002805 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002806 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002807
Steven Cooreman177deba2020-09-07 17:14:14 +02002808 if( iv_length > 0 )
2809 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002810 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2811 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002812 }
Moran Pekerded84402018-06-06 16:36:50 +03002813
Gilles Peskine8817f612018-12-18 00:18:46 +01002814 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2815 output2, output2_buffer_size,
2816 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002817 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002818
Gilles Peskine8817f612018-12-18 00:18:46 +01002819 PSA_ASSERT( psa_cipher_update( &operation2,
2820 output1 + first_part_size,
2821 output1_length - first_part_size,
2822 output2, output2_buffer_size,
2823 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002824 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002825
Gilles Peskine8817f612018-12-18 00:18:46 +01002826 PSA_ASSERT( psa_cipher_finish( &operation2,
2827 output2 + output2_length,
2828 output2_buffer_size - output2_length,
2829 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002830 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002831
Gilles Peskine8817f612018-12-18 00:18:46 +01002832 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002833
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002834 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002835
2836exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002837 psa_cipher_abort( &operation1 );
2838 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002839 mbedtls_free( output1 );
2840 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002841 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002842 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002843}
2844/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002845
Gilles Peskine20035e32018-02-03 22:44:14 +01002846/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002847void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002848 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002849 data_t *nonce,
2850 data_t *additional_data,
2851 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002852 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002853{
Ronald Cron5425a212020-08-04 14:58:35 +02002854 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002855 psa_key_type_t key_type = key_type_arg;
2856 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002857 unsigned char *output_data = NULL;
2858 size_t output_size = 0;
2859 size_t output_length = 0;
2860 unsigned char *output_data2 = NULL;
2861 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02002862 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Steven Cooremanf49478b2021-02-15 15:19:25 +01002863 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002864 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002865 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002866
Gilles Peskine4abf7412018-06-18 16:35:34 +02002867 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02002868 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
2869 * should be exact. */
2870 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
2871 TEST_EQUAL( output_size,
2872 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002873 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002874
Gilles Peskine8817f612018-12-18 00:18:46 +01002875 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002876
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002877 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2878 psa_set_key_algorithm( &attributes, alg );
2879 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002880
Gilles Peskine049c7532019-05-15 20:22:09 +02002881 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002882 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002883
Steven Cooremanf49478b2021-02-15 15:19:25 +01002884 status = psa_aead_encrypt( key, alg,
2885 nonce->x, nonce->len,
2886 additional_data->x,
2887 additional_data->len,
2888 input_data->x, input_data->len,
2889 output_data, output_size,
2890 &output_length );
2891
2892 /* If the operation is not supported, just skip and not fail in case the
2893 * encryption involves a common limitation of cryptography hardwares and
2894 * an alternative implementation. */
2895 if( status == PSA_ERROR_NOT_SUPPORTED )
2896 {
2897 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
2898 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
2899 }
2900
2901 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002902
2903 if( PSA_SUCCESS == expected_result )
2904 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002905 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002906
Gilles Peskine003a4a92019-05-14 16:09:40 +02002907 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
2908 * should be exact. */
2909 TEST_EQUAL( input_data->len,
2910 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
2911
Ronald Cron5425a212020-08-04 14:58:35 +02002912 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01002913 nonce->x, nonce->len,
2914 additional_data->x,
2915 additional_data->len,
2916 output_data, output_length,
2917 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002918 &output_length2 ),
2919 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002920
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002921 ASSERT_COMPARE( input_data->x, input_data->len,
2922 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002923 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002924
Gilles Peskinea1cac842018-06-11 19:33:02 +02002925exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002926 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002927 mbedtls_free( output_data );
2928 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002929 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002930}
2931/* END_CASE */
2932
2933/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002934void aead_encrypt( int key_type_arg, data_t *key_data,
2935 int alg_arg,
2936 data_t *nonce,
2937 data_t *additional_data,
2938 data_t *input_data,
2939 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002940{
Ronald Cron5425a212020-08-04 14:58:35 +02002941 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002942 psa_key_type_t key_type = key_type_arg;
2943 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002944 unsigned char *output_data = NULL;
2945 size_t output_size = 0;
2946 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02002947 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002948 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01002949 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002950
Gilles Peskine4abf7412018-06-18 16:35:34 +02002951 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02002952 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
2953 * should be exact. */
2954 TEST_EQUAL( output_size,
2955 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002956 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002957
Gilles Peskine8817f612018-12-18 00:18:46 +01002958 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002959
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002960 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2961 psa_set_key_algorithm( &attributes, alg );
2962 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002963
Gilles Peskine049c7532019-05-15 20:22:09 +02002964 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002965 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002966
Steven Cooremand588ea12021-01-11 19:36:04 +01002967 status = psa_aead_encrypt( key, alg,
2968 nonce->x, nonce->len,
2969 additional_data->x, additional_data->len,
2970 input_data->x, input_data->len,
2971 output_data, output_size,
2972 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002973
Ronald Cron28a45ed2021-02-09 20:35:42 +01002974 /* If the operation is not supported, just skip and not fail in case the
2975 * encryption involves a common limitation of cryptography hardwares and
2976 * an alternative implementation. */
2977 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01002978 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01002979 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
2980 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01002981 }
Steven Cooremand588ea12021-01-11 19:36:04 +01002982
2983 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002984 ASSERT_COMPARE( expected_result->x, expected_result->len,
2985 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002986
Gilles Peskinea1cac842018-06-11 19:33:02 +02002987exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002988 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002989 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002990 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002991}
2992/* END_CASE */
2993
2994/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002995void aead_decrypt( int key_type_arg, data_t *key_data,
2996 int alg_arg,
2997 data_t *nonce,
2998 data_t *additional_data,
2999 data_t *input_data,
3000 data_t *expected_data,
3001 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003002{
Ronald Cron5425a212020-08-04 14:58:35 +02003003 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003004 psa_key_type_t key_type = key_type_arg;
3005 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003006 unsigned char *output_data = NULL;
3007 size_t output_size = 0;
3008 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003009 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003010 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003011 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003012 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003013
Gilles Peskine003a4a92019-05-14 16:09:40 +02003014 output_size = input_data->len - tag_length;
3015 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3016 * should be exact. */
3017 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3018 TEST_EQUAL( output_size,
3019 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003020 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003021
Gilles Peskine8817f612018-12-18 00:18:46 +01003022 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003023
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003024 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3025 psa_set_key_algorithm( &attributes, alg );
3026 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003027
Gilles Peskine049c7532019-05-15 20:22:09 +02003028 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003029 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003030
Steven Cooremand588ea12021-01-11 19:36:04 +01003031 status = psa_aead_decrypt( key, alg,
3032 nonce->x, nonce->len,
3033 additional_data->x,
3034 additional_data->len,
3035 input_data->x, input_data->len,
3036 output_data, output_size,
3037 &output_length );
3038
Ronald Cron28a45ed2021-02-09 20:35:42 +01003039 /* If the operation is not supported, just skip and not fail in case the
3040 * decryption involves a common limitation of cryptography hardwares and
3041 * an alternative implementation. */
3042 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003043 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003044 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3045 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003046 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003047
3048 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003049
Gilles Peskine2d277862018-06-18 15:41:12 +02003050 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003051 ASSERT_COMPARE( expected_data->x, expected_data->len,
3052 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003053
Gilles Peskinea1cac842018-06-11 19:33:02 +02003054exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003055 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003056 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003057 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003058}
3059/* END_CASE */
3060
3061/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003062void signature_size( int type_arg,
3063 int bits,
3064 int alg_arg,
3065 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003066{
3067 psa_key_type_t type = type_arg;
3068 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003069 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003070
Gilles Peskinefe11b722018-12-18 00:24:04 +01003071 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003072#if defined(MBEDTLS_TEST_DEPRECATED)
3073 TEST_EQUAL( actual_size,
3074 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3075#endif /* MBEDTLS_TEST_DEPRECATED */
3076
Gilles Peskinee59236f2018-01-27 23:32:46 +01003077exit:
3078 ;
3079}
3080/* END_CASE */
3081
3082/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003083void sign_deterministic( int key_type_arg, data_t *key_data,
3084 int alg_arg, data_t *input_data,
3085 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003086{
Ronald Cron5425a212020-08-04 14:58:35 +02003087 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003088 psa_key_type_t key_type = key_type_arg;
3089 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003090 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003091 unsigned char *signature = NULL;
3092 size_t signature_size;
3093 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003094 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003095
Gilles Peskine8817f612018-12-18 00:18:46 +01003096 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003097
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003098 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003099 psa_set_key_algorithm( &attributes, alg );
3100 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003101
Gilles Peskine049c7532019-05-15 20:22:09 +02003102 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003103 &key ) );
3104 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003105 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003106
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003107 /* Allocate a buffer which has the size advertized by the
3108 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003109 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003110 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003111 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003112 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003113 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003114
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003115 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003116 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003117 input_data->x, input_data->len,
3118 signature, signature_size,
3119 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003120 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003121 ASSERT_COMPARE( output_data->x, output_data->len,
3122 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003123
Gilles Peskine0627f982019-11-26 19:12:16 +01003124#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003125 memset( signature, 0, signature_size );
3126 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003127 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003128 input_data->x, input_data->len,
3129 signature, signature_size,
3130 &signature_length ) );
3131 ASSERT_COMPARE( output_data->x, output_data->len,
3132 signature, signature_length );
3133#endif /* MBEDTLS_TEST_DEPRECATED */
3134
Gilles Peskine20035e32018-02-03 22:44:14 +01003135exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003136 /*
3137 * Key attributes may have been returned by psa_get_key_attributes()
3138 * thus reset them as required.
3139 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003140 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003141
Ronald Cron5425a212020-08-04 14:58:35 +02003142 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003143 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003144 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003145}
3146/* END_CASE */
3147
3148/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003149void sign_fail( int key_type_arg, data_t *key_data,
3150 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003151 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003152{
Ronald Cron5425a212020-08-04 14:58:35 +02003153 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003154 psa_key_type_t key_type = key_type_arg;
3155 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003156 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003157 psa_status_t actual_status;
3158 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003159 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003160 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003161 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003162
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003163 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003164
Gilles Peskine8817f612018-12-18 00:18:46 +01003165 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003166
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003167 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003168 psa_set_key_algorithm( &attributes, alg );
3169 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003170
Gilles Peskine049c7532019-05-15 20:22:09 +02003171 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003172 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003173
Ronald Cron5425a212020-08-04 14:58:35 +02003174 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003175 input_data->x, input_data->len,
3176 signature, signature_size,
3177 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003178 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003179 /* The value of *signature_length is unspecified on error, but
3180 * whatever it is, it should be less than signature_size, so that
3181 * if the caller tries to read *signature_length bytes without
3182 * checking the error code then they don't overflow a buffer. */
3183 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003184
Gilles Peskine895242b2019-11-29 12:15:40 +01003185#if defined(MBEDTLS_TEST_DEPRECATED)
3186 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003187 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003188 input_data->x, input_data->len,
3189 signature, signature_size,
3190 &signature_length ),
3191 expected_status );
3192 TEST_ASSERT( signature_length <= signature_size );
3193#endif /* MBEDTLS_TEST_DEPRECATED */
3194
Gilles Peskine20035e32018-02-03 22:44:14 +01003195exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003196 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003197 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003198 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003199 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003200}
3201/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003202
3203/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003204void sign_verify( int key_type_arg, data_t *key_data,
3205 int alg_arg, data_t *input_data )
3206{
Ronald Cron5425a212020-08-04 14:58:35 +02003207 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003208 psa_key_type_t key_type = key_type_arg;
3209 psa_algorithm_t alg = alg_arg;
3210 size_t key_bits;
3211 unsigned char *signature = NULL;
3212 size_t signature_size;
3213 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003214 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003215
Gilles Peskine8817f612018-12-18 00:18:46 +01003216 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003217
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003218 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003219 psa_set_key_algorithm( &attributes, alg );
3220 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003221
Gilles Peskine049c7532019-05-15 20:22:09 +02003222 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003223 &key ) );
3224 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003225 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003226
3227 /* Allocate a buffer which has the size advertized by the
3228 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003229 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003230 key_bits, alg );
3231 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003232 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003233 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003234
3235 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003236 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003237 input_data->x, input_data->len,
3238 signature, signature_size,
3239 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003240 /* Check that the signature length looks sensible. */
3241 TEST_ASSERT( signature_length <= signature_size );
3242 TEST_ASSERT( signature_length > 0 );
3243
3244 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003245 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003246 input_data->x, input_data->len,
3247 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003248
3249 if( input_data->len != 0 )
3250 {
3251 /* Flip a bit in the input and verify that the signature is now
3252 * detected as invalid. Flip a bit at the beginning, not at the end,
3253 * because ECDSA may ignore the last few bits of the input. */
3254 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003255 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003256 input_data->x, input_data->len,
3257 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003258 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003259 }
3260
3261exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003262 /*
3263 * Key attributes may have been returned by psa_get_key_attributes()
3264 * thus reset them as required.
3265 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003266 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003267
Ronald Cron5425a212020-08-04 14:58:35 +02003268 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003269 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003270 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003271}
3272/* END_CASE */
3273
3274/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003275void asymmetric_verify( int key_type_arg, data_t *key_data,
3276 int alg_arg, data_t *hash_data,
3277 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003278{
Ronald Cron5425a212020-08-04 14:58:35 +02003279 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003280 psa_key_type_t key_type = key_type_arg;
3281 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003282 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003283
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003284 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003285
Gilles Peskine8817f612018-12-18 00:18:46 +01003286 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003287
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003288 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003289 psa_set_key_algorithm( &attributes, alg );
3290 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003291
Gilles Peskine049c7532019-05-15 20:22:09 +02003292 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003293 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003294
Ronald Cron5425a212020-08-04 14:58:35 +02003295 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003296 hash_data->x, hash_data->len,
3297 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003298
3299#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003300 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003301 hash_data->x, hash_data->len,
3302 signature_data->x,
3303 signature_data->len ) );
3304
3305#endif /* MBEDTLS_TEST_DEPRECATED */
3306
itayzafrir5c753392018-05-08 11:18:38 +03003307exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003308 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003309 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003310 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003311}
3312/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003313
3314/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003315void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3316 int alg_arg, data_t *hash_data,
3317 data_t *signature_data,
3318 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003319{
Ronald Cron5425a212020-08-04 14:58:35 +02003320 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003321 psa_key_type_t key_type = key_type_arg;
3322 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003323 psa_status_t actual_status;
3324 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003325 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003326
Gilles Peskine8817f612018-12-18 00:18:46 +01003327 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003328
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003329 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003330 psa_set_key_algorithm( &attributes, alg );
3331 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003332
Gilles Peskine049c7532019-05-15 20:22:09 +02003333 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003334 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003335
Ronald Cron5425a212020-08-04 14:58:35 +02003336 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003337 hash_data->x, hash_data->len,
3338 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003339 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003340
Gilles Peskine895242b2019-11-29 12:15:40 +01003341#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003342 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003343 hash_data->x, hash_data->len,
3344 signature_data->x, signature_data->len ),
3345 expected_status );
3346#endif /* MBEDTLS_TEST_DEPRECATED */
3347
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003348exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003349 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003350 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003351 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003352}
3353/* END_CASE */
3354
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003355/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003356void asymmetric_encrypt( int key_type_arg,
3357 data_t *key_data,
3358 int alg_arg,
3359 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003360 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003361 int expected_output_length_arg,
3362 int expected_status_arg )
3363{
Ronald Cron5425a212020-08-04 14:58:35 +02003364 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003365 psa_key_type_t key_type = key_type_arg;
3366 psa_algorithm_t alg = alg_arg;
3367 size_t expected_output_length = expected_output_length_arg;
3368 size_t key_bits;
3369 unsigned char *output = NULL;
3370 size_t output_size;
3371 size_t output_length = ~0;
3372 psa_status_t actual_status;
3373 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003374 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003375
Gilles Peskine8817f612018-12-18 00:18:46 +01003376 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003377
Gilles Peskine656896e2018-06-29 19:12:28 +02003378 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003379 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3380 psa_set_key_algorithm( &attributes, alg );
3381 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003382 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003383 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003384
3385 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003386 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003387 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02003388 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003389 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003390
3391 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003392 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003393 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003394 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003395 output, output_size,
3396 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003397 TEST_EQUAL( actual_status, expected_status );
3398 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003399
Gilles Peskine68428122018-06-30 18:42:41 +02003400 /* If the label is empty, the test framework puts a non-null pointer
3401 * in label->x. Test that a null pointer works as well. */
3402 if( label->len == 0 )
3403 {
3404 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003405 if( output_size != 0 )
3406 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003407 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003408 input_data->x, input_data->len,
3409 NULL, label->len,
3410 output, output_size,
3411 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003412 TEST_EQUAL( actual_status, expected_status );
3413 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003414 }
3415
Gilles Peskine656896e2018-06-29 19:12:28 +02003416exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003417 /*
3418 * Key attributes may have been returned by psa_get_key_attributes()
3419 * thus reset them as required.
3420 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003421 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003422
Ronald Cron5425a212020-08-04 14:58:35 +02003423 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02003424 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003425 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003426}
3427/* END_CASE */
3428
3429/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003430void asymmetric_encrypt_decrypt( int key_type_arg,
3431 data_t *key_data,
3432 int alg_arg,
3433 data_t *input_data,
3434 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003435{
Ronald Cron5425a212020-08-04 14:58:35 +02003436 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003437 psa_key_type_t key_type = key_type_arg;
3438 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003439 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003440 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003441 size_t output_size;
3442 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003443 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003444 size_t output2_size;
3445 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003446 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003447
Gilles Peskine8817f612018-12-18 00:18:46 +01003448 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003449
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003450 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3451 psa_set_key_algorithm( &attributes, alg );
3452 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003453
Gilles Peskine049c7532019-05-15 20:22:09 +02003454 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003455 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003456
3457 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02003458 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003459 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003460 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003461 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003462 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003463 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003464
Gilles Peskineeebd7382018-06-08 18:11:54 +02003465 /* We test encryption by checking that encrypt-then-decrypt gives back
3466 * the original plaintext because of the non-optional random
3467 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02003468 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003469 input_data->x, input_data->len,
3470 label->x, label->len,
3471 output, output_size,
3472 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003473 /* We don't know what ciphertext length to expect, but check that
3474 * it looks sensible. */
3475 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003476
Ronald Cron5425a212020-08-04 14:58:35 +02003477 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003478 output, output_length,
3479 label->x, label->len,
3480 output2, output2_size,
3481 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003482 ASSERT_COMPARE( input_data->x, input_data->len,
3483 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003484
3485exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003486 /*
3487 * Key attributes may have been returned by psa_get_key_attributes()
3488 * thus reset them as required.
3489 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003490 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003491
Ronald Cron5425a212020-08-04 14:58:35 +02003492 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003493 mbedtls_free( output );
3494 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003495 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003496}
3497/* END_CASE */
3498
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003499/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003500void asymmetric_decrypt( int key_type_arg,
3501 data_t *key_data,
3502 int alg_arg,
3503 data_t *input_data,
3504 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003505 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003506{
Ronald Cron5425a212020-08-04 14:58:35 +02003507 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003508 psa_key_type_t key_type = key_type_arg;
3509 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003510 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003511 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003512 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003513 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003514
Jaeden Amero412654a2019-02-06 12:57:46 +00003515 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003516 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003517
Gilles Peskine8817f612018-12-18 00:18:46 +01003518 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003519
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003520 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3521 psa_set_key_algorithm( &attributes, alg );
3522 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003523
Gilles Peskine049c7532019-05-15 20:22:09 +02003524 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003525 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003526
Ronald Cron5425a212020-08-04 14:58:35 +02003527 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003528 input_data->x, input_data->len,
3529 label->x, label->len,
3530 output,
3531 output_size,
3532 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003533 ASSERT_COMPARE( expected_data->x, expected_data->len,
3534 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003535
Gilles Peskine68428122018-06-30 18:42:41 +02003536 /* If the label is empty, the test framework puts a non-null pointer
3537 * in label->x. Test that a null pointer works as well. */
3538 if( label->len == 0 )
3539 {
3540 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003541 if( output_size != 0 )
3542 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003543 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003544 input_data->x, input_data->len,
3545 NULL, label->len,
3546 output,
3547 output_size,
3548 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003549 ASSERT_COMPARE( expected_data->x, expected_data->len,
3550 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003551 }
3552
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003553exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003554 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003555 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003556 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003557 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003558}
3559/* END_CASE */
3560
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003561/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003562void asymmetric_decrypt_fail( int key_type_arg,
3563 data_t *key_data,
3564 int alg_arg,
3565 data_t *input_data,
3566 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00003567 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02003568 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003569{
Ronald Cron5425a212020-08-04 14:58:35 +02003570 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003571 psa_key_type_t key_type = key_type_arg;
3572 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003573 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00003574 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003575 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003576 psa_status_t actual_status;
3577 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003578 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003579
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003580 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003581
Gilles Peskine8817f612018-12-18 00:18:46 +01003582 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003583
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003584 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3585 psa_set_key_algorithm( &attributes, alg );
3586 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003587
Gilles Peskine049c7532019-05-15 20:22:09 +02003588 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003589 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003590
Ronald Cron5425a212020-08-04 14:58:35 +02003591 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003592 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003593 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003594 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003595 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003596 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003597 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003598
Gilles Peskine68428122018-06-30 18:42:41 +02003599 /* If the label is empty, the test framework puts a non-null pointer
3600 * in label->x. Test that a null pointer works as well. */
3601 if( label->len == 0 )
3602 {
3603 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003604 if( output_size != 0 )
3605 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003606 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003607 input_data->x, input_data->len,
3608 NULL, label->len,
3609 output, output_size,
3610 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003611 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003612 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003613 }
3614
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003615exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003616 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003617 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02003618 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003619 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003620}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003621/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003622
3623/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003624void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00003625{
3626 /* Test each valid way of initializing the object, except for `= {0}`, as
3627 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3628 * though it's OK by the C standard. We could test for this, but we'd need
3629 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003630 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003631 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
3632 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
3633 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00003634
3635 memset( &zero, 0, sizeof( zero ) );
3636
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003637 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003638 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003639 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003640 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003641 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003642 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003643 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003644
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003645 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003646 PSA_ASSERT( psa_key_derivation_abort(&func) );
3647 PSA_ASSERT( psa_key_derivation_abort(&init) );
3648 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00003649}
3650/* END_CASE */
3651
Janos Follath16de4a42019-06-13 16:32:24 +01003652/* BEGIN_CASE */
3653void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02003654{
Gilles Peskineea0fb492018-07-12 17:17:20 +02003655 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003656 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003657 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003658
Gilles Peskine8817f612018-12-18 00:18:46 +01003659 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003660
Janos Follath16de4a42019-06-13 16:32:24 +01003661 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003662 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003663
3664exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003665 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003666 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003667}
3668/* END_CASE */
3669
Janos Follathaf3c2a02019-06-12 12:34:34 +01003670/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01003671void derive_set_capacity( int alg_arg, int capacity_arg,
3672 int expected_status_arg )
3673{
3674 psa_algorithm_t alg = alg_arg;
3675 size_t capacity = capacity_arg;
3676 psa_status_t expected_status = expected_status_arg;
3677 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
3678
3679 PSA_ASSERT( psa_crypto_init( ) );
3680
3681 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
3682
3683 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
3684 expected_status );
3685
3686exit:
3687 psa_key_derivation_abort( &operation );
3688 PSA_DONE( );
3689}
3690/* END_CASE */
3691
3692/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01003693void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02003694 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01003695 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02003696 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01003697 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02003698 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003699 int expected_status_arg3,
3700 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01003701{
3702 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02003703 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
3704 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01003705 psa_status_t expected_statuses[] = {expected_status_arg1,
3706 expected_status_arg2,
3707 expected_status_arg3};
3708 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02003709 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
3710 MBEDTLS_SVC_KEY_ID_INIT,
3711 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01003712 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
3713 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3714 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003715 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02003716 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003717 psa_status_t expected_output_status = expected_output_status_arg;
3718 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01003719
3720 PSA_ASSERT( psa_crypto_init( ) );
3721
3722 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
3723 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01003724
3725 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
3726
3727 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
3728 {
Gilles Peskineb8965192019-09-24 16:21:10 +02003729 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01003730 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02003731 psa_set_key_type( &attributes, key_types[i] );
3732 PSA_ASSERT( psa_import_key( &attributes,
3733 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003734 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02003735 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
3736 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
3737 {
3738 // When taking a private key as secret input, use key agreement
3739 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01003740 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
3741 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02003742 expected_statuses[i] );
3743 }
3744 else
3745 {
3746 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02003747 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02003748 expected_statuses[i] );
3749 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02003750 }
3751 else
3752 {
3753 TEST_EQUAL( psa_key_derivation_input_bytes(
3754 &operation, steps[i],
3755 inputs[i]->x, inputs[i]->len ),
3756 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01003757 }
3758 }
3759
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003760 if( output_key_type != PSA_KEY_TYPE_NONE )
3761 {
3762 psa_reset_key_attributes( &attributes );
3763 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
3764 psa_set_key_bits( &attributes, 8 );
3765 actual_output_status =
3766 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02003767 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003768 }
3769 else
3770 {
3771 uint8_t buffer[1];
3772 actual_output_status =
3773 psa_key_derivation_output_bytes( &operation,
3774 buffer, sizeof( buffer ) );
3775 }
3776 TEST_EQUAL( actual_output_status, expected_output_status );
3777
Janos Follathaf3c2a02019-06-12 12:34:34 +01003778exit:
3779 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003780 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
3781 psa_destroy_key( keys[i] );
3782 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01003783 PSA_DONE( );
3784}
3785/* END_CASE */
3786
Janos Follathd958bb72019-07-03 15:02:16 +01003787/* BEGIN_CASE */
3788void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003789{
Janos Follathd958bb72019-07-03 15:02:16 +01003790 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02003791 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003792 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003793 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01003794 unsigned char input1[] = "Input 1";
3795 size_t input1_length = sizeof( input1 );
3796 unsigned char input2[] = "Input 2";
3797 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003798 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003799 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003800 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3801 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3802 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003803 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003804
Gilles Peskine8817f612018-12-18 00:18:46 +01003805 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003806
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003807 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
3808 psa_set_key_algorithm( &attributes, alg );
3809 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003810
Gilles Peskine73676cb2019-05-15 20:15:10 +02003811 PSA_ASSERT( psa_import_key( &attributes,
3812 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02003813 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003814
3815 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01003816 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
3817 input1, input1_length,
3818 input2, input2_length,
3819 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01003820 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003821
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003822 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01003823 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003824 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003825
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003826 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003827
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003828 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02003829 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003830
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003831exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003832 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003833 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003834 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003835}
3836/* END_CASE */
3837
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003838/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003839void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003840{
3841 uint8_t output_buffer[16];
3842 size_t buffer_size = 16;
3843 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003844 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003845
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003846 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
3847 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003848 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003849
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003850 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003851 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003852
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003853 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003854
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003855 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
3856 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003857 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003858
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003859 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003860 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003861
3862exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003863 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003864}
3865/* END_CASE */
3866
3867/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003868void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02003869 int step1_arg, data_t *input1,
3870 int step2_arg, data_t *input2,
3871 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003872 int requested_capacity_arg,
3873 data_t *expected_output1,
3874 data_t *expected_output2 )
3875{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003876 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02003877 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
3878 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02003879 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
3880 MBEDTLS_SVC_KEY_ID_INIT,
3881 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003882 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003883 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003884 uint8_t *expected_outputs[2] =
3885 {expected_output1->x, expected_output2->x};
3886 size_t output_sizes[2] =
3887 {expected_output1->len, expected_output2->len};
3888 size_t output_buffer_size = 0;
3889 uint8_t *output_buffer = NULL;
3890 size_t expected_capacity;
3891 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02003892 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003893 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02003894 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003895
3896 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3897 {
3898 if( output_sizes[i] > output_buffer_size )
3899 output_buffer_size = output_sizes[i];
3900 if( output_sizes[i] == 0 )
3901 expected_outputs[i] = NULL;
3902 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003903 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003904 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003905
Gilles Peskineff5f0e72019-04-18 12:53:30 +02003906 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
3907 psa_set_key_algorithm( &attributes, alg );
3908 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003909
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003910 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02003911 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
3912 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
3913 requested_capacity ) );
3914 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003915 {
Gilles Peskine1468da72019-05-29 17:35:49 +02003916 switch( steps[i] )
3917 {
3918 case 0:
3919 break;
3920 case PSA_KEY_DERIVATION_INPUT_SECRET:
3921 PSA_ASSERT( psa_import_key( &attributes,
3922 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003923 &keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02003924 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02003925 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02003926 break;
3927 default:
3928 PSA_ASSERT( psa_key_derivation_input_bytes(
3929 &operation, steps[i],
3930 inputs[i]->x, inputs[i]->len ) );
3931 break;
3932 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003933 }
Gilles Peskine1468da72019-05-29 17:35:49 +02003934
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003935 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003936 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003937 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003938 expected_capacity = requested_capacity;
3939
3940 /* Expansion phase. */
3941 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3942 {
3943 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003944 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003945 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003946 if( expected_capacity == 0 && output_sizes[i] == 0 )
3947 {
3948 /* Reading 0 bytes when 0 bytes are available can go either way. */
3949 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02003950 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003951 continue;
3952 }
3953 else if( expected_capacity == 0 ||
3954 output_sizes[i] > expected_capacity )
3955 {
3956 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02003957 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003958 expected_capacity = 0;
3959 continue;
3960 }
3961 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003962 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003963 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003964 ASSERT_COMPARE( output_buffer, output_sizes[i],
3965 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003966 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003967 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003968 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003969 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003970 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003971 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003972 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003973
3974exit:
3975 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003976 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003977 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
3978 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003979 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003980}
3981/* END_CASE */
3982
3983/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003984void derive_full( int alg_arg,
3985 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01003986 data_t *input1,
3987 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02003988 int requested_capacity_arg )
3989{
Ronald Cron5425a212020-08-04 14:58:35 +02003990 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02003991 psa_algorithm_t alg = alg_arg;
3992 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003993 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02003994 unsigned char output_buffer[16];
3995 size_t expected_capacity = requested_capacity;
3996 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02003997 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02003998
Gilles Peskine8817f612018-12-18 00:18:46 +01003999 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004000
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004001 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4002 psa_set_key_algorithm( &attributes, alg );
4003 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004004
Gilles Peskine049c7532019-05-15 20:22:09 +02004005 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004006 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004007
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004008 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4009 input1->x, input1->len,
4010 input2->x, input2->len,
4011 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004012 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004013
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004014 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004015 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004016 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004017
4018 /* Expansion phase. */
4019 while( current_capacity > 0 )
4020 {
4021 size_t read_size = sizeof( output_buffer );
4022 if( read_size > current_capacity )
4023 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004024 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004025 output_buffer,
4026 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004027 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004028 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004029 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004030 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004031 }
4032
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004033 /* Check that the operation refuses to go over capacity. */
4034 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004035 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004036
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004037 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004038
4039exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004040 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004041 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004042 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004043}
4044/* END_CASE */
4045
Janos Follathe60c9052019-07-03 13:51:30 +01004046/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004047void derive_key_exercise( int alg_arg,
4048 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004049 data_t *input1,
4050 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004051 int derived_type_arg,
4052 int derived_bits_arg,
4053 int derived_usage_arg,
4054 int derived_alg_arg )
4055{
Ronald Cron5425a212020-08-04 14:58:35 +02004056 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4057 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004058 psa_algorithm_t alg = alg_arg;
4059 psa_key_type_t derived_type = derived_type_arg;
4060 size_t derived_bits = derived_bits_arg;
4061 psa_key_usage_t derived_usage = derived_usage_arg;
4062 psa_algorithm_t derived_alg = derived_alg_arg;
4063 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004064 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004065 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004066 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004067
Gilles Peskine8817f612018-12-18 00:18:46 +01004068 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004069
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004070 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4071 psa_set_key_algorithm( &attributes, alg );
4072 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004073 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004074 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004075
4076 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004077 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4078 input1->x, input1->len,
4079 input2->x, input2->len,
4080 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004081 goto exit;
4082
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004083 psa_set_key_usage_flags( &attributes, derived_usage );
4084 psa_set_key_algorithm( &attributes, derived_alg );
4085 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004086 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004087 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004088 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004089
4090 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004091 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004092 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4093 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004094
4095 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004096 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004097 goto exit;
4098
4099exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004100 /*
4101 * Key attributes may have been returned by psa_get_key_attributes()
4102 * thus reset them as required.
4103 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004104 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004105
4106 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004107 psa_destroy_key( base_key );
4108 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004109 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004110}
4111/* END_CASE */
4112
Janos Follath42fd8882019-07-03 14:17:09 +01004113/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004114void derive_key_export( int alg_arg,
4115 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004116 data_t *input1,
4117 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004118 int bytes1_arg,
4119 int bytes2_arg )
4120{
Ronald Cron5425a212020-08-04 14:58:35 +02004121 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4122 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004123 psa_algorithm_t alg = alg_arg;
4124 size_t bytes1 = bytes1_arg;
4125 size_t bytes2 = bytes2_arg;
4126 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004127 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004128 uint8_t *output_buffer = NULL;
4129 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004130 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4131 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004132 size_t length;
4133
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004134 ASSERT_ALLOC( output_buffer, capacity );
4135 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004136 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004137
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004138 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4139 psa_set_key_algorithm( &base_attributes, alg );
4140 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004141 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004142 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004143
4144 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004145 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4146 input1->x, input1->len,
4147 input2->x, input2->len,
4148 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004149 goto exit;
4150
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004151 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004152 output_buffer,
4153 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004154 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004155
4156 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004157 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4158 input1->x, input1->len,
4159 input2->x, input2->len,
4160 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004161 goto exit;
4162
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004163 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4164 psa_set_key_algorithm( &derived_attributes, 0 );
4165 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004166 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004167 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004168 &derived_key ) );
4169 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004170 export_buffer, bytes1,
4171 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004172 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004173 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004174 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004175 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004176 &derived_key ) );
4177 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004178 export_buffer + bytes1, bytes2,
4179 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004180 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004181
4182 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004183 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4184 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004185
4186exit:
4187 mbedtls_free( output_buffer );
4188 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004189 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004190 psa_destroy_key( base_key );
4191 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004192 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004193}
4194/* END_CASE */
4195
4196/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004197void derive_key( int alg_arg,
4198 data_t *key_data, data_t *input1, data_t *input2,
4199 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004200 int expected_status_arg,
4201 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004202{
Ronald Cron5425a212020-08-04 14:58:35 +02004203 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4204 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004205 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004206 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004207 size_t bits = bits_arg;
4208 psa_status_t expected_status = expected_status_arg;
4209 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4210 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4211 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4212
4213 PSA_ASSERT( psa_crypto_init( ) );
4214
4215 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4216 psa_set_key_algorithm( &base_attributes, alg );
4217 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4218 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004219 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004220
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004221 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4222 input1->x, input1->len,
4223 input2->x, input2->len,
4224 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004225 goto exit;
4226
4227 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4228 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004229 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004230 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004231
4232 psa_status_t status =
4233 psa_key_derivation_output_key( &derived_attributes,
4234 &operation,
4235 &derived_key );
4236 if( is_large_output > 0 )
4237 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4238 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004239
4240exit:
4241 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004242 psa_destroy_key( base_key );
4243 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004244 PSA_DONE( );
4245}
4246/* END_CASE */
4247
4248/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004249void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004250 int our_key_type_arg, int our_key_alg_arg,
4251 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004252 int expected_status_arg )
4253{
Ronald Cron5425a212020-08-04 14:58:35 +02004254 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004255 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004256 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004257 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004258 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004259 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004260 psa_status_t expected_status = expected_status_arg;
4261 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004262
Gilles Peskine8817f612018-12-18 00:18:46 +01004263 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004264
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004265 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004266 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004267 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004268 PSA_ASSERT( psa_import_key( &attributes,
4269 our_key_data->x, our_key_data->len,
4270 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004271
Gilles Peskine77f40d82019-04-11 21:27:06 +02004272 /* The tests currently include inputs that should fail at either step.
4273 * Test cases that fail at the setup step should be changed to call
4274 * key_derivation_setup instead, and this function should be renamed
4275 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004276 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004277 if( status == PSA_SUCCESS )
4278 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004279 TEST_EQUAL( psa_key_derivation_key_agreement(
4280 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4281 our_key,
4282 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004283 expected_status );
4284 }
4285 else
4286 {
4287 TEST_ASSERT( status == expected_status );
4288 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004289
4290exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004291 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004292 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004293 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004294}
4295/* END_CASE */
4296
4297/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004298void raw_key_agreement( int alg_arg,
4299 int our_key_type_arg, data_t *our_key_data,
4300 data_t *peer_key_data,
4301 data_t *expected_output )
4302{
Ronald Cron5425a212020-08-04 14:58:35 +02004303 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004304 psa_algorithm_t alg = alg_arg;
4305 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004306 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004307 unsigned char *output = NULL;
4308 size_t output_length = ~0;
4309
4310 ASSERT_ALLOC( output, expected_output->len );
4311 PSA_ASSERT( psa_crypto_init( ) );
4312
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004313 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4314 psa_set_key_algorithm( &attributes, alg );
4315 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004316 PSA_ASSERT( psa_import_key( &attributes,
4317 our_key_data->x, our_key_data->len,
4318 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004319
Gilles Peskinebe697d82019-05-16 18:00:41 +02004320 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4321 peer_key_data->x, peer_key_data->len,
4322 output, expected_output->len,
4323 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004324 ASSERT_COMPARE( output, output_length,
4325 expected_output->x, expected_output->len );
4326
4327exit:
4328 mbedtls_free( output );
4329 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004330 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004331}
4332/* END_CASE */
4333
4334/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004335void key_agreement_capacity( int alg_arg,
4336 int our_key_type_arg, data_t *our_key_data,
4337 data_t *peer_key_data,
4338 int expected_capacity_arg )
4339{
Ronald Cron5425a212020-08-04 14:58:35 +02004340 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004341 psa_algorithm_t alg = alg_arg;
4342 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004343 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004344 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004345 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004346 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004347
Gilles Peskine8817f612018-12-18 00:18:46 +01004348 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004349
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004350 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4351 psa_set_key_algorithm( &attributes, alg );
4352 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004353 PSA_ASSERT( psa_import_key( &attributes,
4354 our_key_data->x, our_key_data->len,
4355 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004356
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004357 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004358 PSA_ASSERT( psa_key_derivation_key_agreement(
4359 &operation,
4360 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4361 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004362 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4363 {
4364 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004365 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004366 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004367 NULL, 0 ) );
4368 }
Gilles Peskine59685592018-09-18 12:11:34 +02004369
Gilles Peskinebf491972018-10-25 22:36:12 +02004370 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004371 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004372 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004373 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004374
Gilles Peskinebf491972018-10-25 22:36:12 +02004375 /* Test the actual capacity by reading the output. */
4376 while( actual_capacity > sizeof( output ) )
4377 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004378 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004379 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004380 actual_capacity -= sizeof( output );
4381 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004382 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004383 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004384 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004385 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004386
Gilles Peskine59685592018-09-18 12:11:34 +02004387exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004388 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004389 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004390 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004391}
4392/* END_CASE */
4393
4394/* BEGIN_CASE */
4395void key_agreement_output( int alg_arg,
4396 int our_key_type_arg, data_t *our_key_data,
4397 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004398 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004399{
Ronald Cron5425a212020-08-04 14:58:35 +02004400 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004401 psa_algorithm_t alg = alg_arg;
4402 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004403 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004404 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004405 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004406
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004407 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4408 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004409
Gilles Peskine8817f612018-12-18 00:18:46 +01004410 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004411
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004412 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4413 psa_set_key_algorithm( &attributes, alg );
4414 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004415 PSA_ASSERT( psa_import_key( &attributes,
4416 our_key_data->x, our_key_data->len,
4417 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004418
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004419 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004420 PSA_ASSERT( psa_key_derivation_key_agreement(
4421 &operation,
4422 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4423 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004424 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4425 {
4426 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004427 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004428 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004429 NULL, 0 ) );
4430 }
Gilles Peskine59685592018-09-18 12:11:34 +02004431
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_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004435 ASSERT_COMPARE( actual_output, expected_output1->len,
4436 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004437 if( expected_output2->len != 0 )
4438 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004439 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004440 actual_output,
4441 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004442 ASSERT_COMPARE( actual_output, expected_output2->len,
4443 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004444 }
Gilles Peskine59685592018-09-18 12:11:34 +02004445
4446exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004447 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004448 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004449 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004450 mbedtls_free( actual_output );
4451}
4452/* END_CASE */
4453
4454/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004455void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004456{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004457 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004458 unsigned char *output = NULL;
4459 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004460 size_t i;
4461 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004462
Simon Butcher49f8e312020-03-03 15:51:50 +00004463 TEST_ASSERT( bytes_arg >= 0 );
4464
Gilles Peskine91892022021-02-08 19:50:26 +01004465 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004466 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02004467
Gilles Peskine8817f612018-12-18 00:18:46 +01004468 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004469
Gilles Peskinea50d7392018-06-21 10:22:13 +02004470 /* Run several times, to ensure that every output byte will be
4471 * nonzero at least once with overwhelming probability
4472 * (2^(-8*number_of_runs)). */
4473 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004474 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004475 if( bytes != 0 )
4476 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004477 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004478
Gilles Peskinea50d7392018-06-21 10:22:13 +02004479 for( i = 0; i < bytes; i++ )
4480 {
4481 if( output[i] != 0 )
4482 ++changed[i];
4483 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004484 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004485
4486 /* Check that every byte was changed to nonzero at least once. This
4487 * validates that psa_generate_random is overwriting every byte of
4488 * the output buffer. */
4489 for( i = 0; i < bytes; i++ )
4490 {
4491 TEST_ASSERT( changed[i] != 0 );
4492 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004493
4494exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004495 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004496 mbedtls_free( output );
4497 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004498}
4499/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004500
4501/* BEGIN_CASE */
4502void generate_key( int type_arg,
4503 int bits_arg,
4504 int usage_arg,
4505 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004506 int expected_status_arg,
4507 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004508{
Ronald Cron5425a212020-08-04 14:58:35 +02004509 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004510 psa_key_type_t type = type_arg;
4511 psa_key_usage_t usage = usage_arg;
4512 size_t bits = bits_arg;
4513 psa_algorithm_t alg = alg_arg;
4514 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004515 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004516 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004517
Gilles Peskine8817f612018-12-18 00:18:46 +01004518 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004519
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004520 psa_set_key_usage_flags( &attributes, usage );
4521 psa_set_key_algorithm( &attributes, alg );
4522 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004523 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004524
4525 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01004526 psa_status_t status = psa_generate_key( &attributes, &key );
4527
4528 if( is_large_key > 0 )
4529 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4530 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004531 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004532 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004533
4534 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004535 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004536 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
4537 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004538
Gilles Peskine818ca122018-06-20 18:16:48 +02004539 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004540 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004541 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004542
4543exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004544 /*
4545 * Key attributes may have been returned by psa_get_key_attributes()
4546 * thus reset them as required.
4547 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004548 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004549
Ronald Cron5425a212020-08-04 14:58:35 +02004550 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004551 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004552}
4553/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004554
Gilles Peskinee56e8782019-04-26 17:34:02 +02004555/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
4556void generate_key_rsa( int bits_arg,
4557 data_t *e_arg,
4558 int expected_status_arg )
4559{
Ronald Cron5425a212020-08-04 14:58:35 +02004560 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004561 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02004562 size_t bits = bits_arg;
4563 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
4564 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
4565 psa_status_t expected_status = expected_status_arg;
4566 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4567 uint8_t *exported = NULL;
4568 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004569 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004570 size_t exported_length = SIZE_MAX;
4571 uint8_t *e_read_buffer = NULL;
4572 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02004573 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004574 size_t e_read_length = SIZE_MAX;
4575
4576 if( e_arg->len == 0 ||
4577 ( e_arg->len == 3 &&
4578 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
4579 {
4580 is_default_public_exponent = 1;
4581 e_read_size = 0;
4582 }
4583 ASSERT_ALLOC( e_read_buffer, e_read_size );
4584 ASSERT_ALLOC( exported, exported_size );
4585
4586 PSA_ASSERT( psa_crypto_init( ) );
4587
4588 psa_set_key_usage_flags( &attributes, usage );
4589 psa_set_key_algorithm( &attributes, alg );
4590 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
4591 e_arg->x, e_arg->len ) );
4592 psa_set_key_bits( &attributes, bits );
4593
4594 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02004595 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004596 if( expected_status != PSA_SUCCESS )
4597 goto exit;
4598
4599 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004600 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004601 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4602 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4603 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
4604 e_read_buffer, e_read_size,
4605 &e_read_length ) );
4606 if( is_default_public_exponent )
4607 TEST_EQUAL( e_read_length, 0 );
4608 else
4609 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
4610
4611 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004612 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02004613 goto exit;
4614
4615 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02004616 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02004617 exported, exported_size,
4618 &exported_length ) );
4619 {
4620 uint8_t *p = exported;
4621 uint8_t *end = exported + exported_length;
4622 size_t len;
4623 /* RSAPublicKey ::= SEQUENCE {
4624 * modulus INTEGER, -- n
4625 * publicExponent INTEGER } -- e
4626 */
4627 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004628 MBEDTLS_ASN1_SEQUENCE |
4629 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01004630 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004631 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
4632 MBEDTLS_ASN1_INTEGER ) );
4633 if( len >= 1 && p[0] == 0 )
4634 {
4635 ++p;
4636 --len;
4637 }
4638 if( e_arg->len == 0 )
4639 {
4640 TEST_EQUAL( len, 3 );
4641 TEST_EQUAL( p[0], 1 );
4642 TEST_EQUAL( p[1], 0 );
4643 TEST_EQUAL( p[2], 1 );
4644 }
4645 else
4646 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
4647 }
4648
4649exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004650 /*
4651 * Key attributes may have been returned by psa_get_key_attributes() or
4652 * set by psa_set_key_domain_parameters() thus reset them as required.
4653 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02004654 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004655
Ronald Cron5425a212020-08-04 14:58:35 +02004656 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004657 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004658 mbedtls_free( e_read_buffer );
4659 mbedtls_free( exported );
4660}
4661/* END_CASE */
4662
Darryl Greend49a4992018-06-18 17:27:26 +01004663/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004664void persistent_key_load_key_from_storage( data_t *data,
4665 int type_arg, int bits_arg,
4666 int usage_flags_arg, int alg_arg,
4667 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01004668{
Ronald Cron71016a92020-08-28 19:01:50 +02004669 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004670 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02004671 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4672 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004673 psa_key_type_t type = type_arg;
4674 size_t bits = bits_arg;
4675 psa_key_usage_t usage_flags = usage_flags_arg;
4676 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004677 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004678 unsigned char *first_export = NULL;
4679 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004680 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004681 size_t first_exported_length;
4682 size_t second_exported_length;
4683
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004684 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4685 {
4686 ASSERT_ALLOC( first_export, export_size );
4687 ASSERT_ALLOC( second_export, export_size );
4688 }
Darryl Greend49a4992018-06-18 17:27:26 +01004689
Gilles Peskine8817f612018-12-18 00:18:46 +01004690 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004691
Gilles Peskinec87af662019-05-15 16:12:22 +02004692 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004693 psa_set_key_usage_flags( &attributes, usage_flags );
4694 psa_set_key_algorithm( &attributes, alg );
4695 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004696 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004697
Darryl Green0c6575a2018-11-07 16:05:30 +00004698 switch( generation_method )
4699 {
4700 case IMPORT_KEY:
4701 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02004702 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004703 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004704 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004705
Darryl Green0c6575a2018-11-07 16:05:30 +00004706 case GENERATE_KEY:
4707 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02004708 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004709 break;
4710
4711 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01004712#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004713 {
4714 /* Create base key */
4715 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
4716 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4717 psa_set_key_usage_flags( &base_attributes,
4718 PSA_KEY_USAGE_DERIVE );
4719 psa_set_key_algorithm( &base_attributes, derive_alg );
4720 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004721 PSA_ASSERT( psa_import_key( &base_attributes,
4722 data->x, data->len,
4723 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004724 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004725 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004726 PSA_ASSERT( psa_key_derivation_input_key(
4727 &operation,
4728 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004729 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004730 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004731 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004732 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
4733 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004734 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004735 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004736 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02004737 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004738 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01004739#else
4740 TEST_ASSUME( ! "KDF not supported in this configuration" );
4741#endif
4742 break;
4743
4744 default:
4745 TEST_ASSERT( ! "generation_method not implemented in test" );
4746 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00004747 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004748 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01004749
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004750 /* Export the key if permitted by the key policy. */
4751 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4752 {
Ronald Cron5425a212020-08-04 14:58:35 +02004753 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004754 first_export, export_size,
4755 &first_exported_length ) );
4756 if( generation_method == IMPORT_KEY )
4757 ASSERT_COMPARE( data->x, data->len,
4758 first_export, first_exported_length );
4759 }
Darryl Greend49a4992018-06-18 17:27:26 +01004760
4761 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02004762 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004763 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01004764 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004765
Darryl Greend49a4992018-06-18 17:27:26 +01004766 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02004767 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02004768 TEST_ASSERT( mbedtls_svc_key_id_equal(
4769 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004770 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
4771 PSA_KEY_LIFETIME_PERSISTENT );
4772 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4773 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4774 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
4775 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004776
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004777 /* Export the key again if permitted by the key policy. */
4778 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00004779 {
Ronald Cron5425a212020-08-04 14:58:35 +02004780 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004781 second_export, export_size,
4782 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004783 ASSERT_COMPARE( first_export, first_exported_length,
4784 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00004785 }
4786
4787 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004788 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004789 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004790
4791exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004792 /*
4793 * Key attributes may have been returned by psa_get_key_attributes()
4794 * thus reset them as required.
4795 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004796 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004797
Darryl Greend49a4992018-06-18 17:27:26 +01004798 mbedtls_free( first_export );
4799 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004800 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004801 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02004802 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004803 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01004804}
4805/* END_CASE */