blob: 678cb777a2e1a8cfdeee367e6b5bb1aeeb95adca [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
Gilles Peskinef6279312021-05-27 13:21:20 +020019/* If this comes up, it's a bug in the test code or in the test data. */
20#define UNUSED 0xdeadbeef
21
Jaeden Amerof24c7f82018-06-27 17:20:43 +010022/** An invalid export length that will never be set by psa_export_key(). */
23static const size_t INVALID_EXPORT_LENGTH = ~0U;
24
Gilles Peskinea7aa4422018-08-14 15:17:54 +020025/** Test if a buffer contains a constant byte value.
26 *
27 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020028 *
29 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020030 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020031 * \param size Size of the buffer in bytes.
32 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020033 * \return 1 if the buffer is all-bits-zero.
34 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020035 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020036static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037{
38 size_t i;
39 for( i = 0; i < size; i++ )
40 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020042 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020043 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020044 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020045}
Gilles Peskine818ca122018-06-20 18:16:48 +020046
Gilles Peskine0b352bc2018-06-28 00:16:11 +020047/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
48static int asn1_write_10x( unsigned char **p,
49 unsigned char *start,
50 size_t bits,
51 unsigned char x )
52{
53 int ret;
54 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020055 if( bits == 0 )
56 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
57 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020058 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030059 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020060 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
61 *p -= len;
62 ( *p )[len-1] = x;
63 if( bits % 8 == 0 )
64 ( *p )[1] |= 1;
65 else
66 ( *p )[0] |= 1 << ( bits % 8 );
67 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
68 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
69 MBEDTLS_ASN1_INTEGER ) );
70 return( len );
71}
72
73static int construct_fake_rsa_key( unsigned char *buffer,
74 size_t buffer_size,
75 unsigned char **p,
76 size_t bits,
77 int keypair )
78{
79 size_t half_bits = ( bits + 1 ) / 2;
80 int ret;
81 int len = 0;
82 /* Construct something that looks like a DER encoding of
83 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
84 * RSAPrivateKey ::= SEQUENCE {
85 * version Version,
86 * modulus INTEGER, -- n
87 * publicExponent INTEGER, -- e
88 * privateExponent INTEGER, -- d
89 * prime1 INTEGER, -- p
90 * prime2 INTEGER, -- q
91 * exponent1 INTEGER, -- d mod (p-1)
92 * exponent2 INTEGER, -- d mod (q-1)
93 * coefficient INTEGER, -- (inverse of q) mod p
94 * otherPrimeInfos OtherPrimeInfos OPTIONAL
95 * }
96 * Or, for a public key, the same structure with only
97 * version, modulus and publicExponent.
98 */
99 *p = buffer + buffer_size;
100 if( keypair )
101 {
102 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
103 asn1_write_10x( p, buffer, half_bits, 1 ) );
104 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
105 asn1_write_10x( p, buffer, half_bits, 1 ) );
106 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
107 asn1_write_10x( p, buffer, half_bits, 1 ) );
108 MBEDTLS_ASN1_CHK_ADD( len, /* q */
109 asn1_write_10x( p, buffer, half_bits, 1 ) );
110 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
111 asn1_write_10x( p, buffer, half_bits, 3 ) );
112 MBEDTLS_ASN1_CHK_ADD( len, /* d */
113 asn1_write_10x( p, buffer, bits, 1 ) );
114 }
115 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
116 asn1_write_10x( p, buffer, 17, 1 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* n */
118 asn1_write_10x( p, buffer, bits, 1 ) );
119 if( keypair )
120 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
121 mbedtls_asn1_write_int( p, buffer, 0 ) );
122 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
123 {
124 const unsigned char tag =
125 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
126 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
127 }
128 return( len );
129}
130
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100131int exercise_mac_setup( psa_key_type_t key_type,
132 const unsigned char *key_bytes,
133 size_t key_length,
134 psa_algorithm_t alg,
135 psa_mac_operation_t *operation,
136 psa_status_t *status )
137{
Ronald Cron5425a212020-08-04 14:58:35 +0200138 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200139 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100140
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100141 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200142 psa_set_key_algorithm( &attributes, alg );
143 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200144 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100145
Ronald Cron5425a212020-08-04 14:58:35 +0200146 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100147 /* Whether setup succeeded or failed, abort must succeed. */
148 PSA_ASSERT( psa_mac_abort( operation ) );
149 /* If setup failed, reproduce the failure, so that the caller can
150 * test the resulting state of the operation object. */
151 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100152 {
Ronald Cron5425a212020-08-04 14:58:35 +0200153 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100154 }
155
Ronald Cron5425a212020-08-04 14:58:35 +0200156 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100157 return( 1 );
158
159exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200160 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100161 return( 0 );
162}
163
164int exercise_cipher_setup( psa_key_type_t key_type,
165 const unsigned char *key_bytes,
166 size_t key_length,
167 psa_algorithm_t alg,
168 psa_cipher_operation_t *operation,
169 psa_status_t *status )
170{
Ronald Cron5425a212020-08-04 14:58:35 +0200171 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200172 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100173
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200174 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
175 psa_set_key_algorithm( &attributes, alg );
176 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200177 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100178
Ronald Cron5425a212020-08-04 14:58:35 +0200179 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100180 /* Whether setup succeeded or failed, abort must succeed. */
181 PSA_ASSERT( psa_cipher_abort( operation ) );
182 /* If setup failed, reproduce the failure, so that the caller can
183 * test the resulting state of the operation object. */
184 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185 {
Ronald Cron5425a212020-08-04 14:58:35 +0200186 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100187 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100188 }
189
Ronald Cron5425a212020-08-04 14:58:35 +0200190 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100191 return( 1 );
192
193exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200194 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100195 return( 0 );
196}
197
Ronald Cron5425a212020-08-04 14:58:35 +0200198static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200199{
200 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200201 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200202 uint8_t buffer[1];
203 size_t length;
204 int ok = 0;
205
Ronald Cronecfb2372020-07-23 17:13:42 +0200206 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200207 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
208 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
209 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200210 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000211 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200212 TEST_EQUAL(
213 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
214 TEST_EQUAL(
215 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200216 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200217 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
218 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
219 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
220 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
221
Ronald Cron5425a212020-08-04 14:58:35 +0200222 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000223 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200224 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200225 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000226 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200227
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200228 ok = 1;
229
230exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100231 /*
232 * Key attributes may have been returned by psa_get_key_attributes()
233 * thus reset them as required.
234 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200235 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100236
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200237 return( ok );
238}
239
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200240/* Assert that a key isn't reported as having a slot number. */
241#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
242#define ASSERT_NO_SLOT_NUMBER( attributes ) \
243 do \
244 { \
245 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
246 TEST_EQUAL( psa_get_key_slot_number( \
247 attributes, \
248 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
249 PSA_ERROR_INVALID_ARGUMENT ); \
250 } \
251 while( 0 )
252#else /* MBEDTLS_PSA_CRYPTO_SE_C */
253#define ASSERT_NO_SLOT_NUMBER( attributes ) \
254 ( (void) 0 )
255#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
256
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100257/* An overapproximation of the amount of storage needed for a key of the
258 * given type and with the given content. The API doesn't make it easy
259 * to find a good value for the size. The current implementation doesn't
260 * care about the value anyway. */
261#define KEY_BITS_FROM_DATA( type, data ) \
262 ( data )->len
263
Darryl Green0c6575a2018-11-07 16:05:30 +0000264typedef enum {
265 IMPORT_KEY = 0,
266 GENERATE_KEY = 1,
267 DERIVE_KEY = 2
268} generate_method;
269
Gilles Peskinee59236f2018-01-27 23:32:46 +0100270/* END_HEADER */
271
272/* BEGIN_DEPENDENCIES
273 * depends_on:MBEDTLS_PSA_CRYPTO_C
274 * END_DEPENDENCIES
275 */
276
277/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200278void static_checks( )
279{
280 size_t max_truncated_mac_size =
281 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
282
283 /* Check that the length for a truncated MAC always fits in the algorithm
284 * encoding. The shifted mask is the maximum truncated value. The
285 * untruncated algorithm may be one byte larger. */
286 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +0100287
288#if defined(MBEDTLS_TEST_DEPRECATED)
289 /* Check deprecated constants. */
290 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
291 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
292 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
293 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
294 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
295 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
296 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
297 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100298
Paul Elliott8ff510a2020-06-02 17:19:28 +0100299 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
300 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
301 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
302 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
303 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
304 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
305 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
306 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
307 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
308 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
309 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
310 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
311 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
312 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
313 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
314 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
315 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
316 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
317 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
318 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
319 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
320 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
321 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
322 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
323 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
324 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
325 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
326 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
327 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
328 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
329
330 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
331 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
332 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
333 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
334 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
335 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
336 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
337 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100338
Paul Elliott75e27032020-06-03 15:17:39 +0100339 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
340 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
341 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
342 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
343 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
344
345 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
346 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100347#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200348}
349/* END_CASE */
350
351/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200352void import_with_policy( int type_arg,
353 int usage_arg, int alg_arg,
354 int expected_status_arg )
355{
356 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
357 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200358 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200359 psa_key_type_t type = type_arg;
360 psa_key_usage_t usage = usage_arg;
361 psa_algorithm_t alg = alg_arg;
362 psa_status_t expected_status = expected_status_arg;
363 const uint8_t key_material[16] = {0};
364 psa_status_t status;
365
366 PSA_ASSERT( psa_crypto_init( ) );
367
368 psa_set_key_type( &attributes, type );
369 psa_set_key_usage_flags( &attributes, usage );
370 psa_set_key_algorithm( &attributes, alg );
371
372 status = psa_import_key( &attributes,
373 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200374 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200375 TEST_EQUAL( status, expected_status );
376 if( status != PSA_SUCCESS )
377 goto exit;
378
Ronald Cron5425a212020-08-04 14:58:35 +0200379 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200380 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
381 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
382 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200383 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200384
Ronald Cron5425a212020-08-04 14:58:35 +0200385 PSA_ASSERT( psa_destroy_key( key ) );
386 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200387
388exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100389 /*
390 * Key attributes may have been returned by psa_get_key_attributes()
391 * thus reset them as required.
392 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200393 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100394
395 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200396 PSA_DONE( );
397}
398/* END_CASE */
399
400/* BEGIN_CASE */
401void import_with_data( data_t *data, int type_arg,
402 int attr_bits_arg,
403 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200404{
405 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
406 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200407 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200408 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200409 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200410 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100411 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100412
Gilles Peskine8817f612018-12-18 00:18:46 +0100413 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100414
Gilles Peskine4747d192019-04-17 15:05:45 +0200415 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200416 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200417
Ronald Cron5425a212020-08-04 14:58:35 +0200418 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100419 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200420 if( status != PSA_SUCCESS )
421 goto exit;
422
Ronald Cron5425a212020-08-04 14:58:35 +0200423 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200424 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200425 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200426 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200427 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200428
Ronald Cron5425a212020-08-04 14:58:35 +0200429 PSA_ASSERT( psa_destroy_key( key ) );
430 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100431
432exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100433 /*
434 * Key attributes may have been returned by psa_get_key_attributes()
435 * thus reset them as required.
436 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200437 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100438
439 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200440 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100441}
442/* END_CASE */
443
444/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200445void import_large_key( int type_arg, int byte_size_arg,
446 int expected_status_arg )
447{
448 psa_key_type_t type = type_arg;
449 size_t byte_size = byte_size_arg;
450 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
451 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200452 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200453 psa_status_t status;
454 uint8_t *buffer = NULL;
455 size_t buffer_size = byte_size + 1;
456 size_t n;
457
Steven Cooreman69967ce2021-01-18 18:01:08 +0100458 /* Skip the test case if the target running the test cannot
459 * accomodate large keys due to heap size constraints */
460 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200461 memset( buffer, 'K', byte_size );
462
463 PSA_ASSERT( psa_crypto_init( ) );
464
465 /* Try importing the key */
466 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
467 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200468 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100469 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200470 TEST_EQUAL( status, expected_status );
471
472 if( status == PSA_SUCCESS )
473 {
Ronald Cron5425a212020-08-04 14:58:35 +0200474 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200475 TEST_EQUAL( psa_get_key_type( &attributes ), type );
476 TEST_EQUAL( psa_get_key_bits( &attributes ),
477 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200478 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200479 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200480 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200481 for( n = 0; n < byte_size; n++ )
482 TEST_EQUAL( buffer[n], 'K' );
483 for( n = byte_size; n < buffer_size; n++ )
484 TEST_EQUAL( buffer[n], 0 );
485 }
486
487exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100488 /*
489 * Key attributes may have been returned by psa_get_key_attributes()
490 * thus reset them as required.
491 */
492 psa_reset_key_attributes( &attributes );
493
Ronald Cron5425a212020-08-04 14:58:35 +0200494 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200495 PSA_DONE( );
496 mbedtls_free( buffer );
497}
498/* END_CASE */
499
500/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200501void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
502{
Ronald Cron5425a212020-08-04 14:58:35 +0200503 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200504 size_t bits = bits_arg;
505 psa_status_t expected_status = expected_status_arg;
506 psa_status_t status;
507 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200508 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200509 size_t buffer_size = /* Slight overapproximations */
510 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200511 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200512 unsigned char *p;
513 int ret;
514 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200515 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200516
Gilles Peskine8817f612018-12-18 00:18:46 +0100517 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200518 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200519
520 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
521 bits, keypair ) ) >= 0 );
522 length = ret;
523
524 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200525 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200526 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100527 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200528
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200529 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200530 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200531
532exit:
533 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200534 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200535}
536/* END_CASE */
537
538/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300539void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300540 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200541 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100542 int expected_bits,
543 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200544 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100545 int canonical_input )
546{
Ronald Cron5425a212020-08-04 14:58:35 +0200547 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100548 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200549 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200550 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100551 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100552 unsigned char *exported = NULL;
553 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100554 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100555 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100556 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200557 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200558 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100559
Moran Pekercb088e72018-07-17 17:36:59 +0300560 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200561 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100562 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200563 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100564 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100565
Gilles Peskine4747d192019-04-17 15:05:45 +0200566 psa_set_key_usage_flags( &attributes, usage_arg );
567 psa_set_key_algorithm( &attributes, alg );
568 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700569
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100570 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200571 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100572
573 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200574 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200575 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
576 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200577 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100578
579 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200580 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100581 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100582
583 /* The exported length must be set by psa_export_key() to a value between 0
584 * and export_size. On errors, the exported length must be 0. */
585 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
586 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
587 TEST_ASSERT( exported_length <= export_size );
588
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200589 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200590 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100591 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200592 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100593 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100594 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200595 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100596
Gilles Peskineea38a922021-02-13 00:05:16 +0100597 /* Run sanity checks on the exported key. For non-canonical inputs,
598 * this validates the canonical representations. For canonical inputs,
599 * this doesn't directly validate the implementation, but it still helps
600 * by cross-validating the test data with the sanity check code. */
601 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200602 goto exit;
603
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100604 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200605 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100606 else
607 {
Ronald Cron5425a212020-08-04 14:58:35 +0200608 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200609 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200610 &key2 ) );
611 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100612 reexported,
613 export_size,
614 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200615 ASSERT_COMPARE( exported, exported_length,
616 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200617 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100618 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100619 TEST_ASSERT( exported_length <=
620 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
621 psa_get_key_bits( &got_attributes ) ) );
622 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100623
624destroy:
625 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200626 PSA_ASSERT( psa_destroy_key( key ) );
627 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100628
629exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100630 /*
631 * Key attributes may have been returned by psa_get_key_attributes()
632 * thus reset them as required.
633 */
634 psa_reset_key_attributes( &got_attributes );
635
itayzafrir3e02b3b2018-06-12 17:06:52 +0300636 mbedtls_free( exported );
637 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200638 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100639}
640/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100641
Moran Pekerf709f4a2018-06-06 17:26:04 +0300642/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300643void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200644 int type_arg,
645 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100646 int export_size_delta,
647 int expected_export_status_arg,
648 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300649{
Ronald Cron5425a212020-08-04 14:58:35 +0200650 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300651 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200652 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200653 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300654 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300655 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100656 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100657 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200658 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300659
Gilles Peskine8817f612018-12-18 00:18:46 +0100660 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300661
Gilles Peskine4747d192019-04-17 15:05:45 +0200662 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
663 psa_set_key_algorithm( &attributes, alg );
664 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300665
666 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200667 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300668
Gilles Peskine49c25912018-10-29 15:15:31 +0100669 /* Export the public key */
670 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200671 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200672 exported, export_size,
673 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100674 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100675 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100676 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200677 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100678 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200679 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200680 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100681 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100682 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100683 TEST_ASSERT( expected_public_key->len <=
684 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
685 TEST_ASSERT( expected_public_key->len <=
686 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100687 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
688 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100689 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300690
691exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100692 /*
693 * Key attributes may have been returned by psa_get_key_attributes()
694 * thus reset them as required.
695 */
696 psa_reset_key_attributes( &attributes );
697
itayzafrir3e02b3b2018-06-12 17:06:52 +0300698 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200699 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200700 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300701}
702/* END_CASE */
703
Gilles Peskine20035e32018-02-03 22:44:14 +0100704/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200705void import_and_exercise_key( data_t *data,
706 int type_arg,
707 int bits_arg,
708 int alg_arg )
709{
Ronald Cron5425a212020-08-04 14:58:35 +0200710 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200711 psa_key_type_t type = type_arg;
712 size_t bits = bits_arg;
713 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100714 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200715 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200716 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200717
Gilles Peskine8817f612018-12-18 00:18:46 +0100718 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200719
Gilles Peskine4747d192019-04-17 15:05:45 +0200720 psa_set_key_usage_flags( &attributes, usage );
721 psa_set_key_algorithm( &attributes, alg );
722 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200723
724 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200725 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200726
727 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200728 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200729 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
730 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200731
732 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100733 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200734 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200735
Ronald Cron5425a212020-08-04 14:58:35 +0200736 PSA_ASSERT( psa_destroy_key( key ) );
737 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200738
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200739exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100740 /*
741 * Key attributes may have been returned by psa_get_key_attributes()
742 * thus reset them as required.
743 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200744 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100745
746 psa_reset_key_attributes( &attributes );
747 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200748 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200749}
750/* END_CASE */
751
752/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100753void effective_key_attributes( int type_arg, int expected_type_arg,
754 int bits_arg, int expected_bits_arg,
755 int usage_arg, int expected_usage_arg,
756 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200757{
Ronald Cron5425a212020-08-04 14:58:35 +0200758 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100759 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100760 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100761 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100762 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200763 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100764 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200765 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100766 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200767 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200768
Gilles Peskine8817f612018-12-18 00:18:46 +0100769 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200770
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200771 psa_set_key_usage_flags( &attributes, usage );
772 psa_set_key_algorithm( &attributes, alg );
773 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +0100774 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +0200775
Ronald Cron5425a212020-08-04 14:58:35 +0200776 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +0100777 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +0200778
Ronald Cron5425a212020-08-04 14:58:35 +0200779 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +0100780 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
781 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
782 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
783 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200784
785exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100786 /*
787 * Key attributes may have been returned by psa_get_key_attributes()
788 * thus reset them as required.
789 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200790 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100791
792 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200793 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +0200794}
795/* END_CASE */
796
797/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100798void check_key_policy( int type_arg, int bits_arg,
799 int usage_arg, int alg_arg )
800{
801 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
802 usage_arg, usage_arg, alg_arg, alg_arg );
803 goto exit;
804}
805/* END_CASE */
806
807/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200808void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +0000809{
810 /* Test each valid way of initializing the object, except for `= {0}`, as
811 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
812 * though it's OK by the C standard. We could test for this, but we'd need
813 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200814 psa_key_attributes_t func = psa_key_attributes_init( );
815 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
816 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000817
818 memset( &zero, 0, sizeof( zero ) );
819
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200820 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
821 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
822 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000823
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200824 TEST_EQUAL( psa_get_key_type( &func ), 0 );
825 TEST_EQUAL( psa_get_key_type( &init ), 0 );
826 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
827
828 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
829 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
830 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
831
832 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
833 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
834 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
835
836 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
837 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
838 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +0000839}
840/* END_CASE */
841
842/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200843void mac_key_policy( int policy_usage,
844 int policy_alg,
845 int key_type,
846 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100847 int exercise_alg,
848 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200849{
Ronald Cron5425a212020-08-04 14:58:35 +0200850 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200851 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000852 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200853 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100854 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200855 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200856
Gilles Peskine8817f612018-12-18 00:18:46 +0100857 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200858
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200859 psa_set_key_usage_flags( &attributes, policy_usage );
860 psa_set_key_algorithm( &attributes, policy_alg );
861 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200862
Gilles Peskine049c7532019-05-15 20:22:09 +0200863 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200864 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200865
Ronald Cron5425a212020-08-04 14:58:35 +0200866 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100867 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100868 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100869 else
870 TEST_EQUAL( status, expected_status );
871
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200872 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200873
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200874 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200875 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100876 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100877 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100878 else
879 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200880
881exit:
882 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200883 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200884 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200885}
886/* END_CASE */
887
888/* BEGIN_CASE */
889void cipher_key_policy( int policy_usage,
890 int policy_alg,
891 int key_type,
892 data_t *key_data,
893 int exercise_alg )
894{
Ronald Cron5425a212020-08-04 14:58:35 +0200895 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200896 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000897 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200898 psa_status_t status;
899
Gilles Peskine8817f612018-12-18 00:18:46 +0100900 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200901
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200902 psa_set_key_usage_flags( &attributes, policy_usage );
903 psa_set_key_algorithm( &attributes, policy_alg );
904 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200905
Gilles Peskine049c7532019-05-15 20:22:09 +0200906 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200907 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200908
Ronald Cron5425a212020-08-04 14:58:35 +0200909 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200910 if( policy_alg == exercise_alg &&
911 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100912 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200913 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100914 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200915 psa_cipher_abort( &operation );
916
Ronald Cron5425a212020-08-04 14:58:35 +0200917 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200918 if( policy_alg == exercise_alg &&
919 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100920 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200921 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100922 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200923
924exit:
925 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200926 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200927 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200928}
929/* END_CASE */
930
931/* BEGIN_CASE */
932void aead_key_policy( int policy_usage,
933 int policy_alg,
934 int key_type,
935 data_t *key_data,
936 int nonce_length_arg,
937 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100938 int exercise_alg,
939 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200940{
Ronald Cron5425a212020-08-04 14:58:35 +0200941 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200942 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200943 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100944 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200945 unsigned char nonce[16] = {0};
946 size_t nonce_length = nonce_length_arg;
947 unsigned char tag[16];
948 size_t tag_length = tag_length_arg;
949 size_t output_length;
950
951 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
952 TEST_ASSERT( tag_length <= sizeof( tag ) );
953
Gilles Peskine8817f612018-12-18 00:18:46 +0100954 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200955
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200956 psa_set_key_usage_flags( &attributes, policy_usage );
957 psa_set_key_algorithm( &attributes, policy_alg );
958 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200959
Gilles Peskine049c7532019-05-15 20:22:09 +0200960 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200961 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200962
Ronald Cron5425a212020-08-04 14:58:35 +0200963 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200964 nonce, nonce_length,
965 NULL, 0,
966 NULL, 0,
967 tag, tag_length,
968 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100969 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
970 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200971 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100972 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200973
974 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200975 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200976 nonce, nonce_length,
977 NULL, 0,
978 tag, tag_length,
979 NULL, 0,
980 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100981 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
982 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
983 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100984 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200985 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100986 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200987
988exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200989 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200990 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200991}
992/* END_CASE */
993
994/* BEGIN_CASE */
995void asymmetric_encryption_key_policy( int policy_usage,
996 int policy_alg,
997 int key_type,
998 data_t *key_data,
999 int exercise_alg )
1000{
Ronald Cron5425a212020-08-04 14:58:35 +02001001 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001002 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001003 psa_status_t status;
1004 size_t key_bits;
1005 size_t buffer_length;
1006 unsigned char *buffer = NULL;
1007 size_t output_length;
1008
Gilles Peskine8817f612018-12-18 00:18:46 +01001009 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001010
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001011 psa_set_key_usage_flags( &attributes, policy_usage );
1012 psa_set_key_algorithm( &attributes, policy_alg );
1013 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001014
Gilles Peskine049c7532019-05-15 20:22:09 +02001015 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001016 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001017
Ronald Cron5425a212020-08-04 14:58:35 +02001018 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001019 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001020 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1021 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001022 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001023
Ronald Cron5425a212020-08-04 14:58:35 +02001024 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001025 NULL, 0,
1026 NULL, 0,
1027 buffer, buffer_length,
1028 &output_length );
1029 if( policy_alg == exercise_alg &&
1030 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001031 PSA_ASSERT( status );
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
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001035 if( buffer_length != 0 )
1036 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001037 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001038 buffer, buffer_length,
1039 NULL, 0,
1040 buffer, buffer_length,
1041 &output_length );
1042 if( policy_alg == exercise_alg &&
1043 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001044 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001045 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001046 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001047
1048exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001049 /*
1050 * Key attributes may have been returned by psa_get_key_attributes()
1051 * thus reset them as required.
1052 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001053 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001054
1055 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001056 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001057 mbedtls_free( buffer );
1058}
1059/* END_CASE */
1060
1061/* BEGIN_CASE */
1062void asymmetric_signature_key_policy( int policy_usage,
1063 int policy_alg,
1064 int key_type,
1065 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001066 int exercise_alg,
1067 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001068{
Ronald Cron5425a212020-08-04 14:58:35 +02001069 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001070 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001071 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001072 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1073 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1074 * compatible with the policy and `payload_length_arg` is supposed to be
1075 * a valid input length to sign. If `payload_length_arg <= 0`,
1076 * `exercise_alg` is supposed to be forbidden by the policy. */
1077 int compatible_alg = payload_length_arg > 0;
1078 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001079 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001080 size_t signature_length;
1081
Gilles Peskine8817f612018-12-18 00:18:46 +01001082 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001083
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001084 psa_set_key_usage_flags( &attributes, policy_usage );
1085 psa_set_key_algorithm( &attributes, policy_alg );
1086 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001087
Gilles Peskine049c7532019-05-15 20:22:09 +02001088 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001089 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001090
Ronald Cron5425a212020-08-04 14:58:35 +02001091 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001092 payload, payload_length,
1093 signature, sizeof( signature ),
1094 &signature_length );
1095 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001096 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001097 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001098 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001099
1100 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001101 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001102 payload, payload_length,
1103 signature, sizeof( signature ) );
1104 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001105 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001106 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001107 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001108
1109exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001110 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001111 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001112}
1113/* END_CASE */
1114
Janos Follathba3fab92019-06-11 14:50:16 +01001115/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001116void derive_key_policy( int policy_usage,
1117 int policy_alg,
1118 int key_type,
1119 data_t *key_data,
1120 int exercise_alg )
1121{
Ronald Cron5425a212020-08-04 14:58:35 +02001122 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001123 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001124 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001125 psa_status_t status;
1126
Gilles Peskine8817f612018-12-18 00:18:46 +01001127 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001128
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001129 psa_set_key_usage_flags( &attributes, policy_usage );
1130 psa_set_key_algorithm( &attributes, policy_alg );
1131 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001132
Gilles Peskine049c7532019-05-15 20:22:09 +02001133 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001134 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001135
Janos Follathba3fab92019-06-11 14:50:16 +01001136 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1137
1138 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1139 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001140 {
Janos Follathba3fab92019-06-11 14:50:16 +01001141 PSA_ASSERT( psa_key_derivation_input_bytes(
1142 &operation,
1143 PSA_KEY_DERIVATION_INPUT_SEED,
1144 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001145 }
Janos Follathba3fab92019-06-11 14:50:16 +01001146
1147 status = psa_key_derivation_input_key( &operation,
1148 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001149 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001150
Gilles Peskineea0fb492018-07-12 17:17:20 +02001151 if( policy_alg == exercise_alg &&
1152 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001153 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001154 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001155 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001156
1157exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001158 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001159 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001160 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001161}
1162/* END_CASE */
1163
1164/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001165void agreement_key_policy( int policy_usage,
1166 int policy_alg,
1167 int key_type_arg,
1168 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001169 int exercise_alg,
1170 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001171{
Ronald Cron5425a212020-08-04 14:58:35 +02001172 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001173 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001174 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001175 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001176 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001177 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001178
Gilles Peskine8817f612018-12-18 00:18:46 +01001179 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001180
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001181 psa_set_key_usage_flags( &attributes, policy_usage );
1182 psa_set_key_algorithm( &attributes, policy_alg );
1183 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001184
Gilles Peskine049c7532019-05-15 20:22:09 +02001185 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001186 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001187
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001188 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001189 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001190
Steven Cooremance48e852020-10-05 16:02:45 +02001191 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001192
1193exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001194 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001195 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001196 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001197}
1198/* END_CASE */
1199
1200/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001201void key_policy_alg2( int key_type_arg, data_t *key_data,
1202 int usage_arg, int alg_arg, int alg2_arg )
1203{
Ronald Cron5425a212020-08-04 14:58:35 +02001204 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001205 psa_key_type_t key_type = key_type_arg;
1206 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1207 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1208 psa_key_usage_t usage = usage_arg;
1209 psa_algorithm_t alg = alg_arg;
1210 psa_algorithm_t alg2 = alg2_arg;
1211
1212 PSA_ASSERT( psa_crypto_init( ) );
1213
1214 psa_set_key_usage_flags( &attributes, usage );
1215 psa_set_key_algorithm( &attributes, alg );
1216 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1217 psa_set_key_type( &attributes, key_type );
1218 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001219 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001220
Ronald Cron5425a212020-08-04 14:58:35 +02001221 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001222 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1223 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1224 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1225
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001226 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001227 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001228 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001229 goto exit;
1230
1231exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001232 /*
1233 * Key attributes may have been returned by psa_get_key_attributes()
1234 * thus reset them as required.
1235 */
1236 psa_reset_key_attributes( &got_attributes );
1237
Ronald Cron5425a212020-08-04 14:58:35 +02001238 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001239 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001240}
1241/* END_CASE */
1242
1243/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001244void raw_agreement_key_policy( int policy_usage,
1245 int policy_alg,
1246 int key_type_arg,
1247 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001248 int exercise_alg,
1249 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001250{
Ronald Cron5425a212020-08-04 14:58:35 +02001251 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001252 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001253 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001254 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001255 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001256 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001257
1258 PSA_ASSERT( psa_crypto_init( ) );
1259
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001260 psa_set_key_usage_flags( &attributes, policy_usage );
1261 psa_set_key_algorithm( &attributes, policy_alg );
1262 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001263
Gilles Peskine049c7532019-05-15 20:22:09 +02001264 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001265 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001266
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001267 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001268
Steven Cooremance48e852020-10-05 16:02:45 +02001269 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001270
1271exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001272 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001273 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001274 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001275}
1276/* END_CASE */
1277
1278/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001279void copy_success( int source_usage_arg,
1280 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001281 int type_arg, data_t *material,
1282 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001283 int target_usage_arg,
1284 int target_alg_arg, int target_alg2_arg,
1285 int expected_usage_arg,
1286 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001287{
Gilles Peskineca25db92019-04-19 11:43:08 +02001288 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1289 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001290 psa_key_usage_t expected_usage = expected_usage_arg;
1291 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001292 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001293 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1294 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001295 uint8_t *export_buffer = NULL;
1296
Gilles Peskine57ab7212019-01-28 13:03:09 +01001297 PSA_ASSERT( psa_crypto_init( ) );
1298
Gilles Peskineca25db92019-04-19 11:43:08 +02001299 /* Prepare the source key. */
1300 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1301 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001302 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001303 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001304 PSA_ASSERT( psa_import_key( &source_attributes,
1305 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001306 &source_key ) );
1307 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001308
Gilles Peskineca25db92019-04-19 11:43:08 +02001309 /* Prepare the target attributes. */
1310 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001311 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001312 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001313 /* Set volatile lifetime to reset the key identifier to 0. */
1314 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1315 }
1316
Gilles Peskineca25db92019-04-19 11:43:08 +02001317 if( target_usage_arg != -1 )
1318 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1319 if( target_alg_arg != -1 )
1320 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001321 if( target_alg2_arg != -1 )
1322 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001323
1324 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001325 PSA_ASSERT( psa_copy_key( source_key,
1326 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001327
1328 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001329 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001330
1331 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001332 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001333 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1334 psa_get_key_type( &target_attributes ) );
1335 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1336 psa_get_key_bits( &target_attributes ) );
1337 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1338 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001339 TEST_EQUAL( expected_alg2,
1340 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001341 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1342 {
1343 size_t length;
1344 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001345 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001346 material->len, &length ) );
1347 ASSERT_COMPARE( material->x, material->len,
1348 export_buffer, length );
1349 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001350
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001351 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001352 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001353 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001354 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001355
Ronald Cron5425a212020-08-04 14:58:35 +02001356 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001357
1358exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001359 /*
1360 * Source and target key attributes may have been returned by
1361 * psa_get_key_attributes() thus reset them as required.
1362 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001363 psa_reset_key_attributes( &source_attributes );
1364 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001365
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001366 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001367 mbedtls_free( export_buffer );
1368}
1369/* END_CASE */
1370
1371/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001372void copy_fail( int source_usage_arg,
1373 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001374 int type_arg, data_t *material,
1375 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001376 int target_usage_arg,
1377 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001378 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001379 int expected_status_arg )
1380{
1381 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1382 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001383 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1384 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001385 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001386
1387 PSA_ASSERT( psa_crypto_init( ) );
1388
1389 /* Prepare the source key. */
1390 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1391 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001392 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001393 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001394 PSA_ASSERT( psa_import_key( &source_attributes,
1395 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001396 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001397
1398 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001399 psa_set_key_id( &target_attributes, key_id );
1400 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001401 psa_set_key_type( &target_attributes, target_type_arg );
1402 psa_set_key_bits( &target_attributes, target_bits_arg );
1403 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1404 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001405 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001406
1407 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001408 TEST_EQUAL( psa_copy_key( source_key,
1409 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001410 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001411
Ronald Cron5425a212020-08-04 14:58:35 +02001412 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001413
Gilles Peskine4a644642019-05-03 17:14:08 +02001414exit:
1415 psa_reset_key_attributes( &source_attributes );
1416 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001417 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001418}
1419/* END_CASE */
1420
1421/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001422void hash_operation_init( )
1423{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001424 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001425 /* Test each valid way of initializing the object, except for `= {0}`, as
1426 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1427 * though it's OK by the C standard. We could test for this, but we'd need
1428 * to supress the Clang warning for the test. */
1429 psa_hash_operation_t func = psa_hash_operation_init( );
1430 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1431 psa_hash_operation_t zero;
1432
1433 memset( &zero, 0, sizeof( zero ) );
1434
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001435 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001436 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1437 PSA_ERROR_BAD_STATE );
1438 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1439 PSA_ERROR_BAD_STATE );
1440 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1441 PSA_ERROR_BAD_STATE );
1442
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001443 /* A default hash operation should be abortable without error. */
1444 PSA_ASSERT( psa_hash_abort( &func ) );
1445 PSA_ASSERT( psa_hash_abort( &init ) );
1446 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001447}
1448/* END_CASE */
1449
1450/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001451void hash_setup( int alg_arg,
1452 int expected_status_arg )
1453{
1454 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001455 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001456 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001457 psa_status_t status;
1458
Gilles Peskine8817f612018-12-18 00:18:46 +01001459 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001460
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001461 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001462 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001463
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001464 /* Whether setup succeeded or failed, abort must succeed. */
1465 PSA_ASSERT( psa_hash_abort( &operation ) );
1466
1467 /* If setup failed, reproduce the failure, so as to
1468 * test the resulting state of the operation object. */
1469 if( status != PSA_SUCCESS )
1470 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1471
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001472 /* Now the operation object should be reusable. */
1473#if defined(KNOWN_SUPPORTED_HASH_ALG)
1474 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1475 PSA_ASSERT( psa_hash_abort( &operation ) );
1476#endif
1477
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001478exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001479 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001480}
1481/* END_CASE */
1482
1483/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001484void hash_compute_fail( int alg_arg, data_t *input,
1485 int output_size_arg, int expected_status_arg )
1486{
1487 psa_algorithm_t alg = alg_arg;
1488 uint8_t *output = NULL;
1489 size_t output_size = output_size_arg;
1490 size_t output_length = INVALID_EXPORT_LENGTH;
1491 psa_status_t expected_status = expected_status_arg;
1492 psa_status_t status;
1493
1494 ASSERT_ALLOC( output, output_size );
1495
1496 PSA_ASSERT( psa_crypto_init( ) );
1497
1498 status = psa_hash_compute( alg, input->x, input->len,
1499 output, output_size, &output_length );
1500 TEST_EQUAL( status, expected_status );
1501 TEST_ASSERT( output_length <= output_size );
1502
1503exit:
1504 mbedtls_free( output );
1505 PSA_DONE( );
1506}
1507/* END_CASE */
1508
1509/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001510void hash_compare_fail( int alg_arg, data_t *input,
1511 data_t *reference_hash,
1512 int expected_status_arg )
1513{
1514 psa_algorithm_t alg = alg_arg;
1515 psa_status_t expected_status = expected_status_arg;
1516 psa_status_t status;
1517
1518 PSA_ASSERT( psa_crypto_init( ) );
1519
1520 status = psa_hash_compare( alg, input->x, input->len,
1521 reference_hash->x, reference_hash->len );
1522 TEST_EQUAL( status, expected_status );
1523
1524exit:
1525 PSA_DONE( );
1526}
1527/* END_CASE */
1528
1529/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001530void hash_compute_compare( int alg_arg, data_t *input,
1531 data_t *expected_output )
1532{
1533 psa_algorithm_t alg = alg_arg;
1534 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1535 size_t output_length = INVALID_EXPORT_LENGTH;
1536 size_t i;
1537
1538 PSA_ASSERT( psa_crypto_init( ) );
1539
1540 /* Compute with tight buffer */
1541 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001542 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001543 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001544 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001545 ASSERT_COMPARE( output, output_length,
1546 expected_output->x, expected_output->len );
1547
1548 /* Compute with larger buffer */
1549 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1550 output, sizeof( output ),
1551 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001552 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001553 ASSERT_COMPARE( output, output_length,
1554 expected_output->x, expected_output->len );
1555
1556 /* Compare with correct hash */
1557 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1558 output, output_length ) );
1559
1560 /* Compare with trailing garbage */
1561 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1562 output, output_length + 1 ),
1563 PSA_ERROR_INVALID_SIGNATURE );
1564
1565 /* Compare with truncated hash */
1566 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1567 output, output_length - 1 ),
1568 PSA_ERROR_INVALID_SIGNATURE );
1569
1570 /* Compare with corrupted value */
1571 for( i = 0; i < output_length; i++ )
1572 {
Chris Jones9634bb12021-01-20 15:56:42 +00001573 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001574 output[i] ^= 1;
1575 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1576 output, output_length ),
1577 PSA_ERROR_INVALID_SIGNATURE );
1578 output[i] ^= 1;
1579 }
1580
1581exit:
1582 PSA_DONE( );
1583}
1584/* END_CASE */
1585
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001586/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001587void hash_bad_order( )
1588{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001589 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001590 unsigned char input[] = "";
1591 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001592 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001593 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1594 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1595 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001596 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001597 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001598 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001599
Gilles Peskine8817f612018-12-18 00:18:46 +01001600 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001601
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001602 /* Call setup twice in a row. */
1603 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1604 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1605 PSA_ERROR_BAD_STATE );
1606 PSA_ASSERT( psa_hash_abort( &operation ) );
1607
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001608 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001609 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001610 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001611 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001612
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001613 /* Call update after finish. */
1614 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1615 PSA_ASSERT( psa_hash_finish( &operation,
1616 hash, sizeof( hash ), &hash_len ) );
1617 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001618 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001619 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001620
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001621 /* Call verify without calling setup beforehand. */
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 verify after finish. */
1628 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1629 PSA_ASSERT( psa_hash_finish( &operation,
1630 hash, sizeof( hash ), &hash_len ) );
1631 TEST_EQUAL( psa_hash_verify( &operation,
1632 valid_hash, sizeof( valid_hash ) ),
1633 PSA_ERROR_BAD_STATE );
1634 PSA_ASSERT( psa_hash_abort( &operation ) );
1635
1636 /* Call verify twice in a row. */
1637 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1638 PSA_ASSERT( psa_hash_verify( &operation,
1639 valid_hash, sizeof( valid_hash ) ) );
1640 TEST_EQUAL( psa_hash_verify( &operation,
1641 valid_hash, sizeof( valid_hash ) ),
1642 PSA_ERROR_BAD_STATE );
1643 PSA_ASSERT( psa_hash_abort( &operation ) );
1644
1645 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001646 TEST_EQUAL( psa_hash_finish( &operation,
1647 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001648 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001649 PSA_ASSERT( psa_hash_abort( &operation ) );
1650
1651 /* Call finish twice in a row. */
1652 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1653 PSA_ASSERT( psa_hash_finish( &operation,
1654 hash, sizeof( hash ), &hash_len ) );
1655 TEST_EQUAL( psa_hash_finish( &operation,
1656 hash, sizeof( hash ), &hash_len ),
1657 PSA_ERROR_BAD_STATE );
1658 PSA_ASSERT( psa_hash_abort( &operation ) );
1659
1660 /* Call finish after calling verify. */
1661 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1662 PSA_ASSERT( psa_hash_verify( &operation,
1663 valid_hash, sizeof( valid_hash ) ) );
1664 TEST_EQUAL( psa_hash_finish( &operation,
1665 hash, sizeof( hash ), &hash_len ),
1666 PSA_ERROR_BAD_STATE );
1667 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001668
1669exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001670 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001671}
1672/* END_CASE */
1673
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001674/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001675void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001676{
1677 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001678 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1679 * appended to it */
1680 unsigned char hash[] = {
1681 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1682 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1683 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001684 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001685 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001686
Gilles Peskine8817f612018-12-18 00:18:46 +01001687 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001688
itayzafrir27e69452018-11-01 14:26:34 +02001689 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001690 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001691 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001692 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001693
itayzafrir27e69452018-11-01 14:26:34 +02001694 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001695 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001696 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001697 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001698
itayzafrir27e69452018-11-01 14:26:34 +02001699 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001700 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001701 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001702 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001703
itayzafrirec93d302018-10-18 18:01:10 +03001704exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001705 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001706}
1707/* END_CASE */
1708
Ronald Cronee414c72021-03-18 18:50:08 +01001709/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001710void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001711{
1712 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001713 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001714 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001715 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001716 size_t hash_len;
1717
Gilles Peskine8817f612018-12-18 00:18:46 +01001718 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001719
itayzafrir58028322018-10-25 10:22:01 +03001720 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001721 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001722 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001723 hash, expected_size - 1, &hash_len ),
1724 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001725
1726exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001727 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001728}
1729/* END_CASE */
1730
Ronald Cronee414c72021-03-18 18:50:08 +01001731/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001732void hash_clone_source_state( )
1733{
1734 psa_algorithm_t alg = PSA_ALG_SHA_256;
1735 unsigned char hash[PSA_HASH_MAX_SIZE];
1736 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1737 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1738 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1739 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1740 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1741 size_t hash_len;
1742
1743 PSA_ASSERT( psa_crypto_init( ) );
1744 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1745
1746 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1747 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1748 PSA_ASSERT( psa_hash_finish( &op_finished,
1749 hash, sizeof( hash ), &hash_len ) );
1750 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1751 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1752
1753 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1754 PSA_ERROR_BAD_STATE );
1755
1756 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1757 PSA_ASSERT( psa_hash_finish( &op_init,
1758 hash, sizeof( hash ), &hash_len ) );
1759 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1760 PSA_ASSERT( psa_hash_finish( &op_finished,
1761 hash, sizeof( hash ), &hash_len ) );
1762 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1763 PSA_ASSERT( psa_hash_finish( &op_aborted,
1764 hash, sizeof( hash ), &hash_len ) );
1765
1766exit:
1767 psa_hash_abort( &op_source );
1768 psa_hash_abort( &op_init );
1769 psa_hash_abort( &op_setup );
1770 psa_hash_abort( &op_finished );
1771 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001772 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001773}
1774/* END_CASE */
1775
Ronald Cronee414c72021-03-18 18:50:08 +01001776/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001777void hash_clone_target_state( )
1778{
1779 psa_algorithm_t alg = PSA_ALG_SHA_256;
1780 unsigned char hash[PSA_HASH_MAX_SIZE];
1781 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1782 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1783 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1784 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1785 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1786 size_t hash_len;
1787
1788 PSA_ASSERT( psa_crypto_init( ) );
1789
1790 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1791 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1792 PSA_ASSERT( psa_hash_finish( &op_finished,
1793 hash, sizeof( hash ), &hash_len ) );
1794 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1795 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1796
1797 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1798 PSA_ASSERT( psa_hash_finish( &op_target,
1799 hash, sizeof( hash ), &hash_len ) );
1800
1801 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1802 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1803 PSA_ERROR_BAD_STATE );
1804 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1805 PSA_ERROR_BAD_STATE );
1806
1807exit:
1808 psa_hash_abort( &op_target );
1809 psa_hash_abort( &op_init );
1810 psa_hash_abort( &op_setup );
1811 psa_hash_abort( &op_finished );
1812 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001813 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001814}
1815/* END_CASE */
1816
itayzafrir58028322018-10-25 10:22:01 +03001817/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001818void mac_operation_init( )
1819{
Jaeden Amero252ef282019-02-15 14:05:35 +00001820 const uint8_t input[1] = { 0 };
1821
Jaeden Amero769ce272019-01-04 11:48:03 +00001822 /* Test each valid way of initializing the object, except for `= {0}`, as
1823 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1824 * though it's OK by the C standard. We could test for this, but we'd need
1825 * to supress the Clang warning for the test. */
1826 psa_mac_operation_t func = psa_mac_operation_init( );
1827 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1828 psa_mac_operation_t zero;
1829
1830 memset( &zero, 0, sizeof( zero ) );
1831
Jaeden Amero252ef282019-02-15 14:05:35 +00001832 /* A freshly-initialized MAC operation should not be usable. */
1833 TEST_EQUAL( psa_mac_update( &func,
1834 input, sizeof( input ) ),
1835 PSA_ERROR_BAD_STATE );
1836 TEST_EQUAL( psa_mac_update( &init,
1837 input, sizeof( input ) ),
1838 PSA_ERROR_BAD_STATE );
1839 TEST_EQUAL( psa_mac_update( &zero,
1840 input, sizeof( input ) ),
1841 PSA_ERROR_BAD_STATE );
1842
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001843 /* A default MAC operation should be abortable without error. */
1844 PSA_ASSERT( psa_mac_abort( &func ) );
1845 PSA_ASSERT( psa_mac_abort( &init ) );
1846 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001847}
1848/* END_CASE */
1849
1850/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001851void mac_setup( int key_type_arg,
1852 data_t *key,
1853 int alg_arg,
1854 int expected_status_arg )
1855{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001856 psa_key_type_t key_type = key_type_arg;
1857 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001858 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001859 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001860 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1861#if defined(KNOWN_SUPPORTED_MAC_ALG)
1862 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1863#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001864
Gilles Peskine8817f612018-12-18 00:18:46 +01001865 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001866
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001867 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1868 &operation, &status ) )
1869 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001870 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001871
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001872 /* The operation object should be reusable. */
1873#if defined(KNOWN_SUPPORTED_MAC_ALG)
1874 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1875 smoke_test_key_data,
1876 sizeof( smoke_test_key_data ),
1877 KNOWN_SUPPORTED_MAC_ALG,
1878 &operation, &status ) )
1879 goto exit;
1880 TEST_EQUAL( status, PSA_SUCCESS );
1881#endif
1882
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001883exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001884 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001885}
1886/* END_CASE */
1887
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001888/* 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 +00001889void mac_bad_order( )
1890{
Ronald Cron5425a212020-08-04 14:58:35 +02001891 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001892 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1893 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001894 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001895 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1896 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1897 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001898 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001899 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1900 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1901 size_t sign_mac_length = 0;
1902 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1903 const uint8_t verify_mac[] = {
1904 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1905 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1906 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1907
1908 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001909 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001910 psa_set_key_algorithm( &attributes, alg );
1911 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001912
Ronald Cron5425a212020-08-04 14:58:35 +02001913 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1914 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001915
Jaeden Amero252ef282019-02-15 14:05:35 +00001916 /* Call update without calling setup beforehand. */
1917 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1918 PSA_ERROR_BAD_STATE );
1919 PSA_ASSERT( psa_mac_abort( &operation ) );
1920
1921 /* Call sign finish without calling setup beforehand. */
1922 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1923 &sign_mac_length),
1924 PSA_ERROR_BAD_STATE );
1925 PSA_ASSERT( psa_mac_abort( &operation ) );
1926
1927 /* Call verify finish without calling setup beforehand. */
1928 TEST_EQUAL( psa_mac_verify_finish( &operation,
1929 verify_mac, sizeof( verify_mac ) ),
1930 PSA_ERROR_BAD_STATE );
1931 PSA_ASSERT( psa_mac_abort( &operation ) );
1932
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001933 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001934 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
1935 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001936 PSA_ERROR_BAD_STATE );
1937 PSA_ASSERT( psa_mac_abort( &operation ) );
1938
Jaeden Amero252ef282019-02-15 14:05:35 +00001939 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001940 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001941 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1942 PSA_ASSERT( psa_mac_sign_finish( &operation,
1943 sign_mac, sizeof( sign_mac ),
1944 &sign_mac_length ) );
1945 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1946 PSA_ERROR_BAD_STATE );
1947 PSA_ASSERT( psa_mac_abort( &operation ) );
1948
1949 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001950 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001951 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1952 PSA_ASSERT( psa_mac_verify_finish( &operation,
1953 verify_mac, sizeof( verify_mac ) ) );
1954 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1955 PSA_ERROR_BAD_STATE );
1956 PSA_ASSERT( psa_mac_abort( &operation ) );
1957
1958 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001959 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001960 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1961 PSA_ASSERT( psa_mac_sign_finish( &operation,
1962 sign_mac, sizeof( sign_mac ),
1963 &sign_mac_length ) );
1964 TEST_EQUAL( psa_mac_sign_finish( &operation,
1965 sign_mac, sizeof( sign_mac ),
1966 &sign_mac_length ),
1967 PSA_ERROR_BAD_STATE );
1968 PSA_ASSERT( psa_mac_abort( &operation ) );
1969
1970 /* Call verify finish twice in a row. */
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 PSA_ASSERT( psa_mac_verify_finish( &operation,
1974 verify_mac, sizeof( verify_mac ) ) );
1975 TEST_EQUAL( psa_mac_verify_finish( &operation,
1976 verify_mac, sizeof( verify_mac ) ),
1977 PSA_ERROR_BAD_STATE );
1978 PSA_ASSERT( psa_mac_abort( &operation ) );
1979
1980 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02001981 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001982 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1983 TEST_EQUAL( psa_mac_verify_finish( &operation,
1984 verify_mac, sizeof( verify_mac ) ),
1985 PSA_ERROR_BAD_STATE );
1986 PSA_ASSERT( psa_mac_abort( &operation ) );
1987
1988 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02001989 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001990 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1991 TEST_EQUAL( psa_mac_sign_finish( &operation,
1992 sign_mac, sizeof( sign_mac ),
1993 &sign_mac_length ),
1994 PSA_ERROR_BAD_STATE );
1995 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001996
Ronald Cron5425a212020-08-04 14:58:35 +02001997 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001998
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001999exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002000 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002001}
2002/* END_CASE */
2003
2004/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002005void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002006 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002007 int alg_arg,
2008 data_t *input,
2009 data_t *expected_mac )
2010{
Ronald Cron5425a212020-08-04 14:58:35 +02002011 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002012 psa_key_type_t key_type = key_type_arg;
2013 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002014 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002015 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002016 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002017 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002018 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002019 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002020 const size_t output_sizes_to_test[] = {
2021 0,
2022 1,
2023 expected_mac->len - 1,
2024 expected_mac->len,
2025 expected_mac->len + 1,
2026 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002027
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002028 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002029 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002030 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002031
Gilles Peskine8817f612018-12-18 00:18:46 +01002032 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002033
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002034 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002035 psa_set_key_algorithm( &attributes, alg );
2036 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002037
Ronald Cron5425a212020-08-04 14:58:35 +02002038 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2039 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002040
Gilles Peskine8b356b52020-08-25 23:44:59 +02002041 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2042 {
2043 const size_t output_size = output_sizes_to_test[i];
2044 psa_status_t expected_status =
2045 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2046 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002047
Chris Jones9634bb12021-01-20 15:56:42 +00002048 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002049 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002050
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002051 /* Calculate the MAC, one-shot case. */
2052 TEST_EQUAL( psa_mac_compute( key, alg,
2053 input->x, input->len,
2054 actual_mac, output_size, &mac_length ),
2055 expected_status );
2056 if( expected_status == PSA_SUCCESS )
2057 {
2058 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2059 actual_mac, mac_length );
2060 }
2061
2062 if( output_size > 0 )
2063 memset( actual_mac, 0, output_size );
2064
2065 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002066 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002067 PSA_ASSERT( psa_mac_update( &operation,
2068 input->x, input->len ) );
2069 TEST_EQUAL( psa_mac_sign_finish( &operation,
2070 actual_mac, output_size,
2071 &mac_length ),
2072 expected_status );
2073 PSA_ASSERT( psa_mac_abort( &operation ) );
2074
2075 if( expected_status == PSA_SUCCESS )
2076 {
2077 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2078 actual_mac, mac_length );
2079 }
2080 mbedtls_free( actual_mac );
2081 actual_mac = NULL;
2082 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002083
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002084exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002085 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002086 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002087 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002088 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002089}
2090/* END_CASE */
2091
2092/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002093void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002094 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002095 int alg_arg,
2096 data_t *input,
2097 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002098{
Ronald Cron5425a212020-08-04 14:58:35 +02002099 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002100 psa_key_type_t key_type = key_type_arg;
2101 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002102 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002103 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002104 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002105
Gilles Peskine69c12672018-06-28 00:07:19 +02002106 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2107
Gilles Peskine8817f612018-12-18 00:18:46 +01002108 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002109
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002110 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002111 psa_set_key_algorithm( &attributes, alg );
2112 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002113
Ronald Cron5425a212020-08-04 14:58:35 +02002114 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2115 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002116
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002117 /* Verify correct MAC, one-shot case. */
2118 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2119 expected_mac->x, expected_mac->len ) );
2120
2121 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002122 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002123 PSA_ASSERT( psa_mac_update( &operation,
2124 input->x, input->len ) );
2125 PSA_ASSERT( psa_mac_verify_finish( &operation,
2126 expected_mac->x,
2127 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002128
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002129 /* Test a MAC that's too short, one-shot case. */
2130 TEST_EQUAL( psa_mac_verify( key, alg,
2131 input->x, input->len,
2132 expected_mac->x,
2133 expected_mac->len - 1 ),
2134 PSA_ERROR_INVALID_SIGNATURE );
2135
2136 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002137 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002138 PSA_ASSERT( psa_mac_update( &operation,
2139 input->x, input->len ) );
2140 TEST_EQUAL( psa_mac_verify_finish( &operation,
2141 expected_mac->x,
2142 expected_mac->len - 1 ),
2143 PSA_ERROR_INVALID_SIGNATURE );
2144
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002145 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002146 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2147 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002148 TEST_EQUAL( psa_mac_verify( key, alg,
2149 input->x, input->len,
2150 perturbed_mac, expected_mac->len + 1 ),
2151 PSA_ERROR_INVALID_SIGNATURE );
2152
2153 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002154 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002155 PSA_ASSERT( psa_mac_update( &operation,
2156 input->x, input->len ) );
2157 TEST_EQUAL( psa_mac_verify_finish( &operation,
2158 perturbed_mac,
2159 expected_mac->len + 1 ),
2160 PSA_ERROR_INVALID_SIGNATURE );
2161
2162 /* Test changing one byte. */
2163 for( size_t i = 0; i < expected_mac->len; i++ )
2164 {
Chris Jones9634bb12021-01-20 15:56:42 +00002165 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002166 perturbed_mac[i] ^= 1;
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002167
2168 TEST_EQUAL( psa_mac_verify( key, alg,
2169 input->x, input->len,
2170 perturbed_mac, expected_mac->len ),
2171 PSA_ERROR_INVALID_SIGNATURE );
2172
Ronald Cron5425a212020-08-04 14:58:35 +02002173 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002174 PSA_ASSERT( psa_mac_update( &operation,
2175 input->x, input->len ) );
2176 TEST_EQUAL( psa_mac_verify_finish( &operation,
2177 perturbed_mac,
2178 expected_mac->len ),
2179 PSA_ERROR_INVALID_SIGNATURE );
2180 perturbed_mac[i] ^= 1;
2181 }
2182
Gilles Peskine8c9def32018-02-08 10:02:12 +01002183exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002184 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002185 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002186 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002187 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002188}
2189/* END_CASE */
2190
2191/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002192void cipher_operation_init( )
2193{
Jaeden Ameroab439972019-02-15 14:12:05 +00002194 const uint8_t input[1] = { 0 };
2195 unsigned char output[1] = { 0 };
2196 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002197 /* Test each valid way of initializing the object, except for `= {0}`, as
2198 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2199 * though it's OK by the C standard. We could test for this, but we'd need
2200 * to supress the Clang warning for the test. */
2201 psa_cipher_operation_t func = psa_cipher_operation_init( );
2202 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2203 psa_cipher_operation_t zero;
2204
2205 memset( &zero, 0, sizeof( zero ) );
2206
Jaeden Ameroab439972019-02-15 14:12:05 +00002207 /* A freshly-initialized cipher operation should not be usable. */
2208 TEST_EQUAL( psa_cipher_update( &func,
2209 input, sizeof( input ),
2210 output, sizeof( output ),
2211 &output_length ),
2212 PSA_ERROR_BAD_STATE );
2213 TEST_EQUAL( psa_cipher_update( &init,
2214 input, sizeof( input ),
2215 output, sizeof( output ),
2216 &output_length ),
2217 PSA_ERROR_BAD_STATE );
2218 TEST_EQUAL( psa_cipher_update( &zero,
2219 input, sizeof( input ),
2220 output, sizeof( output ),
2221 &output_length ),
2222 PSA_ERROR_BAD_STATE );
2223
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002224 /* A default cipher operation should be abortable without error. */
2225 PSA_ASSERT( psa_cipher_abort( &func ) );
2226 PSA_ASSERT( psa_cipher_abort( &init ) );
2227 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002228}
2229/* END_CASE */
2230
2231/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002232void cipher_setup( int key_type_arg,
2233 data_t *key,
2234 int alg_arg,
2235 int expected_status_arg )
2236{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002237 psa_key_type_t key_type = key_type_arg;
2238 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002239 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002240 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002241 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002242#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002243 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2244#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002245
Gilles Peskine8817f612018-12-18 00:18:46 +01002246 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002247
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002248 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2249 &operation, &status ) )
2250 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002251 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002252
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002253 /* The operation object should be reusable. */
2254#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2255 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2256 smoke_test_key_data,
2257 sizeof( smoke_test_key_data ),
2258 KNOWN_SUPPORTED_CIPHER_ALG,
2259 &operation, &status ) )
2260 goto exit;
2261 TEST_EQUAL( status, PSA_SUCCESS );
2262#endif
2263
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002264exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002265 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002266 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002267}
2268/* END_CASE */
2269
Ronald Cronee414c72021-03-18 18:50:08 +01002270/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002271void cipher_bad_order( )
2272{
Ronald Cron5425a212020-08-04 14:58:35 +02002273 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002274 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2275 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002276 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002277 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002278 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002279 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002280 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2281 0xaa, 0xaa, 0xaa, 0xaa };
2282 const uint8_t text[] = {
2283 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2284 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002285 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002286 size_t length = 0;
2287
2288 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002289 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2290 psa_set_key_algorithm( &attributes, alg );
2291 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002292 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2293 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002294
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002295 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002296 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2297 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002298 PSA_ERROR_BAD_STATE );
2299 PSA_ASSERT( psa_cipher_abort( &operation ) );
2300
2301 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002302 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2303 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002304 PSA_ERROR_BAD_STATE );
2305 PSA_ASSERT( psa_cipher_abort( &operation ) );
2306
Jaeden Ameroab439972019-02-15 14:12:05 +00002307 /* Generate an IV without calling setup beforehand. */
2308 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2309 buffer, sizeof( buffer ),
2310 &length ),
2311 PSA_ERROR_BAD_STATE );
2312 PSA_ASSERT( psa_cipher_abort( &operation ) );
2313
2314 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002315 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002316 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2317 buffer, sizeof( buffer ),
2318 &length ) );
2319 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2320 buffer, sizeof( buffer ),
2321 &length ),
2322 PSA_ERROR_BAD_STATE );
2323 PSA_ASSERT( psa_cipher_abort( &operation ) );
2324
2325 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002326 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002327 PSA_ASSERT( psa_cipher_set_iv( &operation,
2328 iv, sizeof( iv ) ) );
2329 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2330 buffer, sizeof( buffer ),
2331 &length ),
2332 PSA_ERROR_BAD_STATE );
2333 PSA_ASSERT( psa_cipher_abort( &operation ) );
2334
2335 /* Set an IV without calling setup beforehand. */
2336 TEST_EQUAL( psa_cipher_set_iv( &operation,
2337 iv, sizeof( iv ) ),
2338 PSA_ERROR_BAD_STATE );
2339 PSA_ASSERT( psa_cipher_abort( &operation ) );
2340
2341 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002342 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002343 PSA_ASSERT( psa_cipher_set_iv( &operation,
2344 iv, sizeof( iv ) ) );
2345 TEST_EQUAL( psa_cipher_set_iv( &operation,
2346 iv, sizeof( iv ) ),
2347 PSA_ERROR_BAD_STATE );
2348 PSA_ASSERT( psa_cipher_abort( &operation ) );
2349
2350 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002351 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002352 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2353 buffer, sizeof( buffer ),
2354 &length ) );
2355 TEST_EQUAL( psa_cipher_set_iv( &operation,
2356 iv, sizeof( iv ) ),
2357 PSA_ERROR_BAD_STATE );
2358 PSA_ASSERT( psa_cipher_abort( &operation ) );
2359
2360 /* Call update without calling setup beforehand. */
2361 TEST_EQUAL( psa_cipher_update( &operation,
2362 text, sizeof( text ),
2363 buffer, sizeof( buffer ),
2364 &length ),
2365 PSA_ERROR_BAD_STATE );
2366 PSA_ASSERT( psa_cipher_abort( &operation ) );
2367
2368 /* Call update without an IV where an IV is required. */
2369 TEST_EQUAL( psa_cipher_update( &operation,
2370 text, sizeof( text ),
2371 buffer, sizeof( buffer ),
2372 &length ),
2373 PSA_ERROR_BAD_STATE );
2374 PSA_ASSERT( psa_cipher_abort( &operation ) );
2375
2376 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002377 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002378 PSA_ASSERT( psa_cipher_set_iv( &operation,
2379 iv, sizeof( iv ) ) );
2380 PSA_ASSERT( psa_cipher_finish( &operation,
2381 buffer, sizeof( buffer ), &length ) );
2382 TEST_EQUAL( psa_cipher_update( &operation,
2383 text, sizeof( text ),
2384 buffer, sizeof( buffer ),
2385 &length ),
2386 PSA_ERROR_BAD_STATE );
2387 PSA_ASSERT( psa_cipher_abort( &operation ) );
2388
2389 /* Call finish without calling setup beforehand. */
2390 TEST_EQUAL( psa_cipher_finish( &operation,
2391 buffer, sizeof( buffer ), &length ),
2392 PSA_ERROR_BAD_STATE );
2393 PSA_ASSERT( psa_cipher_abort( &operation ) );
2394
2395 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002396 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002397 /* Not calling update means we are encrypting an empty buffer, which is OK
2398 * for cipher modes with padding. */
2399 TEST_EQUAL( psa_cipher_finish( &operation,
2400 buffer, sizeof( buffer ), &length ),
2401 PSA_ERROR_BAD_STATE );
2402 PSA_ASSERT( psa_cipher_abort( &operation ) );
2403
2404 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002405 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002406 PSA_ASSERT( psa_cipher_set_iv( &operation,
2407 iv, sizeof( iv ) ) );
2408 PSA_ASSERT( psa_cipher_finish( &operation,
2409 buffer, sizeof( buffer ), &length ) );
2410 TEST_EQUAL( psa_cipher_finish( &operation,
2411 buffer, sizeof( buffer ), &length ),
2412 PSA_ERROR_BAD_STATE );
2413 PSA_ASSERT( psa_cipher_abort( &operation ) );
2414
Ronald Cron5425a212020-08-04 14:58:35 +02002415 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002416
Jaeden Ameroab439972019-02-15 14:12:05 +00002417exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002418 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002419 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002420}
2421/* END_CASE */
2422
2423/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002424void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002425 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002426 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002427 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002428{
Ronald Cron5425a212020-08-04 14:58:35 +02002429 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002430 psa_status_t status;
2431 psa_key_type_t key_type = key_type_arg;
2432 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002433 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002434 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002435 size_t output_buffer_size = 0;
2436 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002437 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002438 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002439 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002440
Gilles Peskine8817f612018-12-18 00:18:46 +01002441 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002442
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002443 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2444 psa_set_key_algorithm( &attributes, alg );
2445 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002446
Ronald Cron5425a212020-08-04 14:58:35 +02002447 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2448 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002449
Ronald Cron5425a212020-08-04 14:58:35 +02002450 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002451
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002452 if( iv->len > 0 )
2453 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002454 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002455 }
2456
gabor-mezei-armceface22021-01-21 12:26:17 +01002457 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2458 TEST_ASSERT( output_buffer_size <=
2459 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002460 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002461
Gilles Peskine8817f612018-12-18 00:18:46 +01002462 PSA_ASSERT( psa_cipher_update( &operation,
2463 input->x, input->len,
2464 output, output_buffer_size,
2465 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002466 TEST_ASSERT( function_output_length <=
2467 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2468 TEST_ASSERT( function_output_length <=
2469 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002470 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002471
Gilles Peskine50e586b2018-06-08 14:28:46 +02002472 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002473 ( output_buffer_size == 0 ? NULL :
2474 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002475 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002476 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002477 TEST_ASSERT( function_output_length <=
2478 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2479 TEST_ASSERT( function_output_length <=
2480 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002481 total_output_length += function_output_length;
2482
Gilles Peskinefe11b722018-12-18 00:24:04 +01002483 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002484 if( expected_status == PSA_SUCCESS )
2485 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002486 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002487 ASSERT_COMPARE( expected_output->x, expected_output->len,
2488 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002489 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002490
Gilles Peskine50e586b2018-06-08 14:28:46 +02002491exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002492 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002493 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002494 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002495 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002496}
2497/* END_CASE */
2498
2499/* BEGIN_CASE */
2500void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002501 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002502 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002503 int first_part_size_arg,
2504 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002505 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002506{
Ronald Cron5425a212020-08-04 14:58:35 +02002507 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002508 psa_key_type_t key_type = key_type_arg;
2509 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002510 size_t first_part_size = first_part_size_arg;
2511 size_t output1_length = output1_length_arg;
2512 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002513 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002514 size_t output_buffer_size = 0;
2515 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002516 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002517 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002518 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002519
Gilles Peskine8817f612018-12-18 00:18:46 +01002520 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002521
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002522 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2523 psa_set_key_algorithm( &attributes, alg );
2524 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002525
Ronald Cron5425a212020-08-04 14:58:35 +02002526 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2527 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002528
Ronald Cron5425a212020-08-04 14:58:35 +02002529 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002530
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002531 if( iv->len > 0 )
2532 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002533 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002534 }
2535
gabor-mezei-armceface22021-01-21 12:26:17 +01002536 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2537 TEST_ASSERT( output_buffer_size <=
2538 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002539 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002540
Gilles Peskinee0866522019-02-19 19:44:00 +01002541 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002542 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2543 output, output_buffer_size,
2544 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002545 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002546 TEST_ASSERT( function_output_length <=
2547 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2548 TEST_ASSERT( function_output_length <=
2549 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002550 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002551
Gilles Peskine8817f612018-12-18 00:18:46 +01002552 PSA_ASSERT( psa_cipher_update( &operation,
2553 input->x + first_part_size,
2554 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002555 ( output_buffer_size == 0 ? NULL :
2556 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002557 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002558 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002559 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002560 TEST_ASSERT( function_output_length <=
2561 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2562 alg,
2563 input->len - first_part_size ) );
2564 TEST_ASSERT( function_output_length <=
2565 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002566 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002567
Gilles Peskine8817f612018-12-18 00:18:46 +01002568 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002569 ( output_buffer_size == 0 ? NULL :
2570 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002571 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002572 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002573 TEST_ASSERT( function_output_length <=
2574 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2575 TEST_ASSERT( function_output_length <=
2576 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002577 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002578 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002579
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002580 ASSERT_COMPARE( expected_output->x, expected_output->len,
2581 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002582
2583exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002584 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002585 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002586 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002587 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002588}
2589/* END_CASE */
2590
2591/* BEGIN_CASE */
2592void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002593 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002594 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002595 int first_part_size_arg,
2596 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002597 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002598{
Ronald Cron5425a212020-08-04 14:58:35 +02002599 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002600 psa_key_type_t key_type = key_type_arg;
2601 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002602 size_t first_part_size = first_part_size_arg;
2603 size_t output1_length = output1_length_arg;
2604 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002605 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002606 size_t output_buffer_size = 0;
2607 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002608 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002609 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002610 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002611
Gilles Peskine8817f612018-12-18 00:18:46 +01002612 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002613
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002614 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2615 psa_set_key_algorithm( &attributes, alg );
2616 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002617
Ronald Cron5425a212020-08-04 14:58:35 +02002618 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2619 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002620
Ronald Cron5425a212020-08-04 14:58:35 +02002621 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002622
Steven Cooreman177deba2020-09-07 17:14:14 +02002623 if( iv->len > 0 )
2624 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002625 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002626 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002627
gabor-mezei-armceface22021-01-21 12:26:17 +01002628 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2629 TEST_ASSERT( output_buffer_size <=
2630 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002631 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002632
Gilles Peskinee0866522019-02-19 19:44:00 +01002633 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002634 PSA_ASSERT( psa_cipher_update( &operation,
2635 input->x, first_part_size,
2636 output, output_buffer_size,
2637 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002638 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002639 TEST_ASSERT( function_output_length <=
2640 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2641 TEST_ASSERT( function_output_length <=
2642 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002643 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002644
Gilles Peskine8817f612018-12-18 00:18:46 +01002645 PSA_ASSERT( psa_cipher_update( &operation,
2646 input->x + first_part_size,
2647 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002648 ( output_buffer_size == 0 ? NULL :
2649 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002650 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002651 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002652 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002653 TEST_ASSERT( function_output_length <=
2654 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2655 alg,
2656 input->len - first_part_size ) );
2657 TEST_ASSERT( function_output_length <=
2658 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002659 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002660
Gilles Peskine8817f612018-12-18 00:18:46 +01002661 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002662 ( output_buffer_size == 0 ? NULL :
2663 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002664 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002665 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002666 TEST_ASSERT( function_output_length <=
2667 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2668 TEST_ASSERT( function_output_length <=
2669 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002670 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002671 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002672
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002673 ASSERT_COMPARE( expected_output->x, expected_output->len,
2674 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002675
2676exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002677 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002678 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002679 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002680 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002681}
2682/* END_CASE */
2683
Gilles Peskine50e586b2018-06-08 14:28:46 +02002684/* BEGIN_CASE */
2685void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002686 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002687 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002688 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002689{
Ronald Cron5425a212020-08-04 14:58:35 +02002690 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002691 psa_status_t status;
2692 psa_key_type_t key_type = key_type_arg;
2693 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002694 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002695 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002696 size_t output_buffer_size = 0;
2697 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002698 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002699 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002700 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002701
Gilles Peskine8817f612018-12-18 00:18:46 +01002702 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002703
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002704 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2705 psa_set_key_algorithm( &attributes, alg );
2706 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002707
Ronald Cron5425a212020-08-04 14:58:35 +02002708 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2709 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002710
Ronald Cron5425a212020-08-04 14:58:35 +02002711 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002712
Steven Cooreman177deba2020-09-07 17:14:14 +02002713 if( iv->len > 0 )
2714 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002715 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002716 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002717
gabor-mezei-armceface22021-01-21 12:26:17 +01002718 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2719 TEST_ASSERT( output_buffer_size <=
2720 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002721 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002722
Gilles Peskine8817f612018-12-18 00:18:46 +01002723 PSA_ASSERT( psa_cipher_update( &operation,
2724 input->x, input->len,
2725 output, output_buffer_size,
2726 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002727 TEST_ASSERT( function_output_length <=
2728 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2729 TEST_ASSERT( function_output_length <=
2730 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002731 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002732
Gilles Peskine50e586b2018-06-08 14:28:46 +02002733 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002734 ( output_buffer_size == 0 ? NULL :
2735 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002736 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002737 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002738 TEST_ASSERT( function_output_length <=
2739 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2740 TEST_ASSERT( function_output_length <=
2741 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002742 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002743 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002744
2745 if( expected_status == PSA_SUCCESS )
2746 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002747 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002748 ASSERT_COMPARE( expected_output->x, expected_output->len,
2749 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002750 }
2751
Gilles Peskine50e586b2018-06-08 14:28:46 +02002752exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002753 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002754 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002755 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002756 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002757}
2758/* END_CASE */
2759
Gilles Peskine50e586b2018-06-08 14:28:46 +02002760/* BEGIN_CASE */
2761void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002762 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002763 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002764{
Ronald Cron5425a212020-08-04 14:58:35 +02002765 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002766 psa_key_type_t key_type = key_type_arg;
2767 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002768 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002769 size_t iv_size = 16;
2770 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002771 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002772 size_t output1_size = 0;
2773 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002774 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002775 size_t output2_size = 0;
2776 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002777 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002778 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2779 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002780 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002781
Gilles Peskine8817f612018-12-18 00:18:46 +01002782 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002783
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002784 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2785 psa_set_key_algorithm( &attributes, alg );
2786 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002787
Ronald Cron5425a212020-08-04 14:58:35 +02002788 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2789 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002790
Ronald Cron5425a212020-08-04 14:58:35 +02002791 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2792 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002793
Steven Cooreman177deba2020-09-07 17:14:14 +02002794 if( alg != PSA_ALG_ECB_NO_PADDING )
2795 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002796 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2797 iv, iv_size,
2798 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002799 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002800 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2801 TEST_ASSERT( output1_size <=
2802 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002803 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002804
Gilles Peskine8817f612018-12-18 00:18:46 +01002805 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2806 output1, output1_size,
2807 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002808 TEST_ASSERT( output1_length <=
2809 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2810 TEST_ASSERT( output1_length <=
2811 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2812
Gilles Peskine8817f612018-12-18 00:18:46 +01002813 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002814 output1 + output1_length,
2815 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002816 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002817 TEST_ASSERT( function_output_length <=
2818 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2819 TEST_ASSERT( function_output_length <=
2820 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002821
Gilles Peskine048b7f02018-06-08 14:20:49 +02002822 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002823
Gilles Peskine8817f612018-12-18 00:18:46 +01002824 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002825
2826 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002827 TEST_ASSERT( output2_size <=
2828 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2829 TEST_ASSERT( output2_size <=
2830 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002831 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002832
Steven Cooreman177deba2020-09-07 17:14:14 +02002833 if( iv_length > 0 )
2834 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002835 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2836 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002837 }
2838
Gilles Peskine8817f612018-12-18 00:18:46 +01002839 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2840 output2, output2_size,
2841 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002842 TEST_ASSERT( output2_length <=
2843 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
2844 TEST_ASSERT( output2_length <=
2845 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
2846
Gilles Peskine048b7f02018-06-08 14:20:49 +02002847 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002848 PSA_ASSERT( psa_cipher_finish( &operation2,
2849 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002850 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002851 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002852 TEST_ASSERT( function_output_length <=
2853 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2854 TEST_ASSERT( function_output_length <=
2855 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03002856
Gilles Peskine048b7f02018-06-08 14:20:49 +02002857 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002858
Gilles Peskine8817f612018-12-18 00:18:46 +01002859 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002860
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002861 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002862
2863exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002864 psa_cipher_abort( &operation1 );
2865 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002866 mbedtls_free( output1 );
2867 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002868 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002869 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03002870}
2871/* END_CASE */
2872
2873/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002874void cipher_verify_output_multipart( int alg_arg,
2875 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002876 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002877 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002878 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03002879{
Ronald Cron5425a212020-08-04 14:58:35 +02002880 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002881 psa_key_type_t key_type = key_type_arg;
2882 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002883 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002884 unsigned char iv[16] = {0};
2885 size_t iv_size = 16;
2886 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002887 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002888 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002889 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002890 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002891 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002892 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002893 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002894 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2895 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002896 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002897
Gilles Peskine8817f612018-12-18 00:18:46 +01002898 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002899
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002900 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2901 psa_set_key_algorithm( &attributes, alg );
2902 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002903
Ronald Cron5425a212020-08-04 14:58:35 +02002904 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2905 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03002906
Ronald Cron5425a212020-08-04 14:58:35 +02002907 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2908 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002909
Steven Cooreman177deba2020-09-07 17:14:14 +02002910 if( alg != PSA_ALG_ECB_NO_PADDING )
2911 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002912 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2913 iv, iv_size,
2914 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002915 }
2916
gabor-mezei-armceface22021-01-21 12:26:17 +01002917 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2918 TEST_ASSERT( output1_buffer_size <=
2919 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002920 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002921
Gilles Peskinee0866522019-02-19 19:44:00 +01002922 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002923
Gilles Peskine8817f612018-12-18 00:18:46 +01002924 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2925 output1, output1_buffer_size,
2926 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002927 TEST_ASSERT( function_output_length <=
2928 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2929 TEST_ASSERT( function_output_length <=
2930 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002931 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002932
Gilles Peskine8817f612018-12-18 00:18:46 +01002933 PSA_ASSERT( psa_cipher_update( &operation1,
2934 input->x + first_part_size,
2935 input->len - first_part_size,
2936 output1, output1_buffer_size,
2937 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002938 TEST_ASSERT( function_output_length <=
2939 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2940 alg,
2941 input->len - first_part_size ) );
2942 TEST_ASSERT( function_output_length <=
2943 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002944 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002945
Gilles Peskine8817f612018-12-18 00:18:46 +01002946 PSA_ASSERT( psa_cipher_finish( &operation1,
2947 output1 + output1_length,
2948 output1_buffer_size - output1_length,
2949 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002950 TEST_ASSERT( function_output_length <=
2951 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2952 TEST_ASSERT( function_output_length <=
2953 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002954 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002955
Gilles Peskine8817f612018-12-18 00:18:46 +01002956 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002957
Gilles Peskine048b7f02018-06-08 14:20:49 +02002958 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002959 TEST_ASSERT( output2_buffer_size <=
2960 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2961 TEST_ASSERT( output2_buffer_size <=
2962 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002963 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002964
Steven Cooreman177deba2020-09-07 17:14:14 +02002965 if( iv_length > 0 )
2966 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002967 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2968 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002969 }
Moran Pekerded84402018-06-06 16:36:50 +03002970
Gilles Peskine8817f612018-12-18 00:18:46 +01002971 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2972 output2, output2_buffer_size,
2973 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002974 TEST_ASSERT( function_output_length <=
2975 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2976 TEST_ASSERT( function_output_length <=
2977 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002978 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002979
Gilles Peskine8817f612018-12-18 00:18:46 +01002980 PSA_ASSERT( psa_cipher_update( &operation2,
2981 output1 + first_part_size,
2982 output1_length - first_part_size,
2983 output2, output2_buffer_size,
2984 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002985 TEST_ASSERT( function_output_length <=
2986 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2987 alg,
2988 output1_length - first_part_size ) );
2989 TEST_ASSERT( function_output_length <=
2990 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002991 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002992
Gilles Peskine8817f612018-12-18 00:18:46 +01002993 PSA_ASSERT( psa_cipher_finish( &operation2,
2994 output2 + output2_length,
2995 output2_buffer_size - output2_length,
2996 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002997 TEST_ASSERT( function_output_length <=
2998 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2999 TEST_ASSERT( function_output_length <=
3000 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003001 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003002
Gilles Peskine8817f612018-12-18 00:18:46 +01003003 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003004
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003005 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003006
3007exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003008 psa_cipher_abort( &operation1 );
3009 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003010 mbedtls_free( output1 );
3011 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003012 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003013 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003014}
3015/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003016
Gilles Peskine20035e32018-02-03 22:44:14 +01003017/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003018void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003019 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003020 data_t *nonce,
3021 data_t *additional_data,
3022 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003023 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003024{
Ronald Cron5425a212020-08-04 14:58:35 +02003025 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003026 psa_key_type_t key_type = key_type_arg;
3027 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003028 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003029 unsigned char *output_data = NULL;
3030 size_t output_size = 0;
3031 size_t output_length = 0;
3032 unsigned char *output_data2 = NULL;
3033 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003034 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003035 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003036 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003037
Gilles Peskine8817f612018-12-18 00:18:46 +01003038 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003039
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003040 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3041 psa_set_key_algorithm( &attributes, alg );
3042 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003043
Gilles Peskine049c7532019-05-15 20:22:09 +02003044 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003045 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003046 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3047 key_bits = psa_get_key_bits( &attributes );
3048
3049 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3050 alg );
3051 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3052 * should be exact. */
3053 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3054 expected_result != PSA_ERROR_NOT_SUPPORTED )
3055 {
3056 TEST_EQUAL( output_size,
3057 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3058 TEST_ASSERT( output_size <=
3059 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3060 }
3061 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003062
Steven Cooremanf49478b2021-02-15 15:19:25 +01003063 status = psa_aead_encrypt( key, alg,
3064 nonce->x, nonce->len,
3065 additional_data->x,
3066 additional_data->len,
3067 input_data->x, input_data->len,
3068 output_data, output_size,
3069 &output_length );
3070
3071 /* If the operation is not supported, just skip and not fail in case the
3072 * encryption involves a common limitation of cryptography hardwares and
3073 * an alternative implementation. */
3074 if( status == PSA_ERROR_NOT_SUPPORTED )
3075 {
3076 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3077 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3078 }
3079
3080 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003081
3082 if( PSA_SUCCESS == expected_result )
3083 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003084 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003085
Gilles Peskine003a4a92019-05-14 16:09:40 +02003086 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3087 * should be exact. */
3088 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003089 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003090
gabor-mezei-armceface22021-01-21 12:26:17 +01003091 TEST_ASSERT( input_data->len <=
3092 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3093
Ronald Cron5425a212020-08-04 14:58:35 +02003094 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003095 nonce->x, nonce->len,
3096 additional_data->x,
3097 additional_data->len,
3098 output_data, output_length,
3099 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003100 &output_length2 ),
3101 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003102
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003103 ASSERT_COMPARE( input_data->x, input_data->len,
3104 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003105 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003106
Gilles Peskinea1cac842018-06-11 19:33:02 +02003107exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003108 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003109 mbedtls_free( output_data );
3110 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003111 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003112}
3113/* END_CASE */
3114
3115/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003116void aead_encrypt( int key_type_arg, data_t *key_data,
3117 int alg_arg,
3118 data_t *nonce,
3119 data_t *additional_data,
3120 data_t *input_data,
3121 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003122{
Ronald Cron5425a212020-08-04 14:58:35 +02003123 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003124 psa_key_type_t key_type = key_type_arg;
3125 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003126 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003127 unsigned char *output_data = NULL;
3128 size_t output_size = 0;
3129 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003130 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003131 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003132
Gilles Peskine8817f612018-12-18 00:18:46 +01003133 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003134
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003135 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3136 psa_set_key_algorithm( &attributes, alg );
3137 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003138
Gilles Peskine049c7532019-05-15 20:22:09 +02003139 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003140 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003141 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3142 key_bits = psa_get_key_bits( &attributes );
3143
3144 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3145 alg );
3146 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3147 * should be exact. */
3148 TEST_EQUAL( output_size,
3149 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3150 TEST_ASSERT( output_size <=
3151 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3152 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003153
Steven Cooremand588ea12021-01-11 19:36:04 +01003154 status = psa_aead_encrypt( key, alg,
3155 nonce->x, nonce->len,
3156 additional_data->x, additional_data->len,
3157 input_data->x, input_data->len,
3158 output_data, output_size,
3159 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003160
Ronald Cron28a45ed2021-02-09 20:35:42 +01003161 /* If the operation is not supported, just skip and not fail in case the
3162 * encryption involves a common limitation of cryptography hardwares and
3163 * an alternative implementation. */
3164 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003165 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003166 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3167 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003168 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003169
3170 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003171 ASSERT_COMPARE( expected_result->x, expected_result->len,
3172 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003173
Gilles Peskinea1cac842018-06-11 19:33:02 +02003174exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003175 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003176 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003177 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003178}
3179/* END_CASE */
3180
3181/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003182void aead_decrypt( int key_type_arg, data_t *key_data,
3183 int alg_arg,
3184 data_t *nonce,
3185 data_t *additional_data,
3186 data_t *input_data,
3187 data_t *expected_data,
3188 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003189{
Ronald Cron5425a212020-08-04 14:58:35 +02003190 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003191 psa_key_type_t key_type = key_type_arg;
3192 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003193 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003194 unsigned char *output_data = NULL;
3195 size_t output_size = 0;
3196 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003197 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003198 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003199 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003200
Gilles Peskine8817f612018-12-18 00:18:46 +01003201 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003202
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003203 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3204 psa_set_key_algorithm( &attributes, alg );
3205 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003206
Gilles Peskine049c7532019-05-15 20:22:09 +02003207 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003208 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003209 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3210 key_bits = psa_get_key_bits( &attributes );
3211
3212 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3213 alg );
3214 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3215 expected_result != PSA_ERROR_NOT_SUPPORTED )
3216 {
3217 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3218 * should be exact. */
3219 TEST_EQUAL( output_size,
3220 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3221 TEST_ASSERT( output_size <=
3222 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3223 }
3224 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003225
Steven Cooremand588ea12021-01-11 19:36:04 +01003226 status = psa_aead_decrypt( key, alg,
3227 nonce->x, nonce->len,
3228 additional_data->x,
3229 additional_data->len,
3230 input_data->x, input_data->len,
3231 output_data, output_size,
3232 &output_length );
3233
Ronald Cron28a45ed2021-02-09 20:35:42 +01003234 /* If the operation is not supported, just skip and not fail in case the
3235 * decryption involves a common limitation of cryptography hardwares and
3236 * an alternative implementation. */
3237 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003238 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003239 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3240 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003241 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003242
3243 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003244
Gilles Peskine2d277862018-06-18 15:41:12 +02003245 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003246 ASSERT_COMPARE( expected_data->x, expected_data->len,
3247 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003248
Gilles Peskinea1cac842018-06-11 19:33:02 +02003249exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003250 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003251 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003252 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003253}
3254/* END_CASE */
3255
3256/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003257void signature_size( int type_arg,
3258 int bits,
3259 int alg_arg,
3260 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003261{
3262 psa_key_type_t type = type_arg;
3263 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003264 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003265
Gilles Peskinefe11b722018-12-18 00:24:04 +01003266 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003267#if defined(MBEDTLS_TEST_DEPRECATED)
3268 TEST_EQUAL( actual_size,
3269 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3270#endif /* MBEDTLS_TEST_DEPRECATED */
3271
Gilles Peskinee59236f2018-01-27 23:32:46 +01003272exit:
3273 ;
3274}
3275/* END_CASE */
3276
3277/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003278void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3279 int alg_arg, data_t *input_data,
3280 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003281{
Ronald Cron5425a212020-08-04 14:58:35 +02003282 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003283 psa_key_type_t key_type = key_type_arg;
3284 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003285 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003286 unsigned char *signature = NULL;
3287 size_t signature_size;
3288 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003289 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003290
Gilles Peskine8817f612018-12-18 00:18:46 +01003291 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003292
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003293 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003294 psa_set_key_algorithm( &attributes, alg );
3295 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003296
Gilles Peskine049c7532019-05-15 20:22:09 +02003297 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003298 &key ) );
3299 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003300 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003301
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003302 /* Allocate a buffer which has the size advertized by the
3303 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003304 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003305 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003306 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003307 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003308 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003309
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003310 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003311 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003312 input_data->x, input_data->len,
3313 signature, signature_size,
3314 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003315 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003316 ASSERT_COMPARE( output_data->x, output_data->len,
3317 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003318
Gilles Peskine0627f982019-11-26 19:12:16 +01003319#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003320 memset( signature, 0, signature_size );
3321 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003322 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003323 input_data->x, input_data->len,
3324 signature, signature_size,
3325 &signature_length ) );
3326 ASSERT_COMPARE( output_data->x, output_data->len,
3327 signature, signature_length );
3328#endif /* MBEDTLS_TEST_DEPRECATED */
3329
Gilles Peskine20035e32018-02-03 22:44:14 +01003330exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003331 /*
3332 * Key attributes may have been returned by psa_get_key_attributes()
3333 * thus reset them as required.
3334 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003335 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003336
Ronald Cron5425a212020-08-04 14:58:35 +02003337 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003338 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003339 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003340}
3341/* END_CASE */
3342
3343/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003344void sign_hash_fail( int key_type_arg, data_t *key_data,
3345 int alg_arg, data_t *input_data,
3346 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003347{
Ronald Cron5425a212020-08-04 14:58:35 +02003348 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003349 psa_key_type_t key_type = key_type_arg;
3350 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003351 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003352 psa_status_t actual_status;
3353 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003354 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003355 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003356 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003357
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003358 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003359
Gilles Peskine8817f612018-12-18 00:18:46 +01003360 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003361
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003362 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003363 psa_set_key_algorithm( &attributes, alg );
3364 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003365
Gilles Peskine049c7532019-05-15 20:22:09 +02003366 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003367 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003368
Ronald Cron5425a212020-08-04 14:58:35 +02003369 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003370 input_data->x, input_data->len,
3371 signature, signature_size,
3372 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003373 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003374 /* The value of *signature_length is unspecified on error, but
3375 * whatever it is, it should be less than signature_size, so that
3376 * if the caller tries to read *signature_length bytes without
3377 * checking the error code then they don't overflow a buffer. */
3378 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003379
Gilles Peskine895242b2019-11-29 12:15:40 +01003380#if defined(MBEDTLS_TEST_DEPRECATED)
3381 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003382 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003383 input_data->x, input_data->len,
3384 signature, signature_size,
3385 &signature_length ),
3386 expected_status );
3387 TEST_ASSERT( signature_length <= signature_size );
3388#endif /* MBEDTLS_TEST_DEPRECATED */
3389
Gilles Peskine20035e32018-02-03 22:44:14 +01003390exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003391 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003392 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003393 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003394 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003395}
3396/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003397
3398/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003399void sign_verify_hash( int key_type_arg, data_t *key_data,
3400 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003401{
Ronald Cron5425a212020-08-04 14:58:35 +02003402 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003403 psa_key_type_t key_type = key_type_arg;
3404 psa_algorithm_t alg = alg_arg;
3405 size_t key_bits;
3406 unsigned char *signature = NULL;
3407 size_t signature_size;
3408 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003409 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003410
Gilles Peskine8817f612018-12-18 00:18:46 +01003411 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003412
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003413 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003414 psa_set_key_algorithm( &attributes, alg );
3415 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003416
Gilles Peskine049c7532019-05-15 20:22:09 +02003417 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003418 &key ) );
3419 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003420 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003421
3422 /* Allocate a buffer which has the size advertized by the
3423 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003424 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003425 key_bits, alg );
3426 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003427 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003428 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003429
3430 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003431 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003432 input_data->x, input_data->len,
3433 signature, signature_size,
3434 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003435 /* Check that the signature length looks sensible. */
3436 TEST_ASSERT( signature_length <= signature_size );
3437 TEST_ASSERT( signature_length > 0 );
3438
3439 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003440 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003441 input_data->x, input_data->len,
3442 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003443
3444 if( input_data->len != 0 )
3445 {
3446 /* Flip a bit in the input and verify that the signature is now
3447 * detected as invalid. Flip a bit at the beginning, not at the end,
3448 * because ECDSA may ignore the last few bits of the input. */
3449 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003450 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003451 input_data->x, input_data->len,
3452 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003453 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003454 }
3455
3456exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003457 /*
3458 * Key attributes may have been returned by psa_get_key_attributes()
3459 * thus reset them as required.
3460 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003461 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003462
Ronald Cron5425a212020-08-04 14:58:35 +02003463 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003464 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003465 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003466}
3467/* END_CASE */
3468
3469/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003470void verify_hash( int key_type_arg, data_t *key_data,
3471 int alg_arg, data_t *hash_data,
3472 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003473{
Ronald Cron5425a212020-08-04 14:58:35 +02003474 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003475 psa_key_type_t key_type = key_type_arg;
3476 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003477 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003478
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003479 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003480
Gilles Peskine8817f612018-12-18 00:18:46 +01003481 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003482
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003483 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003484 psa_set_key_algorithm( &attributes, alg );
3485 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003486
Gilles Peskine049c7532019-05-15 20:22:09 +02003487 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003488 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003489
Ronald Cron5425a212020-08-04 14:58:35 +02003490 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003491 hash_data->x, hash_data->len,
3492 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003493
3494#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003495 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003496 hash_data->x, hash_data->len,
3497 signature_data->x,
3498 signature_data->len ) );
3499
3500#endif /* MBEDTLS_TEST_DEPRECATED */
3501
itayzafrir5c753392018-05-08 11:18:38 +03003502exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003503 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003504 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003505 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003506}
3507/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003508
3509/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003510void verify_hash_fail( int key_type_arg, data_t *key_data,
3511 int alg_arg, data_t *hash_data,
3512 data_t *signature_data,
3513 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003514{
Ronald Cron5425a212020-08-04 14:58:35 +02003515 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003516 psa_key_type_t key_type = key_type_arg;
3517 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003518 psa_status_t actual_status;
3519 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003520 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003521
Gilles Peskine8817f612018-12-18 00:18:46 +01003522 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003523
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003524 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003525 psa_set_key_algorithm( &attributes, alg );
3526 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003527
Gilles Peskine049c7532019-05-15 20:22:09 +02003528 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003529 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003530
Ronald Cron5425a212020-08-04 14:58:35 +02003531 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003532 hash_data->x, hash_data->len,
3533 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003534 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003535
Gilles Peskine895242b2019-11-29 12:15:40 +01003536#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003537 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003538 hash_data->x, hash_data->len,
3539 signature_data->x, signature_data->len ),
3540 expected_status );
3541#endif /* MBEDTLS_TEST_DEPRECATED */
3542
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003543exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003544 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003545 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003546 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003547}
3548/* END_CASE */
3549
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003550/* BEGIN_CASE */
gabor-mezei-armabd72582021-04-15 18:19:50 +02003551void sign_message_deterministic( int key_type_arg,
3552 data_t *key_data,
3553 int alg_arg,
3554 data_t *input_data,
3555 data_t *output_data )
3556{
3557 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3558 psa_key_type_t key_type = key_type_arg;
3559 psa_algorithm_t alg = alg_arg;
3560 size_t key_bits;
3561 unsigned char *signature = NULL;
3562 size_t signature_size;
3563 size_t signature_length = 0xdeadbeef;
3564 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3565
3566 PSA_ASSERT( psa_crypto_init( ) );
3567
3568 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3569 psa_set_key_algorithm( &attributes, alg );
3570 psa_set_key_type( &attributes, key_type );
3571
3572 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3573 &key ) );
3574 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3575 key_bits = psa_get_key_bits( &attributes );
3576
3577 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3578 TEST_ASSERT( signature_size != 0 );
3579 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3580 ASSERT_ALLOC( signature, signature_size );
3581
3582 PSA_ASSERT( psa_sign_message( key, alg,
3583 input_data->x, input_data->len,
3584 signature, signature_size,
3585 &signature_length ) );
3586
3587 ASSERT_COMPARE( output_data->x, output_data->len,
3588 signature, signature_length );
3589
3590exit:
3591 psa_reset_key_attributes( &attributes );
3592
3593 psa_destroy_key( key );
3594 mbedtls_free( signature );
3595 PSA_DONE( );
3596
3597}
3598/* END_CASE */
3599
3600/* BEGIN_CASE */
3601void sign_message_fail( int key_type_arg,
3602 data_t *key_data,
3603 int alg_arg,
3604 data_t *input_data,
3605 int signature_size_arg,
3606 int expected_status_arg )
3607{
3608 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3609 psa_key_type_t key_type = key_type_arg;
3610 psa_algorithm_t alg = alg_arg;
3611 size_t signature_size = signature_size_arg;
3612 psa_status_t actual_status;
3613 psa_status_t expected_status = expected_status_arg;
3614 unsigned char *signature = NULL;
3615 size_t signature_length = 0xdeadbeef;
3616 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3617
3618 ASSERT_ALLOC( signature, signature_size );
3619
3620 PSA_ASSERT( psa_crypto_init( ) );
3621
3622 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3623 psa_set_key_algorithm( &attributes, alg );
3624 psa_set_key_type( &attributes, key_type );
3625
3626 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3627 &key ) );
3628
3629 actual_status = psa_sign_message( key, alg,
3630 input_data->x, input_data->len,
3631 signature, signature_size,
3632 &signature_length );
3633 TEST_EQUAL( actual_status, expected_status );
3634 /* The value of *signature_length is unspecified on error, but
3635 * whatever it is, it should be less than signature_size, so that
3636 * if the caller tries to read *signature_length bytes without
3637 * checking the error code then they don't overflow a buffer. */
3638 TEST_ASSERT( signature_length <= signature_size );
3639
3640exit:
3641 psa_reset_key_attributes( &attributes );
3642 psa_destroy_key( key );
3643 mbedtls_free( signature );
3644 PSA_DONE( );
3645}
3646/* END_CASE */
3647
3648/* BEGIN_CASE */
3649void sign_verify_message( int key_type_arg,
3650 data_t *key_data,
3651 int alg_arg,
3652 data_t *input_data )
3653{
3654 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3655 psa_key_type_t key_type = key_type_arg;
3656 psa_algorithm_t alg = alg_arg;
3657 size_t key_bits;
3658 unsigned char *signature = NULL;
3659 size_t signature_size;
3660 size_t signature_length = 0xdeadbeef;
3661 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3662
3663 PSA_ASSERT( psa_crypto_init( ) );
3664
3665 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3666 PSA_KEY_USAGE_VERIFY_MESSAGE );
3667 psa_set_key_algorithm( &attributes, alg );
3668 psa_set_key_type( &attributes, key_type );
3669
3670 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3671 &key ) );
3672 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3673 key_bits = psa_get_key_bits( &attributes );
3674
3675 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3676 TEST_ASSERT( signature_size != 0 );
3677 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3678 ASSERT_ALLOC( signature, signature_size );
3679
3680 PSA_ASSERT( psa_sign_message( key, alg,
3681 input_data->x, input_data->len,
3682 signature, signature_size,
3683 &signature_length ) );
3684 TEST_ASSERT( signature_length <= signature_size );
3685 TEST_ASSERT( signature_length > 0 );
3686
3687 PSA_ASSERT( psa_verify_message( key, alg,
3688 input_data->x, input_data->len,
3689 signature, signature_length ) );
3690
3691 if( input_data->len != 0 )
3692 {
3693 /* Flip a bit in the input and verify that the signature is now
3694 * detected as invalid. Flip a bit at the beginning, not at the end,
3695 * because ECDSA may ignore the last few bits of the input. */
3696 input_data->x[0] ^= 1;
3697 TEST_EQUAL( psa_verify_message( key, alg,
3698 input_data->x, input_data->len,
3699 signature, signature_length ),
3700 PSA_ERROR_INVALID_SIGNATURE );
3701 }
3702
3703exit:
3704 psa_reset_key_attributes( &attributes );
3705
3706 psa_destroy_key( key );
3707 mbedtls_free( signature );
3708 PSA_DONE( );
3709}
3710/* END_CASE */
3711
3712/* BEGIN_CASE */
3713void verify_message( int key_type_arg,
3714 data_t *key_data,
3715 int alg_arg,
3716 data_t *input_data,
3717 data_t *signature_data )
3718{
3719 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3720 psa_key_type_t key_type = key_type_arg;
3721 psa_algorithm_t alg = alg_arg;
3722 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3723
3724 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3725
3726 PSA_ASSERT( psa_crypto_init( ) );
3727
3728 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3729 psa_set_key_algorithm( &attributes, alg );
3730 psa_set_key_type( &attributes, key_type );
3731
3732 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3733 &key ) );
3734
3735 PSA_ASSERT( psa_verify_message( key, alg,
3736 input_data->x, input_data->len,
3737 signature_data->x, signature_data->len ) );
3738
3739exit:
3740 psa_reset_key_attributes( &attributes );
3741 psa_destroy_key( key );
3742 PSA_DONE( );
3743}
3744/* END_CASE */
3745
3746/* BEGIN_CASE */
3747void verify_message_fail( int key_type_arg,
3748 data_t *key_data,
3749 int alg_arg,
3750 data_t *hash_data,
3751 data_t *signature_data,
3752 int expected_status_arg )
3753{
3754 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3755 psa_key_type_t key_type = key_type_arg;
3756 psa_algorithm_t alg = alg_arg;
3757 psa_status_t actual_status;
3758 psa_status_t expected_status = expected_status_arg;
3759 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3760
3761 PSA_ASSERT( psa_crypto_init( ) );
3762
3763 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3764 psa_set_key_algorithm( &attributes, alg );
3765 psa_set_key_type( &attributes, key_type );
3766
3767 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3768 &key ) );
3769
3770 actual_status = psa_verify_message( key, alg,
3771 hash_data->x, hash_data->len,
3772 signature_data->x,
3773 signature_data->len );
3774 TEST_EQUAL( actual_status, expected_status );
3775
3776exit:
3777 psa_reset_key_attributes( &attributes );
3778 psa_destroy_key( key );
3779 PSA_DONE( );
3780}
3781/* END_CASE */
3782
3783/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003784void asymmetric_encrypt( int key_type_arg,
3785 data_t *key_data,
3786 int alg_arg,
3787 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003788 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003789 int expected_output_length_arg,
3790 int expected_status_arg )
3791{
Ronald Cron5425a212020-08-04 14:58:35 +02003792 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003793 psa_key_type_t key_type = key_type_arg;
3794 psa_algorithm_t alg = alg_arg;
3795 size_t expected_output_length = expected_output_length_arg;
3796 size_t key_bits;
3797 unsigned char *output = NULL;
3798 size_t output_size;
3799 size_t output_length = ~0;
3800 psa_status_t actual_status;
3801 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003802 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003803
Gilles Peskine8817f612018-12-18 00:18:46 +01003804 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003805
Gilles Peskine656896e2018-06-29 19:12:28 +02003806 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003807 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3808 psa_set_key_algorithm( &attributes, alg );
3809 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003810 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003811 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003812
3813 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003814 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003815 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003816
Gilles Peskine656896e2018-06-29 19:12:28 +02003817 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003818 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003819 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003820
3821 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003822 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003823 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003824 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003825 output, output_size,
3826 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003827 TEST_EQUAL( actual_status, expected_status );
3828 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003829
Gilles Peskine68428122018-06-30 18:42:41 +02003830 /* If the label is empty, the test framework puts a non-null pointer
3831 * in label->x. Test that a null pointer works as well. */
3832 if( label->len == 0 )
3833 {
3834 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003835 if( output_size != 0 )
3836 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003837 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003838 input_data->x, input_data->len,
3839 NULL, label->len,
3840 output, output_size,
3841 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003842 TEST_EQUAL( actual_status, expected_status );
3843 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003844 }
3845
Gilles Peskine656896e2018-06-29 19:12:28 +02003846exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003847 /*
3848 * Key attributes may have been returned by psa_get_key_attributes()
3849 * thus reset them as required.
3850 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003851 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003852
Ronald Cron5425a212020-08-04 14:58:35 +02003853 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02003854 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003855 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003856}
3857/* END_CASE */
3858
3859/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003860void asymmetric_encrypt_decrypt( int key_type_arg,
3861 data_t *key_data,
3862 int alg_arg,
3863 data_t *input_data,
3864 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003865{
Ronald Cron5425a212020-08-04 14:58:35 +02003866 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003867 psa_key_type_t key_type = key_type_arg;
3868 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003869 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003870 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003871 size_t output_size;
3872 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003873 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003874 size_t output2_size;
3875 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003876 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003877
Gilles Peskine8817f612018-12-18 00:18:46 +01003878 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003879
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003880 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3881 psa_set_key_algorithm( &attributes, alg );
3882 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003883
Gilles Peskine049c7532019-05-15 20:22:09 +02003884 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003885 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003886
3887 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02003888 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003889 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003890
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003891 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003892 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003893 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01003894
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003895 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01003896 TEST_ASSERT( output2_size <=
3897 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
3898 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003899 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003900
Gilles Peskineeebd7382018-06-08 18:11:54 +02003901 /* We test encryption by checking that encrypt-then-decrypt gives back
3902 * the original plaintext because of the non-optional random
3903 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02003904 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003905 input_data->x, input_data->len,
3906 label->x, label->len,
3907 output, output_size,
3908 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003909 /* We don't know what ciphertext length to expect, but check that
3910 * it looks sensible. */
3911 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003912
Ronald Cron5425a212020-08-04 14:58:35 +02003913 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003914 output, output_length,
3915 label->x, label->len,
3916 output2, output2_size,
3917 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003918 ASSERT_COMPARE( input_data->x, input_data->len,
3919 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003920
3921exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003922 /*
3923 * Key attributes may have been returned by psa_get_key_attributes()
3924 * thus reset them as required.
3925 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003926 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003927
Ronald Cron5425a212020-08-04 14:58:35 +02003928 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003929 mbedtls_free( output );
3930 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003931 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003932}
3933/* END_CASE */
3934
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003935/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003936void asymmetric_decrypt( int key_type_arg,
3937 data_t *key_data,
3938 int alg_arg,
3939 data_t *input_data,
3940 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003941 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003942{
Ronald Cron5425a212020-08-04 14:58:35 +02003943 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003944 psa_key_type_t key_type = key_type_arg;
3945 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01003946 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003947 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003948 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003949 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003950 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003951
Gilles Peskine8817f612018-12-18 00:18:46 +01003952 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003953
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003954 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3955 psa_set_key_algorithm( &attributes, alg );
3956 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003957
Gilles Peskine049c7532019-05-15 20:22:09 +02003958 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003959 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003960
gabor-mezei-armceface22021-01-21 12:26:17 +01003961 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3962 key_bits = psa_get_key_bits( &attributes );
3963
3964 /* Determine the maximum ciphertext length */
3965 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
3966 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
3967 ASSERT_ALLOC( output, output_size );
3968
Ronald Cron5425a212020-08-04 14:58:35 +02003969 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003970 input_data->x, input_data->len,
3971 label->x, label->len,
3972 output,
3973 output_size,
3974 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003975 ASSERT_COMPARE( expected_data->x, expected_data->len,
3976 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003977
Gilles Peskine68428122018-06-30 18:42:41 +02003978 /* If the label is empty, the test framework puts a non-null pointer
3979 * in label->x. Test that a null pointer works as well. */
3980 if( label->len == 0 )
3981 {
3982 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003983 if( output_size != 0 )
3984 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003985 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003986 input_data->x, input_data->len,
3987 NULL, label->len,
3988 output,
3989 output_size,
3990 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003991 ASSERT_COMPARE( expected_data->x, expected_data->len,
3992 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003993 }
3994
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003995exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003996 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003997 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003998 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003999 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004000}
4001/* END_CASE */
4002
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004003/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004004void asymmetric_decrypt_fail( int key_type_arg,
4005 data_t *key_data,
4006 int alg_arg,
4007 data_t *input_data,
4008 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004009 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004010 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004011{
Ronald Cron5425a212020-08-04 14:58:35 +02004012 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004013 psa_key_type_t key_type = key_type_arg;
4014 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004015 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004016 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004017 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004018 psa_status_t actual_status;
4019 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004020 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004021
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004022 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004023
Gilles Peskine8817f612018-12-18 00:18:46 +01004024 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004025
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004026 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4027 psa_set_key_algorithm( &attributes, alg );
4028 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004029
Gilles Peskine049c7532019-05-15 20:22:09 +02004030 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004031 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004032
Ronald Cron5425a212020-08-04 14:58:35 +02004033 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004034 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004035 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004036 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004037 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004038 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004039 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004040
Gilles Peskine68428122018-06-30 18:42:41 +02004041 /* If the label is empty, the test framework puts a non-null pointer
4042 * in label->x. Test that a null pointer works as well. */
4043 if( label->len == 0 )
4044 {
4045 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004046 if( output_size != 0 )
4047 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004048 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004049 input_data->x, input_data->len,
4050 NULL, label->len,
4051 output, output_size,
4052 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004053 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004054 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004055 }
4056
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004057exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004058 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004059 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004060 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004061 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004062}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004063/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004064
4065/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004066void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004067{
4068 /* Test each valid way of initializing the object, except for `= {0}`, as
4069 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4070 * though it's OK by the C standard. We could test for this, but we'd need
4071 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004072 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004073 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4074 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4075 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004076
4077 memset( &zero, 0, sizeof( zero ) );
4078
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004079 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004080 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004081 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004082 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004083 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004084 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004085 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004086
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004087 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004088 PSA_ASSERT( psa_key_derivation_abort(&func) );
4089 PSA_ASSERT( psa_key_derivation_abort(&init) );
4090 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004091}
4092/* END_CASE */
4093
Janos Follath16de4a42019-06-13 16:32:24 +01004094/* BEGIN_CASE */
4095void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004096{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004097 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004098 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004099 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004100
Gilles Peskine8817f612018-12-18 00:18:46 +01004101 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004102
Janos Follath16de4a42019-06-13 16:32:24 +01004103 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004104 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004105
4106exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004107 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004108 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004109}
4110/* END_CASE */
4111
Janos Follathaf3c2a02019-06-12 12:34:34 +01004112/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004113void derive_set_capacity( int alg_arg, int capacity_arg,
4114 int expected_status_arg )
4115{
4116 psa_algorithm_t alg = alg_arg;
4117 size_t capacity = capacity_arg;
4118 psa_status_t expected_status = expected_status_arg;
4119 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4120
4121 PSA_ASSERT( psa_crypto_init( ) );
4122
4123 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4124
4125 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4126 expected_status );
4127
4128exit:
4129 psa_key_derivation_abort( &operation );
4130 PSA_DONE( );
4131}
4132/* END_CASE */
4133
4134/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004135void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004136 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004137 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004138 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004139 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004140 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004141 int expected_status_arg3,
4142 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004143{
4144 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004145 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4146 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004147 psa_status_t expected_statuses[] = {expected_status_arg1,
4148 expected_status_arg2,
4149 expected_status_arg3};
4150 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004151 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4152 MBEDTLS_SVC_KEY_ID_INIT,
4153 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004154 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4155 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4156 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004157 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004158 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004159 psa_status_t expected_output_status = expected_output_status_arg;
4160 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004161
4162 PSA_ASSERT( psa_crypto_init( ) );
4163
4164 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4165 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004166
4167 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4168
4169 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4170 {
Gilles Peskinef6279312021-05-27 13:21:20 +02004171 mbedtls_test_set_step( i );
4172 if( steps[i] == 0 )
4173 {
4174 /* Skip this step */
4175 }
4176 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004177 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004178 psa_set_key_type( &attributes, key_types[i] );
4179 PSA_ASSERT( psa_import_key( &attributes,
4180 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004181 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004182 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4183 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4184 {
4185 // When taking a private key as secret input, use key agreement
4186 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004187 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4188 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004189 expected_statuses[i] );
4190 }
4191 else
4192 {
4193 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004194 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004195 expected_statuses[i] );
4196 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004197 }
4198 else
4199 {
4200 TEST_EQUAL( psa_key_derivation_input_bytes(
4201 &operation, steps[i],
4202 inputs[i]->x, inputs[i]->len ),
4203 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004204 }
4205 }
4206
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004207 if( output_key_type != PSA_KEY_TYPE_NONE )
4208 {
4209 psa_reset_key_attributes( &attributes );
4210 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4211 psa_set_key_bits( &attributes, 8 );
4212 actual_output_status =
4213 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004214 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004215 }
4216 else
4217 {
4218 uint8_t buffer[1];
4219 actual_output_status =
4220 psa_key_derivation_output_bytes( &operation,
4221 buffer, sizeof( buffer ) );
4222 }
4223 TEST_EQUAL( actual_output_status, expected_output_status );
4224
Janos Follathaf3c2a02019-06-12 12:34:34 +01004225exit:
4226 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004227 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4228 psa_destroy_key( keys[i] );
4229 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004230 PSA_DONE( );
4231}
4232/* END_CASE */
4233
Janos Follathd958bb72019-07-03 15:02:16 +01004234/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004235void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004236{
Janos Follathd958bb72019-07-03 15:02:16 +01004237 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004238 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004239 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004240 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004241 unsigned char input1[] = "Input 1";
4242 size_t input1_length = sizeof( input1 );
4243 unsigned char input2[] = "Input 2";
4244 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004245 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004246 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004247 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4248 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4249 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004250 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004251
Gilles Peskine8817f612018-12-18 00:18:46 +01004252 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004253
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004254 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4255 psa_set_key_algorithm( &attributes, alg );
4256 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004257
Gilles Peskine73676cb2019-05-15 20:15:10 +02004258 PSA_ASSERT( psa_import_key( &attributes,
4259 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004260 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004261
4262 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004263 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4264 input1, input1_length,
4265 input2, input2_length,
4266 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004267 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004268
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004269 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004270 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004271 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004272
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004273 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004274
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004275 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004276 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004277
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004278exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004279 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004280 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004281 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004282}
4283/* END_CASE */
4284
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004285/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004286void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004287{
4288 uint8_t output_buffer[16];
4289 size_t buffer_size = 16;
4290 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004291 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004292
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004293 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4294 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004295 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004296
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004297 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004298 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004299
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004300 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004301
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004302 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4303 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004304 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004305
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004306 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004307 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004308
4309exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004310 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004311}
4312/* END_CASE */
4313
4314/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004315void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004316 int step1_arg, data_t *input1,
4317 int step2_arg, data_t *input2,
4318 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004319 int requested_capacity_arg,
4320 data_t *expected_output1,
4321 data_t *expected_output2 )
4322{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004323 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004324 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4325 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004326 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4327 MBEDTLS_SVC_KEY_ID_INIT,
4328 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004329 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004330 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004331 uint8_t *expected_outputs[2] =
4332 {expected_output1->x, expected_output2->x};
4333 size_t output_sizes[2] =
4334 {expected_output1->len, expected_output2->len};
4335 size_t output_buffer_size = 0;
4336 uint8_t *output_buffer = NULL;
4337 size_t expected_capacity;
4338 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004339 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004340 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004341 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004342
4343 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4344 {
4345 if( output_sizes[i] > output_buffer_size )
4346 output_buffer_size = output_sizes[i];
4347 if( output_sizes[i] == 0 )
4348 expected_outputs[i] = NULL;
4349 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004350 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004351 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004352
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004353 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4354 psa_set_key_algorithm( &attributes, alg );
4355 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004356
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004357 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004358 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4359 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4360 requested_capacity ) );
4361 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004362 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004363 switch( steps[i] )
4364 {
4365 case 0:
4366 break;
4367 case PSA_KEY_DERIVATION_INPUT_SECRET:
4368 PSA_ASSERT( psa_import_key( &attributes,
4369 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004370 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004371
4372 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4373 {
4374 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4375 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4376 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4377 }
4378
Gilles Peskine1468da72019-05-29 17:35:49 +02004379 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004380 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004381 break;
4382 default:
4383 PSA_ASSERT( psa_key_derivation_input_bytes(
4384 &operation, steps[i],
4385 inputs[i]->x, inputs[i]->len ) );
4386 break;
4387 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004388 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004389
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004390 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004391 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004392 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004393 expected_capacity = requested_capacity;
4394
4395 /* Expansion phase. */
4396 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4397 {
4398 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004399 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004400 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004401 if( expected_capacity == 0 && output_sizes[i] == 0 )
4402 {
4403 /* Reading 0 bytes when 0 bytes are available can go either way. */
4404 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004405 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004406 continue;
4407 }
4408 else if( expected_capacity == 0 ||
4409 output_sizes[i] > expected_capacity )
4410 {
4411 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004412 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004413 expected_capacity = 0;
4414 continue;
4415 }
4416 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004417 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004418 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004419 ASSERT_COMPARE( output_buffer, output_sizes[i],
4420 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004421 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004422 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004423 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004424 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004425 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004426 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004427 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004428
4429exit:
4430 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004431 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004432 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4433 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004434 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004435}
4436/* END_CASE */
4437
4438/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004439void derive_full( int alg_arg,
4440 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004441 data_t *input1,
4442 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004443 int requested_capacity_arg )
4444{
Ronald Cron5425a212020-08-04 14:58:35 +02004445 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004446 psa_algorithm_t alg = alg_arg;
4447 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004448 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004449 unsigned char output_buffer[16];
4450 size_t expected_capacity = requested_capacity;
4451 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004452 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004453
Gilles Peskine8817f612018-12-18 00:18:46 +01004454 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004455
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004456 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4457 psa_set_key_algorithm( &attributes, alg );
4458 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004459
Gilles Peskine049c7532019-05-15 20:22:09 +02004460 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004461 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004462
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004463 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4464 input1->x, input1->len,
4465 input2->x, input2->len,
4466 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004467 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004468
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004469 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004470 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004471 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004472
4473 /* Expansion phase. */
4474 while( current_capacity > 0 )
4475 {
4476 size_t read_size = sizeof( output_buffer );
4477 if( read_size > current_capacity )
4478 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004479 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004480 output_buffer,
4481 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004482 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004483 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004484 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004485 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004486 }
4487
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004488 /* Check that the operation refuses to go over capacity. */
4489 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004490 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004491
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004492 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004493
4494exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004495 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004496 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004497 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004498}
4499/* END_CASE */
4500
Janos Follathe60c9052019-07-03 13:51:30 +01004501/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004502void derive_key_exercise( int alg_arg,
4503 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004504 data_t *input1,
4505 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004506 int derived_type_arg,
4507 int derived_bits_arg,
4508 int derived_usage_arg,
4509 int derived_alg_arg )
4510{
Ronald Cron5425a212020-08-04 14:58:35 +02004511 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4512 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004513 psa_algorithm_t alg = alg_arg;
4514 psa_key_type_t derived_type = derived_type_arg;
4515 size_t derived_bits = derived_bits_arg;
4516 psa_key_usage_t derived_usage = derived_usage_arg;
4517 psa_algorithm_t derived_alg = derived_alg_arg;
4518 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004519 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004520 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004521 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004522
Gilles Peskine8817f612018-12-18 00:18:46 +01004523 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004524
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004525 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4526 psa_set_key_algorithm( &attributes, alg );
4527 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004528 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004529 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004530
4531 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004532 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4533 input1->x, input1->len,
4534 input2->x, input2->len,
4535 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004536 goto exit;
4537
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004538 psa_set_key_usage_flags( &attributes, derived_usage );
4539 psa_set_key_algorithm( &attributes, derived_alg );
4540 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004541 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004542 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004543 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004544
4545 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004546 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004547 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4548 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004549
4550 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004551 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004552 goto exit;
4553
4554exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004555 /*
4556 * Key attributes may have been returned by psa_get_key_attributes()
4557 * thus reset them as required.
4558 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004559 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004560
4561 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004562 psa_destroy_key( base_key );
4563 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004564 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004565}
4566/* END_CASE */
4567
Janos Follath42fd8882019-07-03 14:17:09 +01004568/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004569void derive_key_export( int alg_arg,
4570 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004571 data_t *input1,
4572 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004573 int bytes1_arg,
4574 int bytes2_arg )
4575{
Ronald Cron5425a212020-08-04 14:58:35 +02004576 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4577 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004578 psa_algorithm_t alg = alg_arg;
4579 size_t bytes1 = bytes1_arg;
4580 size_t bytes2 = bytes2_arg;
4581 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004582 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004583 uint8_t *output_buffer = NULL;
4584 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004585 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4586 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004587 size_t length;
4588
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004589 ASSERT_ALLOC( output_buffer, capacity );
4590 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004591 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004592
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004593 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4594 psa_set_key_algorithm( &base_attributes, alg );
4595 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004596 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004597 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004598
4599 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004600 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4601 input1->x, input1->len,
4602 input2->x, input2->len,
4603 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004604 goto exit;
4605
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004606 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004607 output_buffer,
4608 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004609 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004610
4611 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004612 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4613 input1->x, input1->len,
4614 input2->x, input2->len,
4615 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004616 goto exit;
4617
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004618 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4619 psa_set_key_algorithm( &derived_attributes, 0 );
4620 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004621 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004622 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004623 &derived_key ) );
4624 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004625 export_buffer, bytes1,
4626 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004627 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004628 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004629 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004630 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004631 &derived_key ) );
4632 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004633 export_buffer + bytes1, bytes2,
4634 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004635 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004636
4637 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004638 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4639 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004640
4641exit:
4642 mbedtls_free( output_buffer );
4643 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004644 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004645 psa_destroy_key( base_key );
4646 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004647 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004648}
4649/* END_CASE */
4650
4651/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004652void derive_key( int alg_arg,
4653 data_t *key_data, data_t *input1, data_t *input2,
4654 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004655 int expected_status_arg,
4656 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004657{
Ronald Cron5425a212020-08-04 14:58:35 +02004658 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4659 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004660 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004661 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004662 size_t bits = bits_arg;
4663 psa_status_t expected_status = expected_status_arg;
4664 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4665 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4666 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4667
4668 PSA_ASSERT( psa_crypto_init( ) );
4669
4670 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4671 psa_set_key_algorithm( &base_attributes, alg );
4672 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4673 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004674 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004675
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004676 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4677 input1->x, input1->len,
4678 input2->x, input2->len,
4679 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004680 goto exit;
4681
4682 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4683 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004684 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004685 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004686
4687 psa_status_t status =
4688 psa_key_derivation_output_key( &derived_attributes,
4689 &operation,
4690 &derived_key );
4691 if( is_large_output > 0 )
4692 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4693 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004694
4695exit:
4696 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004697 psa_destroy_key( base_key );
4698 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004699 PSA_DONE( );
4700}
4701/* END_CASE */
4702
4703/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004704void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004705 int our_key_type_arg, int our_key_alg_arg,
4706 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004707 int expected_status_arg )
4708{
Ronald Cron5425a212020-08-04 14:58:35 +02004709 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004710 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004711 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004712 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004713 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004714 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004715 psa_status_t expected_status = expected_status_arg;
4716 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004717
Gilles Peskine8817f612018-12-18 00:18:46 +01004718 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004719
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004720 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004721 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004722 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004723 PSA_ASSERT( psa_import_key( &attributes,
4724 our_key_data->x, our_key_data->len,
4725 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004726
Gilles Peskine77f40d82019-04-11 21:27:06 +02004727 /* The tests currently include inputs that should fail at either step.
4728 * Test cases that fail at the setup step should be changed to call
4729 * key_derivation_setup instead, and this function should be renamed
4730 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004731 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004732 if( status == PSA_SUCCESS )
4733 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004734 TEST_EQUAL( psa_key_derivation_key_agreement(
4735 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4736 our_key,
4737 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004738 expected_status );
4739 }
4740 else
4741 {
4742 TEST_ASSERT( status == expected_status );
4743 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004744
4745exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004746 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004747 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004748 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004749}
4750/* END_CASE */
4751
4752/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004753void raw_key_agreement( int alg_arg,
4754 int our_key_type_arg, data_t *our_key_data,
4755 data_t *peer_key_data,
4756 data_t *expected_output )
4757{
Ronald Cron5425a212020-08-04 14:58:35 +02004758 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004759 psa_algorithm_t alg = alg_arg;
4760 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004761 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004762 unsigned char *output = NULL;
4763 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004764 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004765
4766 ASSERT_ALLOC( output, expected_output->len );
4767 PSA_ASSERT( psa_crypto_init( ) );
4768
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004769 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4770 psa_set_key_algorithm( &attributes, alg );
4771 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004772 PSA_ASSERT( psa_import_key( &attributes,
4773 our_key_data->x, our_key_data->len,
4774 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004775
gabor-mezei-armceface22021-01-21 12:26:17 +01004776 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4777 key_bits = psa_get_key_bits( &attributes );
4778
Gilles Peskinebe697d82019-05-16 18:00:41 +02004779 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4780 peer_key_data->x, peer_key_data->len,
4781 output, expected_output->len,
4782 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004783 ASSERT_COMPARE( output, output_length,
4784 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01004785 TEST_ASSERT( output_length <=
4786 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
4787 TEST_ASSERT( output_length <=
4788 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004789
4790exit:
4791 mbedtls_free( output );
4792 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004793 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004794}
4795/* END_CASE */
4796
4797/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004798void key_agreement_capacity( int alg_arg,
4799 int our_key_type_arg, data_t *our_key_data,
4800 data_t *peer_key_data,
4801 int expected_capacity_arg )
4802{
Ronald Cron5425a212020-08-04 14:58:35 +02004803 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004804 psa_algorithm_t alg = alg_arg;
4805 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004806 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004807 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004808 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004809 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004810
Gilles Peskine8817f612018-12-18 00:18:46 +01004811 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004812
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004813 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4814 psa_set_key_algorithm( &attributes, alg );
4815 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004816 PSA_ASSERT( psa_import_key( &attributes,
4817 our_key_data->x, our_key_data->len,
4818 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004819
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004820 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004821 PSA_ASSERT( psa_key_derivation_key_agreement(
4822 &operation,
4823 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4824 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004825 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4826 {
4827 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004828 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004829 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004830 NULL, 0 ) );
4831 }
Gilles Peskine59685592018-09-18 12:11:34 +02004832
Gilles Peskinebf491972018-10-25 22:36:12 +02004833 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004834 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004835 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004836 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004837
Gilles Peskinebf491972018-10-25 22:36:12 +02004838 /* Test the actual capacity by reading the output. */
4839 while( actual_capacity > sizeof( output ) )
4840 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004841 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004842 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004843 actual_capacity -= sizeof( output );
4844 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004845 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004846 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004847 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004848 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004849
Gilles Peskine59685592018-09-18 12:11:34 +02004850exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004851 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004852 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004853 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004854}
4855/* END_CASE */
4856
4857/* BEGIN_CASE */
4858void key_agreement_output( int alg_arg,
4859 int our_key_type_arg, data_t *our_key_data,
4860 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004861 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004862{
Ronald Cron5425a212020-08-04 14:58:35 +02004863 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004864 psa_algorithm_t alg = alg_arg;
4865 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004866 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004867 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004868 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004869
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004870 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4871 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004872
Gilles Peskine8817f612018-12-18 00:18:46 +01004873 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004874
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004875 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4876 psa_set_key_algorithm( &attributes, alg );
4877 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004878 PSA_ASSERT( psa_import_key( &attributes,
4879 our_key_data->x, our_key_data->len,
4880 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004881
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004882 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004883 PSA_ASSERT( psa_key_derivation_key_agreement(
4884 &operation,
4885 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4886 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004887 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4888 {
4889 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004890 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004891 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004892 NULL, 0 ) );
4893 }
Gilles Peskine59685592018-09-18 12:11:34 +02004894
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004895 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004896 actual_output,
4897 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004898 ASSERT_COMPARE( actual_output, expected_output1->len,
4899 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004900 if( expected_output2->len != 0 )
4901 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004902 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004903 actual_output,
4904 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004905 ASSERT_COMPARE( actual_output, expected_output2->len,
4906 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004907 }
Gilles Peskine59685592018-09-18 12:11:34 +02004908
4909exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004910 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004911 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004912 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004913 mbedtls_free( actual_output );
4914}
4915/* END_CASE */
4916
4917/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004918void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004919{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004920 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004921 unsigned char *output = NULL;
4922 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004923 size_t i;
4924 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004925
Simon Butcher49f8e312020-03-03 15:51:50 +00004926 TEST_ASSERT( bytes_arg >= 0 );
4927
Gilles Peskine91892022021-02-08 19:50:26 +01004928 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004929 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02004930
Gilles Peskine8817f612018-12-18 00:18:46 +01004931 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004932
Gilles Peskinea50d7392018-06-21 10:22:13 +02004933 /* Run several times, to ensure that every output byte will be
4934 * nonzero at least once with overwhelming probability
4935 * (2^(-8*number_of_runs)). */
4936 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004937 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004938 if( bytes != 0 )
4939 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004940 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004941
Gilles Peskinea50d7392018-06-21 10:22:13 +02004942 for( i = 0; i < bytes; i++ )
4943 {
4944 if( output[i] != 0 )
4945 ++changed[i];
4946 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004947 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004948
4949 /* Check that every byte was changed to nonzero at least once. This
4950 * validates that psa_generate_random is overwriting every byte of
4951 * the output buffer. */
4952 for( i = 0; i < bytes; i++ )
4953 {
4954 TEST_ASSERT( changed[i] != 0 );
4955 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004956
4957exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004958 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004959 mbedtls_free( output );
4960 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004961}
4962/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004963
4964/* BEGIN_CASE */
4965void generate_key( int type_arg,
4966 int bits_arg,
4967 int usage_arg,
4968 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004969 int expected_status_arg,
4970 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004971{
Ronald Cron5425a212020-08-04 14:58:35 +02004972 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004973 psa_key_type_t type = type_arg;
4974 psa_key_usage_t usage = usage_arg;
4975 size_t bits = bits_arg;
4976 psa_algorithm_t alg = alg_arg;
4977 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004978 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004979 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004980
Gilles Peskine8817f612018-12-18 00:18:46 +01004981 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004982
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004983 psa_set_key_usage_flags( &attributes, usage );
4984 psa_set_key_algorithm( &attributes, alg );
4985 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004986 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004987
4988 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01004989 psa_status_t status = psa_generate_key( &attributes, &key );
4990
4991 if( is_large_key > 0 )
4992 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4993 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004994 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004995 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004996
4997 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004998 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004999 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5000 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005001
Gilles Peskine818ca122018-06-20 18:16:48 +02005002 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005003 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005004 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005005
5006exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005007 /*
5008 * Key attributes may have been returned by psa_get_key_attributes()
5009 * thus reset them as required.
5010 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005011 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005012
Ronald Cron5425a212020-08-04 14:58:35 +02005013 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005014 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005015}
5016/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005017
Ronald Cronee414c72021-03-18 18:50:08 +01005018/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR:PSA_WANT_ALG_RSA_PKCS1V15_CRYPT:PSA_WANT_ALG_RSA_PKCS1V15_SIGN:MBEDTLS_GENPRIME */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005019void generate_key_rsa( int bits_arg,
5020 data_t *e_arg,
5021 int expected_status_arg )
5022{
Ronald Cron5425a212020-08-04 14:58:35 +02005023 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005024 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005025 size_t bits = bits_arg;
5026 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5027 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5028 psa_status_t expected_status = expected_status_arg;
5029 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5030 uint8_t *exported = NULL;
5031 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005032 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005033 size_t exported_length = SIZE_MAX;
5034 uint8_t *e_read_buffer = NULL;
5035 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005036 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005037 size_t e_read_length = SIZE_MAX;
5038
5039 if( e_arg->len == 0 ||
5040 ( e_arg->len == 3 &&
5041 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5042 {
5043 is_default_public_exponent = 1;
5044 e_read_size = 0;
5045 }
5046 ASSERT_ALLOC( e_read_buffer, e_read_size );
5047 ASSERT_ALLOC( exported, exported_size );
5048
5049 PSA_ASSERT( psa_crypto_init( ) );
5050
5051 psa_set_key_usage_flags( &attributes, usage );
5052 psa_set_key_algorithm( &attributes, alg );
5053 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5054 e_arg->x, e_arg->len ) );
5055 psa_set_key_bits( &attributes, bits );
5056
5057 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005058 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005059 if( expected_status != PSA_SUCCESS )
5060 goto exit;
5061
5062 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005063 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005064 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5065 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5066 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5067 e_read_buffer, e_read_size,
5068 &e_read_length ) );
5069 if( is_default_public_exponent )
5070 TEST_EQUAL( e_read_length, 0 );
5071 else
5072 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5073
5074 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005075 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005076 goto exit;
5077
5078 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005079 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005080 exported, exported_size,
5081 &exported_length ) );
5082 {
5083 uint8_t *p = exported;
5084 uint8_t *end = exported + exported_length;
5085 size_t len;
5086 /* RSAPublicKey ::= SEQUENCE {
5087 * modulus INTEGER, -- n
5088 * publicExponent INTEGER } -- e
5089 */
5090 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005091 MBEDTLS_ASN1_SEQUENCE |
5092 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005093 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005094 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5095 MBEDTLS_ASN1_INTEGER ) );
5096 if( len >= 1 && p[0] == 0 )
5097 {
5098 ++p;
5099 --len;
5100 }
5101 if( e_arg->len == 0 )
5102 {
5103 TEST_EQUAL( len, 3 );
5104 TEST_EQUAL( p[0], 1 );
5105 TEST_EQUAL( p[1], 0 );
5106 TEST_EQUAL( p[2], 1 );
5107 }
5108 else
5109 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5110 }
5111
5112exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005113 /*
5114 * Key attributes may have been returned by psa_get_key_attributes() or
5115 * set by psa_set_key_domain_parameters() thus reset them as required.
5116 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005117 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005118
Ronald Cron5425a212020-08-04 14:58:35 +02005119 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005120 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005121 mbedtls_free( e_read_buffer );
5122 mbedtls_free( exported );
5123}
5124/* END_CASE */
5125
Darryl Greend49a4992018-06-18 17:27:26 +01005126/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005127void persistent_key_load_key_from_storage( data_t *data,
5128 int type_arg, int bits_arg,
5129 int usage_flags_arg, int alg_arg,
5130 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005131{
Ronald Cron71016a92020-08-28 19:01:50 +02005132 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005133 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005134 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5135 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005136 psa_key_type_t type = type_arg;
5137 size_t bits = bits_arg;
5138 psa_key_usage_t usage_flags = usage_flags_arg;
5139 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005140 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005141 unsigned char *first_export = NULL;
5142 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005143 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005144 size_t first_exported_length;
5145 size_t second_exported_length;
5146
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005147 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5148 {
5149 ASSERT_ALLOC( first_export, export_size );
5150 ASSERT_ALLOC( second_export, export_size );
5151 }
Darryl Greend49a4992018-06-18 17:27:26 +01005152
Gilles Peskine8817f612018-12-18 00:18:46 +01005153 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005154
Gilles Peskinec87af662019-05-15 16:12:22 +02005155 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005156 psa_set_key_usage_flags( &attributes, usage_flags );
5157 psa_set_key_algorithm( &attributes, alg );
5158 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005159 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005160
Darryl Green0c6575a2018-11-07 16:05:30 +00005161 switch( generation_method )
5162 {
5163 case IMPORT_KEY:
5164 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005165 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005166 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005167 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005168
Darryl Green0c6575a2018-11-07 16:05:30 +00005169 case GENERATE_KEY:
5170 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005171 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005172 break;
5173
5174 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005175#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005176 {
5177 /* Create base key */
5178 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5179 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5180 psa_set_key_usage_flags( &base_attributes,
5181 PSA_KEY_USAGE_DERIVE );
5182 psa_set_key_algorithm( &base_attributes, derive_alg );
5183 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005184 PSA_ASSERT( psa_import_key( &base_attributes,
5185 data->x, data->len,
5186 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005187 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005188 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005189 PSA_ASSERT( psa_key_derivation_input_key(
5190 &operation,
5191 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005192 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005193 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005194 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005195 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5196 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005197 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005198 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005199 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005200 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005201 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005202#else
5203 TEST_ASSUME( ! "KDF not supported in this configuration" );
5204#endif
5205 break;
5206
5207 default:
5208 TEST_ASSERT( ! "generation_method not implemented in test" );
5209 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005210 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005211 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005212
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005213 /* Export the key if permitted by the key policy. */
5214 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5215 {
Ronald Cron5425a212020-08-04 14:58:35 +02005216 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005217 first_export, export_size,
5218 &first_exported_length ) );
5219 if( generation_method == IMPORT_KEY )
5220 ASSERT_COMPARE( data->x, data->len,
5221 first_export, first_exported_length );
5222 }
Darryl Greend49a4992018-06-18 17:27:26 +01005223
5224 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005225 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005226 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005227 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005228
Darryl Greend49a4992018-06-18 17:27:26 +01005229 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005230 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005231 TEST_ASSERT( mbedtls_svc_key_id_equal(
5232 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005233 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5234 PSA_KEY_LIFETIME_PERSISTENT );
5235 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5236 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5237 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5238 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005239
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005240 /* Export the key again if permitted by the key policy. */
5241 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005242 {
Ronald Cron5425a212020-08-04 14:58:35 +02005243 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005244 second_export, export_size,
5245 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005246 ASSERT_COMPARE( first_export, first_exported_length,
5247 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005248 }
5249
5250 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005251 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005252 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005253
5254exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005255 /*
5256 * Key attributes may have been returned by psa_get_key_attributes()
5257 * thus reset them as required.
5258 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005259 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005260
Darryl Greend49a4992018-06-18 17:27:26 +01005261 mbedtls_free( first_export );
5262 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005263 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005264 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005265 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005266 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005267}
5268/* END_CASE */