blob: 02e1bb0e8cf9c3230f9326d48f0bd0fbc637db59 [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. */
Dave Rodgman33b58ee2021-06-23 12:48:52 +01002369 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002370 TEST_EQUAL( psa_cipher_update( &operation,
2371 text, sizeof( text ),
2372 buffer, sizeof( buffer ),
2373 &length ),
2374 PSA_ERROR_BAD_STATE );
2375 PSA_ASSERT( psa_cipher_abort( &operation ) );
2376
2377 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002378 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002379 PSA_ASSERT( psa_cipher_set_iv( &operation,
2380 iv, sizeof( iv ) ) );
2381 PSA_ASSERT( psa_cipher_finish( &operation,
2382 buffer, sizeof( buffer ), &length ) );
2383 TEST_EQUAL( psa_cipher_update( &operation,
2384 text, sizeof( text ),
2385 buffer, sizeof( buffer ),
2386 &length ),
2387 PSA_ERROR_BAD_STATE );
2388 PSA_ASSERT( psa_cipher_abort( &operation ) );
2389
2390 /* Call finish without calling setup beforehand. */
2391 TEST_EQUAL( psa_cipher_finish( &operation,
2392 buffer, sizeof( buffer ), &length ),
2393 PSA_ERROR_BAD_STATE );
2394 PSA_ASSERT( psa_cipher_abort( &operation ) );
2395
2396 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002397 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002398 /* Not calling update means we are encrypting an empty buffer, which is OK
2399 * for cipher modes with padding. */
2400 TEST_EQUAL( psa_cipher_finish( &operation,
2401 buffer, sizeof( buffer ), &length ),
2402 PSA_ERROR_BAD_STATE );
2403 PSA_ASSERT( psa_cipher_abort( &operation ) );
2404
2405 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002406 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002407 PSA_ASSERT( psa_cipher_set_iv( &operation,
2408 iv, sizeof( iv ) ) );
2409 PSA_ASSERT( psa_cipher_finish( &operation,
2410 buffer, sizeof( buffer ), &length ) );
2411 TEST_EQUAL( psa_cipher_finish( &operation,
2412 buffer, sizeof( buffer ), &length ),
2413 PSA_ERROR_BAD_STATE );
2414 PSA_ASSERT( psa_cipher_abort( &operation ) );
2415
Ronald Cron5425a212020-08-04 14:58:35 +02002416 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002417
Jaeden Ameroab439972019-02-15 14:12:05 +00002418exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002419 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002420 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002421}
2422/* END_CASE */
2423
2424/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002425void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002426 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002427 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002428 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002429{
Ronald Cron5425a212020-08-04 14:58:35 +02002430 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002431 psa_status_t status;
2432 psa_key_type_t key_type = key_type_arg;
2433 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002434 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002435 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002436 size_t output_buffer_size = 0;
2437 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002438 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002439 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002440 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002441
Gilles Peskine8817f612018-12-18 00:18:46 +01002442 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002443
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002444 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2445 psa_set_key_algorithm( &attributes, alg );
2446 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002447
Ronald Cron5425a212020-08-04 14:58:35 +02002448 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2449 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002450
Ronald Cron5425a212020-08-04 14:58:35 +02002451 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002452
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002453 if( iv->len > 0 )
2454 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002455 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002456 }
2457
gabor-mezei-armceface22021-01-21 12:26:17 +01002458 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2459 TEST_ASSERT( output_buffer_size <=
2460 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002461 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002462
Gilles Peskine8817f612018-12-18 00:18:46 +01002463 PSA_ASSERT( psa_cipher_update( &operation,
2464 input->x, input->len,
2465 output, output_buffer_size,
2466 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002467 TEST_ASSERT( function_output_length <=
2468 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2469 TEST_ASSERT( function_output_length <=
2470 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002471 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002472
Gilles Peskine50e586b2018-06-08 14:28:46 +02002473 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002474 ( output_buffer_size == 0 ? NULL :
2475 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002476 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002477 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002478 TEST_ASSERT( function_output_length <=
2479 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2480 TEST_ASSERT( function_output_length <=
2481 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002482 total_output_length += function_output_length;
2483
Gilles Peskinefe11b722018-12-18 00:24:04 +01002484 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002485 if( expected_status == PSA_SUCCESS )
2486 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002487 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002488 ASSERT_COMPARE( expected_output->x, expected_output->len,
2489 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002490 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002491
Gilles Peskine50e586b2018-06-08 14:28:46 +02002492exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002493 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002494 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002495 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002496 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002497}
2498/* END_CASE */
2499
2500/* BEGIN_CASE */
2501void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002502 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002503 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002504 int first_part_size_arg,
2505 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002506 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002507{
Ronald Cron5425a212020-08-04 14:58:35 +02002508 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002509 psa_key_type_t key_type = key_type_arg;
2510 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002511 size_t first_part_size = first_part_size_arg;
2512 size_t output1_length = output1_length_arg;
2513 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002514 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002515 size_t output_buffer_size = 0;
2516 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002517 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002518 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002519 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002520
Gilles Peskine8817f612018-12-18 00:18:46 +01002521 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002522
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002523 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2524 psa_set_key_algorithm( &attributes, alg );
2525 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002526
Ronald Cron5425a212020-08-04 14:58:35 +02002527 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2528 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002529
Ronald Cron5425a212020-08-04 14:58:35 +02002530 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002531
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002532 if( iv->len > 0 )
2533 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002534 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002535 }
2536
gabor-mezei-armceface22021-01-21 12:26:17 +01002537 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2538 TEST_ASSERT( output_buffer_size <=
2539 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002540 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002541
Gilles Peskinee0866522019-02-19 19:44:00 +01002542 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002543 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2544 output, output_buffer_size,
2545 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002546 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002547 TEST_ASSERT( function_output_length <=
2548 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2549 TEST_ASSERT( function_output_length <=
2550 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002551 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002552
Gilles Peskine8817f612018-12-18 00:18:46 +01002553 PSA_ASSERT( psa_cipher_update( &operation,
2554 input->x + first_part_size,
2555 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002556 ( output_buffer_size == 0 ? NULL :
2557 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002558 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002559 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002560 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002561 TEST_ASSERT( function_output_length <=
2562 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2563 alg,
2564 input->len - first_part_size ) );
2565 TEST_ASSERT( function_output_length <=
2566 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002567 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002568
Gilles Peskine8817f612018-12-18 00:18:46 +01002569 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002570 ( output_buffer_size == 0 ? NULL :
2571 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002572 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002573 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002574 TEST_ASSERT( function_output_length <=
2575 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2576 TEST_ASSERT( function_output_length <=
2577 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002578 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002579 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002580
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002581 ASSERT_COMPARE( expected_output->x, expected_output->len,
2582 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002583
2584exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002585 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002586 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002587 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002588 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002589}
2590/* END_CASE */
2591
2592/* BEGIN_CASE */
2593void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002594 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002595 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002596 int first_part_size_arg,
2597 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002598 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002599{
Ronald Cron5425a212020-08-04 14:58:35 +02002600 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002601 psa_key_type_t key_type = key_type_arg;
2602 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002603 size_t first_part_size = first_part_size_arg;
2604 size_t output1_length = output1_length_arg;
2605 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002606 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002607 size_t output_buffer_size = 0;
2608 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002609 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002610 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002611 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002612
Gilles Peskine8817f612018-12-18 00:18:46 +01002613 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002614
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002615 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2616 psa_set_key_algorithm( &attributes, alg );
2617 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002618
Ronald Cron5425a212020-08-04 14:58:35 +02002619 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2620 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002621
Ronald Cron5425a212020-08-04 14:58:35 +02002622 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002623
Steven Cooreman177deba2020-09-07 17:14:14 +02002624 if( iv->len > 0 )
2625 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002626 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002627 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002628
gabor-mezei-armceface22021-01-21 12:26:17 +01002629 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2630 TEST_ASSERT( output_buffer_size <=
2631 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002632 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002633
Gilles Peskinee0866522019-02-19 19:44:00 +01002634 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002635 PSA_ASSERT( psa_cipher_update( &operation,
2636 input->x, first_part_size,
2637 output, output_buffer_size,
2638 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002639 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002640 TEST_ASSERT( function_output_length <=
2641 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2642 TEST_ASSERT( function_output_length <=
2643 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002644 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002645
Gilles Peskine8817f612018-12-18 00:18:46 +01002646 PSA_ASSERT( psa_cipher_update( &operation,
2647 input->x + first_part_size,
2648 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002649 ( output_buffer_size == 0 ? NULL :
2650 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002651 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002652 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002653 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002654 TEST_ASSERT( function_output_length <=
2655 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2656 alg,
2657 input->len - first_part_size ) );
2658 TEST_ASSERT( function_output_length <=
2659 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002660 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002661
Gilles Peskine8817f612018-12-18 00:18:46 +01002662 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002663 ( output_buffer_size == 0 ? NULL :
2664 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002665 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002666 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002667 TEST_ASSERT( function_output_length <=
2668 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2669 TEST_ASSERT( function_output_length <=
2670 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002671 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002672 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002673
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002674 ASSERT_COMPARE( expected_output->x, expected_output->len,
2675 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002676
2677exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002678 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002679 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002680 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002681 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002682}
2683/* END_CASE */
2684
Gilles Peskine50e586b2018-06-08 14:28:46 +02002685/* BEGIN_CASE */
2686void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002687 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002688 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002689 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002690{
Ronald Cron5425a212020-08-04 14:58:35 +02002691 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002692 psa_status_t status;
2693 psa_key_type_t key_type = key_type_arg;
2694 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002695 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002696 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002697 size_t output_buffer_size = 0;
2698 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002699 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002700 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002701 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002702
Gilles Peskine8817f612018-12-18 00:18:46 +01002703 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002704
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002705 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2706 psa_set_key_algorithm( &attributes, alg );
2707 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002708
Ronald Cron5425a212020-08-04 14:58:35 +02002709 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2710 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002711
Ronald Cron5425a212020-08-04 14:58:35 +02002712 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002713
Steven Cooreman177deba2020-09-07 17:14:14 +02002714 if( iv->len > 0 )
2715 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002716 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002717 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002718
gabor-mezei-armceface22021-01-21 12:26:17 +01002719 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2720 TEST_ASSERT( output_buffer_size <=
2721 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002722 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002723
Gilles Peskine8817f612018-12-18 00:18:46 +01002724 PSA_ASSERT( psa_cipher_update( &operation,
2725 input->x, input->len,
2726 output, output_buffer_size,
2727 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002728 TEST_ASSERT( function_output_length <=
2729 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2730 TEST_ASSERT( function_output_length <=
2731 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002732 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002733
Gilles Peskine50e586b2018-06-08 14:28:46 +02002734 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002735 ( output_buffer_size == 0 ? NULL :
2736 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002737 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002738 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002739 TEST_ASSERT( function_output_length <=
2740 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2741 TEST_ASSERT( function_output_length <=
2742 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002743 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002744 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002745
2746 if( expected_status == PSA_SUCCESS )
2747 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002748 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002749 ASSERT_COMPARE( expected_output->x, expected_output->len,
2750 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002751 }
2752
Gilles Peskine50e586b2018-06-08 14:28:46 +02002753exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002754 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002755 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002756 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002757 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002758}
2759/* END_CASE */
2760
Gilles Peskine50e586b2018-06-08 14:28:46 +02002761/* BEGIN_CASE */
2762void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002763 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002764 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002765{
Ronald Cron5425a212020-08-04 14:58:35 +02002766 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002767 psa_key_type_t key_type = key_type_arg;
2768 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002769 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002770 size_t iv_size = 16;
2771 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002772 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002773 size_t output1_size = 0;
2774 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002775 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002776 size_t output2_size = 0;
2777 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002778 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002779 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2780 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002781 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002782
Gilles Peskine8817f612018-12-18 00:18:46 +01002783 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002784
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002785 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2786 psa_set_key_algorithm( &attributes, alg );
2787 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002788
Ronald Cron5425a212020-08-04 14:58:35 +02002789 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2790 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002791
Ronald Cron5425a212020-08-04 14:58:35 +02002792 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2793 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002794
Steven Cooreman177deba2020-09-07 17:14:14 +02002795 if( alg != PSA_ALG_ECB_NO_PADDING )
2796 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002797 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2798 iv, iv_size,
2799 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002800 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002801 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2802 TEST_ASSERT( output1_size <=
2803 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002804 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002805
Gilles Peskine8817f612018-12-18 00:18:46 +01002806 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2807 output1, output1_size,
2808 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002809 TEST_ASSERT( output1_length <=
2810 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2811 TEST_ASSERT( output1_length <=
2812 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2813
Gilles Peskine8817f612018-12-18 00:18:46 +01002814 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002815 output1 + output1_length,
2816 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002817 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002818 TEST_ASSERT( function_output_length <=
2819 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2820 TEST_ASSERT( function_output_length <=
2821 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002822
Gilles Peskine048b7f02018-06-08 14:20:49 +02002823 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002824
Gilles Peskine8817f612018-12-18 00:18:46 +01002825 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002826
2827 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002828 TEST_ASSERT( output2_size <=
2829 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2830 TEST_ASSERT( output2_size <=
2831 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002832 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002833
Steven Cooreman177deba2020-09-07 17:14:14 +02002834 if( iv_length > 0 )
2835 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002836 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2837 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002838 }
2839
Gilles Peskine8817f612018-12-18 00:18:46 +01002840 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2841 output2, output2_size,
2842 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002843 TEST_ASSERT( output2_length <=
2844 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
2845 TEST_ASSERT( output2_length <=
2846 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
2847
Gilles Peskine048b7f02018-06-08 14:20:49 +02002848 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002849 PSA_ASSERT( psa_cipher_finish( &operation2,
2850 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002851 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002852 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002853 TEST_ASSERT( function_output_length <=
2854 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2855 TEST_ASSERT( function_output_length <=
2856 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03002857
Gilles Peskine048b7f02018-06-08 14:20:49 +02002858 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002859
Gilles Peskine8817f612018-12-18 00:18:46 +01002860 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002861
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002862 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002863
2864exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002865 psa_cipher_abort( &operation1 );
2866 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002867 mbedtls_free( output1 );
2868 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002869 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002870 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03002871}
2872/* END_CASE */
2873
2874/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002875void cipher_verify_output_multipart( int alg_arg,
2876 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002877 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002878 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002879 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03002880{
Ronald Cron5425a212020-08-04 14:58:35 +02002881 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002882 psa_key_type_t key_type = key_type_arg;
2883 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002884 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002885 unsigned char iv[16] = {0};
2886 size_t iv_size = 16;
2887 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002888 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002889 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002890 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002891 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002892 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002893 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002894 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002895 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2896 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002897 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002898
Gilles Peskine8817f612018-12-18 00:18:46 +01002899 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002900
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002901 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2902 psa_set_key_algorithm( &attributes, alg );
2903 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002904
Ronald Cron5425a212020-08-04 14:58:35 +02002905 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2906 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03002907
Ronald Cron5425a212020-08-04 14:58:35 +02002908 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2909 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002910
Steven Cooreman177deba2020-09-07 17:14:14 +02002911 if( alg != PSA_ALG_ECB_NO_PADDING )
2912 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002913 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2914 iv, iv_size,
2915 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002916 }
2917
gabor-mezei-armceface22021-01-21 12:26:17 +01002918 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2919 TEST_ASSERT( output1_buffer_size <=
2920 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002921 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002922
Gilles Peskinee0866522019-02-19 19:44:00 +01002923 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002924
Gilles Peskine8817f612018-12-18 00:18:46 +01002925 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2926 output1, output1_buffer_size,
2927 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002928 TEST_ASSERT( function_output_length <=
2929 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2930 TEST_ASSERT( function_output_length <=
2931 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002932 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002933
Gilles Peskine8817f612018-12-18 00:18:46 +01002934 PSA_ASSERT( psa_cipher_update( &operation1,
2935 input->x + first_part_size,
2936 input->len - first_part_size,
2937 output1, output1_buffer_size,
2938 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002939 TEST_ASSERT( function_output_length <=
2940 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2941 alg,
2942 input->len - first_part_size ) );
2943 TEST_ASSERT( function_output_length <=
2944 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002945 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002946
Gilles Peskine8817f612018-12-18 00:18:46 +01002947 PSA_ASSERT( psa_cipher_finish( &operation1,
2948 output1 + output1_length,
2949 output1_buffer_size - output1_length,
2950 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002951 TEST_ASSERT( function_output_length <=
2952 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2953 TEST_ASSERT( function_output_length <=
2954 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002955 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002956
Gilles Peskine8817f612018-12-18 00:18:46 +01002957 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002958
Gilles Peskine048b7f02018-06-08 14:20:49 +02002959 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002960 TEST_ASSERT( output2_buffer_size <=
2961 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2962 TEST_ASSERT( output2_buffer_size <=
2963 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002964 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002965
Steven Cooreman177deba2020-09-07 17:14:14 +02002966 if( iv_length > 0 )
2967 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002968 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2969 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002970 }
Moran Pekerded84402018-06-06 16:36:50 +03002971
Gilles Peskine8817f612018-12-18 00:18:46 +01002972 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2973 output2, output2_buffer_size,
2974 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002975 TEST_ASSERT( function_output_length <=
2976 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2977 TEST_ASSERT( function_output_length <=
2978 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002979 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002980
Gilles Peskine8817f612018-12-18 00:18:46 +01002981 PSA_ASSERT( psa_cipher_update( &operation2,
2982 output1 + first_part_size,
2983 output1_length - first_part_size,
2984 output2, output2_buffer_size,
2985 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002986 TEST_ASSERT( function_output_length <=
2987 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2988 alg,
2989 output1_length - first_part_size ) );
2990 TEST_ASSERT( function_output_length <=
2991 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002992 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002993
Gilles Peskine8817f612018-12-18 00:18:46 +01002994 PSA_ASSERT( psa_cipher_finish( &operation2,
2995 output2 + output2_length,
2996 output2_buffer_size - output2_length,
2997 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002998 TEST_ASSERT( function_output_length <=
2999 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3000 TEST_ASSERT( function_output_length <=
3001 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003002 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003003
Gilles Peskine8817f612018-12-18 00:18:46 +01003004 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003005
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003006 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003007
3008exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003009 psa_cipher_abort( &operation1 );
3010 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003011 mbedtls_free( output1 );
3012 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003013 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003014 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003015}
3016/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003017
Gilles Peskine20035e32018-02-03 22:44:14 +01003018/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003019void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003020 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003021 data_t *nonce,
3022 data_t *additional_data,
3023 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003024 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003025{
Ronald Cron5425a212020-08-04 14:58:35 +02003026 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003027 psa_key_type_t key_type = key_type_arg;
3028 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003029 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003030 unsigned char *output_data = NULL;
3031 size_t output_size = 0;
3032 size_t output_length = 0;
3033 unsigned char *output_data2 = NULL;
3034 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003035 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003036 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003037 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003038
Gilles Peskine8817f612018-12-18 00:18:46 +01003039 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003040
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003041 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3042 psa_set_key_algorithm( &attributes, alg );
3043 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003044
Gilles Peskine049c7532019-05-15 20:22:09 +02003045 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003046 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003047 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3048 key_bits = psa_get_key_bits( &attributes );
3049
3050 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3051 alg );
3052 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3053 * should be exact. */
3054 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3055 expected_result != PSA_ERROR_NOT_SUPPORTED )
3056 {
3057 TEST_EQUAL( output_size,
3058 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3059 TEST_ASSERT( output_size <=
3060 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3061 }
3062 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003063
Steven Cooremanf49478b2021-02-15 15:19:25 +01003064 status = psa_aead_encrypt( key, alg,
3065 nonce->x, nonce->len,
3066 additional_data->x,
3067 additional_data->len,
3068 input_data->x, input_data->len,
3069 output_data, output_size,
3070 &output_length );
3071
3072 /* If the operation is not supported, just skip and not fail in case the
3073 * encryption involves a common limitation of cryptography hardwares and
3074 * an alternative implementation. */
3075 if( status == PSA_ERROR_NOT_SUPPORTED )
3076 {
3077 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3078 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3079 }
3080
3081 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003082
3083 if( PSA_SUCCESS == expected_result )
3084 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003085 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003086
Gilles Peskine003a4a92019-05-14 16:09:40 +02003087 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3088 * should be exact. */
3089 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003090 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003091
gabor-mezei-armceface22021-01-21 12:26:17 +01003092 TEST_ASSERT( input_data->len <=
3093 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3094
Ronald Cron5425a212020-08-04 14:58:35 +02003095 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003096 nonce->x, nonce->len,
3097 additional_data->x,
3098 additional_data->len,
3099 output_data, output_length,
3100 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003101 &output_length2 ),
3102 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003103
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003104 ASSERT_COMPARE( input_data->x, input_data->len,
3105 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003106 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003107
Gilles Peskinea1cac842018-06-11 19:33:02 +02003108exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003109 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003110 mbedtls_free( output_data );
3111 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003112 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003113}
3114/* END_CASE */
3115
3116/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003117void aead_encrypt( int key_type_arg, data_t *key_data,
3118 int alg_arg,
3119 data_t *nonce,
3120 data_t *additional_data,
3121 data_t *input_data,
3122 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003123{
Ronald Cron5425a212020-08-04 14:58:35 +02003124 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003125 psa_key_type_t key_type = key_type_arg;
3126 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003127 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003128 unsigned char *output_data = NULL;
3129 size_t output_size = 0;
3130 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003131 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003132 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003133
Gilles Peskine8817f612018-12-18 00:18:46 +01003134 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003135
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003136 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3137 psa_set_key_algorithm( &attributes, alg );
3138 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003139
Gilles Peskine049c7532019-05-15 20:22:09 +02003140 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003141 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003142 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3143 key_bits = psa_get_key_bits( &attributes );
3144
3145 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3146 alg );
3147 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3148 * should be exact. */
3149 TEST_EQUAL( output_size,
3150 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3151 TEST_ASSERT( output_size <=
3152 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3153 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003154
Steven Cooremand588ea12021-01-11 19:36:04 +01003155 status = psa_aead_encrypt( key, alg,
3156 nonce->x, nonce->len,
3157 additional_data->x, additional_data->len,
3158 input_data->x, input_data->len,
3159 output_data, output_size,
3160 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003161
Ronald Cron28a45ed2021-02-09 20:35:42 +01003162 /* If the operation is not supported, just skip and not fail in case the
3163 * encryption involves a common limitation of cryptography hardwares and
3164 * an alternative implementation. */
3165 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003166 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003167 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3168 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003169 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003170
3171 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003172 ASSERT_COMPARE( expected_result->x, expected_result->len,
3173 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003174
Gilles Peskinea1cac842018-06-11 19:33:02 +02003175exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003176 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003177 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003178 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003179}
3180/* END_CASE */
3181
3182/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003183void aead_decrypt( int key_type_arg, data_t *key_data,
3184 int alg_arg,
3185 data_t *nonce,
3186 data_t *additional_data,
3187 data_t *input_data,
3188 data_t *expected_data,
3189 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003190{
Ronald Cron5425a212020-08-04 14:58:35 +02003191 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003192 psa_key_type_t key_type = key_type_arg;
3193 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003194 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003195 unsigned char *output_data = NULL;
3196 size_t output_size = 0;
3197 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003198 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003199 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003200 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003201
Gilles Peskine8817f612018-12-18 00:18:46 +01003202 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003203
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003204 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3205 psa_set_key_algorithm( &attributes, alg );
3206 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003207
Gilles Peskine049c7532019-05-15 20:22:09 +02003208 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003209 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003210 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3211 key_bits = psa_get_key_bits( &attributes );
3212
3213 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3214 alg );
3215 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3216 expected_result != PSA_ERROR_NOT_SUPPORTED )
3217 {
3218 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3219 * should be exact. */
3220 TEST_EQUAL( output_size,
3221 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3222 TEST_ASSERT( output_size <=
3223 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3224 }
3225 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003226
Steven Cooremand588ea12021-01-11 19:36:04 +01003227 status = psa_aead_decrypt( key, alg,
3228 nonce->x, nonce->len,
3229 additional_data->x,
3230 additional_data->len,
3231 input_data->x, input_data->len,
3232 output_data, output_size,
3233 &output_length );
3234
Ronald Cron28a45ed2021-02-09 20:35:42 +01003235 /* If the operation is not supported, just skip and not fail in case the
3236 * decryption involves a common limitation of cryptography hardwares and
3237 * an alternative implementation. */
3238 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003239 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003240 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3241 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003242 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003243
3244 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003245
Gilles Peskine2d277862018-06-18 15:41:12 +02003246 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003247 ASSERT_COMPARE( expected_data->x, expected_data->len,
3248 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003249
Gilles Peskinea1cac842018-06-11 19:33:02 +02003250exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003251 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003252 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003253 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003254}
3255/* END_CASE */
3256
3257/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003258void signature_size( int type_arg,
3259 int bits,
3260 int alg_arg,
3261 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003262{
3263 psa_key_type_t type = type_arg;
3264 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003265 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003266
Gilles Peskinefe11b722018-12-18 00:24:04 +01003267 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003268#if defined(MBEDTLS_TEST_DEPRECATED)
3269 TEST_EQUAL( actual_size,
3270 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3271#endif /* MBEDTLS_TEST_DEPRECATED */
3272
Gilles Peskinee59236f2018-01-27 23:32:46 +01003273exit:
3274 ;
3275}
3276/* END_CASE */
3277
3278/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003279void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3280 int alg_arg, data_t *input_data,
3281 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003282{
Ronald Cron5425a212020-08-04 14:58:35 +02003283 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003284 psa_key_type_t key_type = key_type_arg;
3285 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003286 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003287 unsigned char *signature = NULL;
3288 size_t signature_size;
3289 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003290 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003291
Gilles Peskine8817f612018-12-18 00:18:46 +01003292 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003293
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003294 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003295 psa_set_key_algorithm( &attributes, alg );
3296 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003297
Gilles Peskine049c7532019-05-15 20:22:09 +02003298 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003299 &key ) );
3300 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003301 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003302
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003303 /* Allocate a buffer which has the size advertized by the
3304 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003305 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003306 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003307 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003308 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003309 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003310
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003311 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003312 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003313 input_data->x, input_data->len,
3314 signature, signature_size,
3315 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003316 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003317 ASSERT_COMPARE( output_data->x, output_data->len,
3318 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003319
Gilles Peskine0627f982019-11-26 19:12:16 +01003320#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003321 memset( signature, 0, signature_size );
3322 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003323 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003324 input_data->x, input_data->len,
3325 signature, signature_size,
3326 &signature_length ) );
3327 ASSERT_COMPARE( output_data->x, output_data->len,
3328 signature, signature_length );
3329#endif /* MBEDTLS_TEST_DEPRECATED */
3330
Gilles Peskine20035e32018-02-03 22:44:14 +01003331exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003332 /*
3333 * Key attributes may have been returned by psa_get_key_attributes()
3334 * thus reset them as required.
3335 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003336 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003337
Ronald Cron5425a212020-08-04 14:58:35 +02003338 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003339 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003340 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003341}
3342/* END_CASE */
3343
3344/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003345void sign_hash_fail( int key_type_arg, data_t *key_data,
3346 int alg_arg, data_t *input_data,
3347 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003348{
Ronald Cron5425a212020-08-04 14:58:35 +02003349 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003350 psa_key_type_t key_type = key_type_arg;
3351 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003352 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003353 psa_status_t actual_status;
3354 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003355 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003356 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003357 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003358
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003359 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003360
Gilles Peskine8817f612018-12-18 00:18:46 +01003361 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003362
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003363 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003364 psa_set_key_algorithm( &attributes, alg );
3365 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003366
Gilles Peskine049c7532019-05-15 20:22:09 +02003367 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003368 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003369
Ronald Cron5425a212020-08-04 14:58:35 +02003370 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003371 input_data->x, input_data->len,
3372 signature, signature_size,
3373 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003374 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003375 /* The value of *signature_length is unspecified on error, but
3376 * whatever it is, it should be less than signature_size, so that
3377 * if the caller tries to read *signature_length bytes without
3378 * checking the error code then they don't overflow a buffer. */
3379 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003380
Gilles Peskine895242b2019-11-29 12:15:40 +01003381#if defined(MBEDTLS_TEST_DEPRECATED)
3382 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003383 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003384 input_data->x, input_data->len,
3385 signature, signature_size,
3386 &signature_length ),
3387 expected_status );
3388 TEST_ASSERT( signature_length <= signature_size );
3389#endif /* MBEDTLS_TEST_DEPRECATED */
3390
Gilles Peskine20035e32018-02-03 22:44:14 +01003391exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003392 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003393 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003394 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003395 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003396}
3397/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003398
3399/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003400void sign_verify_hash( int key_type_arg, data_t *key_data,
3401 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003402{
Ronald Cron5425a212020-08-04 14:58:35 +02003403 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003404 psa_key_type_t key_type = key_type_arg;
3405 psa_algorithm_t alg = alg_arg;
3406 size_t key_bits;
3407 unsigned char *signature = NULL;
3408 size_t signature_size;
3409 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003410 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003411
Gilles Peskine8817f612018-12-18 00:18:46 +01003412 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003413
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003414 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003415 psa_set_key_algorithm( &attributes, alg );
3416 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003417
Gilles Peskine049c7532019-05-15 20:22:09 +02003418 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003419 &key ) );
3420 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003421 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003422
3423 /* Allocate a buffer which has the size advertized by the
3424 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003425 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003426 key_bits, alg );
3427 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003428 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003429 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003430
3431 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003432 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003433 input_data->x, input_data->len,
3434 signature, signature_size,
3435 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003436 /* Check that the signature length looks sensible. */
3437 TEST_ASSERT( signature_length <= signature_size );
3438 TEST_ASSERT( signature_length > 0 );
3439
3440 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003441 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003442 input_data->x, input_data->len,
3443 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003444
3445 if( input_data->len != 0 )
3446 {
3447 /* Flip a bit in the input and verify that the signature is now
3448 * detected as invalid. Flip a bit at the beginning, not at the end,
3449 * because ECDSA may ignore the last few bits of the input. */
3450 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003451 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003452 input_data->x, input_data->len,
3453 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003454 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003455 }
3456
3457exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003458 /*
3459 * Key attributes may have been returned by psa_get_key_attributes()
3460 * thus reset them as required.
3461 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003462 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003463
Ronald Cron5425a212020-08-04 14:58:35 +02003464 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003465 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003466 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003467}
3468/* END_CASE */
3469
3470/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003471void verify_hash( int key_type_arg, data_t *key_data,
3472 int alg_arg, data_t *hash_data,
3473 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003474{
Ronald Cron5425a212020-08-04 14:58:35 +02003475 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003476 psa_key_type_t key_type = key_type_arg;
3477 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003478 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003479
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003480 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003481
Gilles Peskine8817f612018-12-18 00:18:46 +01003482 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003483
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003484 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003485 psa_set_key_algorithm( &attributes, alg );
3486 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003487
Gilles Peskine049c7532019-05-15 20:22:09 +02003488 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003489 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003490
Ronald Cron5425a212020-08-04 14:58:35 +02003491 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003492 hash_data->x, hash_data->len,
3493 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003494
3495#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003496 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003497 hash_data->x, hash_data->len,
3498 signature_data->x,
3499 signature_data->len ) );
3500
3501#endif /* MBEDTLS_TEST_DEPRECATED */
3502
itayzafrir5c753392018-05-08 11:18:38 +03003503exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003504 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003505 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003506 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003507}
3508/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003509
3510/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003511void verify_hash_fail( int key_type_arg, data_t *key_data,
3512 int alg_arg, data_t *hash_data,
3513 data_t *signature_data,
3514 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003515{
Ronald Cron5425a212020-08-04 14:58:35 +02003516 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003517 psa_key_type_t key_type = key_type_arg;
3518 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003519 psa_status_t actual_status;
3520 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003521 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003522
Gilles Peskine8817f612018-12-18 00:18:46 +01003523 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003524
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003525 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003526 psa_set_key_algorithm( &attributes, alg );
3527 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003528
Gilles Peskine049c7532019-05-15 20:22:09 +02003529 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003530 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003531
Ronald Cron5425a212020-08-04 14:58:35 +02003532 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003533 hash_data->x, hash_data->len,
3534 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003535 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003536
Gilles Peskine895242b2019-11-29 12:15:40 +01003537#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003538 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003539 hash_data->x, hash_data->len,
3540 signature_data->x, signature_data->len ),
3541 expected_status );
3542#endif /* MBEDTLS_TEST_DEPRECATED */
3543
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003544exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003545 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003546 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003547 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003548}
3549/* END_CASE */
3550
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003551/* BEGIN_CASE */
gabor-mezei-armabd72582021-04-15 18:19:50 +02003552void sign_message_deterministic( int key_type_arg,
3553 data_t *key_data,
3554 int alg_arg,
3555 data_t *input_data,
3556 data_t *output_data )
3557{
3558 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3559 psa_key_type_t key_type = key_type_arg;
3560 psa_algorithm_t alg = alg_arg;
3561 size_t key_bits;
3562 unsigned char *signature = NULL;
3563 size_t signature_size;
3564 size_t signature_length = 0xdeadbeef;
3565 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3566
3567 PSA_ASSERT( psa_crypto_init( ) );
3568
3569 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3570 psa_set_key_algorithm( &attributes, alg );
3571 psa_set_key_type( &attributes, key_type );
3572
3573 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3574 &key ) );
3575 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3576 key_bits = psa_get_key_bits( &attributes );
3577
3578 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3579 TEST_ASSERT( signature_size != 0 );
3580 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3581 ASSERT_ALLOC( signature, signature_size );
3582
3583 PSA_ASSERT( psa_sign_message( key, alg,
3584 input_data->x, input_data->len,
3585 signature, signature_size,
3586 &signature_length ) );
3587
3588 ASSERT_COMPARE( output_data->x, output_data->len,
3589 signature, signature_length );
3590
3591exit:
3592 psa_reset_key_attributes( &attributes );
3593
3594 psa_destroy_key( key );
3595 mbedtls_free( signature );
3596 PSA_DONE( );
3597
3598}
3599/* END_CASE */
3600
3601/* BEGIN_CASE */
3602void sign_message_fail( int key_type_arg,
3603 data_t *key_data,
3604 int alg_arg,
3605 data_t *input_data,
3606 int signature_size_arg,
3607 int expected_status_arg )
3608{
3609 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3610 psa_key_type_t key_type = key_type_arg;
3611 psa_algorithm_t alg = alg_arg;
3612 size_t signature_size = signature_size_arg;
3613 psa_status_t actual_status;
3614 psa_status_t expected_status = expected_status_arg;
3615 unsigned char *signature = NULL;
3616 size_t signature_length = 0xdeadbeef;
3617 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3618
3619 ASSERT_ALLOC( signature, signature_size );
3620
3621 PSA_ASSERT( psa_crypto_init( ) );
3622
3623 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3624 psa_set_key_algorithm( &attributes, alg );
3625 psa_set_key_type( &attributes, key_type );
3626
3627 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3628 &key ) );
3629
3630 actual_status = psa_sign_message( key, alg,
3631 input_data->x, input_data->len,
3632 signature, signature_size,
3633 &signature_length );
3634 TEST_EQUAL( actual_status, expected_status );
3635 /* The value of *signature_length is unspecified on error, but
3636 * whatever it is, it should be less than signature_size, so that
3637 * if the caller tries to read *signature_length bytes without
3638 * checking the error code then they don't overflow a buffer. */
3639 TEST_ASSERT( signature_length <= signature_size );
3640
3641exit:
3642 psa_reset_key_attributes( &attributes );
3643 psa_destroy_key( key );
3644 mbedtls_free( signature );
3645 PSA_DONE( );
3646}
3647/* END_CASE */
3648
3649/* BEGIN_CASE */
3650void sign_verify_message( int key_type_arg,
3651 data_t *key_data,
3652 int alg_arg,
3653 data_t *input_data )
3654{
3655 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3656 psa_key_type_t key_type = key_type_arg;
3657 psa_algorithm_t alg = alg_arg;
3658 size_t key_bits;
3659 unsigned char *signature = NULL;
3660 size_t signature_size;
3661 size_t signature_length = 0xdeadbeef;
3662 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3663
3664 PSA_ASSERT( psa_crypto_init( ) );
3665
3666 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3667 PSA_KEY_USAGE_VERIFY_MESSAGE );
3668 psa_set_key_algorithm( &attributes, alg );
3669 psa_set_key_type( &attributes, key_type );
3670
3671 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3672 &key ) );
3673 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3674 key_bits = psa_get_key_bits( &attributes );
3675
3676 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3677 TEST_ASSERT( signature_size != 0 );
3678 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3679 ASSERT_ALLOC( signature, signature_size );
3680
3681 PSA_ASSERT( psa_sign_message( key, alg,
3682 input_data->x, input_data->len,
3683 signature, signature_size,
3684 &signature_length ) );
3685 TEST_ASSERT( signature_length <= signature_size );
3686 TEST_ASSERT( signature_length > 0 );
3687
3688 PSA_ASSERT( psa_verify_message( key, alg,
3689 input_data->x, input_data->len,
3690 signature, signature_length ) );
3691
3692 if( input_data->len != 0 )
3693 {
3694 /* Flip a bit in the input and verify that the signature is now
3695 * detected as invalid. Flip a bit at the beginning, not at the end,
3696 * because ECDSA may ignore the last few bits of the input. */
3697 input_data->x[0] ^= 1;
3698 TEST_EQUAL( psa_verify_message( key, alg,
3699 input_data->x, input_data->len,
3700 signature, signature_length ),
3701 PSA_ERROR_INVALID_SIGNATURE );
3702 }
3703
3704exit:
3705 psa_reset_key_attributes( &attributes );
3706
3707 psa_destroy_key( key );
3708 mbedtls_free( signature );
3709 PSA_DONE( );
3710}
3711/* END_CASE */
3712
3713/* BEGIN_CASE */
3714void verify_message( int key_type_arg,
3715 data_t *key_data,
3716 int alg_arg,
3717 data_t *input_data,
3718 data_t *signature_data )
3719{
3720 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3721 psa_key_type_t key_type = key_type_arg;
3722 psa_algorithm_t alg = alg_arg;
3723 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3724
3725 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3726
3727 PSA_ASSERT( psa_crypto_init( ) );
3728
3729 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3730 psa_set_key_algorithm( &attributes, alg );
3731 psa_set_key_type( &attributes, key_type );
3732
3733 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3734 &key ) );
3735
3736 PSA_ASSERT( psa_verify_message( key, alg,
3737 input_data->x, input_data->len,
3738 signature_data->x, signature_data->len ) );
3739
3740exit:
3741 psa_reset_key_attributes( &attributes );
3742 psa_destroy_key( key );
3743 PSA_DONE( );
3744}
3745/* END_CASE */
3746
3747/* BEGIN_CASE */
3748void verify_message_fail( int key_type_arg,
3749 data_t *key_data,
3750 int alg_arg,
3751 data_t *hash_data,
3752 data_t *signature_data,
3753 int expected_status_arg )
3754{
3755 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3756 psa_key_type_t key_type = key_type_arg;
3757 psa_algorithm_t alg = alg_arg;
3758 psa_status_t actual_status;
3759 psa_status_t expected_status = expected_status_arg;
3760 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3761
3762 PSA_ASSERT( psa_crypto_init( ) );
3763
3764 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3765 psa_set_key_algorithm( &attributes, alg );
3766 psa_set_key_type( &attributes, key_type );
3767
3768 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3769 &key ) );
3770
3771 actual_status = psa_verify_message( key, alg,
3772 hash_data->x, hash_data->len,
3773 signature_data->x,
3774 signature_data->len );
3775 TEST_EQUAL( actual_status, expected_status );
3776
3777exit:
3778 psa_reset_key_attributes( &attributes );
3779 psa_destroy_key( key );
3780 PSA_DONE( );
3781}
3782/* END_CASE */
3783
3784/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003785void asymmetric_encrypt( int key_type_arg,
3786 data_t *key_data,
3787 int alg_arg,
3788 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003789 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003790 int expected_output_length_arg,
3791 int expected_status_arg )
3792{
Ronald Cron5425a212020-08-04 14:58:35 +02003793 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003794 psa_key_type_t key_type = key_type_arg;
3795 psa_algorithm_t alg = alg_arg;
3796 size_t expected_output_length = expected_output_length_arg;
3797 size_t key_bits;
3798 unsigned char *output = NULL;
3799 size_t output_size;
3800 size_t output_length = ~0;
3801 psa_status_t actual_status;
3802 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003803 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003804
Gilles Peskine8817f612018-12-18 00:18:46 +01003805 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003806
Gilles Peskine656896e2018-06-29 19:12:28 +02003807 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003808 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3809 psa_set_key_algorithm( &attributes, alg );
3810 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003811 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003812 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003813
3814 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003815 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003816 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003817
Gilles Peskine656896e2018-06-29 19:12:28 +02003818 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003819 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003820 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003821
3822 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003823 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003824 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003825 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003826 output, output_size,
3827 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003828 TEST_EQUAL( actual_status, expected_status );
3829 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003830
Gilles Peskine68428122018-06-30 18:42:41 +02003831 /* If the label is empty, the test framework puts a non-null pointer
3832 * in label->x. Test that a null pointer works as well. */
3833 if( label->len == 0 )
3834 {
3835 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003836 if( output_size != 0 )
3837 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003838 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003839 input_data->x, input_data->len,
3840 NULL, label->len,
3841 output, output_size,
3842 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003843 TEST_EQUAL( actual_status, expected_status );
3844 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003845 }
3846
Gilles Peskine656896e2018-06-29 19:12:28 +02003847exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003848 /*
3849 * Key attributes may have been returned by psa_get_key_attributes()
3850 * thus reset them as required.
3851 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003852 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003853
Ronald Cron5425a212020-08-04 14:58:35 +02003854 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02003855 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003856 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003857}
3858/* END_CASE */
3859
3860/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003861void asymmetric_encrypt_decrypt( int key_type_arg,
3862 data_t *key_data,
3863 int alg_arg,
3864 data_t *input_data,
3865 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003866{
Ronald Cron5425a212020-08-04 14:58:35 +02003867 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003868 psa_key_type_t key_type = key_type_arg;
3869 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003870 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003871 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003872 size_t output_size;
3873 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003874 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003875 size_t output2_size;
3876 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003877 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003878
Gilles Peskine8817f612018-12-18 00:18:46 +01003879 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003880
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003881 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3882 psa_set_key_algorithm( &attributes, alg );
3883 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003884
Gilles Peskine049c7532019-05-15 20:22:09 +02003885 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003886 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003887
3888 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02003889 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003890 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003891
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003892 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003893 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003894 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01003895
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003896 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01003897 TEST_ASSERT( output2_size <=
3898 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
3899 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003900 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003901
Gilles Peskineeebd7382018-06-08 18:11:54 +02003902 /* We test encryption by checking that encrypt-then-decrypt gives back
3903 * the original plaintext because of the non-optional random
3904 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02003905 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003906 input_data->x, input_data->len,
3907 label->x, label->len,
3908 output, output_size,
3909 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003910 /* We don't know what ciphertext length to expect, but check that
3911 * it looks sensible. */
3912 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003913
Ronald Cron5425a212020-08-04 14:58:35 +02003914 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003915 output, output_length,
3916 label->x, label->len,
3917 output2, output2_size,
3918 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003919 ASSERT_COMPARE( input_data->x, input_data->len,
3920 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003921
3922exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003923 /*
3924 * Key attributes may have been returned by psa_get_key_attributes()
3925 * thus reset them as required.
3926 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003927 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003928
Ronald Cron5425a212020-08-04 14:58:35 +02003929 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003930 mbedtls_free( output );
3931 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003932 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003933}
3934/* END_CASE */
3935
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003936/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003937void asymmetric_decrypt( int key_type_arg,
3938 data_t *key_data,
3939 int alg_arg,
3940 data_t *input_data,
3941 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003942 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003943{
Ronald Cron5425a212020-08-04 14:58:35 +02003944 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003945 psa_key_type_t key_type = key_type_arg;
3946 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01003947 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003948 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003949 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003950 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003951 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003952
Gilles Peskine8817f612018-12-18 00:18:46 +01003953 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003954
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003955 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3956 psa_set_key_algorithm( &attributes, alg );
3957 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003958
Gilles Peskine049c7532019-05-15 20:22:09 +02003959 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003960 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003961
gabor-mezei-armceface22021-01-21 12:26:17 +01003962 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3963 key_bits = psa_get_key_bits( &attributes );
3964
3965 /* Determine the maximum ciphertext length */
3966 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
3967 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
3968 ASSERT_ALLOC( output, output_size );
3969
Ronald Cron5425a212020-08-04 14:58:35 +02003970 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003971 input_data->x, input_data->len,
3972 label->x, label->len,
3973 output,
3974 output_size,
3975 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003976 ASSERT_COMPARE( expected_data->x, expected_data->len,
3977 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003978
Gilles Peskine68428122018-06-30 18:42:41 +02003979 /* If the label is empty, the test framework puts a non-null pointer
3980 * in label->x. Test that a null pointer works as well. */
3981 if( label->len == 0 )
3982 {
3983 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003984 if( output_size != 0 )
3985 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003986 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003987 input_data->x, input_data->len,
3988 NULL, label->len,
3989 output,
3990 output_size,
3991 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003992 ASSERT_COMPARE( expected_data->x, expected_data->len,
3993 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003994 }
3995
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003996exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003997 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003998 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003999 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004000 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004001}
4002/* END_CASE */
4003
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004004/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004005void asymmetric_decrypt_fail( int key_type_arg,
4006 data_t *key_data,
4007 int alg_arg,
4008 data_t *input_data,
4009 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004010 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004011 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004012{
Ronald Cron5425a212020-08-04 14:58:35 +02004013 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004014 psa_key_type_t key_type = key_type_arg;
4015 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004016 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004017 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004018 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004019 psa_status_t actual_status;
4020 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004021 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004022
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004023 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004024
Gilles Peskine8817f612018-12-18 00:18:46 +01004025 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004026
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004027 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4028 psa_set_key_algorithm( &attributes, alg );
4029 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004030
Gilles Peskine049c7532019-05-15 20:22:09 +02004031 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004032 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004033
Ronald Cron5425a212020-08-04 14:58:35 +02004034 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004035 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004036 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004037 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004038 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004039 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004040 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004041
Gilles Peskine68428122018-06-30 18:42:41 +02004042 /* If the label is empty, the test framework puts a non-null pointer
4043 * in label->x. Test that a null pointer works as well. */
4044 if( label->len == 0 )
4045 {
4046 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004047 if( output_size != 0 )
4048 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004049 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004050 input_data->x, input_data->len,
4051 NULL, label->len,
4052 output, output_size,
4053 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004054 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004055 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004056 }
4057
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004058exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004059 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004060 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004061 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004062 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004063}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004064/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004065
4066/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004067void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004068{
4069 /* Test each valid way of initializing the object, except for `= {0}`, as
4070 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4071 * though it's OK by the C standard. We could test for this, but we'd need
4072 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004073 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004074 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4075 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4076 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004077
4078 memset( &zero, 0, sizeof( zero ) );
4079
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004080 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004081 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004082 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004083 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004084 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004085 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004086 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004087
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004088 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004089 PSA_ASSERT( psa_key_derivation_abort(&func) );
4090 PSA_ASSERT( psa_key_derivation_abort(&init) );
4091 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004092}
4093/* END_CASE */
4094
Janos Follath16de4a42019-06-13 16:32:24 +01004095/* BEGIN_CASE */
4096void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004097{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004098 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004099 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004100 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004101
Gilles Peskine8817f612018-12-18 00:18:46 +01004102 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004103
Janos Follath16de4a42019-06-13 16:32:24 +01004104 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004105 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004106
4107exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004108 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004109 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004110}
4111/* END_CASE */
4112
Janos Follathaf3c2a02019-06-12 12:34:34 +01004113/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004114void derive_set_capacity( int alg_arg, int capacity_arg,
4115 int expected_status_arg )
4116{
4117 psa_algorithm_t alg = alg_arg;
4118 size_t capacity = capacity_arg;
4119 psa_status_t expected_status = expected_status_arg;
4120 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4121
4122 PSA_ASSERT( psa_crypto_init( ) );
4123
4124 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4125
4126 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4127 expected_status );
4128
4129exit:
4130 psa_key_derivation_abort( &operation );
4131 PSA_DONE( );
4132}
4133/* END_CASE */
4134
4135/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004136void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004137 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004138 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004139 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004140 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004141 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004142 int expected_status_arg3,
4143 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004144{
4145 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004146 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4147 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004148 psa_status_t expected_statuses[] = {expected_status_arg1,
4149 expected_status_arg2,
4150 expected_status_arg3};
4151 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004152 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4153 MBEDTLS_SVC_KEY_ID_INIT,
4154 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004155 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4156 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4157 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004158 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004159 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004160 psa_status_t expected_output_status = expected_output_status_arg;
4161 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004162
4163 PSA_ASSERT( psa_crypto_init( ) );
4164
4165 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4166 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004167
4168 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4169
4170 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4171 {
Gilles Peskinef6279312021-05-27 13:21:20 +02004172 mbedtls_test_set_step( i );
4173 if( steps[i] == 0 )
4174 {
4175 /* Skip this step */
4176 }
4177 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004178 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004179 psa_set_key_type( &attributes, key_types[i] );
4180 PSA_ASSERT( psa_import_key( &attributes,
4181 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004182 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004183 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4184 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4185 {
4186 // When taking a private key as secret input, use key agreement
4187 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004188 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4189 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004190 expected_statuses[i] );
4191 }
4192 else
4193 {
4194 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004195 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004196 expected_statuses[i] );
4197 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004198 }
4199 else
4200 {
4201 TEST_EQUAL( psa_key_derivation_input_bytes(
4202 &operation, steps[i],
4203 inputs[i]->x, inputs[i]->len ),
4204 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004205 }
4206 }
4207
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004208 if( output_key_type != PSA_KEY_TYPE_NONE )
4209 {
4210 psa_reset_key_attributes( &attributes );
4211 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4212 psa_set_key_bits( &attributes, 8 );
4213 actual_output_status =
4214 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004215 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004216 }
4217 else
4218 {
4219 uint8_t buffer[1];
4220 actual_output_status =
4221 psa_key_derivation_output_bytes( &operation,
4222 buffer, sizeof( buffer ) );
4223 }
4224 TEST_EQUAL( actual_output_status, expected_output_status );
4225
Janos Follathaf3c2a02019-06-12 12:34:34 +01004226exit:
4227 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004228 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4229 psa_destroy_key( keys[i] );
4230 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004231 PSA_DONE( );
4232}
4233/* END_CASE */
4234
Janos Follathd958bb72019-07-03 15:02:16 +01004235/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004236void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004237{
Janos Follathd958bb72019-07-03 15:02:16 +01004238 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004239 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004240 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004241 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004242 unsigned char input1[] = "Input 1";
4243 size_t input1_length = sizeof( input1 );
4244 unsigned char input2[] = "Input 2";
4245 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004246 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004247 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004248 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4249 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4250 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004251 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004252
Gilles Peskine8817f612018-12-18 00:18:46 +01004253 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004254
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004255 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4256 psa_set_key_algorithm( &attributes, alg );
4257 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004258
Gilles Peskine73676cb2019-05-15 20:15:10 +02004259 PSA_ASSERT( psa_import_key( &attributes,
4260 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004261 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004262
4263 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004264 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4265 input1, input1_length,
4266 input2, input2_length,
4267 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004268 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004269
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004270 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004271 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004272 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004273
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004274 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004275
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004276 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004277 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004278
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004279exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004280 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004281 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004282 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004283}
4284/* END_CASE */
4285
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004286/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004287void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004288{
4289 uint8_t output_buffer[16];
4290 size_t buffer_size = 16;
4291 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004292 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004293
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004294 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4295 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004296 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004297
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004298 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004299 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004300
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004301 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004302
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004303 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4304 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004305 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004306
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004307 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004308 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004309
4310exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004311 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004312}
4313/* END_CASE */
4314
4315/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004316void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004317 int step1_arg, data_t *input1,
4318 int step2_arg, data_t *input2,
4319 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004320 int requested_capacity_arg,
4321 data_t *expected_output1,
4322 data_t *expected_output2 )
4323{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004324 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004325 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4326 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004327 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4328 MBEDTLS_SVC_KEY_ID_INIT,
4329 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004330 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004331 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004332 uint8_t *expected_outputs[2] =
4333 {expected_output1->x, expected_output2->x};
4334 size_t output_sizes[2] =
4335 {expected_output1->len, expected_output2->len};
4336 size_t output_buffer_size = 0;
4337 uint8_t *output_buffer = NULL;
4338 size_t expected_capacity;
4339 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004340 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004341 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004342 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004343
4344 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4345 {
4346 if( output_sizes[i] > output_buffer_size )
4347 output_buffer_size = output_sizes[i];
4348 if( output_sizes[i] == 0 )
4349 expected_outputs[i] = NULL;
4350 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004351 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004352 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004353
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004354 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4355 psa_set_key_algorithm( &attributes, alg );
4356 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004357
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004358 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004359 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4360 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4361 requested_capacity ) );
4362 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004363 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004364 switch( steps[i] )
4365 {
4366 case 0:
4367 break;
4368 case PSA_KEY_DERIVATION_INPUT_SECRET:
4369 PSA_ASSERT( psa_import_key( &attributes,
4370 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004371 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004372
4373 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4374 {
4375 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4376 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4377 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4378 }
4379
Gilles Peskine1468da72019-05-29 17:35:49 +02004380 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004381 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004382 break;
4383 default:
4384 PSA_ASSERT( psa_key_derivation_input_bytes(
4385 &operation, steps[i],
4386 inputs[i]->x, inputs[i]->len ) );
4387 break;
4388 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004389 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004390
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004391 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004392 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004393 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004394 expected_capacity = requested_capacity;
4395
4396 /* Expansion phase. */
4397 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4398 {
4399 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004400 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004401 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004402 if( expected_capacity == 0 && output_sizes[i] == 0 )
4403 {
4404 /* Reading 0 bytes when 0 bytes are available can go either way. */
4405 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004406 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004407 continue;
4408 }
4409 else if( expected_capacity == 0 ||
4410 output_sizes[i] > expected_capacity )
4411 {
4412 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004413 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004414 expected_capacity = 0;
4415 continue;
4416 }
4417 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004418 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004419 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004420 ASSERT_COMPARE( output_buffer, output_sizes[i],
4421 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004422 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004423 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004424 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004425 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004426 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004427 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004428 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004429
4430exit:
4431 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004432 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004433 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4434 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004435 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004436}
4437/* END_CASE */
4438
4439/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004440void derive_full( int alg_arg,
4441 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004442 data_t *input1,
4443 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004444 int requested_capacity_arg )
4445{
Ronald Cron5425a212020-08-04 14:58:35 +02004446 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004447 psa_algorithm_t alg = alg_arg;
4448 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004449 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004450 unsigned char output_buffer[16];
4451 size_t expected_capacity = requested_capacity;
4452 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004453 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004454
Gilles Peskine8817f612018-12-18 00:18:46 +01004455 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004456
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004457 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4458 psa_set_key_algorithm( &attributes, alg );
4459 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004460
Gilles Peskine049c7532019-05-15 20:22:09 +02004461 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004462 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004463
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004464 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4465 input1->x, input1->len,
4466 input2->x, input2->len,
4467 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004468 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004469
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004470 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004471 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004472 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004473
4474 /* Expansion phase. */
4475 while( current_capacity > 0 )
4476 {
4477 size_t read_size = sizeof( output_buffer );
4478 if( read_size > current_capacity )
4479 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004480 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004481 output_buffer,
4482 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004483 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004484 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004485 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004486 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004487 }
4488
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004489 /* Check that the operation refuses to go over capacity. */
4490 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004491 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004492
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004493 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004494
4495exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004496 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004497 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004498 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004499}
4500/* END_CASE */
4501
Janos Follathe60c9052019-07-03 13:51:30 +01004502/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004503void derive_key_exercise( int alg_arg,
4504 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004505 data_t *input1,
4506 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004507 int derived_type_arg,
4508 int derived_bits_arg,
4509 int derived_usage_arg,
4510 int derived_alg_arg )
4511{
Ronald Cron5425a212020-08-04 14:58:35 +02004512 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4513 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004514 psa_algorithm_t alg = alg_arg;
4515 psa_key_type_t derived_type = derived_type_arg;
4516 size_t derived_bits = derived_bits_arg;
4517 psa_key_usage_t derived_usage = derived_usage_arg;
4518 psa_algorithm_t derived_alg = derived_alg_arg;
4519 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004520 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004521 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004522 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004523
Gilles Peskine8817f612018-12-18 00:18:46 +01004524 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004525
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004526 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4527 psa_set_key_algorithm( &attributes, alg );
4528 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004529 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004530 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004531
4532 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004533 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4534 input1->x, input1->len,
4535 input2->x, input2->len,
4536 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004537 goto exit;
4538
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004539 psa_set_key_usage_flags( &attributes, derived_usage );
4540 psa_set_key_algorithm( &attributes, derived_alg );
4541 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004542 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004543 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004544 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004545
4546 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004547 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004548 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4549 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004550
4551 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004552 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004553 goto exit;
4554
4555exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004556 /*
4557 * Key attributes may have been returned by psa_get_key_attributes()
4558 * thus reset them as required.
4559 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004560 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004561
4562 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004563 psa_destroy_key( base_key );
4564 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004565 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004566}
4567/* END_CASE */
4568
Janos Follath42fd8882019-07-03 14:17:09 +01004569/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004570void derive_key_export( int alg_arg,
4571 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004572 data_t *input1,
4573 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004574 int bytes1_arg,
4575 int bytes2_arg )
4576{
Ronald Cron5425a212020-08-04 14:58:35 +02004577 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4578 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004579 psa_algorithm_t alg = alg_arg;
4580 size_t bytes1 = bytes1_arg;
4581 size_t bytes2 = bytes2_arg;
4582 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004583 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004584 uint8_t *output_buffer = NULL;
4585 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004586 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4587 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004588 size_t length;
4589
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004590 ASSERT_ALLOC( output_buffer, capacity );
4591 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004592 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004593
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004594 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4595 psa_set_key_algorithm( &base_attributes, alg );
4596 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004597 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004598 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004599
4600 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004601 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4602 input1->x, input1->len,
4603 input2->x, input2->len,
4604 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004605 goto exit;
4606
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004607 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004608 output_buffer,
4609 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004610 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004611
4612 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004613 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4614 input1->x, input1->len,
4615 input2->x, input2->len,
4616 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004617 goto exit;
4618
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004619 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4620 psa_set_key_algorithm( &derived_attributes, 0 );
4621 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004622 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004623 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004624 &derived_key ) );
4625 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004626 export_buffer, bytes1,
4627 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004628 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004629 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004630 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004631 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004632 &derived_key ) );
4633 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004634 export_buffer + bytes1, bytes2,
4635 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004636 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004637
4638 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004639 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4640 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004641
4642exit:
4643 mbedtls_free( output_buffer );
4644 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004645 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004646 psa_destroy_key( base_key );
4647 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004648 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004649}
4650/* END_CASE */
4651
4652/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004653void derive_key( int alg_arg,
4654 data_t *key_data, data_t *input1, data_t *input2,
4655 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004656 int expected_status_arg,
4657 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004658{
Ronald Cron5425a212020-08-04 14:58:35 +02004659 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4660 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004661 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004662 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004663 size_t bits = bits_arg;
4664 psa_status_t expected_status = expected_status_arg;
4665 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4666 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4667 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4668
4669 PSA_ASSERT( psa_crypto_init( ) );
4670
4671 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4672 psa_set_key_algorithm( &base_attributes, alg );
4673 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4674 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004675 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004676
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004677 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4678 input1->x, input1->len,
4679 input2->x, input2->len,
4680 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004681 goto exit;
4682
4683 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4684 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004685 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004686 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004687
4688 psa_status_t status =
4689 psa_key_derivation_output_key( &derived_attributes,
4690 &operation,
4691 &derived_key );
4692 if( is_large_output > 0 )
4693 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4694 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004695
4696exit:
4697 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004698 psa_destroy_key( base_key );
4699 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004700 PSA_DONE( );
4701}
4702/* END_CASE */
4703
4704/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004705void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004706 int our_key_type_arg, int our_key_alg_arg,
4707 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004708 int expected_status_arg )
4709{
Ronald Cron5425a212020-08-04 14:58:35 +02004710 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004711 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004712 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004713 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004714 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004715 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004716 psa_status_t expected_status = expected_status_arg;
4717 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004718
Gilles Peskine8817f612018-12-18 00:18:46 +01004719 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004720
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004721 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004722 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004723 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004724 PSA_ASSERT( psa_import_key( &attributes,
4725 our_key_data->x, our_key_data->len,
4726 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004727
Gilles Peskine77f40d82019-04-11 21:27:06 +02004728 /* The tests currently include inputs that should fail at either step.
4729 * Test cases that fail at the setup step should be changed to call
4730 * key_derivation_setup instead, and this function should be renamed
4731 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004732 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004733 if( status == PSA_SUCCESS )
4734 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004735 TEST_EQUAL( psa_key_derivation_key_agreement(
4736 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4737 our_key,
4738 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004739 expected_status );
4740 }
4741 else
4742 {
4743 TEST_ASSERT( status == expected_status );
4744 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004745
4746exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004747 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004748 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004749 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004750}
4751/* END_CASE */
4752
4753/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004754void raw_key_agreement( int alg_arg,
4755 int our_key_type_arg, data_t *our_key_data,
4756 data_t *peer_key_data,
4757 data_t *expected_output )
4758{
Ronald Cron5425a212020-08-04 14:58:35 +02004759 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004760 psa_algorithm_t alg = alg_arg;
4761 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004762 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004763 unsigned char *output = NULL;
4764 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004765 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004766
4767 ASSERT_ALLOC( output, expected_output->len );
4768 PSA_ASSERT( psa_crypto_init( ) );
4769
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004770 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4771 psa_set_key_algorithm( &attributes, alg );
4772 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004773 PSA_ASSERT( psa_import_key( &attributes,
4774 our_key_data->x, our_key_data->len,
4775 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004776
gabor-mezei-armceface22021-01-21 12:26:17 +01004777 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4778 key_bits = psa_get_key_bits( &attributes );
4779
Gilles Peskinebe697d82019-05-16 18:00:41 +02004780 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4781 peer_key_data->x, peer_key_data->len,
4782 output, expected_output->len,
4783 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004784 ASSERT_COMPARE( output, output_length,
4785 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01004786 TEST_ASSERT( output_length <=
4787 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
4788 TEST_ASSERT( output_length <=
4789 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004790
4791exit:
4792 mbedtls_free( output );
4793 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004794 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004795}
4796/* END_CASE */
4797
4798/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004799void key_agreement_capacity( int alg_arg,
4800 int our_key_type_arg, data_t *our_key_data,
4801 data_t *peer_key_data,
4802 int expected_capacity_arg )
4803{
Ronald Cron5425a212020-08-04 14:58:35 +02004804 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004805 psa_algorithm_t alg = alg_arg;
4806 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004807 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004808 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004809 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004810 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004811
Gilles Peskine8817f612018-12-18 00:18:46 +01004812 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004813
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004814 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4815 psa_set_key_algorithm( &attributes, alg );
4816 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004817 PSA_ASSERT( psa_import_key( &attributes,
4818 our_key_data->x, our_key_data->len,
4819 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004820
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004821 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004822 PSA_ASSERT( psa_key_derivation_key_agreement(
4823 &operation,
4824 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4825 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004826 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4827 {
4828 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004829 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004830 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004831 NULL, 0 ) );
4832 }
Gilles Peskine59685592018-09-18 12:11:34 +02004833
Gilles Peskinebf491972018-10-25 22:36:12 +02004834 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004835 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004836 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004837 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004838
Gilles Peskinebf491972018-10-25 22:36:12 +02004839 /* Test the actual capacity by reading the output. */
4840 while( actual_capacity > sizeof( output ) )
4841 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004842 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004843 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004844 actual_capacity -= sizeof( output );
4845 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004846 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004847 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004848 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004849 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004850
Gilles Peskine59685592018-09-18 12:11:34 +02004851exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004852 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004853 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004854 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004855}
4856/* END_CASE */
4857
4858/* BEGIN_CASE */
4859void key_agreement_output( int alg_arg,
4860 int our_key_type_arg, data_t *our_key_data,
4861 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004862 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004863{
Ronald Cron5425a212020-08-04 14:58:35 +02004864 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004865 psa_algorithm_t alg = alg_arg;
4866 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004867 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004868 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004869 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004870
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004871 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4872 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004873
Gilles Peskine8817f612018-12-18 00:18:46 +01004874 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004875
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004876 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4877 psa_set_key_algorithm( &attributes, alg );
4878 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004879 PSA_ASSERT( psa_import_key( &attributes,
4880 our_key_data->x, our_key_data->len,
4881 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004882
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004883 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004884 PSA_ASSERT( psa_key_derivation_key_agreement(
4885 &operation,
4886 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4887 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004888 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4889 {
4890 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004891 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004892 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004893 NULL, 0 ) );
4894 }
Gilles Peskine59685592018-09-18 12:11:34 +02004895
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004896 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004897 actual_output,
4898 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004899 ASSERT_COMPARE( actual_output, expected_output1->len,
4900 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004901 if( expected_output2->len != 0 )
4902 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004903 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004904 actual_output,
4905 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004906 ASSERT_COMPARE( actual_output, expected_output2->len,
4907 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004908 }
Gilles Peskine59685592018-09-18 12:11:34 +02004909
4910exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004911 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004912 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004913 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004914 mbedtls_free( actual_output );
4915}
4916/* END_CASE */
4917
4918/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004919void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004920{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004921 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004922 unsigned char *output = NULL;
4923 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004924 size_t i;
4925 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004926
Simon Butcher49f8e312020-03-03 15:51:50 +00004927 TEST_ASSERT( bytes_arg >= 0 );
4928
Gilles Peskine91892022021-02-08 19:50:26 +01004929 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004930 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02004931
Gilles Peskine8817f612018-12-18 00:18:46 +01004932 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004933
Gilles Peskinea50d7392018-06-21 10:22:13 +02004934 /* Run several times, to ensure that every output byte will be
4935 * nonzero at least once with overwhelming probability
4936 * (2^(-8*number_of_runs)). */
4937 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004938 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004939 if( bytes != 0 )
4940 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004941 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004942
Gilles Peskinea50d7392018-06-21 10:22:13 +02004943 for( i = 0; i < bytes; i++ )
4944 {
4945 if( output[i] != 0 )
4946 ++changed[i];
4947 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004948 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004949
4950 /* Check that every byte was changed to nonzero at least once. This
4951 * validates that psa_generate_random is overwriting every byte of
4952 * the output buffer. */
4953 for( i = 0; i < bytes; i++ )
4954 {
4955 TEST_ASSERT( changed[i] != 0 );
4956 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004957
4958exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004959 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004960 mbedtls_free( output );
4961 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004962}
4963/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004964
4965/* BEGIN_CASE */
4966void generate_key( int type_arg,
4967 int bits_arg,
4968 int usage_arg,
4969 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004970 int expected_status_arg,
4971 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004972{
Ronald Cron5425a212020-08-04 14:58:35 +02004973 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004974 psa_key_type_t type = type_arg;
4975 psa_key_usage_t usage = usage_arg;
4976 size_t bits = bits_arg;
4977 psa_algorithm_t alg = alg_arg;
4978 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004979 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004980 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004981
Gilles Peskine8817f612018-12-18 00:18:46 +01004982 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004983
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004984 psa_set_key_usage_flags( &attributes, usage );
4985 psa_set_key_algorithm( &attributes, alg );
4986 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004987 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004988
4989 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01004990 psa_status_t status = psa_generate_key( &attributes, &key );
4991
4992 if( is_large_key > 0 )
4993 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4994 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004995 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004996 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004997
4998 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004999 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005000 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5001 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005002
Gilles Peskine818ca122018-06-20 18:16:48 +02005003 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005004 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005005 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005006
5007exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005008 /*
5009 * Key attributes may have been returned by psa_get_key_attributes()
5010 * thus reset them as required.
5011 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005012 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005013
Ronald Cron5425a212020-08-04 14:58:35 +02005014 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005015 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005016}
5017/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005018
Ronald Cronee414c72021-03-18 18:50:08 +01005019/* 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 +02005020void generate_key_rsa( int bits_arg,
5021 data_t *e_arg,
5022 int expected_status_arg )
5023{
Ronald Cron5425a212020-08-04 14:58:35 +02005024 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005025 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005026 size_t bits = bits_arg;
5027 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5028 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5029 psa_status_t expected_status = expected_status_arg;
5030 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5031 uint8_t *exported = NULL;
5032 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005033 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005034 size_t exported_length = SIZE_MAX;
5035 uint8_t *e_read_buffer = NULL;
5036 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005037 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005038 size_t e_read_length = SIZE_MAX;
5039
5040 if( e_arg->len == 0 ||
5041 ( e_arg->len == 3 &&
5042 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5043 {
5044 is_default_public_exponent = 1;
5045 e_read_size = 0;
5046 }
5047 ASSERT_ALLOC( e_read_buffer, e_read_size );
5048 ASSERT_ALLOC( exported, exported_size );
5049
5050 PSA_ASSERT( psa_crypto_init( ) );
5051
5052 psa_set_key_usage_flags( &attributes, usage );
5053 psa_set_key_algorithm( &attributes, alg );
5054 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5055 e_arg->x, e_arg->len ) );
5056 psa_set_key_bits( &attributes, bits );
5057
5058 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005059 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005060 if( expected_status != PSA_SUCCESS )
5061 goto exit;
5062
5063 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005064 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005065 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5066 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5067 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5068 e_read_buffer, e_read_size,
5069 &e_read_length ) );
5070 if( is_default_public_exponent )
5071 TEST_EQUAL( e_read_length, 0 );
5072 else
5073 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5074
5075 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005076 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005077 goto exit;
5078
5079 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005080 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005081 exported, exported_size,
5082 &exported_length ) );
5083 {
5084 uint8_t *p = exported;
5085 uint8_t *end = exported + exported_length;
5086 size_t len;
5087 /* RSAPublicKey ::= SEQUENCE {
5088 * modulus INTEGER, -- n
5089 * publicExponent INTEGER } -- e
5090 */
5091 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005092 MBEDTLS_ASN1_SEQUENCE |
5093 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005094 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005095 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5096 MBEDTLS_ASN1_INTEGER ) );
5097 if( len >= 1 && p[0] == 0 )
5098 {
5099 ++p;
5100 --len;
5101 }
5102 if( e_arg->len == 0 )
5103 {
5104 TEST_EQUAL( len, 3 );
5105 TEST_EQUAL( p[0], 1 );
5106 TEST_EQUAL( p[1], 0 );
5107 TEST_EQUAL( p[2], 1 );
5108 }
5109 else
5110 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5111 }
5112
5113exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005114 /*
5115 * Key attributes may have been returned by psa_get_key_attributes() or
5116 * set by psa_set_key_domain_parameters() thus reset them as required.
5117 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005118 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005119
Ronald Cron5425a212020-08-04 14:58:35 +02005120 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005121 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005122 mbedtls_free( e_read_buffer );
5123 mbedtls_free( exported );
5124}
5125/* END_CASE */
5126
Darryl Greend49a4992018-06-18 17:27:26 +01005127/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005128void persistent_key_load_key_from_storage( data_t *data,
5129 int type_arg, int bits_arg,
5130 int usage_flags_arg, int alg_arg,
5131 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005132{
Ronald Cron71016a92020-08-28 19:01:50 +02005133 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005134 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005135 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5136 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005137 psa_key_type_t type = type_arg;
5138 size_t bits = bits_arg;
5139 psa_key_usage_t usage_flags = usage_flags_arg;
5140 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005141 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005142 unsigned char *first_export = NULL;
5143 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005144 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005145 size_t first_exported_length;
5146 size_t second_exported_length;
5147
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005148 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5149 {
5150 ASSERT_ALLOC( first_export, export_size );
5151 ASSERT_ALLOC( second_export, export_size );
5152 }
Darryl Greend49a4992018-06-18 17:27:26 +01005153
Gilles Peskine8817f612018-12-18 00:18:46 +01005154 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005155
Gilles Peskinec87af662019-05-15 16:12:22 +02005156 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005157 psa_set_key_usage_flags( &attributes, usage_flags );
5158 psa_set_key_algorithm( &attributes, alg );
5159 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005160 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005161
Darryl Green0c6575a2018-11-07 16:05:30 +00005162 switch( generation_method )
5163 {
5164 case IMPORT_KEY:
5165 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005166 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005167 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005168 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005169
Darryl Green0c6575a2018-11-07 16:05:30 +00005170 case GENERATE_KEY:
5171 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005172 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005173 break;
5174
5175 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005176#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005177 {
5178 /* Create base key */
5179 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5180 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5181 psa_set_key_usage_flags( &base_attributes,
5182 PSA_KEY_USAGE_DERIVE );
5183 psa_set_key_algorithm( &base_attributes, derive_alg );
5184 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005185 PSA_ASSERT( psa_import_key( &base_attributes,
5186 data->x, data->len,
5187 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005188 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005189 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005190 PSA_ASSERT( psa_key_derivation_input_key(
5191 &operation,
5192 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005193 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005194 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005195 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005196 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5197 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005198 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005199 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005200 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005201 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005202 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005203#else
5204 TEST_ASSUME( ! "KDF not supported in this configuration" );
5205#endif
5206 break;
5207
5208 default:
5209 TEST_ASSERT( ! "generation_method not implemented in test" );
5210 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005211 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005212 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005213
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005214 /* Export the key if permitted by the key policy. */
5215 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5216 {
Ronald Cron5425a212020-08-04 14:58:35 +02005217 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005218 first_export, export_size,
5219 &first_exported_length ) );
5220 if( generation_method == IMPORT_KEY )
5221 ASSERT_COMPARE( data->x, data->len,
5222 first_export, first_exported_length );
5223 }
Darryl Greend49a4992018-06-18 17:27:26 +01005224
5225 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005226 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005227 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005228 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005229
Darryl Greend49a4992018-06-18 17:27:26 +01005230 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005231 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005232 TEST_ASSERT( mbedtls_svc_key_id_equal(
5233 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005234 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5235 PSA_KEY_LIFETIME_PERSISTENT );
5236 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5237 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5238 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5239 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005240
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005241 /* Export the key again if permitted by the key policy. */
5242 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005243 {
Ronald Cron5425a212020-08-04 14:58:35 +02005244 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005245 second_export, export_size,
5246 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005247 ASSERT_COMPARE( first_export, first_exported_length,
5248 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005249 }
5250
5251 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005252 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005253 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005254
5255exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005256 /*
5257 * Key attributes may have been returned by psa_get_key_attributes()
5258 * thus reset them as required.
5259 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005260 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005261
Darryl Greend49a4992018-06-18 17:27:26 +01005262 mbedtls_free( first_export );
5263 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005264 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005265 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005266 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005267 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005268}
5269/* END_CASE */