blob: 35b7f708084e0295fc1ec9e4e9d6fc9ce178da30 [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 );
gabor-mezei-arm4d9009e2021-05-13 12:05:01 +0200381 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
382 update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200383 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200384 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200385
Ronald Cron5425a212020-08-04 14:58:35 +0200386 PSA_ASSERT( psa_destroy_key( key ) );
387 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200388
389exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100390 /*
391 * Key attributes may have been returned by psa_get_key_attributes()
392 * thus reset them as required.
393 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200394 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100395
396 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200397 PSA_DONE( );
398}
399/* END_CASE */
400
401/* BEGIN_CASE */
402void import_with_data( data_t *data, int type_arg,
403 int attr_bits_arg,
404 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200405{
406 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
407 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200408 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200409 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200410 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200411 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100412 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100413
Gilles Peskine8817f612018-12-18 00:18:46 +0100414 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100415
Gilles Peskine4747d192019-04-17 15:05:45 +0200416 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200417 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200418
Ronald Cron5425a212020-08-04 14:58:35 +0200419 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100420 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200421 if( status != PSA_SUCCESS )
422 goto exit;
423
Ronald Cron5425a212020-08-04 14:58:35 +0200424 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200425 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200426 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200427 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200428 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200429
Ronald Cron5425a212020-08-04 14:58:35 +0200430 PSA_ASSERT( psa_destroy_key( key ) );
431 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100432
433exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100434 /*
435 * Key attributes may have been returned by psa_get_key_attributes()
436 * thus reset them as required.
437 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200438 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100439
440 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200441 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100442}
443/* END_CASE */
444
445/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200446void import_large_key( int type_arg, int byte_size_arg,
447 int expected_status_arg )
448{
449 psa_key_type_t type = type_arg;
450 size_t byte_size = byte_size_arg;
451 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
452 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200453 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200454 psa_status_t status;
455 uint8_t *buffer = NULL;
456 size_t buffer_size = byte_size + 1;
457 size_t n;
458
Steven Cooreman69967ce2021-01-18 18:01:08 +0100459 /* Skip the test case if the target running the test cannot
460 * accomodate large keys due to heap size constraints */
461 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200462 memset( buffer, 'K', byte_size );
463
464 PSA_ASSERT( psa_crypto_init( ) );
465
466 /* Try importing the key */
467 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
468 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200469 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100470 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200471 TEST_EQUAL( status, expected_status );
472
473 if( status == PSA_SUCCESS )
474 {
Ronald Cron5425a212020-08-04 14:58:35 +0200475 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200476 TEST_EQUAL( psa_get_key_type( &attributes ), type );
477 TEST_EQUAL( psa_get_key_bits( &attributes ),
478 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200479 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200480 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200481 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200482 for( n = 0; n < byte_size; n++ )
483 TEST_EQUAL( buffer[n], 'K' );
484 for( n = byte_size; n < buffer_size; n++ )
485 TEST_EQUAL( buffer[n], 0 );
486 }
487
488exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100489 /*
490 * Key attributes may have been returned by psa_get_key_attributes()
491 * thus reset them as required.
492 */
493 psa_reset_key_attributes( &attributes );
494
Ronald Cron5425a212020-08-04 14:58:35 +0200495 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200496 PSA_DONE( );
497 mbedtls_free( buffer );
498}
499/* END_CASE */
500
501/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200502void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
503{
Ronald Cron5425a212020-08-04 14:58:35 +0200504 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200505 size_t bits = bits_arg;
506 psa_status_t expected_status = expected_status_arg;
507 psa_status_t status;
508 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200509 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200510 size_t buffer_size = /* Slight overapproximations */
511 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200512 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200513 unsigned char *p;
514 int ret;
515 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200516 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200517
Gilles Peskine8817f612018-12-18 00:18:46 +0100518 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200519 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200520
521 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
522 bits, keypair ) ) >= 0 );
523 length = ret;
524
525 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200526 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200527 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100528 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200529
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200530 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200531 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200532
533exit:
534 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200535 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200536}
537/* END_CASE */
538
539/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300540void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300541 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200542 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100543 int expected_bits,
544 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200545 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100546 int canonical_input )
547{
Ronald Cron5425a212020-08-04 14:58:35 +0200548 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100549 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200550 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200551 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100552 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100553 unsigned char *exported = NULL;
554 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100555 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100556 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100557 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200558 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200559 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100560
Moran Pekercb088e72018-07-17 17:36:59 +0300561 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200562 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100563 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200564 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100565 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100566
Gilles Peskine4747d192019-04-17 15:05:45 +0200567 psa_set_key_usage_flags( &attributes, usage_arg );
568 psa_set_key_algorithm( &attributes, alg );
569 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700570
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100571 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200572 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100573
574 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200575 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200576 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
577 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200578 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100579
580 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200581 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100582 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100583
584 /* The exported length must be set by psa_export_key() to a value between 0
585 * and export_size. On errors, the exported length must be 0. */
586 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
587 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
588 TEST_ASSERT( exported_length <= export_size );
589
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200590 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200591 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100592 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200593 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100594 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100595 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200596 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100597
Gilles Peskineea38a922021-02-13 00:05:16 +0100598 /* Run sanity checks on the exported key. For non-canonical inputs,
599 * this validates the canonical representations. For canonical inputs,
600 * this doesn't directly validate the implementation, but it still helps
601 * by cross-validating the test data with the sanity check code. */
602 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200603 goto exit;
604
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100605 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200606 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100607 else
608 {
Ronald Cron5425a212020-08-04 14:58:35 +0200609 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200610 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200611 &key2 ) );
612 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100613 reexported,
614 export_size,
615 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200616 ASSERT_COMPARE( exported, exported_length,
617 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200618 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100619 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100620 TEST_ASSERT( exported_length <=
621 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
622 psa_get_key_bits( &got_attributes ) ) );
623 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100624
625destroy:
626 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200627 PSA_ASSERT( psa_destroy_key( key ) );
628 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100629
630exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100631 /*
632 * Key attributes may have been returned by psa_get_key_attributes()
633 * thus reset them as required.
634 */
635 psa_reset_key_attributes( &got_attributes );
636
itayzafrir3e02b3b2018-06-12 17:06:52 +0300637 mbedtls_free( exported );
638 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200639 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100640}
641/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100642
Moran Pekerf709f4a2018-06-06 17:26:04 +0300643/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300644void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200645 int type_arg,
646 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100647 int export_size_delta,
648 int expected_export_status_arg,
649 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300650{
Ronald Cron5425a212020-08-04 14:58:35 +0200651 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300652 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200653 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200654 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300655 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300656 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100657 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100658 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200659 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300660
Gilles Peskine8817f612018-12-18 00:18:46 +0100661 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300662
Gilles Peskine4747d192019-04-17 15:05:45 +0200663 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
664 psa_set_key_algorithm( &attributes, alg );
665 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300666
667 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200668 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300669
Gilles Peskine49c25912018-10-29 15:15:31 +0100670 /* Export the public key */
671 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200672 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200673 exported, export_size,
674 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100675 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100676 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100677 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200678 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100679 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200680 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200681 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100682 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100683 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100684 TEST_ASSERT( expected_public_key->len <=
685 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
686 TEST_ASSERT( expected_public_key->len <=
687 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100688 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
689 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100690 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300691
692exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100693 /*
694 * Key attributes may have been returned by psa_get_key_attributes()
695 * thus reset them as required.
696 */
697 psa_reset_key_attributes( &attributes );
698
itayzafrir3e02b3b2018-06-12 17:06:52 +0300699 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200700 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200701 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300702}
703/* END_CASE */
704
Gilles Peskine20035e32018-02-03 22:44:14 +0100705/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200706void import_and_exercise_key( data_t *data,
707 int type_arg,
708 int bits_arg,
709 int alg_arg )
710{
Ronald Cron5425a212020-08-04 14:58:35 +0200711 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200712 psa_key_type_t type = type_arg;
713 size_t bits = bits_arg;
714 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100715 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200716 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200717 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200718
Gilles Peskine8817f612018-12-18 00:18:46 +0100719 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200720
Gilles Peskine4747d192019-04-17 15:05:45 +0200721 psa_set_key_usage_flags( &attributes, usage );
722 psa_set_key_algorithm( &attributes, alg );
723 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200724
725 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200726 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200727
728 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200729 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200730 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
731 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200732
733 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100734 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200735 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200736
Ronald Cron5425a212020-08-04 14:58:35 +0200737 PSA_ASSERT( psa_destroy_key( key ) );
738 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200739
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200740exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100741 /*
742 * Key attributes may have been returned by psa_get_key_attributes()
743 * thus reset them as required.
744 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200745 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100746
747 psa_reset_key_attributes( &attributes );
748 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200749 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200750}
751/* END_CASE */
752
753/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100754void effective_key_attributes( int type_arg, int expected_type_arg,
755 int bits_arg, int expected_bits_arg,
756 int usage_arg, int expected_usage_arg,
757 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200758{
Ronald Cron5425a212020-08-04 14:58:35 +0200759 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100760 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100761 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100762 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100763 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200764 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100765 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200766 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100767 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200768 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200769
Gilles Peskine8817f612018-12-18 00:18:46 +0100770 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200771
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200772 psa_set_key_usage_flags( &attributes, usage );
773 psa_set_key_algorithm( &attributes, alg );
774 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +0100775 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +0200776
Ronald Cron5425a212020-08-04 14:58:35 +0200777 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +0100778 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +0200779
Ronald Cron5425a212020-08-04 14:58:35 +0200780 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +0100781 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
782 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
783 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
784 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200785
786exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100787 /*
788 * Key attributes may have been returned by psa_get_key_attributes()
789 * thus reset them as required.
790 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200791 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100792
793 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200794 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +0200795}
796/* END_CASE */
797
798/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100799void check_key_policy( int type_arg, int bits_arg,
800 int usage_arg, int alg_arg )
801{
802 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
803 usage_arg, usage_arg, alg_arg, alg_arg );
804 goto exit;
805}
806/* END_CASE */
807
808/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200809void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +0000810{
811 /* Test each valid way of initializing the object, except for `= {0}`, as
812 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
813 * though it's OK by the C standard. We could test for this, but we'd need
814 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200815 psa_key_attributes_t func = psa_key_attributes_init( );
816 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
817 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000818
819 memset( &zero, 0, sizeof( zero ) );
820
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200821 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
822 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
823 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000824
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200825 TEST_EQUAL( psa_get_key_type( &func ), 0 );
826 TEST_EQUAL( psa_get_key_type( &init ), 0 );
827 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
828
829 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
830 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
831 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
832
833 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
834 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
835 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
836
837 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
838 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
839 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +0000840}
841/* END_CASE */
842
843/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200844void mac_key_policy( int policy_usage,
845 int policy_alg,
846 int key_type,
847 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100848 int exercise_alg,
849 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200850{
Ronald Cron5425a212020-08-04 14:58:35 +0200851 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200852 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000853 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200854 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100855 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200856 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200857
Gilles Peskine8817f612018-12-18 00:18:46 +0100858 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200859
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200860 psa_set_key_usage_flags( &attributes, policy_usage );
861 psa_set_key_algorithm( &attributes, policy_alg );
862 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200863
Gilles Peskine049c7532019-05-15 20:22:09 +0200864 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200865 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200866
Ronald Cron5425a212020-08-04 14:58:35 +0200867 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100868 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100869 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100870 else
871 TEST_EQUAL( status, expected_status );
872
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200873 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200874
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200875 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200876 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100877 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100878 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100879 else
880 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200881
882exit:
883 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200884 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200885 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200886}
887/* END_CASE */
888
889/* BEGIN_CASE */
890void cipher_key_policy( int policy_usage,
891 int policy_alg,
892 int key_type,
893 data_t *key_data,
894 int exercise_alg )
895{
Ronald Cron5425a212020-08-04 14:58:35 +0200896 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200897 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000898 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200899 psa_status_t status;
900
Gilles Peskine8817f612018-12-18 00:18:46 +0100901 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200902
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200903 psa_set_key_usage_flags( &attributes, policy_usage );
904 psa_set_key_algorithm( &attributes, policy_alg );
905 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200906
Gilles Peskine049c7532019-05-15 20:22:09 +0200907 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200908 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200909
Ronald Cron5425a212020-08-04 14:58:35 +0200910 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200911 if( policy_alg == exercise_alg &&
912 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100913 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200914 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100915 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200916 psa_cipher_abort( &operation );
917
Ronald Cron5425a212020-08-04 14:58:35 +0200918 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200919 if( policy_alg == exercise_alg &&
920 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100921 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200922 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100923 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200924
925exit:
926 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200927 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200928 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200929}
930/* END_CASE */
931
932/* BEGIN_CASE */
933void aead_key_policy( int policy_usage,
934 int policy_alg,
935 int key_type,
936 data_t *key_data,
937 int nonce_length_arg,
938 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100939 int exercise_alg,
940 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200941{
Ronald Cron5425a212020-08-04 14:58:35 +0200942 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200943 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200944 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100945 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200946 unsigned char nonce[16] = {0};
947 size_t nonce_length = nonce_length_arg;
948 unsigned char tag[16];
949 size_t tag_length = tag_length_arg;
950 size_t output_length;
951
952 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
953 TEST_ASSERT( tag_length <= sizeof( tag ) );
954
Gilles Peskine8817f612018-12-18 00:18:46 +0100955 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200956
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200957 psa_set_key_usage_flags( &attributes, policy_usage );
958 psa_set_key_algorithm( &attributes, policy_alg );
959 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200960
Gilles Peskine049c7532019-05-15 20:22:09 +0200961 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200962 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200963
Ronald Cron5425a212020-08-04 14:58:35 +0200964 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200965 nonce, nonce_length,
966 NULL, 0,
967 NULL, 0,
968 tag, tag_length,
969 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100970 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
971 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200972 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100973 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200974
975 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200976 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200977 nonce, nonce_length,
978 NULL, 0,
979 tag, tag_length,
980 NULL, 0,
981 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100982 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
983 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
984 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100985 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200986 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100987 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200988
989exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200990 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200991 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200992}
993/* END_CASE */
994
995/* BEGIN_CASE */
996void asymmetric_encryption_key_policy( int policy_usage,
997 int policy_alg,
998 int key_type,
999 data_t *key_data,
1000 int exercise_alg )
1001{
Ronald Cron5425a212020-08-04 14:58:35 +02001002 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001003 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001004 psa_status_t status;
1005 size_t key_bits;
1006 size_t buffer_length;
1007 unsigned char *buffer = NULL;
1008 size_t output_length;
1009
Gilles Peskine8817f612018-12-18 00:18:46 +01001010 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001011
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001012 psa_set_key_usage_flags( &attributes, policy_usage );
1013 psa_set_key_algorithm( &attributes, policy_alg );
1014 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001015
Gilles Peskine049c7532019-05-15 20:22:09 +02001016 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001017 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001018
Ronald Cron5425a212020-08-04 14:58:35 +02001019 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001020 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001021 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1022 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001023 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001024
Ronald Cron5425a212020-08-04 14:58:35 +02001025 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001026 NULL, 0,
1027 NULL, 0,
1028 buffer, buffer_length,
1029 &output_length );
1030 if( policy_alg == exercise_alg &&
1031 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001032 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001033 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001034 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001035
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001036 if( buffer_length != 0 )
1037 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001038 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001039 buffer, buffer_length,
1040 NULL, 0,
1041 buffer, buffer_length,
1042 &output_length );
1043 if( policy_alg == exercise_alg &&
1044 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001045 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001046 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001047 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001048
1049exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001050 /*
1051 * Key attributes may have been returned by psa_get_key_attributes()
1052 * thus reset them as required.
1053 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001054 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001055
1056 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001057 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001058 mbedtls_free( buffer );
1059}
1060/* END_CASE */
1061
1062/* BEGIN_CASE */
1063void asymmetric_signature_key_policy( int policy_usage,
1064 int policy_alg,
1065 int key_type,
1066 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001067 int exercise_alg,
1068 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001069{
Ronald Cron5425a212020-08-04 14:58:35 +02001070 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001071 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001072 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001073 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1074 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1075 * compatible with the policy and `payload_length_arg` is supposed to be
1076 * a valid input length to sign. If `payload_length_arg <= 0`,
1077 * `exercise_alg` is supposed to be forbidden by the policy. */
1078 int compatible_alg = payload_length_arg > 0;
1079 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001080 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001081 size_t signature_length;
1082
Gilles Peskine8817f612018-12-18 00:18:46 +01001083 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001084
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001085 psa_set_key_usage_flags( &attributes, policy_usage );
1086 psa_set_key_algorithm( &attributes, policy_alg );
1087 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001088
Gilles Peskine049c7532019-05-15 20:22:09 +02001089 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001090 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001091
Ronald Cron5425a212020-08-04 14:58:35 +02001092 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001093 payload, payload_length,
1094 signature, sizeof( signature ),
1095 &signature_length );
1096 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001097 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001098 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001099 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001100
1101 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001102 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001103 payload, payload_length,
1104 signature, sizeof( signature ) );
1105 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001106 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001107 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001108 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001109
1110exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001111 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001112 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001113}
1114/* END_CASE */
1115
Janos Follathba3fab92019-06-11 14:50:16 +01001116/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001117void derive_key_policy( int policy_usage,
1118 int policy_alg,
1119 int key_type,
1120 data_t *key_data,
1121 int exercise_alg )
1122{
Ronald Cron5425a212020-08-04 14:58:35 +02001123 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001124 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001125 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001126 psa_status_t status;
1127
Gilles Peskine8817f612018-12-18 00:18:46 +01001128 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001129
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001130 psa_set_key_usage_flags( &attributes, policy_usage );
1131 psa_set_key_algorithm( &attributes, policy_alg );
1132 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001133
Gilles Peskine049c7532019-05-15 20:22:09 +02001134 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001135 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001136
Janos Follathba3fab92019-06-11 14:50:16 +01001137 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1138
1139 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1140 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001141 {
Janos Follathba3fab92019-06-11 14:50:16 +01001142 PSA_ASSERT( psa_key_derivation_input_bytes(
1143 &operation,
1144 PSA_KEY_DERIVATION_INPUT_SEED,
1145 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001146 }
Janos Follathba3fab92019-06-11 14:50:16 +01001147
1148 status = psa_key_derivation_input_key( &operation,
1149 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001150 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001151
Gilles Peskineea0fb492018-07-12 17:17:20 +02001152 if( policy_alg == exercise_alg &&
1153 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001154 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001155 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001156 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001157
1158exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001159 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001160 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001161 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001162}
1163/* END_CASE */
1164
1165/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001166void agreement_key_policy( int policy_usage,
1167 int policy_alg,
1168 int key_type_arg,
1169 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001170 int exercise_alg,
1171 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001172{
Ronald Cron5425a212020-08-04 14:58:35 +02001173 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001174 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001175 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001176 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001177 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001178 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001179
Gilles Peskine8817f612018-12-18 00:18:46 +01001180 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001181
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001182 psa_set_key_usage_flags( &attributes, policy_usage );
1183 psa_set_key_algorithm( &attributes, policy_alg );
1184 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001185
Gilles Peskine049c7532019-05-15 20:22:09 +02001186 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001187 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001188
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001189 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001190 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001191
Steven Cooremance48e852020-10-05 16:02:45 +02001192 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001193
1194exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001195 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001196 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001197 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001198}
1199/* END_CASE */
1200
1201/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001202void key_policy_alg2( int key_type_arg, data_t *key_data,
1203 int usage_arg, int alg_arg, int alg2_arg )
1204{
Ronald Cron5425a212020-08-04 14:58:35 +02001205 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001206 psa_key_type_t key_type = key_type_arg;
1207 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1208 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1209 psa_key_usage_t usage = usage_arg;
1210 psa_algorithm_t alg = alg_arg;
1211 psa_algorithm_t alg2 = alg2_arg;
1212
1213 PSA_ASSERT( psa_crypto_init( ) );
1214
1215 psa_set_key_usage_flags( &attributes, usage );
1216 psa_set_key_algorithm( &attributes, alg );
1217 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1218 psa_set_key_type( &attributes, key_type );
1219 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001220 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001221
gabor-mezei-arm4d9009e2021-05-13 12:05:01 +02001222 /* Update the usage flags to obtain extended usage flags */
1223 usage = update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001224 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001225 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1226 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1227 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1228
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001229 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001230 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001231 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001232 goto exit;
1233
1234exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001235 /*
1236 * Key attributes may have been returned by psa_get_key_attributes()
1237 * thus reset them as required.
1238 */
1239 psa_reset_key_attributes( &got_attributes );
1240
Ronald Cron5425a212020-08-04 14:58:35 +02001241 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001242 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001243}
1244/* END_CASE */
1245
1246/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001247void raw_agreement_key_policy( int policy_usage,
1248 int policy_alg,
1249 int key_type_arg,
1250 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001251 int exercise_alg,
1252 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001253{
Ronald Cron5425a212020-08-04 14:58:35 +02001254 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001255 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001256 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001257 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001258 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001259 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001260
1261 PSA_ASSERT( psa_crypto_init( ) );
1262
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001263 psa_set_key_usage_flags( &attributes, policy_usage );
1264 psa_set_key_algorithm( &attributes, policy_alg );
1265 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001266
Gilles Peskine049c7532019-05-15 20:22:09 +02001267 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001268 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001269
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001270 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001271
Steven Cooremance48e852020-10-05 16:02:45 +02001272 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001273
1274exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001275 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001276 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001277 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001278}
1279/* END_CASE */
1280
1281/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001282void copy_success( int source_usage_arg,
1283 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001284 int type_arg, data_t *material,
1285 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001286 int target_usage_arg,
1287 int target_alg_arg, int target_alg2_arg,
1288 int expected_usage_arg,
1289 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001290{
Gilles Peskineca25db92019-04-19 11:43:08 +02001291 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1292 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001293 psa_key_usage_t expected_usage = expected_usage_arg;
1294 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001295 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001296 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1297 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001298 uint8_t *export_buffer = NULL;
1299
Gilles Peskine57ab7212019-01-28 13:03:09 +01001300 PSA_ASSERT( psa_crypto_init( ) );
1301
Gilles Peskineca25db92019-04-19 11:43:08 +02001302 /* Prepare the source key. */
1303 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1304 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001305 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001306 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001307 PSA_ASSERT( psa_import_key( &source_attributes,
1308 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001309 &source_key ) );
1310 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001311
Gilles Peskineca25db92019-04-19 11:43:08 +02001312 /* Prepare the target attributes. */
1313 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001314 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001315 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001316 /* Set volatile lifetime to reset the key identifier to 0. */
1317 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1318 }
1319
Gilles Peskineca25db92019-04-19 11:43:08 +02001320 if( target_usage_arg != -1 )
1321 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1322 if( target_alg_arg != -1 )
1323 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001324 if( target_alg2_arg != -1 )
1325 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001326
1327 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001328 PSA_ASSERT( psa_copy_key( source_key,
1329 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001330
1331 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001332 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001333
1334 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001335 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001336 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1337 psa_get_key_type( &target_attributes ) );
1338 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1339 psa_get_key_bits( &target_attributes ) );
1340 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1341 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001342 TEST_EQUAL( expected_alg2,
1343 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001344 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1345 {
1346 size_t length;
1347 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001348 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001349 material->len, &length ) );
1350 ASSERT_COMPARE( material->x, material->len,
1351 export_buffer, length );
1352 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001353
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001354 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001355 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001356 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001357 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001358
Ronald Cron5425a212020-08-04 14:58:35 +02001359 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001360
1361exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001362 /*
1363 * Source and target key attributes may have been returned by
1364 * psa_get_key_attributes() thus reset them as required.
1365 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001366 psa_reset_key_attributes( &source_attributes );
1367 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001368
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001369 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001370 mbedtls_free( export_buffer );
1371}
1372/* END_CASE */
1373
1374/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001375void copy_fail( int source_usage_arg,
1376 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001377 int type_arg, data_t *material,
1378 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001379 int target_usage_arg,
1380 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001381 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001382 int expected_status_arg )
1383{
1384 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1385 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001386 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1387 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001388 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001389
1390 PSA_ASSERT( psa_crypto_init( ) );
1391
1392 /* Prepare the source key. */
1393 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1394 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001395 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001396 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001397 PSA_ASSERT( psa_import_key( &source_attributes,
1398 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001399 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001400
1401 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001402 psa_set_key_id( &target_attributes, key_id );
1403 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001404 psa_set_key_type( &target_attributes, target_type_arg );
1405 psa_set_key_bits( &target_attributes, target_bits_arg );
1406 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1407 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001408 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001409
1410 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001411 TEST_EQUAL( psa_copy_key( source_key,
1412 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001413 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001414
Ronald Cron5425a212020-08-04 14:58:35 +02001415 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001416
Gilles Peskine4a644642019-05-03 17:14:08 +02001417exit:
1418 psa_reset_key_attributes( &source_attributes );
1419 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001420 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001421}
1422/* END_CASE */
1423
1424/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001425void hash_operation_init( )
1426{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001427 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001428 /* Test each valid way of initializing the object, except for `= {0}`, as
1429 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1430 * though it's OK by the C standard. We could test for this, but we'd need
1431 * to supress the Clang warning for the test. */
1432 psa_hash_operation_t func = psa_hash_operation_init( );
1433 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1434 psa_hash_operation_t zero;
1435
1436 memset( &zero, 0, sizeof( zero ) );
1437
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001438 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001439 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1440 PSA_ERROR_BAD_STATE );
1441 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1442 PSA_ERROR_BAD_STATE );
1443 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1444 PSA_ERROR_BAD_STATE );
1445
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001446 /* A default hash operation should be abortable without error. */
1447 PSA_ASSERT( psa_hash_abort( &func ) );
1448 PSA_ASSERT( psa_hash_abort( &init ) );
1449 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001450}
1451/* END_CASE */
1452
1453/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001454void hash_setup( int alg_arg,
1455 int expected_status_arg )
1456{
1457 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001458 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001459 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001460 psa_status_t status;
1461
Gilles Peskine8817f612018-12-18 00:18:46 +01001462 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001463
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001464 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001465 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001466
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001467 /* Whether setup succeeded or failed, abort must succeed. */
1468 PSA_ASSERT( psa_hash_abort( &operation ) );
1469
1470 /* If setup failed, reproduce the failure, so as to
1471 * test the resulting state of the operation object. */
1472 if( status != PSA_SUCCESS )
1473 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1474
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001475 /* Now the operation object should be reusable. */
1476#if defined(KNOWN_SUPPORTED_HASH_ALG)
1477 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1478 PSA_ASSERT( psa_hash_abort( &operation ) );
1479#endif
1480
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001481exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001482 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001483}
1484/* END_CASE */
1485
1486/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001487void hash_compute_fail( int alg_arg, data_t *input,
1488 int output_size_arg, int expected_status_arg )
1489{
1490 psa_algorithm_t alg = alg_arg;
1491 uint8_t *output = NULL;
1492 size_t output_size = output_size_arg;
1493 size_t output_length = INVALID_EXPORT_LENGTH;
1494 psa_status_t expected_status = expected_status_arg;
1495 psa_status_t status;
1496
1497 ASSERT_ALLOC( output, output_size );
1498
1499 PSA_ASSERT( psa_crypto_init( ) );
1500
1501 status = psa_hash_compute( alg, input->x, input->len,
1502 output, output_size, &output_length );
1503 TEST_EQUAL( status, expected_status );
1504 TEST_ASSERT( output_length <= output_size );
1505
1506exit:
1507 mbedtls_free( output );
1508 PSA_DONE( );
1509}
1510/* END_CASE */
1511
1512/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001513void hash_compare_fail( int alg_arg, data_t *input,
1514 data_t *reference_hash,
1515 int expected_status_arg )
1516{
1517 psa_algorithm_t alg = alg_arg;
1518 psa_status_t expected_status = expected_status_arg;
1519 psa_status_t status;
1520
1521 PSA_ASSERT( psa_crypto_init( ) );
1522
1523 status = psa_hash_compare( alg, input->x, input->len,
1524 reference_hash->x, reference_hash->len );
1525 TEST_EQUAL( status, expected_status );
1526
1527exit:
1528 PSA_DONE( );
1529}
1530/* END_CASE */
1531
1532/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001533void hash_compute_compare( int alg_arg, data_t *input,
1534 data_t *expected_output )
1535{
1536 psa_algorithm_t alg = alg_arg;
1537 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1538 size_t output_length = INVALID_EXPORT_LENGTH;
1539 size_t i;
1540
1541 PSA_ASSERT( psa_crypto_init( ) );
1542
1543 /* Compute with tight buffer */
1544 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001545 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001546 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001547 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001548 ASSERT_COMPARE( output, output_length,
1549 expected_output->x, expected_output->len );
1550
1551 /* Compute with larger buffer */
1552 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1553 output, sizeof( output ),
1554 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001555 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001556 ASSERT_COMPARE( output, output_length,
1557 expected_output->x, expected_output->len );
1558
1559 /* Compare with correct hash */
1560 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1561 output, output_length ) );
1562
1563 /* Compare with trailing garbage */
1564 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1565 output, output_length + 1 ),
1566 PSA_ERROR_INVALID_SIGNATURE );
1567
1568 /* Compare with truncated hash */
1569 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1570 output, output_length - 1 ),
1571 PSA_ERROR_INVALID_SIGNATURE );
1572
1573 /* Compare with corrupted value */
1574 for( i = 0; i < output_length; i++ )
1575 {
Chris Jones9634bb12021-01-20 15:56:42 +00001576 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001577 output[i] ^= 1;
1578 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1579 output, output_length ),
1580 PSA_ERROR_INVALID_SIGNATURE );
1581 output[i] ^= 1;
1582 }
1583
1584exit:
1585 PSA_DONE( );
1586}
1587/* END_CASE */
1588
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001589/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001590void hash_bad_order( )
1591{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001592 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001593 unsigned char input[] = "";
1594 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001595 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001596 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1597 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1598 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001599 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001600 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001601 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001602
Gilles Peskine8817f612018-12-18 00:18:46 +01001603 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001604
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001605 /* Call setup twice in a row. */
1606 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1607 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1608 PSA_ERROR_BAD_STATE );
1609 PSA_ASSERT( psa_hash_abort( &operation ) );
1610
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001611 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001612 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001613 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001614 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001615
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001616 /* Call update after finish. */
1617 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1618 PSA_ASSERT( psa_hash_finish( &operation,
1619 hash, sizeof( hash ), &hash_len ) );
1620 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001621 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001622 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001623
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001624 /* Call verify without calling setup beforehand. */
1625 TEST_EQUAL( psa_hash_verify( &operation,
1626 valid_hash, sizeof( valid_hash ) ),
1627 PSA_ERROR_BAD_STATE );
1628 PSA_ASSERT( psa_hash_abort( &operation ) );
1629
1630 /* Call verify after finish. */
1631 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1632 PSA_ASSERT( psa_hash_finish( &operation,
1633 hash, sizeof( hash ), &hash_len ) );
1634 TEST_EQUAL( psa_hash_verify( &operation,
1635 valid_hash, sizeof( valid_hash ) ),
1636 PSA_ERROR_BAD_STATE );
1637 PSA_ASSERT( psa_hash_abort( &operation ) );
1638
1639 /* Call verify twice in a row. */
1640 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1641 PSA_ASSERT( psa_hash_verify( &operation,
1642 valid_hash, sizeof( valid_hash ) ) );
1643 TEST_EQUAL( psa_hash_verify( &operation,
1644 valid_hash, sizeof( valid_hash ) ),
1645 PSA_ERROR_BAD_STATE );
1646 PSA_ASSERT( psa_hash_abort( &operation ) );
1647
1648 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001649 TEST_EQUAL( psa_hash_finish( &operation,
1650 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001651 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001652 PSA_ASSERT( psa_hash_abort( &operation ) );
1653
1654 /* Call finish twice in a row. */
1655 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1656 PSA_ASSERT( psa_hash_finish( &operation,
1657 hash, sizeof( hash ), &hash_len ) );
1658 TEST_EQUAL( psa_hash_finish( &operation,
1659 hash, sizeof( hash ), &hash_len ),
1660 PSA_ERROR_BAD_STATE );
1661 PSA_ASSERT( psa_hash_abort( &operation ) );
1662
1663 /* Call finish after calling verify. */
1664 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1665 PSA_ASSERT( psa_hash_verify( &operation,
1666 valid_hash, sizeof( valid_hash ) ) );
1667 TEST_EQUAL( psa_hash_finish( &operation,
1668 hash, sizeof( hash ), &hash_len ),
1669 PSA_ERROR_BAD_STATE );
1670 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001671
1672exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001673 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001674}
1675/* END_CASE */
1676
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001677/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001678void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001679{
1680 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001681 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1682 * appended to it */
1683 unsigned char hash[] = {
1684 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1685 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1686 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001687 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001688 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001689
Gilles Peskine8817f612018-12-18 00:18:46 +01001690 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001691
itayzafrir27e69452018-11-01 14:26:34 +02001692 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001693 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001694 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001695 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001696
itayzafrir27e69452018-11-01 14:26:34 +02001697 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001698 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001699 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001700 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001701
itayzafrir27e69452018-11-01 14:26:34 +02001702 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001703 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001704 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001705 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001706
itayzafrirec93d302018-10-18 18:01:10 +03001707exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001708 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001709}
1710/* END_CASE */
1711
Ronald Cronee414c72021-03-18 18:50:08 +01001712/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001713void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001714{
1715 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001716 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001717 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001718 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001719 size_t hash_len;
1720
Gilles Peskine8817f612018-12-18 00:18:46 +01001721 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001722
itayzafrir58028322018-10-25 10:22:01 +03001723 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001724 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001725 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001726 hash, expected_size - 1, &hash_len ),
1727 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001728
1729exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001730 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001731}
1732/* END_CASE */
1733
Ronald Cronee414c72021-03-18 18:50:08 +01001734/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001735void hash_clone_source_state( )
1736{
1737 psa_algorithm_t alg = PSA_ALG_SHA_256;
1738 unsigned char hash[PSA_HASH_MAX_SIZE];
1739 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1740 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1741 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1742 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1743 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1744 size_t hash_len;
1745
1746 PSA_ASSERT( psa_crypto_init( ) );
1747 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1748
1749 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1750 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1751 PSA_ASSERT( psa_hash_finish( &op_finished,
1752 hash, sizeof( hash ), &hash_len ) );
1753 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1754 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1755
1756 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1757 PSA_ERROR_BAD_STATE );
1758
1759 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1760 PSA_ASSERT( psa_hash_finish( &op_init,
1761 hash, sizeof( hash ), &hash_len ) );
1762 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1763 PSA_ASSERT( psa_hash_finish( &op_finished,
1764 hash, sizeof( hash ), &hash_len ) );
1765 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1766 PSA_ASSERT( psa_hash_finish( &op_aborted,
1767 hash, sizeof( hash ), &hash_len ) );
1768
1769exit:
1770 psa_hash_abort( &op_source );
1771 psa_hash_abort( &op_init );
1772 psa_hash_abort( &op_setup );
1773 psa_hash_abort( &op_finished );
1774 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001775 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001776}
1777/* END_CASE */
1778
Ronald Cronee414c72021-03-18 18:50:08 +01001779/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001780void hash_clone_target_state( )
1781{
1782 psa_algorithm_t alg = PSA_ALG_SHA_256;
1783 unsigned char hash[PSA_HASH_MAX_SIZE];
1784 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1785 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1786 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1787 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1788 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1789 size_t hash_len;
1790
1791 PSA_ASSERT( psa_crypto_init( ) );
1792
1793 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1794 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1795 PSA_ASSERT( psa_hash_finish( &op_finished,
1796 hash, sizeof( hash ), &hash_len ) );
1797 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1798 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1799
1800 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1801 PSA_ASSERT( psa_hash_finish( &op_target,
1802 hash, sizeof( hash ), &hash_len ) );
1803
1804 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1805 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1806 PSA_ERROR_BAD_STATE );
1807 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1808 PSA_ERROR_BAD_STATE );
1809
1810exit:
1811 psa_hash_abort( &op_target );
1812 psa_hash_abort( &op_init );
1813 psa_hash_abort( &op_setup );
1814 psa_hash_abort( &op_finished );
1815 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001816 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001817}
1818/* END_CASE */
1819
itayzafrir58028322018-10-25 10:22:01 +03001820/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001821void mac_operation_init( )
1822{
Jaeden Amero252ef282019-02-15 14:05:35 +00001823 const uint8_t input[1] = { 0 };
1824
Jaeden Amero769ce272019-01-04 11:48:03 +00001825 /* Test each valid way of initializing the object, except for `= {0}`, as
1826 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1827 * though it's OK by the C standard. We could test for this, but we'd need
1828 * to supress the Clang warning for the test. */
1829 psa_mac_operation_t func = psa_mac_operation_init( );
1830 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1831 psa_mac_operation_t zero;
1832
1833 memset( &zero, 0, sizeof( zero ) );
1834
Jaeden Amero252ef282019-02-15 14:05:35 +00001835 /* A freshly-initialized MAC operation should not be usable. */
1836 TEST_EQUAL( psa_mac_update( &func,
1837 input, sizeof( input ) ),
1838 PSA_ERROR_BAD_STATE );
1839 TEST_EQUAL( psa_mac_update( &init,
1840 input, sizeof( input ) ),
1841 PSA_ERROR_BAD_STATE );
1842 TEST_EQUAL( psa_mac_update( &zero,
1843 input, sizeof( input ) ),
1844 PSA_ERROR_BAD_STATE );
1845
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001846 /* A default MAC operation should be abortable without error. */
1847 PSA_ASSERT( psa_mac_abort( &func ) );
1848 PSA_ASSERT( psa_mac_abort( &init ) );
1849 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001850}
1851/* END_CASE */
1852
1853/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001854void mac_setup( int key_type_arg,
1855 data_t *key,
1856 int alg_arg,
1857 int expected_status_arg )
1858{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001859 psa_key_type_t key_type = key_type_arg;
1860 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001861 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001862 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001863 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1864#if defined(KNOWN_SUPPORTED_MAC_ALG)
1865 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1866#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001867
Gilles Peskine8817f612018-12-18 00:18:46 +01001868 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001869
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001870 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1871 &operation, &status ) )
1872 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001873 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001874
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001875 /* The operation object should be reusable. */
1876#if defined(KNOWN_SUPPORTED_MAC_ALG)
1877 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1878 smoke_test_key_data,
1879 sizeof( smoke_test_key_data ),
1880 KNOWN_SUPPORTED_MAC_ALG,
1881 &operation, &status ) )
1882 goto exit;
1883 TEST_EQUAL( status, PSA_SUCCESS );
1884#endif
1885
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001886exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001887 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001888}
1889/* END_CASE */
1890
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001891/* 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 +00001892void mac_bad_order( )
1893{
Ronald Cron5425a212020-08-04 14:58:35 +02001894 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001895 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1896 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001897 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001898 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1899 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1900 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001901 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001902 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1903 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1904 size_t sign_mac_length = 0;
1905 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1906 const uint8_t verify_mac[] = {
1907 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1908 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1909 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1910
1911 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001912 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001913 psa_set_key_algorithm( &attributes, alg );
1914 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001915
Ronald Cron5425a212020-08-04 14:58:35 +02001916 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1917 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001918
Jaeden Amero252ef282019-02-15 14:05:35 +00001919 /* Call update without calling setup beforehand. */
1920 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1921 PSA_ERROR_BAD_STATE );
1922 PSA_ASSERT( psa_mac_abort( &operation ) );
1923
1924 /* Call sign finish without calling setup beforehand. */
1925 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1926 &sign_mac_length),
1927 PSA_ERROR_BAD_STATE );
1928 PSA_ASSERT( psa_mac_abort( &operation ) );
1929
1930 /* Call verify finish without calling setup beforehand. */
1931 TEST_EQUAL( psa_mac_verify_finish( &operation,
1932 verify_mac, sizeof( verify_mac ) ),
1933 PSA_ERROR_BAD_STATE );
1934 PSA_ASSERT( psa_mac_abort( &operation ) );
1935
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001936 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001937 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
1938 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001939 PSA_ERROR_BAD_STATE );
1940 PSA_ASSERT( psa_mac_abort( &operation ) );
1941
Jaeden Amero252ef282019-02-15 14:05:35 +00001942 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001943 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001944 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1945 PSA_ASSERT( psa_mac_sign_finish( &operation,
1946 sign_mac, sizeof( sign_mac ),
1947 &sign_mac_length ) );
1948 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1949 PSA_ERROR_BAD_STATE );
1950 PSA_ASSERT( psa_mac_abort( &operation ) );
1951
1952 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001953 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001954 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1955 PSA_ASSERT( psa_mac_verify_finish( &operation,
1956 verify_mac, sizeof( verify_mac ) ) );
1957 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1958 PSA_ERROR_BAD_STATE );
1959 PSA_ASSERT( psa_mac_abort( &operation ) );
1960
1961 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001962 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001963 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1964 PSA_ASSERT( psa_mac_sign_finish( &operation,
1965 sign_mac, sizeof( sign_mac ),
1966 &sign_mac_length ) );
1967 TEST_EQUAL( psa_mac_sign_finish( &operation,
1968 sign_mac, sizeof( sign_mac ),
1969 &sign_mac_length ),
1970 PSA_ERROR_BAD_STATE );
1971 PSA_ASSERT( psa_mac_abort( &operation ) );
1972
1973 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001974 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001975 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1976 PSA_ASSERT( psa_mac_verify_finish( &operation,
1977 verify_mac, sizeof( verify_mac ) ) );
1978 TEST_EQUAL( psa_mac_verify_finish( &operation,
1979 verify_mac, sizeof( verify_mac ) ),
1980 PSA_ERROR_BAD_STATE );
1981 PSA_ASSERT( psa_mac_abort( &operation ) );
1982
1983 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02001984 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001985 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1986 TEST_EQUAL( psa_mac_verify_finish( &operation,
1987 verify_mac, sizeof( verify_mac ) ),
1988 PSA_ERROR_BAD_STATE );
1989 PSA_ASSERT( psa_mac_abort( &operation ) );
1990
1991 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02001992 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001993 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1994 TEST_EQUAL( psa_mac_sign_finish( &operation,
1995 sign_mac, sizeof( sign_mac ),
1996 &sign_mac_length ),
1997 PSA_ERROR_BAD_STATE );
1998 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001999
Ronald Cron5425a212020-08-04 14:58:35 +02002000 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002001
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002002exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002003 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002004}
2005/* END_CASE */
2006
2007/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002008void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002009 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002010 int alg_arg,
2011 data_t *input,
2012 data_t *expected_mac )
2013{
Ronald Cron5425a212020-08-04 14:58:35 +02002014 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002015 psa_key_type_t key_type = key_type_arg;
2016 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002017 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002018 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002019 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002020 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002021 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002022 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002023 const size_t output_sizes_to_test[] = {
2024 0,
2025 1,
2026 expected_mac->len - 1,
2027 expected_mac->len,
2028 expected_mac->len + 1,
2029 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002030
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002031 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002032 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002033 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002034
Gilles Peskine8817f612018-12-18 00:18:46 +01002035 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002036
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002037 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002038 psa_set_key_algorithm( &attributes, alg );
2039 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002040
Ronald Cron5425a212020-08-04 14:58:35 +02002041 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2042 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002043
Gilles Peskine8b356b52020-08-25 23:44:59 +02002044 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2045 {
2046 const size_t output_size = output_sizes_to_test[i];
2047 psa_status_t expected_status =
2048 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2049 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002050
Chris Jones9634bb12021-01-20 15:56:42 +00002051 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002052 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002053
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002054 /* Calculate the MAC, one-shot case. */
2055 TEST_EQUAL( psa_mac_compute( key, alg,
2056 input->x, input->len,
2057 actual_mac, output_size, &mac_length ),
2058 expected_status );
2059 if( expected_status == PSA_SUCCESS )
2060 {
2061 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2062 actual_mac, mac_length );
2063 }
2064
2065 if( output_size > 0 )
2066 memset( actual_mac, 0, output_size );
2067
2068 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002069 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002070 PSA_ASSERT( psa_mac_update( &operation,
2071 input->x, input->len ) );
2072 TEST_EQUAL( psa_mac_sign_finish( &operation,
2073 actual_mac, output_size,
2074 &mac_length ),
2075 expected_status );
2076 PSA_ASSERT( psa_mac_abort( &operation ) );
2077
2078 if( expected_status == PSA_SUCCESS )
2079 {
2080 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2081 actual_mac, mac_length );
2082 }
2083 mbedtls_free( actual_mac );
2084 actual_mac = NULL;
2085 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002086
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002087exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002088 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002089 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002090 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002091 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002092}
2093/* END_CASE */
2094
2095/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002096void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002097 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002098 int alg_arg,
2099 data_t *input,
2100 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002101{
Ronald Cron5425a212020-08-04 14:58:35 +02002102 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002103 psa_key_type_t key_type = key_type_arg;
2104 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002105 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002106 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002107 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002108
Gilles Peskine69c12672018-06-28 00:07:19 +02002109 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2110
Gilles Peskine8817f612018-12-18 00:18:46 +01002111 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002112
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002113 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002114 psa_set_key_algorithm( &attributes, alg );
2115 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002116
Ronald Cron5425a212020-08-04 14:58:35 +02002117 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2118 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002119
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002120 /* Verify correct MAC, one-shot case. */
2121 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2122 expected_mac->x, expected_mac->len ) );
2123
2124 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002125 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002126 PSA_ASSERT( psa_mac_update( &operation,
2127 input->x, input->len ) );
2128 PSA_ASSERT( psa_mac_verify_finish( &operation,
2129 expected_mac->x,
2130 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002131
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002132 /* Test a MAC that's too short, one-shot case. */
2133 TEST_EQUAL( psa_mac_verify( key, alg,
2134 input->x, input->len,
2135 expected_mac->x,
2136 expected_mac->len - 1 ),
2137 PSA_ERROR_INVALID_SIGNATURE );
2138
2139 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002140 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002141 PSA_ASSERT( psa_mac_update( &operation,
2142 input->x, input->len ) );
2143 TEST_EQUAL( psa_mac_verify_finish( &operation,
2144 expected_mac->x,
2145 expected_mac->len - 1 ),
2146 PSA_ERROR_INVALID_SIGNATURE );
2147
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002148 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002149 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2150 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002151 TEST_EQUAL( psa_mac_verify( key, alg,
2152 input->x, input->len,
2153 perturbed_mac, expected_mac->len + 1 ),
2154 PSA_ERROR_INVALID_SIGNATURE );
2155
2156 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002157 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002158 PSA_ASSERT( psa_mac_update( &operation,
2159 input->x, input->len ) );
2160 TEST_EQUAL( psa_mac_verify_finish( &operation,
2161 perturbed_mac,
2162 expected_mac->len + 1 ),
2163 PSA_ERROR_INVALID_SIGNATURE );
2164
2165 /* Test changing one byte. */
2166 for( size_t i = 0; i < expected_mac->len; i++ )
2167 {
Chris Jones9634bb12021-01-20 15:56:42 +00002168 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002169 perturbed_mac[i] ^= 1;
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002170
2171 TEST_EQUAL( psa_mac_verify( key, alg,
2172 input->x, input->len,
2173 perturbed_mac, expected_mac->len ),
2174 PSA_ERROR_INVALID_SIGNATURE );
2175
Ronald Cron5425a212020-08-04 14:58:35 +02002176 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002177 PSA_ASSERT( psa_mac_update( &operation,
2178 input->x, input->len ) );
2179 TEST_EQUAL( psa_mac_verify_finish( &operation,
2180 perturbed_mac,
2181 expected_mac->len ),
2182 PSA_ERROR_INVALID_SIGNATURE );
2183 perturbed_mac[i] ^= 1;
2184 }
2185
Gilles Peskine8c9def32018-02-08 10:02:12 +01002186exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002187 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002188 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002189 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002190 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002191}
2192/* END_CASE */
2193
2194/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002195void cipher_operation_init( )
2196{
Jaeden Ameroab439972019-02-15 14:12:05 +00002197 const uint8_t input[1] = { 0 };
2198 unsigned char output[1] = { 0 };
2199 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002200 /* Test each valid way of initializing the object, except for `= {0}`, as
2201 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2202 * though it's OK by the C standard. We could test for this, but we'd need
2203 * to supress the Clang warning for the test. */
2204 psa_cipher_operation_t func = psa_cipher_operation_init( );
2205 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2206 psa_cipher_operation_t zero;
2207
2208 memset( &zero, 0, sizeof( zero ) );
2209
Jaeden Ameroab439972019-02-15 14:12:05 +00002210 /* A freshly-initialized cipher operation should not be usable. */
2211 TEST_EQUAL( psa_cipher_update( &func,
2212 input, sizeof( input ),
2213 output, sizeof( output ),
2214 &output_length ),
2215 PSA_ERROR_BAD_STATE );
2216 TEST_EQUAL( psa_cipher_update( &init,
2217 input, sizeof( input ),
2218 output, sizeof( output ),
2219 &output_length ),
2220 PSA_ERROR_BAD_STATE );
2221 TEST_EQUAL( psa_cipher_update( &zero,
2222 input, sizeof( input ),
2223 output, sizeof( output ),
2224 &output_length ),
2225 PSA_ERROR_BAD_STATE );
2226
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002227 /* A default cipher operation should be abortable without error. */
2228 PSA_ASSERT( psa_cipher_abort( &func ) );
2229 PSA_ASSERT( psa_cipher_abort( &init ) );
2230 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002231}
2232/* END_CASE */
2233
2234/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002235void cipher_setup( int key_type_arg,
2236 data_t *key,
2237 int alg_arg,
2238 int expected_status_arg )
2239{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002240 psa_key_type_t key_type = key_type_arg;
2241 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002242 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002243 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002244 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002245#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002246 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2247#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002248
Gilles Peskine8817f612018-12-18 00:18:46 +01002249 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002250
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002251 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2252 &operation, &status ) )
2253 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002254 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002255
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002256 /* The operation object should be reusable. */
2257#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2258 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2259 smoke_test_key_data,
2260 sizeof( smoke_test_key_data ),
2261 KNOWN_SUPPORTED_CIPHER_ALG,
2262 &operation, &status ) )
2263 goto exit;
2264 TEST_EQUAL( status, PSA_SUCCESS );
2265#endif
2266
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002267exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002268 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002269 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002270}
2271/* END_CASE */
2272
Ronald Cronee414c72021-03-18 18:50:08 +01002273/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002274void cipher_bad_order( )
2275{
Ronald Cron5425a212020-08-04 14:58:35 +02002276 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002277 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2278 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002279 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002280 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002281 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002282 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002283 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2284 0xaa, 0xaa, 0xaa, 0xaa };
2285 const uint8_t text[] = {
2286 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2287 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002288 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002289 size_t length = 0;
2290
2291 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002292 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2293 psa_set_key_algorithm( &attributes, alg );
2294 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002295 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2296 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002297
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002298 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002299 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2300 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002301 PSA_ERROR_BAD_STATE );
2302 PSA_ASSERT( psa_cipher_abort( &operation ) );
2303
2304 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002305 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2306 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002307 PSA_ERROR_BAD_STATE );
2308 PSA_ASSERT( psa_cipher_abort( &operation ) );
2309
Jaeden Ameroab439972019-02-15 14:12:05 +00002310 /* Generate an IV without calling setup beforehand. */
2311 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2312 buffer, sizeof( buffer ),
2313 &length ),
2314 PSA_ERROR_BAD_STATE );
2315 PSA_ASSERT( psa_cipher_abort( &operation ) );
2316
2317 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002318 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002319 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2320 buffer, sizeof( buffer ),
2321 &length ) );
2322 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2323 buffer, sizeof( buffer ),
2324 &length ),
2325 PSA_ERROR_BAD_STATE );
2326 PSA_ASSERT( psa_cipher_abort( &operation ) );
2327
2328 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002329 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002330 PSA_ASSERT( psa_cipher_set_iv( &operation,
2331 iv, sizeof( iv ) ) );
2332 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2333 buffer, sizeof( buffer ),
2334 &length ),
2335 PSA_ERROR_BAD_STATE );
2336 PSA_ASSERT( psa_cipher_abort( &operation ) );
2337
2338 /* Set an IV without calling setup beforehand. */
2339 TEST_EQUAL( psa_cipher_set_iv( &operation,
2340 iv, sizeof( iv ) ),
2341 PSA_ERROR_BAD_STATE );
2342 PSA_ASSERT( psa_cipher_abort( &operation ) );
2343
2344 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002345 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002346 PSA_ASSERT( psa_cipher_set_iv( &operation,
2347 iv, sizeof( iv ) ) );
2348 TEST_EQUAL( psa_cipher_set_iv( &operation,
2349 iv, sizeof( iv ) ),
2350 PSA_ERROR_BAD_STATE );
2351 PSA_ASSERT( psa_cipher_abort( &operation ) );
2352
2353 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002354 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002355 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2356 buffer, sizeof( buffer ),
2357 &length ) );
2358 TEST_EQUAL( psa_cipher_set_iv( &operation,
2359 iv, sizeof( iv ) ),
2360 PSA_ERROR_BAD_STATE );
2361 PSA_ASSERT( psa_cipher_abort( &operation ) );
2362
2363 /* Call update without calling setup beforehand. */
2364 TEST_EQUAL( psa_cipher_update( &operation,
2365 text, sizeof( text ),
2366 buffer, sizeof( buffer ),
2367 &length ),
2368 PSA_ERROR_BAD_STATE );
2369 PSA_ASSERT( psa_cipher_abort( &operation ) );
2370
2371 /* Call update without an IV where an IV is required. */
2372 TEST_EQUAL( psa_cipher_update( &operation,
2373 text, sizeof( text ),
2374 buffer, sizeof( buffer ),
2375 &length ),
2376 PSA_ERROR_BAD_STATE );
2377 PSA_ASSERT( psa_cipher_abort( &operation ) );
2378
2379 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002380 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002381 PSA_ASSERT( psa_cipher_set_iv( &operation,
2382 iv, sizeof( iv ) ) );
2383 PSA_ASSERT( psa_cipher_finish( &operation,
2384 buffer, sizeof( buffer ), &length ) );
2385 TEST_EQUAL( psa_cipher_update( &operation,
2386 text, sizeof( text ),
2387 buffer, sizeof( buffer ),
2388 &length ),
2389 PSA_ERROR_BAD_STATE );
2390 PSA_ASSERT( psa_cipher_abort( &operation ) );
2391
2392 /* Call finish without calling setup beforehand. */
2393 TEST_EQUAL( psa_cipher_finish( &operation,
2394 buffer, sizeof( buffer ), &length ),
2395 PSA_ERROR_BAD_STATE );
2396 PSA_ASSERT( psa_cipher_abort( &operation ) );
2397
2398 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002399 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002400 /* Not calling update means we are encrypting an empty buffer, which is OK
2401 * for cipher modes with padding. */
2402 TEST_EQUAL( psa_cipher_finish( &operation,
2403 buffer, sizeof( buffer ), &length ),
2404 PSA_ERROR_BAD_STATE );
2405 PSA_ASSERT( psa_cipher_abort( &operation ) );
2406
2407 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002408 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002409 PSA_ASSERT( psa_cipher_set_iv( &operation,
2410 iv, sizeof( iv ) ) );
2411 PSA_ASSERT( psa_cipher_finish( &operation,
2412 buffer, sizeof( buffer ), &length ) );
2413 TEST_EQUAL( psa_cipher_finish( &operation,
2414 buffer, sizeof( buffer ), &length ),
2415 PSA_ERROR_BAD_STATE );
2416 PSA_ASSERT( psa_cipher_abort( &operation ) );
2417
Ronald Cron5425a212020-08-04 14:58:35 +02002418 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002419
Jaeden Ameroab439972019-02-15 14:12:05 +00002420exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002421 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002422 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002423}
2424/* END_CASE */
2425
2426/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002427void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002428 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002429 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002430 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002431{
Ronald Cron5425a212020-08-04 14:58:35 +02002432 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002433 psa_status_t status;
2434 psa_key_type_t key_type = key_type_arg;
2435 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002436 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002437 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002438 size_t output_buffer_size = 0;
2439 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002440 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002441 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002442 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002443
Gilles Peskine8817f612018-12-18 00:18:46 +01002444 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002445
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002446 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2447 psa_set_key_algorithm( &attributes, alg );
2448 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002449
Ronald Cron5425a212020-08-04 14:58:35 +02002450 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2451 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002452
Ronald Cron5425a212020-08-04 14:58:35 +02002453 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002454
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002455 if( iv->len > 0 )
2456 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002457 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002458 }
2459
gabor-mezei-armceface22021-01-21 12:26:17 +01002460 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2461 TEST_ASSERT( output_buffer_size <=
2462 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002463 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002464
Gilles Peskine8817f612018-12-18 00:18:46 +01002465 PSA_ASSERT( psa_cipher_update( &operation,
2466 input->x, input->len,
2467 output, output_buffer_size,
2468 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002469 TEST_ASSERT( function_output_length <=
2470 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2471 TEST_ASSERT( function_output_length <=
2472 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002473 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002474
Gilles Peskine50e586b2018-06-08 14:28:46 +02002475 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002476 ( output_buffer_size == 0 ? NULL :
2477 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002478 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002479 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002480 TEST_ASSERT( function_output_length <=
2481 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2482 TEST_ASSERT( function_output_length <=
2483 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002484 total_output_length += function_output_length;
2485
Gilles Peskinefe11b722018-12-18 00:24:04 +01002486 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002487 if( expected_status == PSA_SUCCESS )
2488 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002489 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002490 ASSERT_COMPARE( expected_output->x, expected_output->len,
2491 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002492 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002493
Gilles Peskine50e586b2018-06-08 14:28:46 +02002494exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002495 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002496 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002497 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002498 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002499}
2500/* END_CASE */
2501
2502/* BEGIN_CASE */
2503void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002504 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002505 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002506 int first_part_size_arg,
2507 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002508 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002509{
Ronald Cron5425a212020-08-04 14:58:35 +02002510 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002511 psa_key_type_t key_type = key_type_arg;
2512 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002513 size_t first_part_size = first_part_size_arg;
2514 size_t output1_length = output1_length_arg;
2515 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002516 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002517 size_t output_buffer_size = 0;
2518 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002519 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002520 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002521 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002522
Gilles Peskine8817f612018-12-18 00:18:46 +01002523 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002524
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002525 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2526 psa_set_key_algorithm( &attributes, alg );
2527 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002528
Ronald Cron5425a212020-08-04 14:58:35 +02002529 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2530 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002531
Ronald Cron5425a212020-08-04 14:58:35 +02002532 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002533
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002534 if( iv->len > 0 )
2535 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002536 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002537 }
2538
gabor-mezei-armceface22021-01-21 12:26:17 +01002539 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2540 TEST_ASSERT( output_buffer_size <=
2541 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002542 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002543
Gilles Peskinee0866522019-02-19 19:44:00 +01002544 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002545 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2546 output, output_buffer_size,
2547 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002548 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002549 TEST_ASSERT( function_output_length <=
2550 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2551 TEST_ASSERT( function_output_length <=
2552 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002553 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002554
Gilles Peskine8817f612018-12-18 00:18:46 +01002555 PSA_ASSERT( psa_cipher_update( &operation,
2556 input->x + first_part_size,
2557 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002558 ( output_buffer_size == 0 ? NULL :
2559 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002560 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002561 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002562 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002563 TEST_ASSERT( function_output_length <=
2564 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2565 alg,
2566 input->len - first_part_size ) );
2567 TEST_ASSERT( function_output_length <=
2568 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002569 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002570
Gilles Peskine8817f612018-12-18 00:18:46 +01002571 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002572 ( output_buffer_size == 0 ? NULL :
2573 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002574 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002575 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002576 TEST_ASSERT( function_output_length <=
2577 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2578 TEST_ASSERT( function_output_length <=
2579 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002580 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002581 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002582
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002583 ASSERT_COMPARE( expected_output->x, expected_output->len,
2584 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002585
2586exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002587 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002588 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002589 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002590 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002591}
2592/* END_CASE */
2593
2594/* BEGIN_CASE */
2595void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002596 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002597 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002598 int first_part_size_arg,
2599 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002600 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002601{
Ronald Cron5425a212020-08-04 14:58:35 +02002602 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002603 psa_key_type_t key_type = key_type_arg;
2604 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002605 size_t first_part_size = first_part_size_arg;
2606 size_t output1_length = output1_length_arg;
2607 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002608 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002609 size_t output_buffer_size = 0;
2610 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002611 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002612 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002613 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002614
Gilles Peskine8817f612018-12-18 00:18:46 +01002615 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002616
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002617 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2618 psa_set_key_algorithm( &attributes, alg );
2619 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002620
Ronald Cron5425a212020-08-04 14:58:35 +02002621 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2622 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002623
Ronald Cron5425a212020-08-04 14:58:35 +02002624 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002625
Steven Cooreman177deba2020-09-07 17:14:14 +02002626 if( iv->len > 0 )
2627 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002628 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002629 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002630
gabor-mezei-armceface22021-01-21 12:26:17 +01002631 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2632 TEST_ASSERT( output_buffer_size <=
2633 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002634 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002635
Gilles Peskinee0866522019-02-19 19:44:00 +01002636 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002637 PSA_ASSERT( psa_cipher_update( &operation,
2638 input->x, first_part_size,
2639 output, output_buffer_size,
2640 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002641 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002642 TEST_ASSERT( function_output_length <=
2643 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2644 TEST_ASSERT( function_output_length <=
2645 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002646 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002647
Gilles Peskine8817f612018-12-18 00:18:46 +01002648 PSA_ASSERT( psa_cipher_update( &operation,
2649 input->x + first_part_size,
2650 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002651 ( output_buffer_size == 0 ? NULL :
2652 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002653 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002654 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002655 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002656 TEST_ASSERT( function_output_length <=
2657 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2658 alg,
2659 input->len - first_part_size ) );
2660 TEST_ASSERT( function_output_length <=
2661 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002662 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002663
Gilles Peskine8817f612018-12-18 00:18:46 +01002664 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002665 ( output_buffer_size == 0 ? NULL :
2666 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002667 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002668 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002669 TEST_ASSERT( function_output_length <=
2670 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2671 TEST_ASSERT( function_output_length <=
2672 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002673 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002674 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002675
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002676 ASSERT_COMPARE( expected_output->x, expected_output->len,
2677 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002678
2679exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002680 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002681 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002682 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002683 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002684}
2685/* END_CASE */
2686
Gilles Peskine50e586b2018-06-08 14:28:46 +02002687/* BEGIN_CASE */
2688void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002689 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002690 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002691 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002692{
Ronald Cron5425a212020-08-04 14:58:35 +02002693 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002694 psa_status_t status;
2695 psa_key_type_t key_type = key_type_arg;
2696 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002697 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002698 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002699 size_t output_buffer_size = 0;
2700 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002701 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002702 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002703 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002704
Gilles Peskine8817f612018-12-18 00:18:46 +01002705 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002706
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002707 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2708 psa_set_key_algorithm( &attributes, alg );
2709 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002710
Ronald Cron5425a212020-08-04 14:58:35 +02002711 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2712 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002713
Ronald Cron5425a212020-08-04 14:58:35 +02002714 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002715
Steven Cooreman177deba2020-09-07 17:14:14 +02002716 if( iv->len > 0 )
2717 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002718 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002719 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002720
gabor-mezei-armceface22021-01-21 12:26:17 +01002721 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2722 TEST_ASSERT( output_buffer_size <=
2723 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002724 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002725
Gilles Peskine8817f612018-12-18 00:18:46 +01002726 PSA_ASSERT( psa_cipher_update( &operation,
2727 input->x, input->len,
2728 output, output_buffer_size,
2729 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002730 TEST_ASSERT( function_output_length <=
2731 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2732 TEST_ASSERT( function_output_length <=
2733 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002734 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002735
Gilles Peskine50e586b2018-06-08 14:28:46 +02002736 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002737 ( output_buffer_size == 0 ? NULL :
2738 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002739 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002740 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002741 TEST_ASSERT( function_output_length <=
2742 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2743 TEST_ASSERT( function_output_length <=
2744 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002745 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002746 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002747
2748 if( expected_status == PSA_SUCCESS )
2749 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002750 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002751 ASSERT_COMPARE( expected_output->x, expected_output->len,
2752 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002753 }
2754
Gilles Peskine50e586b2018-06-08 14:28:46 +02002755exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002756 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002757 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002758 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002759 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002760}
2761/* END_CASE */
2762
Gilles Peskine50e586b2018-06-08 14:28:46 +02002763/* BEGIN_CASE */
2764void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002765 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002766 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002767{
Ronald Cron5425a212020-08-04 14:58:35 +02002768 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002769 psa_key_type_t key_type = key_type_arg;
2770 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002771 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002772 size_t iv_size = 16;
2773 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002774 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002775 size_t output1_size = 0;
2776 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002777 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002778 size_t output2_size = 0;
2779 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002780 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002781 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2782 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002783 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002784
Gilles Peskine8817f612018-12-18 00:18:46 +01002785 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002786
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002787 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2788 psa_set_key_algorithm( &attributes, alg );
2789 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002790
Ronald Cron5425a212020-08-04 14:58:35 +02002791 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2792 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002793
Ronald Cron5425a212020-08-04 14:58:35 +02002794 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2795 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002796
Steven Cooreman177deba2020-09-07 17:14:14 +02002797 if( alg != PSA_ALG_ECB_NO_PADDING )
2798 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002799 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2800 iv, iv_size,
2801 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002802 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002803 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2804 TEST_ASSERT( output1_size <=
2805 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002806 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002807
Gilles Peskine8817f612018-12-18 00:18:46 +01002808 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2809 output1, output1_size,
2810 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002811 TEST_ASSERT( output1_length <=
2812 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2813 TEST_ASSERT( output1_length <=
2814 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2815
Gilles Peskine8817f612018-12-18 00:18:46 +01002816 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002817 output1 + output1_length,
2818 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002819 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002820 TEST_ASSERT( function_output_length <=
2821 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2822 TEST_ASSERT( function_output_length <=
2823 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002824
Gilles Peskine048b7f02018-06-08 14:20:49 +02002825 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002826
Gilles Peskine8817f612018-12-18 00:18:46 +01002827 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002828
2829 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002830 TEST_ASSERT( output2_size <=
2831 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2832 TEST_ASSERT( output2_size <=
2833 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002834 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002835
Steven Cooreman177deba2020-09-07 17:14:14 +02002836 if( iv_length > 0 )
2837 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002838 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2839 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002840 }
2841
Gilles Peskine8817f612018-12-18 00:18:46 +01002842 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2843 output2, output2_size,
2844 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002845 TEST_ASSERT( output2_length <=
2846 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
2847 TEST_ASSERT( output2_length <=
2848 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
2849
Gilles Peskine048b7f02018-06-08 14:20:49 +02002850 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002851 PSA_ASSERT( psa_cipher_finish( &operation2,
2852 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002853 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002854 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002855 TEST_ASSERT( function_output_length <=
2856 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2857 TEST_ASSERT( function_output_length <=
2858 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03002859
Gilles Peskine048b7f02018-06-08 14:20:49 +02002860 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002861
Gilles Peskine8817f612018-12-18 00:18:46 +01002862 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002863
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002864 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002865
2866exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002867 psa_cipher_abort( &operation1 );
2868 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002869 mbedtls_free( output1 );
2870 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002871 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002872 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03002873}
2874/* END_CASE */
2875
2876/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002877void cipher_verify_output_multipart( int alg_arg,
2878 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002879 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002880 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002881 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03002882{
Ronald Cron5425a212020-08-04 14:58:35 +02002883 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002884 psa_key_type_t key_type = key_type_arg;
2885 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002886 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002887 unsigned char iv[16] = {0};
2888 size_t iv_size = 16;
2889 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002890 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002891 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002892 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002893 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002894 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002895 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002896 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002897 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2898 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002899 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002900
Gilles Peskine8817f612018-12-18 00:18:46 +01002901 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002902
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002903 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2904 psa_set_key_algorithm( &attributes, alg );
2905 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002906
Ronald Cron5425a212020-08-04 14:58:35 +02002907 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2908 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03002909
Ronald Cron5425a212020-08-04 14:58:35 +02002910 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2911 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002912
Steven Cooreman177deba2020-09-07 17:14:14 +02002913 if( alg != PSA_ALG_ECB_NO_PADDING )
2914 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002915 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2916 iv, iv_size,
2917 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002918 }
2919
gabor-mezei-armceface22021-01-21 12:26:17 +01002920 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2921 TEST_ASSERT( output1_buffer_size <=
2922 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002923 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002924
Gilles Peskinee0866522019-02-19 19:44:00 +01002925 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002926
Gilles Peskine8817f612018-12-18 00:18:46 +01002927 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2928 output1, output1_buffer_size,
2929 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002930 TEST_ASSERT( function_output_length <=
2931 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2932 TEST_ASSERT( function_output_length <=
2933 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002934 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002935
Gilles Peskine8817f612018-12-18 00:18:46 +01002936 PSA_ASSERT( psa_cipher_update( &operation1,
2937 input->x + first_part_size,
2938 input->len - first_part_size,
2939 output1, output1_buffer_size,
2940 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002941 TEST_ASSERT( function_output_length <=
2942 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2943 alg,
2944 input->len - first_part_size ) );
2945 TEST_ASSERT( function_output_length <=
2946 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002947 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002948
Gilles Peskine8817f612018-12-18 00:18:46 +01002949 PSA_ASSERT( psa_cipher_finish( &operation1,
2950 output1 + output1_length,
2951 output1_buffer_size - output1_length,
2952 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002953 TEST_ASSERT( function_output_length <=
2954 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2955 TEST_ASSERT( function_output_length <=
2956 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002957 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002958
Gilles Peskine8817f612018-12-18 00:18:46 +01002959 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002960
Gilles Peskine048b7f02018-06-08 14:20:49 +02002961 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002962 TEST_ASSERT( output2_buffer_size <=
2963 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2964 TEST_ASSERT( output2_buffer_size <=
2965 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002966 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002967
Steven Cooreman177deba2020-09-07 17:14:14 +02002968 if( iv_length > 0 )
2969 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002970 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2971 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002972 }
Moran Pekerded84402018-06-06 16:36:50 +03002973
Gilles Peskine8817f612018-12-18 00:18:46 +01002974 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2975 output2, output2_buffer_size,
2976 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002977 TEST_ASSERT( function_output_length <=
2978 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2979 TEST_ASSERT( function_output_length <=
2980 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002981 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002982
Gilles Peskine8817f612018-12-18 00:18:46 +01002983 PSA_ASSERT( psa_cipher_update( &operation2,
2984 output1 + first_part_size,
2985 output1_length - first_part_size,
2986 output2, output2_buffer_size,
2987 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002988 TEST_ASSERT( function_output_length <=
2989 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2990 alg,
2991 output1_length - first_part_size ) );
2992 TEST_ASSERT( function_output_length <=
2993 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002994 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002995
Gilles Peskine8817f612018-12-18 00:18:46 +01002996 PSA_ASSERT( psa_cipher_finish( &operation2,
2997 output2 + output2_length,
2998 output2_buffer_size - output2_length,
2999 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003000 TEST_ASSERT( function_output_length <=
3001 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3002 TEST_ASSERT( function_output_length <=
3003 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003004 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003005
Gilles Peskine8817f612018-12-18 00:18:46 +01003006 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003007
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003008 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003009
3010exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003011 psa_cipher_abort( &operation1 );
3012 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003013 mbedtls_free( output1 );
3014 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003015 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003016 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003017}
3018/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003019
Gilles Peskine20035e32018-02-03 22:44:14 +01003020/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003021void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003022 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003023 data_t *nonce,
3024 data_t *additional_data,
3025 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003026 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003027{
Ronald Cron5425a212020-08-04 14:58:35 +02003028 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003029 psa_key_type_t key_type = key_type_arg;
3030 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003031 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003032 unsigned char *output_data = NULL;
3033 size_t output_size = 0;
3034 size_t output_length = 0;
3035 unsigned char *output_data2 = NULL;
3036 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003037 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003038 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003039 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003040
Gilles Peskine8817f612018-12-18 00:18:46 +01003041 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003042
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003043 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3044 psa_set_key_algorithm( &attributes, alg );
3045 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003046
Gilles Peskine049c7532019-05-15 20:22:09 +02003047 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003048 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003049 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3050 key_bits = psa_get_key_bits( &attributes );
3051
3052 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3053 alg );
3054 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3055 * should be exact. */
3056 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3057 expected_result != PSA_ERROR_NOT_SUPPORTED )
3058 {
3059 TEST_EQUAL( output_size,
3060 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3061 TEST_ASSERT( output_size <=
3062 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3063 }
3064 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003065
Steven Cooremanf49478b2021-02-15 15:19:25 +01003066 status = psa_aead_encrypt( key, alg,
3067 nonce->x, nonce->len,
3068 additional_data->x,
3069 additional_data->len,
3070 input_data->x, input_data->len,
3071 output_data, output_size,
3072 &output_length );
3073
3074 /* If the operation is not supported, just skip and not fail in case the
3075 * encryption involves a common limitation of cryptography hardwares and
3076 * an alternative implementation. */
3077 if( status == PSA_ERROR_NOT_SUPPORTED )
3078 {
3079 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3080 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3081 }
3082
3083 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003084
3085 if( PSA_SUCCESS == expected_result )
3086 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003087 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003088
Gilles Peskine003a4a92019-05-14 16:09:40 +02003089 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3090 * should be exact. */
3091 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003092 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003093
gabor-mezei-armceface22021-01-21 12:26:17 +01003094 TEST_ASSERT( input_data->len <=
3095 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3096
Ronald Cron5425a212020-08-04 14:58:35 +02003097 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003098 nonce->x, nonce->len,
3099 additional_data->x,
3100 additional_data->len,
3101 output_data, output_length,
3102 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003103 &output_length2 ),
3104 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003105
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003106 ASSERT_COMPARE( input_data->x, input_data->len,
3107 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003108 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003109
Gilles Peskinea1cac842018-06-11 19:33:02 +02003110exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003111 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003112 mbedtls_free( output_data );
3113 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003114 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003115}
3116/* END_CASE */
3117
3118/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003119void aead_encrypt( int key_type_arg, data_t *key_data,
3120 int alg_arg,
3121 data_t *nonce,
3122 data_t *additional_data,
3123 data_t *input_data,
3124 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003125{
Ronald Cron5425a212020-08-04 14:58:35 +02003126 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003127 psa_key_type_t key_type = key_type_arg;
3128 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003129 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003130 unsigned char *output_data = NULL;
3131 size_t output_size = 0;
3132 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003133 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003134 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003135
Gilles Peskine8817f612018-12-18 00:18:46 +01003136 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003137
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003138 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3139 psa_set_key_algorithm( &attributes, alg );
3140 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003141
Gilles Peskine049c7532019-05-15 20:22:09 +02003142 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003143 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003144 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3145 key_bits = psa_get_key_bits( &attributes );
3146
3147 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3148 alg );
3149 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3150 * should be exact. */
3151 TEST_EQUAL( output_size,
3152 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3153 TEST_ASSERT( output_size <=
3154 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3155 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003156
Steven Cooremand588ea12021-01-11 19:36:04 +01003157 status = psa_aead_encrypt( key, alg,
3158 nonce->x, nonce->len,
3159 additional_data->x, additional_data->len,
3160 input_data->x, input_data->len,
3161 output_data, output_size,
3162 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003163
Ronald Cron28a45ed2021-02-09 20:35:42 +01003164 /* If the operation is not supported, just skip and not fail in case the
3165 * encryption involves a common limitation of cryptography hardwares and
3166 * an alternative implementation. */
3167 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003168 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003169 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3170 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003171 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003172
3173 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003174 ASSERT_COMPARE( expected_result->x, expected_result->len,
3175 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003176
Gilles Peskinea1cac842018-06-11 19:33:02 +02003177exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003178 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003179 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003180 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003181}
3182/* END_CASE */
3183
3184/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003185void aead_decrypt( int key_type_arg, data_t *key_data,
3186 int alg_arg,
3187 data_t *nonce,
3188 data_t *additional_data,
3189 data_t *input_data,
3190 data_t *expected_data,
3191 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003192{
Ronald Cron5425a212020-08-04 14:58:35 +02003193 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003194 psa_key_type_t key_type = key_type_arg;
3195 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003196 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003197 unsigned char *output_data = NULL;
3198 size_t output_size = 0;
3199 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003200 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003201 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003202 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003203
Gilles Peskine8817f612018-12-18 00:18:46 +01003204 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003205
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003206 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3207 psa_set_key_algorithm( &attributes, alg );
3208 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003209
Gilles Peskine049c7532019-05-15 20:22:09 +02003210 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003211 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003212 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3213 key_bits = psa_get_key_bits( &attributes );
3214
3215 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3216 alg );
3217 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3218 expected_result != PSA_ERROR_NOT_SUPPORTED )
3219 {
3220 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3221 * should be exact. */
3222 TEST_EQUAL( output_size,
3223 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3224 TEST_ASSERT( output_size <=
3225 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3226 }
3227 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003228
Steven Cooremand588ea12021-01-11 19:36:04 +01003229 status = psa_aead_decrypt( key, alg,
3230 nonce->x, nonce->len,
3231 additional_data->x,
3232 additional_data->len,
3233 input_data->x, input_data->len,
3234 output_data, output_size,
3235 &output_length );
3236
Ronald Cron28a45ed2021-02-09 20:35:42 +01003237 /* If the operation is not supported, just skip and not fail in case the
3238 * decryption involves a common limitation of cryptography hardwares and
3239 * an alternative implementation. */
3240 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003241 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003242 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3243 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003244 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003245
3246 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003247
Gilles Peskine2d277862018-06-18 15:41:12 +02003248 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003249 ASSERT_COMPARE( expected_data->x, expected_data->len,
3250 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003251
Gilles Peskinea1cac842018-06-11 19:33:02 +02003252exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003253 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003254 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003255 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003256}
3257/* END_CASE */
3258
3259/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003260void signature_size( int type_arg,
3261 int bits,
3262 int alg_arg,
3263 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003264{
3265 psa_key_type_t type = type_arg;
3266 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003267 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003268
Gilles Peskinefe11b722018-12-18 00:24:04 +01003269 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003270#if defined(MBEDTLS_TEST_DEPRECATED)
3271 TEST_EQUAL( actual_size,
3272 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3273#endif /* MBEDTLS_TEST_DEPRECATED */
3274
Gilles Peskinee59236f2018-01-27 23:32:46 +01003275exit:
3276 ;
3277}
3278/* END_CASE */
3279
3280/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003281void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3282 int alg_arg, data_t *input_data,
3283 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003284{
Ronald Cron5425a212020-08-04 14:58:35 +02003285 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003286 psa_key_type_t key_type = key_type_arg;
3287 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003288 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003289 unsigned char *signature = NULL;
3290 size_t signature_size;
3291 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003292 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003293
Gilles Peskine8817f612018-12-18 00:18:46 +01003294 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003295
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003296 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003297 psa_set_key_algorithm( &attributes, alg );
3298 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003299
Gilles Peskine049c7532019-05-15 20:22:09 +02003300 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003301 &key ) );
3302 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003303 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003304
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003305 /* Allocate a buffer which has the size advertized by the
3306 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003307 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003308 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003309 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003310 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003311 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003312
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003313 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003314 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003315 input_data->x, input_data->len,
3316 signature, signature_size,
3317 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003318 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003319 ASSERT_COMPARE( output_data->x, output_data->len,
3320 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003321
Gilles Peskine0627f982019-11-26 19:12:16 +01003322#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003323 memset( signature, 0, signature_size );
3324 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003325 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003326 input_data->x, input_data->len,
3327 signature, signature_size,
3328 &signature_length ) );
3329 ASSERT_COMPARE( output_data->x, output_data->len,
3330 signature, signature_length );
3331#endif /* MBEDTLS_TEST_DEPRECATED */
3332
Gilles Peskine20035e32018-02-03 22:44:14 +01003333exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003334 /*
3335 * Key attributes may have been returned by psa_get_key_attributes()
3336 * thus reset them as required.
3337 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003338 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003339
Ronald Cron5425a212020-08-04 14:58:35 +02003340 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003341 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003342 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003343}
3344/* END_CASE */
3345
3346/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003347void sign_hash_fail( int key_type_arg, data_t *key_data,
3348 int alg_arg, data_t *input_data,
3349 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003350{
Ronald Cron5425a212020-08-04 14:58:35 +02003351 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003352 psa_key_type_t key_type = key_type_arg;
3353 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003354 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003355 psa_status_t actual_status;
3356 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003357 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003358 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003359 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003360
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003361 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003362
Gilles Peskine8817f612018-12-18 00:18:46 +01003363 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003364
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003365 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003366 psa_set_key_algorithm( &attributes, alg );
3367 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003368
Gilles Peskine049c7532019-05-15 20:22:09 +02003369 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003370 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003371
Ronald Cron5425a212020-08-04 14:58:35 +02003372 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003373 input_data->x, input_data->len,
3374 signature, signature_size,
3375 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003376 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003377 /* The value of *signature_length is unspecified on error, but
3378 * whatever it is, it should be less than signature_size, so that
3379 * if the caller tries to read *signature_length bytes without
3380 * checking the error code then they don't overflow a buffer. */
3381 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003382
Gilles Peskine895242b2019-11-29 12:15:40 +01003383#if defined(MBEDTLS_TEST_DEPRECATED)
3384 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003385 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003386 input_data->x, input_data->len,
3387 signature, signature_size,
3388 &signature_length ),
3389 expected_status );
3390 TEST_ASSERT( signature_length <= signature_size );
3391#endif /* MBEDTLS_TEST_DEPRECATED */
3392
Gilles Peskine20035e32018-02-03 22:44:14 +01003393exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003394 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003395 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003396 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003397 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003398}
3399/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003400
3401/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003402void sign_verify_hash( int key_type_arg, data_t *key_data,
3403 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003404{
Ronald Cron5425a212020-08-04 14:58:35 +02003405 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003406 psa_key_type_t key_type = key_type_arg;
3407 psa_algorithm_t alg = alg_arg;
3408 size_t key_bits;
3409 unsigned char *signature = NULL;
3410 size_t signature_size;
3411 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003412 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003413
Gilles Peskine8817f612018-12-18 00:18:46 +01003414 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003415
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003416 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003417 psa_set_key_algorithm( &attributes, alg );
3418 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003419
Gilles Peskine049c7532019-05-15 20:22:09 +02003420 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003421 &key ) );
3422 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003423 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003424
3425 /* Allocate a buffer which has the size advertized by the
3426 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003427 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003428 key_bits, alg );
3429 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003430 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003431 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003432
3433 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003434 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003435 input_data->x, input_data->len,
3436 signature, signature_size,
3437 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003438 /* Check that the signature length looks sensible. */
3439 TEST_ASSERT( signature_length <= signature_size );
3440 TEST_ASSERT( signature_length > 0 );
3441
3442 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003443 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003444 input_data->x, input_data->len,
3445 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003446
3447 if( input_data->len != 0 )
3448 {
3449 /* Flip a bit in the input and verify that the signature is now
3450 * detected as invalid. Flip a bit at the beginning, not at the end,
3451 * because ECDSA may ignore the last few bits of the input. */
3452 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003453 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003454 input_data->x, input_data->len,
3455 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003456 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003457 }
3458
3459exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003460 /*
3461 * Key attributes may have been returned by psa_get_key_attributes()
3462 * thus reset them as required.
3463 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003464 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003465
Ronald Cron5425a212020-08-04 14:58:35 +02003466 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003467 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003468 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003469}
3470/* END_CASE */
3471
3472/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003473void verify_hash( int key_type_arg, data_t *key_data,
3474 int alg_arg, data_t *hash_data,
3475 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003476{
Ronald Cron5425a212020-08-04 14:58:35 +02003477 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003478 psa_key_type_t key_type = key_type_arg;
3479 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003480 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003481
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003482 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003483
Gilles Peskine8817f612018-12-18 00:18:46 +01003484 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003485
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003486 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003487 psa_set_key_algorithm( &attributes, alg );
3488 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003489
Gilles Peskine049c7532019-05-15 20:22:09 +02003490 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003491 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003492
Ronald Cron5425a212020-08-04 14:58:35 +02003493 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003494 hash_data->x, hash_data->len,
3495 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003496
3497#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003498 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003499 hash_data->x, hash_data->len,
3500 signature_data->x,
3501 signature_data->len ) );
3502
3503#endif /* MBEDTLS_TEST_DEPRECATED */
3504
itayzafrir5c753392018-05-08 11:18:38 +03003505exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003506 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003507 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003508 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003509}
3510/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003511
3512/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003513void verify_hash_fail( int key_type_arg, data_t *key_data,
3514 int alg_arg, data_t *hash_data,
3515 data_t *signature_data,
3516 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003517{
Ronald Cron5425a212020-08-04 14:58:35 +02003518 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003519 psa_key_type_t key_type = key_type_arg;
3520 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003521 psa_status_t actual_status;
3522 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003523 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003524
Gilles Peskine8817f612018-12-18 00:18:46 +01003525 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003526
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003527 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003528 psa_set_key_algorithm( &attributes, alg );
3529 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003530
Gilles Peskine049c7532019-05-15 20:22:09 +02003531 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003532 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003533
Ronald Cron5425a212020-08-04 14:58:35 +02003534 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003535 hash_data->x, hash_data->len,
3536 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003537 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003538
Gilles Peskine895242b2019-11-29 12:15:40 +01003539#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003540 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003541 hash_data->x, hash_data->len,
3542 signature_data->x, signature_data->len ),
3543 expected_status );
3544#endif /* MBEDTLS_TEST_DEPRECATED */
3545
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003546exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003547 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003548 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003549 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003550}
3551/* END_CASE */
3552
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003553/* BEGIN_CASE */
gabor-mezei-armabd72582021-04-15 18:19:50 +02003554void sign_message_deterministic( int key_type_arg,
3555 data_t *key_data,
3556 int alg_arg,
3557 data_t *input_data,
3558 data_t *output_data )
3559{
3560 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3561 psa_key_type_t key_type = key_type_arg;
3562 psa_algorithm_t alg = alg_arg;
3563 size_t key_bits;
3564 unsigned char *signature = NULL;
3565 size_t signature_size;
3566 size_t signature_length = 0xdeadbeef;
3567 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3568
3569 PSA_ASSERT( psa_crypto_init( ) );
3570
3571 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3572 psa_set_key_algorithm( &attributes, alg );
3573 psa_set_key_type( &attributes, key_type );
3574
3575 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3576 &key ) );
3577 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3578 key_bits = psa_get_key_bits( &attributes );
3579
3580 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3581 TEST_ASSERT( signature_size != 0 );
3582 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3583 ASSERT_ALLOC( signature, signature_size );
3584
3585 PSA_ASSERT( psa_sign_message( key, alg,
3586 input_data->x, input_data->len,
3587 signature, signature_size,
3588 &signature_length ) );
3589
3590 ASSERT_COMPARE( output_data->x, output_data->len,
3591 signature, signature_length );
3592
3593exit:
3594 psa_reset_key_attributes( &attributes );
3595
3596 psa_destroy_key( key );
3597 mbedtls_free( signature );
3598 PSA_DONE( );
3599
3600}
3601/* END_CASE */
3602
3603/* BEGIN_CASE */
3604void sign_message_fail( int key_type_arg,
3605 data_t *key_data,
3606 int alg_arg,
3607 data_t *input_data,
3608 int signature_size_arg,
3609 int expected_status_arg )
3610{
3611 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3612 psa_key_type_t key_type = key_type_arg;
3613 psa_algorithm_t alg = alg_arg;
3614 size_t signature_size = signature_size_arg;
3615 psa_status_t actual_status;
3616 psa_status_t expected_status = expected_status_arg;
3617 unsigned char *signature = NULL;
3618 size_t signature_length = 0xdeadbeef;
3619 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3620
3621 ASSERT_ALLOC( signature, signature_size );
3622
3623 PSA_ASSERT( psa_crypto_init( ) );
3624
3625 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3626 psa_set_key_algorithm( &attributes, alg );
3627 psa_set_key_type( &attributes, key_type );
3628
3629 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3630 &key ) );
3631
3632 actual_status = psa_sign_message( key, alg,
3633 input_data->x, input_data->len,
3634 signature, signature_size,
3635 &signature_length );
3636 TEST_EQUAL( actual_status, expected_status );
3637 /* The value of *signature_length is unspecified on error, but
3638 * whatever it is, it should be less than signature_size, so that
3639 * if the caller tries to read *signature_length bytes without
3640 * checking the error code then they don't overflow a buffer. */
3641 TEST_ASSERT( signature_length <= signature_size );
3642
3643exit:
3644 psa_reset_key_attributes( &attributes );
3645 psa_destroy_key( key );
3646 mbedtls_free( signature );
3647 PSA_DONE( );
3648}
3649/* END_CASE */
3650
3651/* BEGIN_CASE */
3652void sign_verify_message( int key_type_arg,
3653 data_t *key_data,
3654 int alg_arg,
3655 data_t *input_data )
3656{
3657 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3658 psa_key_type_t key_type = key_type_arg;
3659 psa_algorithm_t alg = alg_arg;
3660 size_t key_bits;
3661 unsigned char *signature = NULL;
3662 size_t signature_size;
3663 size_t signature_length = 0xdeadbeef;
3664 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3665
3666 PSA_ASSERT( psa_crypto_init( ) );
3667
3668 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3669 PSA_KEY_USAGE_VERIFY_MESSAGE );
3670 psa_set_key_algorithm( &attributes, alg );
3671 psa_set_key_type( &attributes, key_type );
3672
3673 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3674 &key ) );
3675 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3676 key_bits = psa_get_key_bits( &attributes );
3677
3678 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3679 TEST_ASSERT( signature_size != 0 );
3680 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3681 ASSERT_ALLOC( signature, signature_size );
3682
3683 PSA_ASSERT( psa_sign_message( key, alg,
3684 input_data->x, input_data->len,
3685 signature, signature_size,
3686 &signature_length ) );
3687 TEST_ASSERT( signature_length <= signature_size );
3688 TEST_ASSERT( signature_length > 0 );
3689
3690 PSA_ASSERT( psa_verify_message( key, alg,
3691 input_data->x, input_data->len,
3692 signature, signature_length ) );
3693
3694 if( input_data->len != 0 )
3695 {
3696 /* Flip a bit in the input and verify that the signature is now
3697 * detected as invalid. Flip a bit at the beginning, not at the end,
3698 * because ECDSA may ignore the last few bits of the input. */
3699 input_data->x[0] ^= 1;
3700 TEST_EQUAL( psa_verify_message( key, alg,
3701 input_data->x, input_data->len,
3702 signature, signature_length ),
3703 PSA_ERROR_INVALID_SIGNATURE );
3704 }
3705
3706exit:
3707 psa_reset_key_attributes( &attributes );
3708
3709 psa_destroy_key( key );
3710 mbedtls_free( signature );
3711 PSA_DONE( );
3712}
3713/* END_CASE */
3714
3715/* BEGIN_CASE */
3716void verify_message( int key_type_arg,
3717 data_t *key_data,
3718 int alg_arg,
3719 data_t *input_data,
3720 data_t *signature_data )
3721{
3722 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3723 psa_key_type_t key_type = key_type_arg;
3724 psa_algorithm_t alg = alg_arg;
3725 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3726
3727 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3728
3729 PSA_ASSERT( psa_crypto_init( ) );
3730
3731 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3732 psa_set_key_algorithm( &attributes, alg );
3733 psa_set_key_type( &attributes, key_type );
3734
3735 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3736 &key ) );
3737
3738 PSA_ASSERT( psa_verify_message( key, alg,
3739 input_data->x, input_data->len,
3740 signature_data->x, signature_data->len ) );
3741
3742exit:
3743 psa_reset_key_attributes( &attributes );
3744 psa_destroy_key( key );
3745 PSA_DONE( );
3746}
3747/* END_CASE */
3748
3749/* BEGIN_CASE */
3750void verify_message_fail( int key_type_arg,
3751 data_t *key_data,
3752 int alg_arg,
3753 data_t *hash_data,
3754 data_t *signature_data,
3755 int expected_status_arg )
3756{
3757 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3758 psa_key_type_t key_type = key_type_arg;
3759 psa_algorithm_t alg = alg_arg;
3760 psa_status_t actual_status;
3761 psa_status_t expected_status = expected_status_arg;
3762 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3763
3764 PSA_ASSERT( psa_crypto_init( ) );
3765
3766 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3767 psa_set_key_algorithm( &attributes, alg );
3768 psa_set_key_type( &attributes, key_type );
3769
3770 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3771 &key ) );
3772
3773 actual_status = psa_verify_message( key, alg,
3774 hash_data->x, hash_data->len,
3775 signature_data->x,
3776 signature_data->len );
3777 TEST_EQUAL( actual_status, expected_status );
3778
3779exit:
3780 psa_reset_key_attributes( &attributes );
3781 psa_destroy_key( key );
3782 PSA_DONE( );
3783}
3784/* END_CASE */
3785
3786/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003787void asymmetric_encrypt( int key_type_arg,
3788 data_t *key_data,
3789 int alg_arg,
3790 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003791 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003792 int expected_output_length_arg,
3793 int expected_status_arg )
3794{
Ronald Cron5425a212020-08-04 14:58:35 +02003795 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003796 psa_key_type_t key_type = key_type_arg;
3797 psa_algorithm_t alg = alg_arg;
3798 size_t expected_output_length = expected_output_length_arg;
3799 size_t key_bits;
3800 unsigned char *output = NULL;
3801 size_t output_size;
3802 size_t output_length = ~0;
3803 psa_status_t actual_status;
3804 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003805 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003806
Gilles Peskine8817f612018-12-18 00:18:46 +01003807 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003808
Gilles Peskine656896e2018-06-29 19:12:28 +02003809 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003810 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3811 psa_set_key_algorithm( &attributes, alg );
3812 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003813 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003814 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003815
3816 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003817 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003818 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003819
Gilles Peskine656896e2018-06-29 19:12:28 +02003820 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003821 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003822 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003823
3824 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003825 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003826 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003827 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003828 output, output_size,
3829 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003830 TEST_EQUAL( actual_status, expected_status );
3831 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003832
Gilles Peskine68428122018-06-30 18:42:41 +02003833 /* If the label is empty, the test framework puts a non-null pointer
3834 * in label->x. Test that a null pointer works as well. */
3835 if( label->len == 0 )
3836 {
3837 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003838 if( output_size != 0 )
3839 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003840 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003841 input_data->x, input_data->len,
3842 NULL, label->len,
3843 output, output_size,
3844 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003845 TEST_EQUAL( actual_status, expected_status );
3846 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003847 }
3848
Gilles Peskine656896e2018-06-29 19:12:28 +02003849exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003850 /*
3851 * Key attributes may have been returned by psa_get_key_attributes()
3852 * thus reset them as required.
3853 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003854 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003855
Ronald Cron5425a212020-08-04 14:58:35 +02003856 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02003857 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003858 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003859}
3860/* END_CASE */
3861
3862/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003863void asymmetric_encrypt_decrypt( int key_type_arg,
3864 data_t *key_data,
3865 int alg_arg,
3866 data_t *input_data,
3867 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003868{
Ronald Cron5425a212020-08-04 14:58:35 +02003869 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003870 psa_key_type_t key_type = key_type_arg;
3871 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003872 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003873 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003874 size_t output_size;
3875 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003876 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003877 size_t output2_size;
3878 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003879 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003880
Gilles Peskine8817f612018-12-18 00:18:46 +01003881 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003882
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003883 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3884 psa_set_key_algorithm( &attributes, alg );
3885 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003886
Gilles Peskine049c7532019-05-15 20:22:09 +02003887 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003888 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003889
3890 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02003891 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003892 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003893
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003894 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003895 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003896 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01003897
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003898 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01003899 TEST_ASSERT( output2_size <=
3900 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
3901 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003902 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003903
Gilles Peskineeebd7382018-06-08 18:11:54 +02003904 /* We test encryption by checking that encrypt-then-decrypt gives back
3905 * the original plaintext because of the non-optional random
3906 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02003907 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003908 input_data->x, input_data->len,
3909 label->x, label->len,
3910 output, output_size,
3911 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003912 /* We don't know what ciphertext length to expect, but check that
3913 * it looks sensible. */
3914 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003915
Ronald Cron5425a212020-08-04 14:58:35 +02003916 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003917 output, output_length,
3918 label->x, label->len,
3919 output2, output2_size,
3920 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003921 ASSERT_COMPARE( input_data->x, input_data->len,
3922 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003923
3924exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003925 /*
3926 * Key attributes may have been returned by psa_get_key_attributes()
3927 * thus reset them as required.
3928 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003929 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003930
Ronald Cron5425a212020-08-04 14:58:35 +02003931 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003932 mbedtls_free( output );
3933 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003934 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003935}
3936/* END_CASE */
3937
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003938/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003939void asymmetric_decrypt( int key_type_arg,
3940 data_t *key_data,
3941 int alg_arg,
3942 data_t *input_data,
3943 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003944 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003945{
Ronald Cron5425a212020-08-04 14:58:35 +02003946 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003947 psa_key_type_t key_type = key_type_arg;
3948 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01003949 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003950 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003951 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003952 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003953 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003954
Gilles Peskine8817f612018-12-18 00:18:46 +01003955 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003956
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003957 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3958 psa_set_key_algorithm( &attributes, alg );
3959 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003960
Gilles Peskine049c7532019-05-15 20:22:09 +02003961 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003962 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003963
gabor-mezei-armceface22021-01-21 12:26:17 +01003964 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3965 key_bits = psa_get_key_bits( &attributes );
3966
3967 /* Determine the maximum ciphertext length */
3968 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
3969 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
3970 ASSERT_ALLOC( output, output_size );
3971
Ronald Cron5425a212020-08-04 14:58:35 +02003972 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003973 input_data->x, input_data->len,
3974 label->x, label->len,
3975 output,
3976 output_size,
3977 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003978 ASSERT_COMPARE( expected_data->x, expected_data->len,
3979 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003980
Gilles Peskine68428122018-06-30 18:42:41 +02003981 /* If the label is empty, the test framework puts a non-null pointer
3982 * in label->x. Test that a null pointer works as well. */
3983 if( label->len == 0 )
3984 {
3985 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003986 if( output_size != 0 )
3987 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003988 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003989 input_data->x, input_data->len,
3990 NULL, label->len,
3991 output,
3992 output_size,
3993 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003994 ASSERT_COMPARE( expected_data->x, expected_data->len,
3995 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003996 }
3997
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003998exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003999 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004000 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004001 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004002 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004003}
4004/* END_CASE */
4005
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004006/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004007void asymmetric_decrypt_fail( int key_type_arg,
4008 data_t *key_data,
4009 int alg_arg,
4010 data_t *input_data,
4011 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004012 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004013 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004014{
Ronald Cron5425a212020-08-04 14:58:35 +02004015 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004016 psa_key_type_t key_type = key_type_arg;
4017 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004018 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004019 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004020 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004021 psa_status_t actual_status;
4022 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004023 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004024
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004025 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004026
Gilles Peskine8817f612018-12-18 00:18:46 +01004027 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004028
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004029 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4030 psa_set_key_algorithm( &attributes, alg );
4031 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004032
Gilles Peskine049c7532019-05-15 20:22:09 +02004033 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004034 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004035
Ronald Cron5425a212020-08-04 14:58:35 +02004036 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004037 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004038 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004039 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004040 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004041 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004042 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004043
Gilles Peskine68428122018-06-30 18:42:41 +02004044 /* If the label is empty, the test framework puts a non-null pointer
4045 * in label->x. Test that a null pointer works as well. */
4046 if( label->len == 0 )
4047 {
4048 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004049 if( output_size != 0 )
4050 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004051 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004052 input_data->x, input_data->len,
4053 NULL, label->len,
4054 output, output_size,
4055 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004056 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004057 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004058 }
4059
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004060exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004061 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004062 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004063 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004064 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004065}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004066/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004067
4068/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004069void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004070{
4071 /* Test each valid way of initializing the object, except for `= {0}`, as
4072 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4073 * though it's OK by the C standard. We could test for this, but we'd need
4074 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004075 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004076 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4077 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4078 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004079
4080 memset( &zero, 0, sizeof( zero ) );
4081
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004082 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004083 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &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( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004086 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004087 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004088 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004089
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004090 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004091 PSA_ASSERT( psa_key_derivation_abort(&func) );
4092 PSA_ASSERT( psa_key_derivation_abort(&init) );
4093 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004094}
4095/* END_CASE */
4096
Janos Follath16de4a42019-06-13 16:32:24 +01004097/* BEGIN_CASE */
4098void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004099{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004100 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004101 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004102 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004103
Gilles Peskine8817f612018-12-18 00:18:46 +01004104 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004105
Janos Follath16de4a42019-06-13 16:32:24 +01004106 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004107 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004108
4109exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004110 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004111 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004112}
4113/* END_CASE */
4114
Janos Follathaf3c2a02019-06-12 12:34:34 +01004115/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004116void derive_set_capacity( int alg_arg, int capacity_arg,
4117 int expected_status_arg )
4118{
4119 psa_algorithm_t alg = alg_arg;
4120 size_t capacity = capacity_arg;
4121 psa_status_t expected_status = expected_status_arg;
4122 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4123
4124 PSA_ASSERT( psa_crypto_init( ) );
4125
4126 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4127
4128 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4129 expected_status );
4130
4131exit:
4132 psa_key_derivation_abort( &operation );
4133 PSA_DONE( );
4134}
4135/* END_CASE */
4136
4137/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004138void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004139 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004140 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004141 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004142 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004143 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004144 int expected_status_arg3,
4145 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004146{
4147 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004148 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4149 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004150 psa_status_t expected_statuses[] = {expected_status_arg1,
4151 expected_status_arg2,
4152 expected_status_arg3};
4153 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004154 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4155 MBEDTLS_SVC_KEY_ID_INIT,
4156 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004157 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4158 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4159 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004160 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004161 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004162 psa_status_t expected_output_status = expected_output_status_arg;
4163 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004164
4165 PSA_ASSERT( psa_crypto_init( ) );
4166
4167 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4168 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004169
4170 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4171
4172 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4173 {
Gilles Peskinef6279312021-05-27 13:21:20 +02004174 mbedtls_test_set_step( i );
4175 if( steps[i] == 0 )
4176 {
4177 /* Skip this step */
4178 }
4179 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004180 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004181 psa_set_key_type( &attributes, key_types[i] );
4182 PSA_ASSERT( psa_import_key( &attributes,
4183 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004184 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004185 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4186 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4187 {
4188 // When taking a private key as secret input, use key agreement
4189 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004190 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4191 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004192 expected_statuses[i] );
4193 }
4194 else
4195 {
4196 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004197 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004198 expected_statuses[i] );
4199 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004200 }
4201 else
4202 {
4203 TEST_EQUAL( psa_key_derivation_input_bytes(
4204 &operation, steps[i],
4205 inputs[i]->x, inputs[i]->len ),
4206 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004207 }
4208 }
4209
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004210 if( output_key_type != PSA_KEY_TYPE_NONE )
4211 {
4212 psa_reset_key_attributes( &attributes );
4213 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4214 psa_set_key_bits( &attributes, 8 );
4215 actual_output_status =
4216 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004217 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004218 }
4219 else
4220 {
4221 uint8_t buffer[1];
4222 actual_output_status =
4223 psa_key_derivation_output_bytes( &operation,
4224 buffer, sizeof( buffer ) );
4225 }
4226 TEST_EQUAL( actual_output_status, expected_output_status );
4227
Janos Follathaf3c2a02019-06-12 12:34:34 +01004228exit:
4229 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004230 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4231 psa_destroy_key( keys[i] );
4232 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004233 PSA_DONE( );
4234}
4235/* END_CASE */
4236
Janos Follathd958bb72019-07-03 15:02:16 +01004237/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004238void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004239{
Janos Follathd958bb72019-07-03 15:02:16 +01004240 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004241 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004242 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004243 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004244 unsigned char input1[] = "Input 1";
4245 size_t input1_length = sizeof( input1 );
4246 unsigned char input2[] = "Input 2";
4247 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004248 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004249 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004250 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4251 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4252 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004253 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004254
Gilles Peskine8817f612018-12-18 00:18:46 +01004255 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004256
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004257 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4258 psa_set_key_algorithm( &attributes, alg );
4259 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004260
Gilles Peskine73676cb2019-05-15 20:15:10 +02004261 PSA_ASSERT( psa_import_key( &attributes,
4262 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004263 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004264
4265 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004266 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4267 input1, input1_length,
4268 input2, input2_length,
4269 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004270 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004271
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004272 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004273 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004274 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004275
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004276 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004277
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004278 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004279 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004280
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004281exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004282 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004283 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004284 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004285}
4286/* END_CASE */
4287
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004288/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004289void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004290{
4291 uint8_t output_buffer[16];
4292 size_t buffer_size = 16;
4293 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004294 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004295
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004296 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4297 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004298 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004299
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004300 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004301 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004302
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004303 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004304
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004305 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4306 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004307 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004308
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004309 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004310 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004311
4312exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004313 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004314}
4315/* END_CASE */
4316
4317/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004318void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004319 int step1_arg, data_t *input1,
4320 int step2_arg, data_t *input2,
4321 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004322 int requested_capacity_arg,
4323 data_t *expected_output1,
4324 data_t *expected_output2 )
4325{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004326 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004327 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4328 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004329 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4330 MBEDTLS_SVC_KEY_ID_INIT,
4331 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004332 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004333 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004334 uint8_t *expected_outputs[2] =
4335 {expected_output1->x, expected_output2->x};
4336 size_t output_sizes[2] =
4337 {expected_output1->len, expected_output2->len};
4338 size_t output_buffer_size = 0;
4339 uint8_t *output_buffer = NULL;
4340 size_t expected_capacity;
4341 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004342 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004343 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004344 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004345
4346 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4347 {
4348 if( output_sizes[i] > output_buffer_size )
4349 output_buffer_size = output_sizes[i];
4350 if( output_sizes[i] == 0 )
4351 expected_outputs[i] = NULL;
4352 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004353 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004354 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004355
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004356 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4357 psa_set_key_algorithm( &attributes, alg );
4358 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004359
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004360 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004361 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4362 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4363 requested_capacity ) );
4364 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004365 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004366 switch( steps[i] )
4367 {
4368 case 0:
4369 break;
4370 case PSA_KEY_DERIVATION_INPUT_SECRET:
4371 PSA_ASSERT( psa_import_key( &attributes,
4372 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004373 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004374
4375 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4376 {
4377 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4378 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4379 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4380 }
4381
Gilles Peskine1468da72019-05-29 17:35:49 +02004382 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004383 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004384 break;
4385 default:
4386 PSA_ASSERT( psa_key_derivation_input_bytes(
4387 &operation, steps[i],
4388 inputs[i]->x, inputs[i]->len ) );
4389 break;
4390 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004391 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004392
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004393 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004394 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004395 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004396 expected_capacity = requested_capacity;
4397
4398 /* Expansion phase. */
4399 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4400 {
4401 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004402 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004403 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004404 if( expected_capacity == 0 && output_sizes[i] == 0 )
4405 {
4406 /* Reading 0 bytes when 0 bytes are available can go either way. */
4407 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004408 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004409 continue;
4410 }
4411 else if( expected_capacity == 0 ||
4412 output_sizes[i] > expected_capacity )
4413 {
4414 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004415 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004416 expected_capacity = 0;
4417 continue;
4418 }
4419 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004420 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004421 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004422 ASSERT_COMPARE( output_buffer, output_sizes[i],
4423 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004424 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004425 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004426 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004427 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004428 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004429 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004430 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004431
4432exit:
4433 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004434 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004435 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4436 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004437 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004438}
4439/* END_CASE */
4440
4441/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004442void derive_full( int alg_arg,
4443 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004444 data_t *input1,
4445 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004446 int requested_capacity_arg )
4447{
Ronald Cron5425a212020-08-04 14:58:35 +02004448 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004449 psa_algorithm_t alg = alg_arg;
4450 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004451 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004452 unsigned char output_buffer[16];
4453 size_t expected_capacity = requested_capacity;
4454 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004455 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004456
Gilles Peskine8817f612018-12-18 00:18:46 +01004457 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004458
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004459 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4460 psa_set_key_algorithm( &attributes, alg );
4461 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004462
Gilles Peskine049c7532019-05-15 20:22:09 +02004463 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004464 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004465
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004466 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4467 input1->x, input1->len,
4468 input2->x, input2->len,
4469 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004470 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004471
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004472 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004473 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004474 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004475
4476 /* Expansion phase. */
4477 while( current_capacity > 0 )
4478 {
4479 size_t read_size = sizeof( output_buffer );
4480 if( read_size > current_capacity )
4481 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004482 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004483 output_buffer,
4484 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004485 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004486 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004487 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004488 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004489 }
4490
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004491 /* Check that the operation refuses to go over capacity. */
4492 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004493 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004494
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004495 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004496
4497exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004498 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004499 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004500 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004501}
4502/* END_CASE */
4503
Janos Follathe60c9052019-07-03 13:51:30 +01004504/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004505void derive_key_exercise( int alg_arg,
4506 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004507 data_t *input1,
4508 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004509 int derived_type_arg,
4510 int derived_bits_arg,
4511 int derived_usage_arg,
4512 int derived_alg_arg )
4513{
Ronald Cron5425a212020-08-04 14:58:35 +02004514 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4515 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004516 psa_algorithm_t alg = alg_arg;
4517 psa_key_type_t derived_type = derived_type_arg;
4518 size_t derived_bits = derived_bits_arg;
4519 psa_key_usage_t derived_usage = derived_usage_arg;
4520 psa_algorithm_t derived_alg = derived_alg_arg;
4521 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004522 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004523 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004524 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004525
Gilles Peskine8817f612018-12-18 00:18:46 +01004526 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004527
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004528 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4529 psa_set_key_algorithm( &attributes, alg );
4530 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004531 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004532 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004533
4534 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004535 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4536 input1->x, input1->len,
4537 input2->x, input2->len,
4538 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004539 goto exit;
4540
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004541 psa_set_key_usage_flags( &attributes, derived_usage );
4542 psa_set_key_algorithm( &attributes, derived_alg );
4543 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004544 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004545 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004546 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004547
4548 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004549 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004550 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4551 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004552
4553 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004554 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004555 goto exit;
4556
4557exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004558 /*
4559 * Key attributes may have been returned by psa_get_key_attributes()
4560 * thus reset them as required.
4561 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004562 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004563
4564 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004565 psa_destroy_key( base_key );
4566 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004567 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004568}
4569/* END_CASE */
4570
Janos Follath42fd8882019-07-03 14:17:09 +01004571/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004572void derive_key_export( int alg_arg,
4573 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004574 data_t *input1,
4575 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004576 int bytes1_arg,
4577 int bytes2_arg )
4578{
Ronald Cron5425a212020-08-04 14:58:35 +02004579 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4580 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004581 psa_algorithm_t alg = alg_arg;
4582 size_t bytes1 = bytes1_arg;
4583 size_t bytes2 = bytes2_arg;
4584 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004585 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004586 uint8_t *output_buffer = NULL;
4587 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004588 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4589 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004590 size_t length;
4591
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004592 ASSERT_ALLOC( output_buffer, capacity );
4593 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004594 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004595
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004596 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4597 psa_set_key_algorithm( &base_attributes, alg );
4598 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004599 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004600 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004601
4602 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004603 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4604 input1->x, input1->len,
4605 input2->x, input2->len,
4606 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004607 goto exit;
4608
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004609 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004610 output_buffer,
4611 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004612 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004613
4614 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004615 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4616 input1->x, input1->len,
4617 input2->x, input2->len,
4618 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004619 goto exit;
4620
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004621 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4622 psa_set_key_algorithm( &derived_attributes, 0 );
4623 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004624 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004625 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004626 &derived_key ) );
4627 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004628 export_buffer, bytes1,
4629 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004630 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004631 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004632 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004633 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004634 &derived_key ) );
4635 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004636 export_buffer + bytes1, bytes2,
4637 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004638 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004639
4640 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004641 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4642 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004643
4644exit:
4645 mbedtls_free( output_buffer );
4646 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004647 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004648 psa_destroy_key( base_key );
4649 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004650 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004651}
4652/* END_CASE */
4653
4654/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004655void derive_key( int alg_arg,
4656 data_t *key_data, data_t *input1, data_t *input2,
4657 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004658 int expected_status_arg,
4659 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004660{
Ronald Cron5425a212020-08-04 14:58:35 +02004661 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4662 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004663 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004664 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004665 size_t bits = bits_arg;
4666 psa_status_t expected_status = expected_status_arg;
4667 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4668 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4669 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4670
4671 PSA_ASSERT( psa_crypto_init( ) );
4672
4673 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4674 psa_set_key_algorithm( &base_attributes, alg );
4675 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4676 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004677 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004678
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004679 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4680 input1->x, input1->len,
4681 input2->x, input2->len,
4682 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004683 goto exit;
4684
4685 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4686 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004687 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004688 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004689
4690 psa_status_t status =
4691 psa_key_derivation_output_key( &derived_attributes,
4692 &operation,
4693 &derived_key );
4694 if( is_large_output > 0 )
4695 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4696 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004697
4698exit:
4699 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004700 psa_destroy_key( base_key );
4701 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004702 PSA_DONE( );
4703}
4704/* END_CASE */
4705
4706/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004707void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004708 int our_key_type_arg, int our_key_alg_arg,
4709 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004710 int expected_status_arg )
4711{
Ronald Cron5425a212020-08-04 14:58:35 +02004712 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004713 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004714 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004715 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004716 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004717 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004718 psa_status_t expected_status = expected_status_arg;
4719 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004720
Gilles Peskine8817f612018-12-18 00:18:46 +01004721 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004722
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004723 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004724 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004725 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004726 PSA_ASSERT( psa_import_key( &attributes,
4727 our_key_data->x, our_key_data->len,
4728 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004729
Gilles Peskine77f40d82019-04-11 21:27:06 +02004730 /* The tests currently include inputs that should fail at either step.
4731 * Test cases that fail at the setup step should be changed to call
4732 * key_derivation_setup instead, and this function should be renamed
4733 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004734 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004735 if( status == PSA_SUCCESS )
4736 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004737 TEST_EQUAL( psa_key_derivation_key_agreement(
4738 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4739 our_key,
4740 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004741 expected_status );
4742 }
4743 else
4744 {
4745 TEST_ASSERT( status == expected_status );
4746 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004747
4748exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004749 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004750 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004751 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004752}
4753/* END_CASE */
4754
4755/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004756void raw_key_agreement( int alg_arg,
4757 int our_key_type_arg, data_t *our_key_data,
4758 data_t *peer_key_data,
4759 data_t *expected_output )
4760{
Ronald Cron5425a212020-08-04 14:58:35 +02004761 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004762 psa_algorithm_t alg = alg_arg;
4763 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004764 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004765 unsigned char *output = NULL;
4766 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004767 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004768
4769 ASSERT_ALLOC( output, expected_output->len );
4770 PSA_ASSERT( psa_crypto_init( ) );
4771
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004772 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4773 psa_set_key_algorithm( &attributes, alg );
4774 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004775 PSA_ASSERT( psa_import_key( &attributes,
4776 our_key_data->x, our_key_data->len,
4777 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004778
gabor-mezei-armceface22021-01-21 12:26:17 +01004779 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4780 key_bits = psa_get_key_bits( &attributes );
4781
Gilles Peskinebe697d82019-05-16 18:00:41 +02004782 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4783 peer_key_data->x, peer_key_data->len,
4784 output, expected_output->len,
4785 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004786 ASSERT_COMPARE( output, output_length,
4787 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01004788 TEST_ASSERT( output_length <=
4789 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
4790 TEST_ASSERT( output_length <=
4791 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004792
4793exit:
4794 mbedtls_free( output );
4795 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004796 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004797}
4798/* END_CASE */
4799
4800/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004801void key_agreement_capacity( int alg_arg,
4802 int our_key_type_arg, data_t *our_key_data,
4803 data_t *peer_key_data,
4804 int expected_capacity_arg )
4805{
Ronald Cron5425a212020-08-04 14:58:35 +02004806 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004807 psa_algorithm_t alg = alg_arg;
4808 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004809 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004810 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004811 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004812 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004813
Gilles Peskine8817f612018-12-18 00:18:46 +01004814 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004815
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004816 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4817 psa_set_key_algorithm( &attributes, alg );
4818 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004819 PSA_ASSERT( psa_import_key( &attributes,
4820 our_key_data->x, our_key_data->len,
4821 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004822
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004823 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004824 PSA_ASSERT( psa_key_derivation_key_agreement(
4825 &operation,
4826 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4827 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004828 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4829 {
4830 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004831 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004832 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004833 NULL, 0 ) );
4834 }
Gilles Peskine59685592018-09-18 12:11:34 +02004835
Gilles Peskinebf491972018-10-25 22:36:12 +02004836 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004837 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004838 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004839 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004840
Gilles Peskinebf491972018-10-25 22:36:12 +02004841 /* Test the actual capacity by reading the output. */
4842 while( actual_capacity > sizeof( output ) )
4843 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004844 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004845 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004846 actual_capacity -= sizeof( output );
4847 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004848 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004849 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004850 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004851 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004852
Gilles Peskine59685592018-09-18 12:11:34 +02004853exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004854 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004855 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004856 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004857}
4858/* END_CASE */
4859
4860/* BEGIN_CASE */
4861void key_agreement_output( int alg_arg,
4862 int our_key_type_arg, data_t *our_key_data,
4863 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004864 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004865{
Ronald Cron5425a212020-08-04 14:58:35 +02004866 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004867 psa_algorithm_t alg = alg_arg;
4868 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004869 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004870 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004871 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004872
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004873 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4874 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004875
Gilles Peskine8817f612018-12-18 00:18:46 +01004876 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004877
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004878 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4879 psa_set_key_algorithm( &attributes, alg );
4880 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004881 PSA_ASSERT( psa_import_key( &attributes,
4882 our_key_data->x, our_key_data->len,
4883 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004884
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004885 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004886 PSA_ASSERT( psa_key_derivation_key_agreement(
4887 &operation,
4888 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4889 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004890 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4891 {
4892 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004893 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004894 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004895 NULL, 0 ) );
4896 }
Gilles Peskine59685592018-09-18 12:11:34 +02004897
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004898 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004899 actual_output,
4900 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004901 ASSERT_COMPARE( actual_output, expected_output1->len,
4902 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004903 if( expected_output2->len != 0 )
4904 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004905 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004906 actual_output,
4907 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004908 ASSERT_COMPARE( actual_output, expected_output2->len,
4909 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004910 }
Gilles Peskine59685592018-09-18 12:11:34 +02004911
4912exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004913 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004914 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004915 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004916 mbedtls_free( actual_output );
4917}
4918/* END_CASE */
4919
4920/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004921void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004922{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004923 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004924 unsigned char *output = NULL;
4925 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004926 size_t i;
4927 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004928
Simon Butcher49f8e312020-03-03 15:51:50 +00004929 TEST_ASSERT( bytes_arg >= 0 );
4930
Gilles Peskine91892022021-02-08 19:50:26 +01004931 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004932 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02004933
Gilles Peskine8817f612018-12-18 00:18:46 +01004934 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004935
Gilles Peskinea50d7392018-06-21 10:22:13 +02004936 /* Run several times, to ensure that every output byte will be
4937 * nonzero at least once with overwhelming probability
4938 * (2^(-8*number_of_runs)). */
4939 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004940 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004941 if( bytes != 0 )
4942 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004943 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004944
Gilles Peskinea50d7392018-06-21 10:22:13 +02004945 for( i = 0; i < bytes; i++ )
4946 {
4947 if( output[i] != 0 )
4948 ++changed[i];
4949 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004950 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004951
4952 /* Check that every byte was changed to nonzero at least once. This
4953 * validates that psa_generate_random is overwriting every byte of
4954 * the output buffer. */
4955 for( i = 0; i < bytes; i++ )
4956 {
4957 TEST_ASSERT( changed[i] != 0 );
4958 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004959
4960exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004961 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004962 mbedtls_free( output );
4963 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004964}
4965/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004966
4967/* BEGIN_CASE */
4968void generate_key( int type_arg,
4969 int bits_arg,
4970 int usage_arg,
4971 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004972 int expected_status_arg,
4973 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004974{
Ronald Cron5425a212020-08-04 14:58:35 +02004975 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004976 psa_key_type_t type = type_arg;
4977 psa_key_usage_t usage = usage_arg;
4978 size_t bits = bits_arg;
4979 psa_algorithm_t alg = alg_arg;
4980 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004981 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004982 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004983
Gilles Peskine8817f612018-12-18 00:18:46 +01004984 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004985
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004986 psa_set_key_usage_flags( &attributes, usage );
4987 psa_set_key_algorithm( &attributes, alg );
4988 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004989 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004990
4991 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01004992 psa_status_t status = psa_generate_key( &attributes, &key );
4993
4994 if( is_large_key > 0 )
4995 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4996 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004997 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004998 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004999
5000 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005001 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005002 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5003 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005004
Gilles Peskine818ca122018-06-20 18:16:48 +02005005 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005006 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005007 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005008
5009exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005010 /*
5011 * Key attributes may have been returned by psa_get_key_attributes()
5012 * thus reset them as required.
5013 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005014 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005015
Ronald Cron5425a212020-08-04 14:58:35 +02005016 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005017 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005018}
5019/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005020
Ronald Cronee414c72021-03-18 18:50:08 +01005021/* 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 +02005022void generate_key_rsa( int bits_arg,
5023 data_t *e_arg,
5024 int expected_status_arg )
5025{
Ronald Cron5425a212020-08-04 14:58:35 +02005026 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005027 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005028 size_t bits = bits_arg;
5029 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5030 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5031 psa_status_t expected_status = expected_status_arg;
5032 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5033 uint8_t *exported = NULL;
5034 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005035 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005036 size_t exported_length = SIZE_MAX;
5037 uint8_t *e_read_buffer = NULL;
5038 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005039 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005040 size_t e_read_length = SIZE_MAX;
5041
5042 if( e_arg->len == 0 ||
5043 ( e_arg->len == 3 &&
5044 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5045 {
5046 is_default_public_exponent = 1;
5047 e_read_size = 0;
5048 }
5049 ASSERT_ALLOC( e_read_buffer, e_read_size );
5050 ASSERT_ALLOC( exported, exported_size );
5051
5052 PSA_ASSERT( psa_crypto_init( ) );
5053
5054 psa_set_key_usage_flags( &attributes, usage );
5055 psa_set_key_algorithm( &attributes, alg );
5056 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5057 e_arg->x, e_arg->len ) );
5058 psa_set_key_bits( &attributes, bits );
5059
5060 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005061 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005062 if( expected_status != PSA_SUCCESS )
5063 goto exit;
5064
5065 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005066 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005067 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5068 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5069 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5070 e_read_buffer, e_read_size,
5071 &e_read_length ) );
5072 if( is_default_public_exponent )
5073 TEST_EQUAL( e_read_length, 0 );
5074 else
5075 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5076
5077 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005078 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005079 goto exit;
5080
5081 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005082 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005083 exported, exported_size,
5084 &exported_length ) );
5085 {
5086 uint8_t *p = exported;
5087 uint8_t *end = exported + exported_length;
5088 size_t len;
5089 /* RSAPublicKey ::= SEQUENCE {
5090 * modulus INTEGER, -- n
5091 * publicExponent INTEGER } -- e
5092 */
5093 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005094 MBEDTLS_ASN1_SEQUENCE |
5095 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005096 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005097 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5098 MBEDTLS_ASN1_INTEGER ) );
5099 if( len >= 1 && p[0] == 0 )
5100 {
5101 ++p;
5102 --len;
5103 }
5104 if( e_arg->len == 0 )
5105 {
5106 TEST_EQUAL( len, 3 );
5107 TEST_EQUAL( p[0], 1 );
5108 TEST_EQUAL( p[1], 0 );
5109 TEST_EQUAL( p[2], 1 );
5110 }
5111 else
5112 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5113 }
5114
5115exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005116 /*
5117 * Key attributes may have been returned by psa_get_key_attributes() or
5118 * set by psa_set_key_domain_parameters() thus reset them as required.
5119 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005120 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005121
Ronald Cron5425a212020-08-04 14:58:35 +02005122 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005123 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005124 mbedtls_free( e_read_buffer );
5125 mbedtls_free( exported );
5126}
5127/* END_CASE */
5128
Darryl Greend49a4992018-06-18 17:27:26 +01005129/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005130void persistent_key_load_key_from_storage( data_t *data,
5131 int type_arg, int bits_arg,
5132 int usage_flags_arg, int alg_arg,
5133 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005134{
Ronald Cron71016a92020-08-28 19:01:50 +02005135 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005136 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005137 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5138 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005139 psa_key_type_t type = type_arg;
5140 size_t bits = bits_arg;
5141 psa_key_usage_t usage_flags = usage_flags_arg;
5142 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005143 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005144 unsigned char *first_export = NULL;
5145 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005146 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005147 size_t first_exported_length;
5148 size_t second_exported_length;
5149
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005150 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5151 {
5152 ASSERT_ALLOC( first_export, export_size );
5153 ASSERT_ALLOC( second_export, export_size );
5154 }
Darryl Greend49a4992018-06-18 17:27:26 +01005155
Gilles Peskine8817f612018-12-18 00:18:46 +01005156 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005157
Gilles Peskinec87af662019-05-15 16:12:22 +02005158 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005159 psa_set_key_usage_flags( &attributes, usage_flags );
5160 psa_set_key_algorithm( &attributes, alg );
5161 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005162 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005163
Darryl Green0c6575a2018-11-07 16:05:30 +00005164 switch( generation_method )
5165 {
5166 case IMPORT_KEY:
5167 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005168 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005169 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005170 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005171
Darryl Green0c6575a2018-11-07 16:05:30 +00005172 case GENERATE_KEY:
5173 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005174 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005175 break;
5176
5177 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005178#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005179 {
5180 /* Create base key */
5181 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5182 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5183 psa_set_key_usage_flags( &base_attributes,
5184 PSA_KEY_USAGE_DERIVE );
5185 psa_set_key_algorithm( &base_attributes, derive_alg );
5186 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005187 PSA_ASSERT( psa_import_key( &base_attributes,
5188 data->x, data->len,
5189 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005190 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005191 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005192 PSA_ASSERT( psa_key_derivation_input_key(
5193 &operation,
5194 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005195 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005196 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005197 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005198 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5199 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005200 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005201 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005202 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005203 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005204 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005205#else
5206 TEST_ASSUME( ! "KDF not supported in this configuration" );
5207#endif
5208 break;
5209
5210 default:
5211 TEST_ASSERT( ! "generation_method not implemented in test" );
5212 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005213 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005214 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005215
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005216 /* Export the key if permitted by the key policy. */
5217 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5218 {
Ronald Cron5425a212020-08-04 14:58:35 +02005219 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005220 first_export, export_size,
5221 &first_exported_length ) );
5222 if( generation_method == IMPORT_KEY )
5223 ASSERT_COMPARE( data->x, data->len,
5224 first_export, first_exported_length );
5225 }
Darryl Greend49a4992018-06-18 17:27:26 +01005226
5227 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005228 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005229 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005230 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005231
Darryl Greend49a4992018-06-18 17:27:26 +01005232 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005233 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005234 TEST_ASSERT( mbedtls_svc_key_id_equal(
5235 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005236 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5237 PSA_KEY_LIFETIME_PERSISTENT );
5238 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5239 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4d9009e2021-05-13 12:05:01 +02005240 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
5241 update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005242 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005243
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005244 /* Export the key again if permitted by the key policy. */
5245 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005246 {
Ronald Cron5425a212020-08-04 14:58:35 +02005247 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005248 second_export, export_size,
5249 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005250 ASSERT_COMPARE( first_export, first_exported_length,
5251 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005252 }
5253
5254 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005255 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005256 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005257
5258exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005259 /*
5260 * Key attributes may have been returned by psa_get_key_attributes()
5261 * thus reset them as required.
5262 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005263 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005264
Darryl Greend49a4992018-06-18 17:27:26 +01005265 mbedtls_free( first_export );
5266 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005267 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005268 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005269 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005270 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005271}
5272/* END_CASE */