blob: 044ddf90074a58af8d9a57ebf16511b3e8f23777 [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 ),
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200382 mbedtls_test_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,
gabor-mezei-arm58e510f2021-06-28 14:36:03 +0200800 int usage_arg, int alg_arg )
Gilles Peskine06c28892019-11-26 18:07:46 +0100801{
802 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-arm58e510f2021-06-28 14:36:03 +0200803 usage_arg,
804 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200805 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +0100806 goto exit;
807}
808/* END_CASE */
809
810/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200811void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +0000812{
813 /* Test each valid way of initializing the object, except for `= {0}`, as
814 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
815 * though it's OK by the C standard. We could test for this, but we'd need
816 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200817 psa_key_attributes_t func = psa_key_attributes_init( );
818 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
819 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000820
821 memset( &zero, 0, sizeof( zero ) );
822
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200823 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
824 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
825 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000826
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200827 TEST_EQUAL( psa_get_key_type( &func ), 0 );
828 TEST_EQUAL( psa_get_key_type( &init ), 0 );
829 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
830
831 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
832 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
833 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
834
835 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
836 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
837 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
838
839 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
840 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
841 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +0000842}
843/* END_CASE */
844
845/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200846void mac_key_policy( int policy_usage_arg,
847 int policy_alg_arg,
848 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200849 data_t *key_data,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200850 int exercise_alg_arg,
851 int expected_usage_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100852 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200853{
Ronald Cron5425a212020-08-04 14:58:35 +0200854 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200855 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000856 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200857 psa_key_type_t key_type = key_type_arg;
858 psa_algorithm_t policy_alg = policy_alg_arg;
859 psa_algorithm_t exercise_alg = exercise_alg_arg;
860 psa_key_usage_t policy_usage = policy_usage_arg;
861 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200862 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100863 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200864 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200865
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200866 /* Check if all implicit usage flags are deployed
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200867 in the expected usage flags. */
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200868 TEST_EQUAL( expected_usage,
869 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200870
Gilles Peskine8817f612018-12-18 00:18:46 +0100871 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200872
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200873 psa_set_key_usage_flags( &attributes, policy_usage );
874 psa_set_key_algorithm( &attributes, policy_alg );
875 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200876
Gilles Peskine049c7532019-05-15 20:22:09 +0200877 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200878 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200879
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200880 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
881
Ronald Cron5425a212020-08-04 14:58:35 +0200882 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100883 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100884 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100885 else
886 TEST_EQUAL( status, expected_status );
887
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200888 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200889
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200890 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200891 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100892 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100893 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100894 else
895 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200896
897exit:
898 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200899 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200900 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200901}
902/* END_CASE */
903
904/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200905void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200906 int policy_alg,
907 int key_type,
908 data_t *key_data,
909 int exercise_alg )
910{
Ronald Cron5425a212020-08-04 14:58:35 +0200911 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200912 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000913 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200914 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200915 psa_status_t status;
916
Gilles Peskine8817f612018-12-18 00:18:46 +0100917 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200918
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200919 psa_set_key_usage_flags( &attributes, policy_usage );
920 psa_set_key_algorithm( &attributes, policy_alg );
921 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200922
Gilles Peskine049c7532019-05-15 20:22:09 +0200923 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200924 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200925
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200926 /* Check if no key usage flag implication is done */
927 TEST_EQUAL( policy_usage,
928 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200929
Ronald Cron5425a212020-08-04 14:58:35 +0200930 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200931 if( policy_alg == exercise_alg &&
932 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100933 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200934 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100935 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200936 psa_cipher_abort( &operation );
937
Ronald Cron5425a212020-08-04 14:58:35 +0200938 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200939 if( policy_alg == exercise_alg &&
940 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100941 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200942 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100943 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200944
945exit:
946 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200947 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200948 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200949}
950/* END_CASE */
951
952/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200953void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200954 int policy_alg,
955 int key_type,
956 data_t *key_data,
957 int nonce_length_arg,
958 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100959 int exercise_alg,
960 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200961{
Ronald Cron5425a212020-08-04 14:58:35 +0200962 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200963 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200964 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200965 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100966 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200967 unsigned char nonce[16] = {0};
968 size_t nonce_length = nonce_length_arg;
969 unsigned char tag[16];
970 size_t tag_length = tag_length_arg;
971 size_t output_length;
972
973 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
974 TEST_ASSERT( tag_length <= sizeof( tag ) );
975
Gilles Peskine8817f612018-12-18 00:18:46 +0100976 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200977
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200978 psa_set_key_usage_flags( &attributes, policy_usage );
979 psa_set_key_algorithm( &attributes, policy_alg );
980 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200981
Gilles Peskine049c7532019-05-15 20:22:09 +0200982 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200983 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200984
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200985 /* Check if no key usage implication is done */
986 TEST_EQUAL( policy_usage,
987 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200988
Ronald Cron5425a212020-08-04 14:58:35 +0200989 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200990 nonce, nonce_length,
991 NULL, 0,
992 NULL, 0,
993 tag, tag_length,
994 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100995 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
996 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200997 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100998 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200999
1000 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001001 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001002 nonce, nonce_length,
1003 NULL, 0,
1004 tag, tag_length,
1005 NULL, 0,
1006 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001007 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1008 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1009 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001010 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001011 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001012 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001013
1014exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001015 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001016 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001017}
1018/* END_CASE */
1019
1020/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001021void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001022 int policy_alg,
1023 int key_type,
1024 data_t *key_data,
1025 int exercise_alg )
1026{
Ronald Cron5425a212020-08-04 14:58:35 +02001027 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001028 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001029 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001030 psa_status_t status;
1031 size_t key_bits;
1032 size_t buffer_length;
1033 unsigned char *buffer = NULL;
1034 size_t output_length;
1035
Gilles Peskine8817f612018-12-18 00:18:46 +01001036 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001037
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001038 psa_set_key_usage_flags( &attributes, policy_usage );
1039 psa_set_key_algorithm( &attributes, policy_alg );
1040 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001041
Gilles Peskine049c7532019-05-15 20:22:09 +02001042 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001043 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001044
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001045 /* Check if no key usage implication is done */
1046 TEST_EQUAL( policy_usage,
1047 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001048
Ronald Cron5425a212020-08-04 14:58:35 +02001049 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001050 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001051 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1052 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001053 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001054
Ronald Cron5425a212020-08-04 14:58:35 +02001055 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001056 NULL, 0,
1057 NULL, 0,
1058 buffer, buffer_length,
1059 &output_length );
1060 if( policy_alg == exercise_alg &&
1061 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001062 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001063 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001064 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001065
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001066 if( buffer_length != 0 )
1067 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001068 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001069 buffer, buffer_length,
1070 NULL, 0,
1071 buffer, buffer_length,
1072 &output_length );
1073 if( policy_alg == exercise_alg &&
1074 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001075 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001076 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001077 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001078
1079exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001080 /*
1081 * Key attributes may have been returned by psa_get_key_attributes()
1082 * thus reset them as required.
1083 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001084 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001085
1086 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001087 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001088 mbedtls_free( buffer );
1089}
1090/* END_CASE */
1091
1092/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001093void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001094 int policy_alg,
1095 int key_type,
1096 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001097 int exercise_alg,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001098 int payload_length_arg,
1099 int hashing_permitted,
1100 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001101{
Ronald Cron5425a212020-08-04 14:58:35 +02001102 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001103 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001104 psa_key_usage_t policy_usage = policy_usage_arg;
1105 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001106 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001107 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1108 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1109 * compatible with the policy and `payload_length_arg` is supposed to be
1110 * a valid input length to sign. If `payload_length_arg <= 0`,
1111 * `exercise_alg` is supposed to be forbidden by the policy. */
1112 int compatible_alg = payload_length_arg > 0;
1113 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001114 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001115 size_t signature_length;
1116
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001117 /* Check if all implicit usage flags are deployed
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001118 in the expected usage flags. */
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001119 TEST_EQUAL( expected_usage,
1120 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001121
Gilles Peskine8817f612018-12-18 00:18:46 +01001122 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001123
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001124 psa_set_key_usage_flags( &attributes, policy_usage );
1125 psa_set_key_algorithm( &attributes, policy_alg );
1126 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001127
Gilles Peskine049c7532019-05-15 20:22:09 +02001128 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001129 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001130
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001131 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1132
Ronald Cron5425a212020-08-04 14:58:35 +02001133 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001134 payload, payload_length,
1135 signature, sizeof( signature ),
1136 &signature_length );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001137 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001138 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001139 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001140 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001141
1142 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001143 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001144 payload, payload_length,
1145 signature, sizeof( signature ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001146 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001147 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001148 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001149 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001150
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001151 if( hashing_permitted )
1152 {
1153 status = psa_sign_message( key, exercise_alg,
1154 payload, payload_length,
1155 signature, sizeof( signature ),
1156 &signature_length );
1157 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1158 PSA_ASSERT( status );
1159 else
1160 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1161
1162 memset( signature, 0, sizeof( signature ) );
1163 status = psa_verify_message( key, exercise_alg,
1164 payload, payload_length,
1165 signature, sizeof( signature ) );
1166 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1167 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1168 else
1169 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1170 }
1171
Gilles Peskined5b33222018-06-18 22:20:03 +02001172exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001173 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001174 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001175}
1176/* END_CASE */
1177
Janos Follathba3fab92019-06-11 14:50:16 +01001178/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001179void derive_key_policy( int policy_usage,
1180 int policy_alg,
1181 int key_type,
1182 data_t *key_data,
1183 int exercise_alg )
1184{
Ronald Cron5425a212020-08-04 14:58:35 +02001185 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001186 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001187 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001188 psa_status_t status;
1189
Gilles Peskine8817f612018-12-18 00:18:46 +01001190 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001191
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001192 psa_set_key_usage_flags( &attributes, policy_usage );
1193 psa_set_key_algorithm( &attributes, policy_alg );
1194 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001195
Gilles Peskine049c7532019-05-15 20:22:09 +02001196 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001197 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001198
Janos Follathba3fab92019-06-11 14:50:16 +01001199 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1200
1201 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1202 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001203 {
Janos Follathba3fab92019-06-11 14:50:16 +01001204 PSA_ASSERT( psa_key_derivation_input_bytes(
1205 &operation,
1206 PSA_KEY_DERIVATION_INPUT_SEED,
1207 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001208 }
Janos Follathba3fab92019-06-11 14:50:16 +01001209
1210 status = psa_key_derivation_input_key( &operation,
1211 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001212 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001213
Gilles Peskineea0fb492018-07-12 17:17:20 +02001214 if( policy_alg == exercise_alg &&
1215 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001216 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001217 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001218 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001219
1220exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001221 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001222 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001223 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001224}
1225/* END_CASE */
1226
1227/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001228void agreement_key_policy( int policy_usage,
1229 int policy_alg,
1230 int key_type_arg,
1231 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001232 int exercise_alg,
1233 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001234{
Ronald Cron5425a212020-08-04 14:58:35 +02001235 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001236 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001237 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001238 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001239 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001240 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001241
Gilles Peskine8817f612018-12-18 00:18:46 +01001242 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001243
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001244 psa_set_key_usage_flags( &attributes, policy_usage );
1245 psa_set_key_algorithm( &attributes, policy_alg );
1246 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001247
Gilles Peskine049c7532019-05-15 20:22:09 +02001248 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001249 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001250
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001251 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001252 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001253
Steven Cooremance48e852020-10-05 16:02:45 +02001254 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001255
1256exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001257 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001258 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001259 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001260}
1261/* END_CASE */
1262
1263/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001264void key_policy_alg2( int key_type_arg, data_t *key_data,
1265 int usage_arg, int alg_arg, int alg2_arg )
1266{
Ronald Cron5425a212020-08-04 14:58:35 +02001267 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001268 psa_key_type_t key_type = key_type_arg;
1269 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1270 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1271 psa_key_usage_t usage = usage_arg;
1272 psa_algorithm_t alg = alg_arg;
1273 psa_algorithm_t alg2 = alg2_arg;
1274
1275 PSA_ASSERT( psa_crypto_init( ) );
1276
1277 psa_set_key_usage_flags( &attributes, usage );
1278 psa_set_key_algorithm( &attributes, alg );
1279 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1280 psa_set_key_type( &attributes, key_type );
1281 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001282 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001283
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001284 /* Update the usage flags to obtain implicit usage flags */
1285 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001286 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001287 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1288 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1289 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1290
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001291 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001292 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001293 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001294 goto exit;
1295
1296exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001297 /*
1298 * Key attributes may have been returned by psa_get_key_attributes()
1299 * thus reset them as required.
1300 */
1301 psa_reset_key_attributes( &got_attributes );
1302
Ronald Cron5425a212020-08-04 14:58:35 +02001303 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001304 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001305}
1306/* END_CASE */
1307
1308/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001309void raw_agreement_key_policy( int policy_usage,
1310 int policy_alg,
1311 int key_type_arg,
1312 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001313 int exercise_alg,
1314 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001315{
Ronald Cron5425a212020-08-04 14:58:35 +02001316 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001317 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001318 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001319 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001320 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001321 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001322
1323 PSA_ASSERT( psa_crypto_init( ) );
1324
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001325 psa_set_key_usage_flags( &attributes, policy_usage );
1326 psa_set_key_algorithm( &attributes, policy_alg );
1327 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001328
Gilles Peskine049c7532019-05-15 20:22:09 +02001329 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001330 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001331
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001332 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001333
Steven Cooremance48e852020-10-05 16:02:45 +02001334 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001335
1336exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001337 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001338 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001339 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001340}
1341/* END_CASE */
1342
1343/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001344void copy_success( int source_usage_arg,
1345 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001346 int type_arg, data_t *material,
1347 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001348 int target_usage_arg,
1349 int target_alg_arg, int target_alg2_arg,
1350 int expected_usage_arg,
1351 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001352{
Gilles Peskineca25db92019-04-19 11:43:08 +02001353 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1354 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001355 psa_key_usage_t expected_usage = expected_usage_arg;
1356 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001357 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001358 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1359 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001360 uint8_t *export_buffer = NULL;
1361
Gilles Peskine57ab7212019-01-28 13:03:09 +01001362 PSA_ASSERT( psa_crypto_init( ) );
1363
Gilles Peskineca25db92019-04-19 11:43:08 +02001364 /* Prepare the source key. */
1365 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1366 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001367 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001368 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001369 PSA_ASSERT( psa_import_key( &source_attributes,
1370 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001371 &source_key ) );
1372 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001373
Gilles Peskineca25db92019-04-19 11:43:08 +02001374 /* Prepare the target attributes. */
1375 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001376 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001377 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001378 /* Set volatile lifetime to reset the key identifier to 0. */
1379 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1380 }
1381
Gilles Peskineca25db92019-04-19 11:43:08 +02001382 if( target_usage_arg != -1 )
1383 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1384 if( target_alg_arg != -1 )
1385 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001386 if( target_alg2_arg != -1 )
1387 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001388
1389 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001390 PSA_ASSERT( psa_copy_key( source_key,
1391 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001392
1393 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001394 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001395
1396 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001397 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001398 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1399 psa_get_key_type( &target_attributes ) );
1400 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1401 psa_get_key_bits( &target_attributes ) );
1402 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1403 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001404 TEST_EQUAL( expected_alg2,
1405 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001406 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1407 {
1408 size_t length;
1409 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001410 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001411 material->len, &length ) );
1412 ASSERT_COMPARE( material->x, material->len,
1413 export_buffer, length );
1414 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001415
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001416 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001417 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001418 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001419 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001420
Ronald Cron5425a212020-08-04 14:58:35 +02001421 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001422
1423exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001424 /*
1425 * Source and target key attributes may have been returned by
1426 * psa_get_key_attributes() thus reset them as required.
1427 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001428 psa_reset_key_attributes( &source_attributes );
1429 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001430
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001431 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001432 mbedtls_free( export_buffer );
1433}
1434/* END_CASE */
1435
1436/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001437void copy_fail( int source_usage_arg,
1438 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001439 int type_arg, data_t *material,
1440 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001441 int target_usage_arg,
1442 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001443 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001444 int expected_status_arg )
1445{
1446 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1447 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001448 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1449 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001450 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001451
1452 PSA_ASSERT( psa_crypto_init( ) );
1453
1454 /* Prepare the source key. */
1455 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1456 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001457 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001458 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001459 PSA_ASSERT( psa_import_key( &source_attributes,
1460 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001461 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001462
1463 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001464 psa_set_key_id( &target_attributes, key_id );
1465 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001466 psa_set_key_type( &target_attributes, target_type_arg );
1467 psa_set_key_bits( &target_attributes, target_bits_arg );
1468 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1469 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001470 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001471
1472 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001473 TEST_EQUAL( psa_copy_key( source_key,
1474 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001475 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001476
Ronald Cron5425a212020-08-04 14:58:35 +02001477 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001478
Gilles Peskine4a644642019-05-03 17:14:08 +02001479exit:
1480 psa_reset_key_attributes( &source_attributes );
1481 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001482 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001483}
1484/* END_CASE */
1485
1486/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001487void hash_operation_init( )
1488{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001489 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001490 /* Test each valid way of initializing the object, except for `= {0}`, as
1491 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1492 * though it's OK by the C standard. We could test for this, but we'd need
1493 * to supress the Clang warning for the test. */
1494 psa_hash_operation_t func = psa_hash_operation_init( );
1495 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1496 psa_hash_operation_t zero;
1497
1498 memset( &zero, 0, sizeof( zero ) );
1499
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001500 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001501 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1502 PSA_ERROR_BAD_STATE );
1503 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1504 PSA_ERROR_BAD_STATE );
1505 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1506 PSA_ERROR_BAD_STATE );
1507
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001508 /* A default hash operation should be abortable without error. */
1509 PSA_ASSERT( psa_hash_abort( &func ) );
1510 PSA_ASSERT( psa_hash_abort( &init ) );
1511 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001512}
1513/* END_CASE */
1514
1515/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001516void hash_setup( int alg_arg,
1517 int expected_status_arg )
1518{
1519 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001520 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001521 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001522 psa_status_t status;
1523
Gilles Peskine8817f612018-12-18 00:18:46 +01001524 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001525
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001526 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001527 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001528
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001529 /* Whether setup succeeded or failed, abort must succeed. */
1530 PSA_ASSERT( psa_hash_abort( &operation ) );
1531
1532 /* If setup failed, reproduce the failure, so as to
1533 * test the resulting state of the operation object. */
1534 if( status != PSA_SUCCESS )
1535 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1536
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001537 /* Now the operation object should be reusable. */
1538#if defined(KNOWN_SUPPORTED_HASH_ALG)
1539 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1540 PSA_ASSERT( psa_hash_abort( &operation ) );
1541#endif
1542
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001543exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001544 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001545}
1546/* END_CASE */
1547
1548/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001549void hash_compute_fail( int alg_arg, data_t *input,
1550 int output_size_arg, int expected_status_arg )
1551{
1552 psa_algorithm_t alg = alg_arg;
1553 uint8_t *output = NULL;
1554 size_t output_size = output_size_arg;
1555 size_t output_length = INVALID_EXPORT_LENGTH;
1556 psa_status_t expected_status = expected_status_arg;
1557 psa_status_t status;
1558
1559 ASSERT_ALLOC( output, output_size );
1560
1561 PSA_ASSERT( psa_crypto_init( ) );
1562
1563 status = psa_hash_compute( alg, input->x, input->len,
1564 output, output_size, &output_length );
1565 TEST_EQUAL( status, expected_status );
1566 TEST_ASSERT( output_length <= output_size );
1567
1568exit:
1569 mbedtls_free( output );
1570 PSA_DONE( );
1571}
1572/* END_CASE */
1573
1574/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001575void hash_compare_fail( int alg_arg, data_t *input,
1576 data_t *reference_hash,
1577 int expected_status_arg )
1578{
1579 psa_algorithm_t alg = alg_arg;
1580 psa_status_t expected_status = expected_status_arg;
1581 psa_status_t status;
1582
1583 PSA_ASSERT( psa_crypto_init( ) );
1584
1585 status = psa_hash_compare( alg, input->x, input->len,
1586 reference_hash->x, reference_hash->len );
1587 TEST_EQUAL( status, expected_status );
1588
1589exit:
1590 PSA_DONE( );
1591}
1592/* END_CASE */
1593
1594/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001595void hash_compute_compare( int alg_arg, data_t *input,
1596 data_t *expected_output )
1597{
1598 psa_algorithm_t alg = alg_arg;
1599 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1600 size_t output_length = INVALID_EXPORT_LENGTH;
1601 size_t i;
1602
1603 PSA_ASSERT( psa_crypto_init( ) );
1604
1605 /* Compute with tight buffer */
1606 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001607 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001608 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001609 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001610 ASSERT_COMPARE( output, output_length,
1611 expected_output->x, expected_output->len );
1612
1613 /* Compute with larger buffer */
1614 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1615 output, sizeof( output ),
1616 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001617 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001618 ASSERT_COMPARE( output, output_length,
1619 expected_output->x, expected_output->len );
1620
1621 /* Compare with correct hash */
1622 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1623 output, output_length ) );
1624
1625 /* Compare with trailing garbage */
1626 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1627 output, output_length + 1 ),
1628 PSA_ERROR_INVALID_SIGNATURE );
1629
1630 /* Compare with truncated hash */
1631 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1632 output, output_length - 1 ),
1633 PSA_ERROR_INVALID_SIGNATURE );
1634
1635 /* Compare with corrupted value */
1636 for( i = 0; i < output_length; i++ )
1637 {
Chris Jones9634bb12021-01-20 15:56:42 +00001638 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001639 output[i] ^= 1;
1640 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1641 output, output_length ),
1642 PSA_ERROR_INVALID_SIGNATURE );
1643 output[i] ^= 1;
1644 }
1645
1646exit:
1647 PSA_DONE( );
1648}
1649/* END_CASE */
1650
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001651/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001652void hash_bad_order( )
1653{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001654 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001655 unsigned char input[] = "";
1656 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001657 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001658 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1659 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1660 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001661 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001662 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001663 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001664
Gilles Peskine8817f612018-12-18 00:18:46 +01001665 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001666
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001667 /* Call setup twice in a row. */
1668 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1669 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1670 PSA_ERROR_BAD_STATE );
1671 PSA_ASSERT( psa_hash_abort( &operation ) );
1672
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001673 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001674 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001675 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001676 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001677
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001678 /* Call update after finish. */
1679 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1680 PSA_ASSERT( psa_hash_finish( &operation,
1681 hash, sizeof( hash ), &hash_len ) );
1682 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001683 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001684 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001685
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001686 /* Call verify without calling setup beforehand. */
1687 TEST_EQUAL( psa_hash_verify( &operation,
1688 valid_hash, sizeof( valid_hash ) ),
1689 PSA_ERROR_BAD_STATE );
1690 PSA_ASSERT( psa_hash_abort( &operation ) );
1691
1692 /* Call verify after finish. */
1693 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1694 PSA_ASSERT( psa_hash_finish( &operation,
1695 hash, sizeof( hash ), &hash_len ) );
1696 TEST_EQUAL( psa_hash_verify( &operation,
1697 valid_hash, sizeof( valid_hash ) ),
1698 PSA_ERROR_BAD_STATE );
1699 PSA_ASSERT( psa_hash_abort( &operation ) );
1700
1701 /* Call verify twice in a row. */
1702 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1703 PSA_ASSERT( psa_hash_verify( &operation,
1704 valid_hash, sizeof( valid_hash ) ) );
1705 TEST_EQUAL( psa_hash_verify( &operation,
1706 valid_hash, sizeof( valid_hash ) ),
1707 PSA_ERROR_BAD_STATE );
1708 PSA_ASSERT( psa_hash_abort( &operation ) );
1709
1710 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001711 TEST_EQUAL( psa_hash_finish( &operation,
1712 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001713 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001714 PSA_ASSERT( psa_hash_abort( &operation ) );
1715
1716 /* Call finish twice in a row. */
1717 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1718 PSA_ASSERT( psa_hash_finish( &operation,
1719 hash, sizeof( hash ), &hash_len ) );
1720 TEST_EQUAL( psa_hash_finish( &operation,
1721 hash, sizeof( hash ), &hash_len ),
1722 PSA_ERROR_BAD_STATE );
1723 PSA_ASSERT( psa_hash_abort( &operation ) );
1724
1725 /* Call finish after calling verify. */
1726 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1727 PSA_ASSERT( psa_hash_verify( &operation,
1728 valid_hash, sizeof( valid_hash ) ) );
1729 TEST_EQUAL( psa_hash_finish( &operation,
1730 hash, sizeof( hash ), &hash_len ),
1731 PSA_ERROR_BAD_STATE );
1732 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001733
1734exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001735 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001736}
1737/* END_CASE */
1738
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001739/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001740void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001741{
1742 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001743 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1744 * appended to it */
1745 unsigned char hash[] = {
1746 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1747 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1748 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001749 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001750 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001751
Gilles Peskine8817f612018-12-18 00:18:46 +01001752 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001753
itayzafrir27e69452018-11-01 14:26:34 +02001754 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001755 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001756 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001757 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001758
itayzafrir27e69452018-11-01 14:26:34 +02001759 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001760 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001761 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001762 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001763
itayzafrir27e69452018-11-01 14:26:34 +02001764 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001765 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001766 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001767 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001768
itayzafrirec93d302018-10-18 18:01:10 +03001769exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001770 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001771}
1772/* END_CASE */
1773
Ronald Cronee414c72021-03-18 18:50:08 +01001774/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001775void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001776{
1777 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001778 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001779 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001780 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001781 size_t hash_len;
1782
Gilles Peskine8817f612018-12-18 00:18:46 +01001783 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001784
itayzafrir58028322018-10-25 10:22:01 +03001785 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001786 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001787 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001788 hash, expected_size - 1, &hash_len ),
1789 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001790
1791exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001792 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001793}
1794/* END_CASE */
1795
Ronald Cronee414c72021-03-18 18:50:08 +01001796/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001797void hash_clone_source_state( )
1798{
1799 psa_algorithm_t alg = PSA_ALG_SHA_256;
1800 unsigned char hash[PSA_HASH_MAX_SIZE];
1801 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1802 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1803 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1804 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1805 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1806 size_t hash_len;
1807
1808 PSA_ASSERT( psa_crypto_init( ) );
1809 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1810
1811 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1812 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1813 PSA_ASSERT( psa_hash_finish( &op_finished,
1814 hash, sizeof( hash ), &hash_len ) );
1815 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1816 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1817
1818 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1819 PSA_ERROR_BAD_STATE );
1820
1821 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1822 PSA_ASSERT( psa_hash_finish( &op_init,
1823 hash, sizeof( hash ), &hash_len ) );
1824 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1825 PSA_ASSERT( psa_hash_finish( &op_finished,
1826 hash, sizeof( hash ), &hash_len ) );
1827 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1828 PSA_ASSERT( psa_hash_finish( &op_aborted,
1829 hash, sizeof( hash ), &hash_len ) );
1830
1831exit:
1832 psa_hash_abort( &op_source );
1833 psa_hash_abort( &op_init );
1834 psa_hash_abort( &op_setup );
1835 psa_hash_abort( &op_finished );
1836 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001837 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001838}
1839/* END_CASE */
1840
Ronald Cronee414c72021-03-18 18:50:08 +01001841/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001842void hash_clone_target_state( )
1843{
1844 psa_algorithm_t alg = PSA_ALG_SHA_256;
1845 unsigned char hash[PSA_HASH_MAX_SIZE];
1846 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1847 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1848 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1849 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1850 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1851 size_t hash_len;
1852
1853 PSA_ASSERT( psa_crypto_init( ) );
1854
1855 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1856 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1857 PSA_ASSERT( psa_hash_finish( &op_finished,
1858 hash, sizeof( hash ), &hash_len ) );
1859 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1860 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1861
1862 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1863 PSA_ASSERT( psa_hash_finish( &op_target,
1864 hash, sizeof( hash ), &hash_len ) );
1865
1866 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1867 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1868 PSA_ERROR_BAD_STATE );
1869 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1870 PSA_ERROR_BAD_STATE );
1871
1872exit:
1873 psa_hash_abort( &op_target );
1874 psa_hash_abort( &op_init );
1875 psa_hash_abort( &op_setup );
1876 psa_hash_abort( &op_finished );
1877 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001878 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001879}
1880/* END_CASE */
1881
itayzafrir58028322018-10-25 10:22:01 +03001882/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001883void mac_operation_init( )
1884{
Jaeden Amero252ef282019-02-15 14:05:35 +00001885 const uint8_t input[1] = { 0 };
1886
Jaeden Amero769ce272019-01-04 11:48:03 +00001887 /* Test each valid way of initializing the object, except for `= {0}`, as
1888 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1889 * though it's OK by the C standard. We could test for this, but we'd need
1890 * to supress the Clang warning for the test. */
1891 psa_mac_operation_t func = psa_mac_operation_init( );
1892 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1893 psa_mac_operation_t zero;
1894
1895 memset( &zero, 0, sizeof( zero ) );
1896
Jaeden Amero252ef282019-02-15 14:05:35 +00001897 /* A freshly-initialized MAC operation should not be usable. */
1898 TEST_EQUAL( psa_mac_update( &func,
1899 input, sizeof( input ) ),
1900 PSA_ERROR_BAD_STATE );
1901 TEST_EQUAL( psa_mac_update( &init,
1902 input, sizeof( input ) ),
1903 PSA_ERROR_BAD_STATE );
1904 TEST_EQUAL( psa_mac_update( &zero,
1905 input, sizeof( input ) ),
1906 PSA_ERROR_BAD_STATE );
1907
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001908 /* A default MAC operation should be abortable without error. */
1909 PSA_ASSERT( psa_mac_abort( &func ) );
1910 PSA_ASSERT( psa_mac_abort( &init ) );
1911 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001912}
1913/* END_CASE */
1914
1915/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001916void mac_setup( int key_type_arg,
1917 data_t *key,
1918 int alg_arg,
1919 int expected_status_arg )
1920{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001921 psa_key_type_t key_type = key_type_arg;
1922 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001923 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001924 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001925 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1926#if defined(KNOWN_SUPPORTED_MAC_ALG)
1927 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1928#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001929
Gilles Peskine8817f612018-12-18 00:18:46 +01001930 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001931
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001932 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1933 &operation, &status ) )
1934 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001935 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001936
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001937 /* The operation object should be reusable. */
1938#if defined(KNOWN_SUPPORTED_MAC_ALG)
1939 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1940 smoke_test_key_data,
1941 sizeof( smoke_test_key_data ),
1942 KNOWN_SUPPORTED_MAC_ALG,
1943 &operation, &status ) )
1944 goto exit;
1945 TEST_EQUAL( status, PSA_SUCCESS );
1946#endif
1947
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001948exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001949 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001950}
1951/* END_CASE */
1952
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001953/* 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 +00001954void mac_bad_order( )
1955{
Ronald Cron5425a212020-08-04 14:58:35 +02001956 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001957 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1958 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001959 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001960 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1961 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1962 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001963 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001964 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1965 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1966 size_t sign_mac_length = 0;
1967 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1968 const uint8_t verify_mac[] = {
1969 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1970 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1971 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1972
1973 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001974 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001975 psa_set_key_algorithm( &attributes, alg );
1976 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001977
Ronald Cron5425a212020-08-04 14:58:35 +02001978 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1979 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001980
Jaeden Amero252ef282019-02-15 14:05:35 +00001981 /* Call update without calling setup beforehand. */
1982 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1983 PSA_ERROR_BAD_STATE );
1984 PSA_ASSERT( psa_mac_abort( &operation ) );
1985
1986 /* Call sign finish without calling setup beforehand. */
1987 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1988 &sign_mac_length),
1989 PSA_ERROR_BAD_STATE );
1990 PSA_ASSERT( psa_mac_abort( &operation ) );
1991
1992 /* Call verify finish without calling setup beforehand. */
1993 TEST_EQUAL( psa_mac_verify_finish( &operation,
1994 verify_mac, sizeof( verify_mac ) ),
1995 PSA_ERROR_BAD_STATE );
1996 PSA_ASSERT( psa_mac_abort( &operation ) );
1997
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001998 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001999 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2000 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002001 PSA_ERROR_BAD_STATE );
2002 PSA_ASSERT( psa_mac_abort( &operation ) );
2003
Jaeden Amero252ef282019-02-15 14:05:35 +00002004 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002005 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002006 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2007 PSA_ASSERT( psa_mac_sign_finish( &operation,
2008 sign_mac, sizeof( sign_mac ),
2009 &sign_mac_length ) );
2010 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2011 PSA_ERROR_BAD_STATE );
2012 PSA_ASSERT( psa_mac_abort( &operation ) );
2013
2014 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002015 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002016 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2017 PSA_ASSERT( psa_mac_verify_finish( &operation,
2018 verify_mac, sizeof( verify_mac ) ) );
2019 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2020 PSA_ERROR_BAD_STATE );
2021 PSA_ASSERT( psa_mac_abort( &operation ) );
2022
2023 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002024 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002025 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2026 PSA_ASSERT( psa_mac_sign_finish( &operation,
2027 sign_mac, sizeof( sign_mac ),
2028 &sign_mac_length ) );
2029 TEST_EQUAL( psa_mac_sign_finish( &operation,
2030 sign_mac, sizeof( sign_mac ),
2031 &sign_mac_length ),
2032 PSA_ERROR_BAD_STATE );
2033 PSA_ASSERT( psa_mac_abort( &operation ) );
2034
2035 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002036 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002037 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2038 PSA_ASSERT( psa_mac_verify_finish( &operation,
2039 verify_mac, sizeof( verify_mac ) ) );
2040 TEST_EQUAL( psa_mac_verify_finish( &operation,
2041 verify_mac, sizeof( verify_mac ) ),
2042 PSA_ERROR_BAD_STATE );
2043 PSA_ASSERT( psa_mac_abort( &operation ) );
2044
2045 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002046 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002047 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2048 TEST_EQUAL( psa_mac_verify_finish( &operation,
2049 verify_mac, sizeof( verify_mac ) ),
2050 PSA_ERROR_BAD_STATE );
2051 PSA_ASSERT( psa_mac_abort( &operation ) );
2052
2053 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002054 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002055 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2056 TEST_EQUAL( psa_mac_sign_finish( &operation,
2057 sign_mac, sizeof( sign_mac ),
2058 &sign_mac_length ),
2059 PSA_ERROR_BAD_STATE );
2060 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002061
Ronald Cron5425a212020-08-04 14:58:35 +02002062 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002063
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002064exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002065 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002066}
2067/* END_CASE */
2068
2069/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002070void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002071 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002072 int alg_arg,
2073 data_t *input,
2074 data_t *expected_mac )
2075{
Ronald Cron5425a212020-08-04 14:58:35 +02002076 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002077 psa_key_type_t key_type = key_type_arg;
2078 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002079 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002080 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002081 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002082 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002083 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002084 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002085 const size_t output_sizes_to_test[] = {
2086 0,
2087 1,
2088 expected_mac->len - 1,
2089 expected_mac->len,
2090 expected_mac->len + 1,
2091 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002092
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002093 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002094 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002095 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002096
Gilles Peskine8817f612018-12-18 00:18:46 +01002097 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002098
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002099 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002100 psa_set_key_algorithm( &attributes, alg );
2101 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002102
Ronald Cron5425a212020-08-04 14:58:35 +02002103 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2104 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002105
Gilles Peskine8b356b52020-08-25 23:44:59 +02002106 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2107 {
2108 const size_t output_size = output_sizes_to_test[i];
2109 psa_status_t expected_status =
2110 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2111 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002112
Chris Jones9634bb12021-01-20 15:56:42 +00002113 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002114 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002115
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002116 /* Calculate the MAC, one-shot case. */
2117 TEST_EQUAL( psa_mac_compute( key, alg,
2118 input->x, input->len,
2119 actual_mac, output_size, &mac_length ),
2120 expected_status );
2121 if( expected_status == PSA_SUCCESS )
2122 {
2123 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2124 actual_mac, mac_length );
2125 }
2126
2127 if( output_size > 0 )
2128 memset( actual_mac, 0, output_size );
2129
2130 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002131 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002132 PSA_ASSERT( psa_mac_update( &operation,
2133 input->x, input->len ) );
2134 TEST_EQUAL( psa_mac_sign_finish( &operation,
2135 actual_mac, output_size,
2136 &mac_length ),
2137 expected_status );
2138 PSA_ASSERT( psa_mac_abort( &operation ) );
2139
2140 if( expected_status == PSA_SUCCESS )
2141 {
2142 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2143 actual_mac, mac_length );
2144 }
2145 mbedtls_free( actual_mac );
2146 actual_mac = NULL;
2147 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002148
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002149exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002150 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002151 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002152 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002153 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002154}
2155/* END_CASE */
2156
2157/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002158void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002159 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002160 int alg_arg,
2161 data_t *input,
2162 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002163{
Ronald Cron5425a212020-08-04 14:58:35 +02002164 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002165 psa_key_type_t key_type = key_type_arg;
2166 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002167 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002168 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002169 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002170
Gilles Peskine69c12672018-06-28 00:07:19 +02002171 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2172
Gilles Peskine8817f612018-12-18 00:18:46 +01002173 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002174
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002175 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002176 psa_set_key_algorithm( &attributes, alg );
2177 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002178
Ronald Cron5425a212020-08-04 14:58:35 +02002179 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2180 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002181
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002182 /* Verify correct MAC, one-shot case. */
2183 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2184 expected_mac->x, expected_mac->len ) );
2185
2186 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002187 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002188 PSA_ASSERT( psa_mac_update( &operation,
2189 input->x, input->len ) );
2190 PSA_ASSERT( psa_mac_verify_finish( &operation,
2191 expected_mac->x,
2192 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002193
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002194 /* Test a MAC that's too short, one-shot case. */
2195 TEST_EQUAL( psa_mac_verify( key, alg,
2196 input->x, input->len,
2197 expected_mac->x,
2198 expected_mac->len - 1 ),
2199 PSA_ERROR_INVALID_SIGNATURE );
2200
2201 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002202 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002203 PSA_ASSERT( psa_mac_update( &operation,
2204 input->x, input->len ) );
2205 TEST_EQUAL( psa_mac_verify_finish( &operation,
2206 expected_mac->x,
2207 expected_mac->len - 1 ),
2208 PSA_ERROR_INVALID_SIGNATURE );
2209
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002210 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002211 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2212 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002213 TEST_EQUAL( psa_mac_verify( key, alg,
2214 input->x, input->len,
2215 perturbed_mac, expected_mac->len + 1 ),
2216 PSA_ERROR_INVALID_SIGNATURE );
2217
2218 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002219 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002220 PSA_ASSERT( psa_mac_update( &operation,
2221 input->x, input->len ) );
2222 TEST_EQUAL( psa_mac_verify_finish( &operation,
2223 perturbed_mac,
2224 expected_mac->len + 1 ),
2225 PSA_ERROR_INVALID_SIGNATURE );
2226
2227 /* Test changing one byte. */
2228 for( size_t i = 0; i < expected_mac->len; i++ )
2229 {
Chris Jones9634bb12021-01-20 15:56:42 +00002230 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002231 perturbed_mac[i] ^= 1;
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002232
2233 TEST_EQUAL( psa_mac_verify( key, alg,
2234 input->x, input->len,
2235 perturbed_mac, expected_mac->len ),
2236 PSA_ERROR_INVALID_SIGNATURE );
2237
Ronald Cron5425a212020-08-04 14:58:35 +02002238 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002239 PSA_ASSERT( psa_mac_update( &operation,
2240 input->x, input->len ) );
2241 TEST_EQUAL( psa_mac_verify_finish( &operation,
2242 perturbed_mac,
2243 expected_mac->len ),
2244 PSA_ERROR_INVALID_SIGNATURE );
2245 perturbed_mac[i] ^= 1;
2246 }
2247
Gilles Peskine8c9def32018-02-08 10:02:12 +01002248exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002249 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002250 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002251 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002252 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002253}
2254/* END_CASE */
2255
2256/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002257void cipher_operation_init( )
2258{
Jaeden Ameroab439972019-02-15 14:12:05 +00002259 const uint8_t input[1] = { 0 };
2260 unsigned char output[1] = { 0 };
2261 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002262 /* Test each valid way of initializing the object, except for `= {0}`, as
2263 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2264 * though it's OK by the C standard. We could test for this, but we'd need
2265 * to supress the Clang warning for the test. */
2266 psa_cipher_operation_t func = psa_cipher_operation_init( );
2267 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2268 psa_cipher_operation_t zero;
2269
2270 memset( &zero, 0, sizeof( zero ) );
2271
Jaeden Ameroab439972019-02-15 14:12:05 +00002272 /* A freshly-initialized cipher operation should not be usable. */
2273 TEST_EQUAL( psa_cipher_update( &func,
2274 input, sizeof( input ),
2275 output, sizeof( output ),
2276 &output_length ),
2277 PSA_ERROR_BAD_STATE );
2278 TEST_EQUAL( psa_cipher_update( &init,
2279 input, sizeof( input ),
2280 output, sizeof( output ),
2281 &output_length ),
2282 PSA_ERROR_BAD_STATE );
2283 TEST_EQUAL( psa_cipher_update( &zero,
2284 input, sizeof( input ),
2285 output, sizeof( output ),
2286 &output_length ),
2287 PSA_ERROR_BAD_STATE );
2288
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002289 /* A default cipher operation should be abortable without error. */
2290 PSA_ASSERT( psa_cipher_abort( &func ) );
2291 PSA_ASSERT( psa_cipher_abort( &init ) );
2292 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002293}
2294/* END_CASE */
2295
2296/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002297void cipher_setup( int key_type_arg,
2298 data_t *key,
2299 int alg_arg,
2300 int expected_status_arg )
2301{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002302 psa_key_type_t key_type = key_type_arg;
2303 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002304 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002305 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002306 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002307#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002308 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2309#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002310
Gilles Peskine8817f612018-12-18 00:18:46 +01002311 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002312
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002313 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2314 &operation, &status ) )
2315 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002316 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002317
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002318 /* The operation object should be reusable. */
2319#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2320 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2321 smoke_test_key_data,
2322 sizeof( smoke_test_key_data ),
2323 KNOWN_SUPPORTED_CIPHER_ALG,
2324 &operation, &status ) )
2325 goto exit;
2326 TEST_EQUAL( status, PSA_SUCCESS );
2327#endif
2328
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002329exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002330 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002331 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002332}
2333/* END_CASE */
2334
Ronald Cronee414c72021-03-18 18:50:08 +01002335/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002336void cipher_bad_order( )
2337{
Ronald Cron5425a212020-08-04 14:58:35 +02002338 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002339 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2340 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002341 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002342 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002343 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002344 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002345 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2346 0xaa, 0xaa, 0xaa, 0xaa };
2347 const uint8_t text[] = {
2348 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2349 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002350 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002351 size_t length = 0;
2352
2353 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002354 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2355 psa_set_key_algorithm( &attributes, alg );
2356 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002357 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2358 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002359
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002360 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002361 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2362 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002363 PSA_ERROR_BAD_STATE );
2364 PSA_ASSERT( psa_cipher_abort( &operation ) );
2365
2366 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002367 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2368 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002369 PSA_ERROR_BAD_STATE );
2370 PSA_ASSERT( psa_cipher_abort( &operation ) );
2371
Jaeden Ameroab439972019-02-15 14:12:05 +00002372 /* Generate an IV without calling setup beforehand. */
2373 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2374 buffer, sizeof( buffer ),
2375 &length ),
2376 PSA_ERROR_BAD_STATE );
2377 PSA_ASSERT( psa_cipher_abort( &operation ) );
2378
2379 /* Generate an IV twice in a row. */
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_generate_iv( &operation,
2382 buffer, sizeof( buffer ),
2383 &length ) );
2384 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2385 buffer, sizeof( buffer ),
2386 &length ),
2387 PSA_ERROR_BAD_STATE );
2388 PSA_ASSERT( psa_cipher_abort( &operation ) );
2389
2390 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002391 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002392 PSA_ASSERT( psa_cipher_set_iv( &operation,
2393 iv, sizeof( iv ) ) );
2394 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2395 buffer, sizeof( buffer ),
2396 &length ),
2397 PSA_ERROR_BAD_STATE );
2398 PSA_ASSERT( psa_cipher_abort( &operation ) );
2399
2400 /* Set an IV without calling setup beforehand. */
2401 TEST_EQUAL( psa_cipher_set_iv( &operation,
2402 iv, sizeof( iv ) ),
2403 PSA_ERROR_BAD_STATE );
2404 PSA_ASSERT( psa_cipher_abort( &operation ) );
2405
2406 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002407 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002408 PSA_ASSERT( psa_cipher_set_iv( &operation,
2409 iv, sizeof( iv ) ) );
2410 TEST_EQUAL( psa_cipher_set_iv( &operation,
2411 iv, sizeof( iv ) ),
2412 PSA_ERROR_BAD_STATE );
2413 PSA_ASSERT( psa_cipher_abort( &operation ) );
2414
2415 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002416 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002417 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2418 buffer, sizeof( buffer ),
2419 &length ) );
2420 TEST_EQUAL( psa_cipher_set_iv( &operation,
2421 iv, sizeof( iv ) ),
2422 PSA_ERROR_BAD_STATE );
2423 PSA_ASSERT( psa_cipher_abort( &operation ) );
2424
2425 /* Call update without calling setup beforehand. */
2426 TEST_EQUAL( psa_cipher_update( &operation,
2427 text, sizeof( text ),
2428 buffer, sizeof( buffer ),
2429 &length ),
2430 PSA_ERROR_BAD_STATE );
2431 PSA_ASSERT( psa_cipher_abort( &operation ) );
2432
2433 /* Call update without an IV where an IV is required. */
2434 TEST_EQUAL( psa_cipher_update( &operation,
2435 text, sizeof( text ),
2436 buffer, sizeof( buffer ),
2437 &length ),
2438 PSA_ERROR_BAD_STATE );
2439 PSA_ASSERT( psa_cipher_abort( &operation ) );
2440
2441 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002442 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002443 PSA_ASSERT( psa_cipher_set_iv( &operation,
2444 iv, sizeof( iv ) ) );
2445 PSA_ASSERT( psa_cipher_finish( &operation,
2446 buffer, sizeof( buffer ), &length ) );
2447 TEST_EQUAL( psa_cipher_update( &operation,
2448 text, sizeof( text ),
2449 buffer, sizeof( buffer ),
2450 &length ),
2451 PSA_ERROR_BAD_STATE );
2452 PSA_ASSERT( psa_cipher_abort( &operation ) );
2453
2454 /* Call finish without calling setup beforehand. */
2455 TEST_EQUAL( psa_cipher_finish( &operation,
2456 buffer, sizeof( buffer ), &length ),
2457 PSA_ERROR_BAD_STATE );
2458 PSA_ASSERT( psa_cipher_abort( &operation ) );
2459
2460 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002461 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002462 /* Not calling update means we are encrypting an empty buffer, which is OK
2463 * for cipher modes with padding. */
2464 TEST_EQUAL( psa_cipher_finish( &operation,
2465 buffer, sizeof( buffer ), &length ),
2466 PSA_ERROR_BAD_STATE );
2467 PSA_ASSERT( psa_cipher_abort( &operation ) );
2468
2469 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002470 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002471 PSA_ASSERT( psa_cipher_set_iv( &operation,
2472 iv, sizeof( iv ) ) );
2473 PSA_ASSERT( psa_cipher_finish( &operation,
2474 buffer, sizeof( buffer ), &length ) );
2475 TEST_EQUAL( psa_cipher_finish( &operation,
2476 buffer, sizeof( buffer ), &length ),
2477 PSA_ERROR_BAD_STATE );
2478 PSA_ASSERT( psa_cipher_abort( &operation ) );
2479
Ronald Cron5425a212020-08-04 14:58:35 +02002480 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002481
Jaeden Ameroab439972019-02-15 14:12:05 +00002482exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002483 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002484 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002485}
2486/* END_CASE */
2487
2488/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002489void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002490 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002491 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002492 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002493{
Ronald Cron5425a212020-08-04 14:58:35 +02002494 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002495 psa_status_t status;
2496 psa_key_type_t key_type = key_type_arg;
2497 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002498 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002499 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002500 size_t output_buffer_size = 0;
2501 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002502 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002503 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002504 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002505
Gilles Peskine8817f612018-12-18 00:18:46 +01002506 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002507
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002508 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2509 psa_set_key_algorithm( &attributes, alg );
2510 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002511
Ronald Cron5425a212020-08-04 14:58:35 +02002512 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2513 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002514
Ronald Cron5425a212020-08-04 14:58:35 +02002515 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002516
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002517 if( iv->len > 0 )
2518 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002519 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002520 }
2521
gabor-mezei-armceface22021-01-21 12:26:17 +01002522 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2523 TEST_ASSERT( output_buffer_size <=
2524 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002525 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002526
Gilles Peskine8817f612018-12-18 00:18:46 +01002527 PSA_ASSERT( psa_cipher_update( &operation,
2528 input->x, input->len,
2529 output, output_buffer_size,
2530 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002531 TEST_ASSERT( function_output_length <=
2532 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2533 TEST_ASSERT( function_output_length <=
2534 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002535 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002536
Gilles Peskine50e586b2018-06-08 14:28:46 +02002537 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002538 ( output_buffer_size == 0 ? NULL :
2539 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002540 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002541 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002542 TEST_ASSERT( function_output_length <=
2543 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2544 TEST_ASSERT( function_output_length <=
2545 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002546 total_output_length += function_output_length;
2547
Gilles Peskinefe11b722018-12-18 00:24:04 +01002548 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002549 if( expected_status == PSA_SUCCESS )
2550 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002551 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002552 ASSERT_COMPARE( expected_output->x, expected_output->len,
2553 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002554 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002555
Gilles Peskine50e586b2018-06-08 14:28:46 +02002556exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002557 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002558 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002559 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002560 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002561}
2562/* END_CASE */
2563
2564/* BEGIN_CASE */
2565void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002566 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002567 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002568 int first_part_size_arg,
2569 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002570 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002571{
Ronald Cron5425a212020-08-04 14:58:35 +02002572 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002573 psa_key_type_t key_type = key_type_arg;
2574 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002575 size_t first_part_size = first_part_size_arg;
2576 size_t output1_length = output1_length_arg;
2577 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002578 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002579 size_t output_buffer_size = 0;
2580 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002581 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002582 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002583 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002584
Gilles Peskine8817f612018-12-18 00:18:46 +01002585 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002586
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002587 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2588 psa_set_key_algorithm( &attributes, alg );
2589 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002590
Ronald Cron5425a212020-08-04 14:58:35 +02002591 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2592 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002593
Ronald Cron5425a212020-08-04 14:58:35 +02002594 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002595
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002596 if( iv->len > 0 )
2597 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002598 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002599 }
2600
gabor-mezei-armceface22021-01-21 12:26:17 +01002601 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2602 TEST_ASSERT( output_buffer_size <=
2603 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002604 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002605
Gilles Peskinee0866522019-02-19 19:44:00 +01002606 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002607 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2608 output, output_buffer_size,
2609 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002610 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002611 TEST_ASSERT( function_output_length <=
2612 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2613 TEST_ASSERT( function_output_length <=
2614 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002615 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002616
Gilles Peskine8817f612018-12-18 00:18:46 +01002617 PSA_ASSERT( psa_cipher_update( &operation,
2618 input->x + first_part_size,
2619 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002620 ( output_buffer_size == 0 ? NULL :
2621 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002622 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002623 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002624 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002625 TEST_ASSERT( function_output_length <=
2626 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2627 alg,
2628 input->len - first_part_size ) );
2629 TEST_ASSERT( function_output_length <=
2630 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002631 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002632
Gilles Peskine8817f612018-12-18 00:18:46 +01002633 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002634 ( output_buffer_size == 0 ? NULL :
2635 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002636 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002637 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002638 TEST_ASSERT( function_output_length <=
2639 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2640 TEST_ASSERT( function_output_length <=
2641 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002642 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002643 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002644
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002645 ASSERT_COMPARE( expected_output->x, expected_output->len,
2646 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002647
2648exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002649 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002650 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002651 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002652 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002653}
2654/* END_CASE */
2655
2656/* BEGIN_CASE */
2657void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002658 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002659 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002660 int first_part_size_arg,
2661 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002662 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002663{
Ronald Cron5425a212020-08-04 14:58:35 +02002664 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002665 psa_key_type_t key_type = key_type_arg;
2666 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002667 size_t first_part_size = first_part_size_arg;
2668 size_t output1_length = output1_length_arg;
2669 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002670 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002671 size_t output_buffer_size = 0;
2672 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002673 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002674 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002675 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002676
Gilles Peskine8817f612018-12-18 00:18:46 +01002677 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002678
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002679 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2680 psa_set_key_algorithm( &attributes, alg );
2681 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002682
Ronald Cron5425a212020-08-04 14:58:35 +02002683 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2684 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002685
Ronald Cron5425a212020-08-04 14:58:35 +02002686 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002687
Steven Cooreman177deba2020-09-07 17:14:14 +02002688 if( iv->len > 0 )
2689 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002690 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002691 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002692
gabor-mezei-armceface22021-01-21 12:26:17 +01002693 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2694 TEST_ASSERT( output_buffer_size <=
2695 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002696 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002697
Gilles Peskinee0866522019-02-19 19:44:00 +01002698 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002699 PSA_ASSERT( psa_cipher_update( &operation,
2700 input->x, first_part_size,
2701 output, output_buffer_size,
2702 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002703 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002704 TEST_ASSERT( function_output_length <=
2705 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2706 TEST_ASSERT( function_output_length <=
2707 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002708 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002709
Gilles Peskine8817f612018-12-18 00:18:46 +01002710 PSA_ASSERT( psa_cipher_update( &operation,
2711 input->x + first_part_size,
2712 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002713 ( output_buffer_size == 0 ? NULL :
2714 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002715 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002716 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002717 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002718 TEST_ASSERT( function_output_length <=
2719 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2720 alg,
2721 input->len - first_part_size ) );
2722 TEST_ASSERT( function_output_length <=
2723 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002724 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002725
Gilles Peskine8817f612018-12-18 00:18:46 +01002726 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002727 ( output_buffer_size == 0 ? NULL :
2728 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002729 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002730 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002731 TEST_ASSERT( function_output_length <=
2732 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2733 TEST_ASSERT( function_output_length <=
2734 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002735 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002736 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002737
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002738 ASSERT_COMPARE( expected_output->x, expected_output->len,
2739 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002740
2741exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002742 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002743 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002744 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002745 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002746}
2747/* END_CASE */
2748
Gilles Peskine50e586b2018-06-08 14:28:46 +02002749/* BEGIN_CASE */
2750void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002751 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002752 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002753 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002754{
Ronald Cron5425a212020-08-04 14:58:35 +02002755 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002756 psa_status_t status;
2757 psa_key_type_t key_type = key_type_arg;
2758 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002759 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002760 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002761 size_t output_buffer_size = 0;
2762 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002763 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002764 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002765 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002766
Gilles Peskine8817f612018-12-18 00:18:46 +01002767 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002768
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002769 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2770 psa_set_key_algorithm( &attributes, alg );
2771 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002772
Ronald Cron5425a212020-08-04 14:58:35 +02002773 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2774 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002775
Ronald Cron5425a212020-08-04 14:58:35 +02002776 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002777
Steven Cooreman177deba2020-09-07 17:14:14 +02002778 if( iv->len > 0 )
2779 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002780 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002781 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002782
gabor-mezei-armceface22021-01-21 12:26:17 +01002783 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2784 TEST_ASSERT( output_buffer_size <=
2785 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002786 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002787
Gilles Peskine8817f612018-12-18 00:18:46 +01002788 PSA_ASSERT( psa_cipher_update( &operation,
2789 input->x, input->len,
2790 output, output_buffer_size,
2791 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002792 TEST_ASSERT( function_output_length <=
2793 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2794 TEST_ASSERT( function_output_length <=
2795 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002796 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002797
Gilles Peskine50e586b2018-06-08 14:28:46 +02002798 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002799 ( output_buffer_size == 0 ? NULL :
2800 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002801 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002802 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002803 TEST_ASSERT( function_output_length <=
2804 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2805 TEST_ASSERT( function_output_length <=
2806 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002807 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002808 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002809
2810 if( expected_status == PSA_SUCCESS )
2811 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002812 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002813 ASSERT_COMPARE( expected_output->x, expected_output->len,
2814 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002815 }
2816
Gilles Peskine50e586b2018-06-08 14:28:46 +02002817exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002818 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002819 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002820 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002821 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002822}
2823/* END_CASE */
2824
Gilles Peskine50e586b2018-06-08 14:28:46 +02002825/* BEGIN_CASE */
2826void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002827 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002828 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002829{
Ronald Cron5425a212020-08-04 14:58:35 +02002830 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002831 psa_key_type_t key_type = key_type_arg;
2832 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002833 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002834 size_t iv_size = 16;
2835 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002836 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002837 size_t output1_size = 0;
2838 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002839 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002840 size_t output2_size = 0;
2841 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002842 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002843 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2844 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002845 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002846
Gilles Peskine8817f612018-12-18 00:18:46 +01002847 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002848
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002849 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2850 psa_set_key_algorithm( &attributes, alg );
2851 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002852
Ronald Cron5425a212020-08-04 14:58:35 +02002853 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2854 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002855
Ronald Cron5425a212020-08-04 14:58:35 +02002856 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2857 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002858
Steven Cooreman177deba2020-09-07 17:14:14 +02002859 if( alg != PSA_ALG_ECB_NO_PADDING )
2860 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002861 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2862 iv, iv_size,
2863 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002864 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002865 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2866 TEST_ASSERT( output1_size <=
2867 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002868 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002869
Gilles Peskine8817f612018-12-18 00:18:46 +01002870 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2871 output1, output1_size,
2872 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002873 TEST_ASSERT( output1_length <=
2874 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2875 TEST_ASSERT( output1_length <=
2876 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2877
Gilles Peskine8817f612018-12-18 00:18:46 +01002878 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002879 output1 + output1_length,
2880 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002881 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002882 TEST_ASSERT( function_output_length <=
2883 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2884 TEST_ASSERT( function_output_length <=
2885 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002886
Gilles Peskine048b7f02018-06-08 14:20:49 +02002887 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002888
Gilles Peskine8817f612018-12-18 00:18:46 +01002889 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002890
2891 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002892 TEST_ASSERT( output2_size <=
2893 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2894 TEST_ASSERT( output2_size <=
2895 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002896 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002897
Steven Cooreman177deba2020-09-07 17:14:14 +02002898 if( iv_length > 0 )
2899 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002900 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2901 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002902 }
2903
Gilles Peskine8817f612018-12-18 00:18:46 +01002904 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2905 output2, output2_size,
2906 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002907 TEST_ASSERT( output2_length <=
2908 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
2909 TEST_ASSERT( output2_length <=
2910 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
2911
Gilles Peskine048b7f02018-06-08 14:20:49 +02002912 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002913 PSA_ASSERT( psa_cipher_finish( &operation2,
2914 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002915 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002916 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002917 TEST_ASSERT( function_output_length <=
2918 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2919 TEST_ASSERT( function_output_length <=
2920 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03002921
Gilles Peskine048b7f02018-06-08 14:20:49 +02002922 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002923
Gilles Peskine8817f612018-12-18 00:18:46 +01002924 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002925
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002926 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002927
2928exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002929 psa_cipher_abort( &operation1 );
2930 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002931 mbedtls_free( output1 );
2932 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002933 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002934 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03002935}
2936/* END_CASE */
2937
2938/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002939void cipher_verify_output_multipart( int alg_arg,
2940 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002941 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002942 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002943 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03002944{
Ronald Cron5425a212020-08-04 14:58:35 +02002945 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002946 psa_key_type_t key_type = key_type_arg;
2947 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002948 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002949 unsigned char iv[16] = {0};
2950 size_t iv_size = 16;
2951 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002952 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002953 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002954 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002955 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002956 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002957 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002958 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002959 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2960 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002961 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002962
Gilles Peskine8817f612018-12-18 00:18:46 +01002963 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002964
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002965 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2966 psa_set_key_algorithm( &attributes, alg );
2967 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002968
Ronald Cron5425a212020-08-04 14:58:35 +02002969 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2970 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03002971
Ronald Cron5425a212020-08-04 14:58:35 +02002972 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2973 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002974
Steven Cooreman177deba2020-09-07 17:14:14 +02002975 if( alg != PSA_ALG_ECB_NO_PADDING )
2976 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002977 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2978 iv, iv_size,
2979 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002980 }
2981
gabor-mezei-armceface22021-01-21 12:26:17 +01002982 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2983 TEST_ASSERT( output1_buffer_size <=
2984 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002985 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002986
Gilles Peskinee0866522019-02-19 19:44:00 +01002987 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002988
Gilles Peskine8817f612018-12-18 00:18:46 +01002989 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2990 output1, output1_buffer_size,
2991 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002992 TEST_ASSERT( function_output_length <=
2993 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2994 TEST_ASSERT( function_output_length <=
2995 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002996 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002997
Gilles Peskine8817f612018-12-18 00:18:46 +01002998 PSA_ASSERT( psa_cipher_update( &operation1,
2999 input->x + first_part_size,
3000 input->len - first_part_size,
3001 output1, output1_buffer_size,
3002 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003003 TEST_ASSERT( function_output_length <=
3004 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3005 alg,
3006 input->len - first_part_size ) );
3007 TEST_ASSERT( function_output_length <=
3008 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003009 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003010
Gilles Peskine8817f612018-12-18 00:18:46 +01003011 PSA_ASSERT( psa_cipher_finish( &operation1,
3012 output1 + output1_length,
3013 output1_buffer_size - output1_length,
3014 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003015 TEST_ASSERT( function_output_length <=
3016 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3017 TEST_ASSERT( function_output_length <=
3018 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003019 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003020
Gilles Peskine8817f612018-12-18 00:18:46 +01003021 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003022
Gilles Peskine048b7f02018-06-08 14:20:49 +02003023 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003024 TEST_ASSERT( output2_buffer_size <=
3025 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3026 TEST_ASSERT( output2_buffer_size <=
3027 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003028 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003029
Steven Cooreman177deba2020-09-07 17:14:14 +02003030 if( iv_length > 0 )
3031 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003032 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3033 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003034 }
Moran Pekerded84402018-06-06 16:36:50 +03003035
Gilles Peskine8817f612018-12-18 00:18:46 +01003036 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3037 output2, output2_buffer_size,
3038 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003039 TEST_ASSERT( function_output_length <=
3040 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3041 TEST_ASSERT( function_output_length <=
3042 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003043 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003044
Gilles Peskine8817f612018-12-18 00:18:46 +01003045 PSA_ASSERT( psa_cipher_update( &operation2,
3046 output1 + first_part_size,
3047 output1_length - first_part_size,
3048 output2, output2_buffer_size,
3049 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003050 TEST_ASSERT( function_output_length <=
3051 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3052 alg,
3053 output1_length - first_part_size ) );
3054 TEST_ASSERT( function_output_length <=
3055 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003056 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003057
Gilles Peskine8817f612018-12-18 00:18:46 +01003058 PSA_ASSERT( psa_cipher_finish( &operation2,
3059 output2 + output2_length,
3060 output2_buffer_size - output2_length,
3061 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003062 TEST_ASSERT( function_output_length <=
3063 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3064 TEST_ASSERT( function_output_length <=
3065 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003066 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003067
Gilles Peskine8817f612018-12-18 00:18:46 +01003068 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003069
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003070 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003071
3072exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003073 psa_cipher_abort( &operation1 );
3074 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003075 mbedtls_free( output1 );
3076 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003077 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003078 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003079}
3080/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003081
Gilles Peskine20035e32018-02-03 22:44:14 +01003082/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003083void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003084 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003085 data_t *nonce,
3086 data_t *additional_data,
3087 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003088 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003089{
Ronald Cron5425a212020-08-04 14:58:35 +02003090 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003091 psa_key_type_t key_type = key_type_arg;
3092 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003093 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003094 unsigned char *output_data = NULL;
3095 size_t output_size = 0;
3096 size_t output_length = 0;
3097 unsigned char *output_data2 = NULL;
3098 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003099 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003100 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003101 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003102
Gilles Peskine8817f612018-12-18 00:18:46 +01003103 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003104
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003105 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3106 psa_set_key_algorithm( &attributes, alg );
3107 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003108
Gilles Peskine049c7532019-05-15 20:22:09 +02003109 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003110 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003111 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3112 key_bits = psa_get_key_bits( &attributes );
3113
3114 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3115 alg );
3116 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3117 * should be exact. */
3118 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3119 expected_result != PSA_ERROR_NOT_SUPPORTED )
3120 {
3121 TEST_EQUAL( output_size,
3122 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3123 TEST_ASSERT( output_size <=
3124 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3125 }
3126 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003127
Steven Cooremanf49478b2021-02-15 15:19:25 +01003128 status = psa_aead_encrypt( key, alg,
3129 nonce->x, nonce->len,
3130 additional_data->x,
3131 additional_data->len,
3132 input_data->x, input_data->len,
3133 output_data, output_size,
3134 &output_length );
3135
3136 /* If the operation is not supported, just skip and not fail in case the
3137 * encryption involves a common limitation of cryptography hardwares and
3138 * an alternative implementation. */
3139 if( status == PSA_ERROR_NOT_SUPPORTED )
3140 {
3141 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3142 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3143 }
3144
3145 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003146
3147 if( PSA_SUCCESS == expected_result )
3148 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003149 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003150
Gilles Peskine003a4a92019-05-14 16:09:40 +02003151 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3152 * should be exact. */
3153 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003154 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003155
gabor-mezei-armceface22021-01-21 12:26:17 +01003156 TEST_ASSERT( input_data->len <=
3157 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3158
Ronald Cron5425a212020-08-04 14:58:35 +02003159 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003160 nonce->x, nonce->len,
3161 additional_data->x,
3162 additional_data->len,
3163 output_data, output_length,
3164 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003165 &output_length2 ),
3166 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003167
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003168 ASSERT_COMPARE( input_data->x, input_data->len,
3169 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003170 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003171
Gilles Peskinea1cac842018-06-11 19:33:02 +02003172exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003173 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003174 mbedtls_free( output_data );
3175 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003176 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003177}
3178/* END_CASE */
3179
3180/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003181void aead_encrypt( int key_type_arg, data_t *key_data,
3182 int alg_arg,
3183 data_t *nonce,
3184 data_t *additional_data,
3185 data_t *input_data,
3186 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003187{
Ronald Cron5425a212020-08-04 14:58:35 +02003188 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003189 psa_key_type_t key_type = key_type_arg;
3190 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003191 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003192 unsigned char *output_data = NULL;
3193 size_t output_size = 0;
3194 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003195 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003196 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003197
Gilles Peskine8817f612018-12-18 00:18:46 +01003198 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003199
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003200 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3201 psa_set_key_algorithm( &attributes, alg );
3202 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003203
Gilles Peskine049c7532019-05-15 20:22:09 +02003204 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003205 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003206 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3207 key_bits = psa_get_key_bits( &attributes );
3208
3209 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3210 alg );
3211 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3212 * should be exact. */
3213 TEST_EQUAL( output_size,
3214 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3215 TEST_ASSERT( output_size <=
3216 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3217 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003218
Steven Cooremand588ea12021-01-11 19:36:04 +01003219 status = psa_aead_encrypt( key, alg,
3220 nonce->x, nonce->len,
3221 additional_data->x, additional_data->len,
3222 input_data->x, input_data->len,
3223 output_data, output_size,
3224 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003225
Ronald Cron28a45ed2021-02-09 20:35:42 +01003226 /* If the operation is not supported, just skip and not fail in case the
3227 * encryption involves a common limitation of cryptography hardwares and
3228 * an alternative implementation. */
3229 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003230 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003231 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3232 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003233 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003234
3235 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003236 ASSERT_COMPARE( expected_result->x, expected_result->len,
3237 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003238
Gilles Peskinea1cac842018-06-11 19:33:02 +02003239exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003240 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003241 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003242 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003243}
3244/* END_CASE */
3245
3246/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003247void aead_decrypt( int key_type_arg, data_t *key_data,
3248 int alg_arg,
3249 data_t *nonce,
3250 data_t *additional_data,
3251 data_t *input_data,
3252 data_t *expected_data,
3253 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003254{
Ronald Cron5425a212020-08-04 14:58:35 +02003255 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003256 psa_key_type_t key_type = key_type_arg;
3257 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003258 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003259 unsigned char *output_data = NULL;
3260 size_t output_size = 0;
3261 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003262 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003263 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003264 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003265
Gilles Peskine8817f612018-12-18 00:18:46 +01003266 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003267
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003268 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3269 psa_set_key_algorithm( &attributes, alg );
3270 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003271
Gilles Peskine049c7532019-05-15 20:22:09 +02003272 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003273 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003274 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3275 key_bits = psa_get_key_bits( &attributes );
3276
3277 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3278 alg );
3279 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3280 expected_result != PSA_ERROR_NOT_SUPPORTED )
3281 {
3282 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3283 * should be exact. */
3284 TEST_EQUAL( output_size,
3285 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3286 TEST_ASSERT( output_size <=
3287 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3288 }
3289 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003290
Steven Cooremand588ea12021-01-11 19:36:04 +01003291 status = psa_aead_decrypt( key, alg,
3292 nonce->x, nonce->len,
3293 additional_data->x,
3294 additional_data->len,
3295 input_data->x, input_data->len,
3296 output_data, output_size,
3297 &output_length );
3298
Ronald Cron28a45ed2021-02-09 20:35:42 +01003299 /* If the operation is not supported, just skip and not fail in case the
3300 * decryption involves a common limitation of cryptography hardwares and
3301 * an alternative implementation. */
3302 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003303 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003304 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3305 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003306 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003307
3308 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003309
Gilles Peskine2d277862018-06-18 15:41:12 +02003310 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003311 ASSERT_COMPARE( expected_data->x, expected_data->len,
3312 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003313
Gilles Peskinea1cac842018-06-11 19:33:02 +02003314exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003315 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003316 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003317 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003318}
3319/* END_CASE */
3320
3321/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003322void signature_size( int type_arg,
3323 int bits,
3324 int alg_arg,
3325 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003326{
3327 psa_key_type_t type = type_arg;
3328 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003329 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003330
Gilles Peskinefe11b722018-12-18 00:24:04 +01003331 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003332#if defined(MBEDTLS_TEST_DEPRECATED)
3333 TEST_EQUAL( actual_size,
3334 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3335#endif /* MBEDTLS_TEST_DEPRECATED */
3336
Gilles Peskinee59236f2018-01-27 23:32:46 +01003337exit:
3338 ;
3339}
3340/* END_CASE */
3341
3342/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003343void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3344 int alg_arg, data_t *input_data,
3345 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003346{
Ronald Cron5425a212020-08-04 14:58:35 +02003347 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003348 psa_key_type_t key_type = key_type_arg;
3349 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003350 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003351 unsigned char *signature = NULL;
3352 size_t signature_size;
3353 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003354 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003355
Gilles Peskine8817f612018-12-18 00:18:46 +01003356 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003357
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003358 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003359 psa_set_key_algorithm( &attributes, alg );
3360 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003361
Gilles Peskine049c7532019-05-15 20:22:09 +02003362 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003363 &key ) );
3364 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003365 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003366
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003367 /* Allocate a buffer which has the size advertized by the
3368 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003369 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003370 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003371 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003372 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003373 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003374
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003375 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003376 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003377 input_data->x, input_data->len,
3378 signature, signature_size,
3379 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003380 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003381 ASSERT_COMPARE( output_data->x, output_data->len,
3382 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003383
Gilles Peskine0627f982019-11-26 19:12:16 +01003384#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003385 memset( signature, 0, signature_size );
3386 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003387 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003388 input_data->x, input_data->len,
3389 signature, signature_size,
3390 &signature_length ) );
3391 ASSERT_COMPARE( output_data->x, output_data->len,
3392 signature, signature_length );
3393#endif /* MBEDTLS_TEST_DEPRECATED */
3394
Gilles Peskine20035e32018-02-03 22:44:14 +01003395exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003396 /*
3397 * Key attributes may have been returned by psa_get_key_attributes()
3398 * thus reset them as required.
3399 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003400 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003401
Ronald Cron5425a212020-08-04 14:58:35 +02003402 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003403 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003404 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003405}
3406/* END_CASE */
3407
3408/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003409void sign_hash_fail( int key_type_arg, data_t *key_data,
3410 int alg_arg, data_t *input_data,
3411 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003412{
Ronald Cron5425a212020-08-04 14:58:35 +02003413 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003414 psa_key_type_t key_type = key_type_arg;
3415 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003416 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003417 psa_status_t actual_status;
3418 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003419 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003420 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003421 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003422
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003423 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003424
Gilles Peskine8817f612018-12-18 00:18:46 +01003425 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003426
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003427 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003428 psa_set_key_algorithm( &attributes, alg );
3429 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003430
Gilles Peskine049c7532019-05-15 20:22:09 +02003431 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003432 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003433
Ronald Cron5425a212020-08-04 14:58:35 +02003434 actual_status = 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 Peskinefe11b722018-12-18 00:24:04 +01003438 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003439 /* The value of *signature_length is unspecified on error, but
3440 * whatever it is, it should be less than signature_size, so that
3441 * if the caller tries to read *signature_length bytes without
3442 * checking the error code then they don't overflow a buffer. */
3443 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003444
Gilles Peskine895242b2019-11-29 12:15:40 +01003445#if defined(MBEDTLS_TEST_DEPRECATED)
3446 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003447 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003448 input_data->x, input_data->len,
3449 signature, signature_size,
3450 &signature_length ),
3451 expected_status );
3452 TEST_ASSERT( signature_length <= signature_size );
3453#endif /* MBEDTLS_TEST_DEPRECATED */
3454
Gilles Peskine20035e32018-02-03 22:44:14 +01003455exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003456 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003457 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003458 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003459 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003460}
3461/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003462
3463/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003464void sign_verify_hash( int key_type_arg, data_t *key_data,
3465 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003466{
Ronald Cron5425a212020-08-04 14:58:35 +02003467 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003468 psa_key_type_t key_type = key_type_arg;
3469 psa_algorithm_t alg = alg_arg;
3470 size_t key_bits;
3471 unsigned char *signature = NULL;
3472 size_t signature_size;
3473 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003474 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003475
Gilles Peskine8817f612018-12-18 00:18:46 +01003476 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003477
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003478 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003479 psa_set_key_algorithm( &attributes, alg );
3480 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003481
Gilles Peskine049c7532019-05-15 20:22:09 +02003482 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003483 &key ) );
3484 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003485 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003486
3487 /* Allocate a buffer which has the size advertized by the
3488 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003489 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003490 key_bits, alg );
3491 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003492 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003493 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003494
3495 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003496 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003497 input_data->x, input_data->len,
3498 signature, signature_size,
3499 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003500 /* Check that the signature length looks sensible. */
3501 TEST_ASSERT( signature_length <= signature_size );
3502 TEST_ASSERT( signature_length > 0 );
3503
3504 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003505 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003506 input_data->x, input_data->len,
3507 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003508
3509 if( input_data->len != 0 )
3510 {
3511 /* Flip a bit in the input and verify that the signature is now
3512 * detected as invalid. Flip a bit at the beginning, not at the end,
3513 * because ECDSA may ignore the last few bits of the input. */
3514 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003515 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003516 input_data->x, input_data->len,
3517 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003518 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003519 }
3520
3521exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003522 /*
3523 * Key attributes may have been returned by psa_get_key_attributes()
3524 * thus reset them as required.
3525 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003526 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003527
Ronald Cron5425a212020-08-04 14:58:35 +02003528 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003529 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003530 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003531}
3532/* END_CASE */
3533
3534/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003535void verify_hash( int key_type_arg, data_t *key_data,
3536 int alg_arg, data_t *hash_data,
3537 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003538{
Ronald Cron5425a212020-08-04 14:58:35 +02003539 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003540 psa_key_type_t key_type = key_type_arg;
3541 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003542 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003543
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003544 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003545
Gilles Peskine8817f612018-12-18 00:18:46 +01003546 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003547
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003548 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003549 psa_set_key_algorithm( &attributes, alg );
3550 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003551
Gilles Peskine049c7532019-05-15 20:22:09 +02003552 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003553 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003554
Ronald Cron5425a212020-08-04 14:58:35 +02003555 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003556 hash_data->x, hash_data->len,
3557 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003558
3559#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003560 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003561 hash_data->x, hash_data->len,
3562 signature_data->x,
3563 signature_data->len ) );
3564
3565#endif /* MBEDTLS_TEST_DEPRECATED */
3566
itayzafrir5c753392018-05-08 11:18:38 +03003567exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003568 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003569 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003570 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003571}
3572/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003573
3574/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003575void verify_hash_fail( int key_type_arg, data_t *key_data,
3576 int alg_arg, data_t *hash_data,
3577 data_t *signature_data,
3578 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003579{
Ronald Cron5425a212020-08-04 14:58:35 +02003580 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003581 psa_key_type_t key_type = key_type_arg;
3582 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003583 psa_status_t actual_status;
3584 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003585 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003586
Gilles Peskine8817f612018-12-18 00:18:46 +01003587 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003588
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003589 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003590 psa_set_key_algorithm( &attributes, alg );
3591 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003592
Gilles Peskine049c7532019-05-15 20:22:09 +02003593 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003594 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003595
Ronald Cron5425a212020-08-04 14:58:35 +02003596 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003597 hash_data->x, hash_data->len,
3598 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003599 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003600
Gilles Peskine895242b2019-11-29 12:15:40 +01003601#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003602 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003603 hash_data->x, hash_data->len,
3604 signature_data->x, signature_data->len ),
3605 expected_status );
3606#endif /* MBEDTLS_TEST_DEPRECATED */
3607
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003608exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003609 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003610 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003611 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003612}
3613/* END_CASE */
3614
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003615/* BEGIN_CASE */
gabor-mezei-armabd72582021-04-15 18:19:50 +02003616void sign_message_deterministic( int key_type_arg,
3617 data_t *key_data,
3618 int alg_arg,
3619 data_t *input_data,
3620 data_t *output_data )
3621{
3622 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3623 psa_key_type_t key_type = key_type_arg;
3624 psa_algorithm_t alg = alg_arg;
3625 size_t key_bits;
3626 unsigned char *signature = NULL;
3627 size_t signature_size;
3628 size_t signature_length = 0xdeadbeef;
3629 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3630
3631 PSA_ASSERT( psa_crypto_init( ) );
3632
3633 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3634 psa_set_key_algorithm( &attributes, alg );
3635 psa_set_key_type( &attributes, key_type );
3636
3637 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3638 &key ) );
3639 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3640 key_bits = psa_get_key_bits( &attributes );
3641
3642 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3643 TEST_ASSERT( signature_size != 0 );
3644 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3645 ASSERT_ALLOC( signature, signature_size );
3646
3647 PSA_ASSERT( psa_sign_message( key, alg,
3648 input_data->x, input_data->len,
3649 signature, signature_size,
3650 &signature_length ) );
3651
3652 ASSERT_COMPARE( output_data->x, output_data->len,
3653 signature, signature_length );
3654
3655exit:
3656 psa_reset_key_attributes( &attributes );
3657
3658 psa_destroy_key( key );
3659 mbedtls_free( signature );
3660 PSA_DONE( );
3661
3662}
3663/* END_CASE */
3664
3665/* BEGIN_CASE */
3666void sign_message_fail( int key_type_arg,
3667 data_t *key_data,
3668 int alg_arg,
3669 data_t *input_data,
3670 int signature_size_arg,
3671 int expected_status_arg )
3672{
3673 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3674 psa_key_type_t key_type = key_type_arg;
3675 psa_algorithm_t alg = alg_arg;
3676 size_t signature_size = signature_size_arg;
3677 psa_status_t actual_status;
3678 psa_status_t expected_status = expected_status_arg;
3679 unsigned char *signature = NULL;
3680 size_t signature_length = 0xdeadbeef;
3681 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3682
3683 ASSERT_ALLOC( signature, signature_size );
3684
3685 PSA_ASSERT( psa_crypto_init( ) );
3686
3687 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3688 psa_set_key_algorithm( &attributes, alg );
3689 psa_set_key_type( &attributes, key_type );
3690
3691 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3692 &key ) );
3693
3694 actual_status = psa_sign_message( key, alg,
3695 input_data->x, input_data->len,
3696 signature, signature_size,
3697 &signature_length );
3698 TEST_EQUAL( actual_status, expected_status );
3699 /* The value of *signature_length is unspecified on error, but
3700 * whatever it is, it should be less than signature_size, so that
3701 * if the caller tries to read *signature_length bytes without
3702 * checking the error code then they don't overflow a buffer. */
3703 TEST_ASSERT( signature_length <= signature_size );
3704
3705exit:
3706 psa_reset_key_attributes( &attributes );
3707 psa_destroy_key( key );
3708 mbedtls_free( signature );
3709 PSA_DONE( );
3710}
3711/* END_CASE */
3712
3713/* BEGIN_CASE */
3714void sign_verify_message( int key_type_arg,
3715 data_t *key_data,
3716 int alg_arg,
3717 data_t *input_data )
3718{
3719 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3720 psa_key_type_t key_type = key_type_arg;
3721 psa_algorithm_t alg = alg_arg;
3722 size_t key_bits;
3723 unsigned char *signature = NULL;
3724 size_t signature_size;
3725 size_t signature_length = 0xdeadbeef;
3726 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3727
3728 PSA_ASSERT( psa_crypto_init( ) );
3729
3730 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3731 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 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3738 key_bits = psa_get_key_bits( &attributes );
3739
3740 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3741 TEST_ASSERT( signature_size != 0 );
3742 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3743 ASSERT_ALLOC( signature, signature_size );
3744
3745 PSA_ASSERT( psa_sign_message( key, alg,
3746 input_data->x, input_data->len,
3747 signature, signature_size,
3748 &signature_length ) );
3749 TEST_ASSERT( signature_length <= signature_size );
3750 TEST_ASSERT( signature_length > 0 );
3751
3752 PSA_ASSERT( psa_verify_message( key, alg,
3753 input_data->x, input_data->len,
3754 signature, signature_length ) );
3755
3756 if( input_data->len != 0 )
3757 {
3758 /* Flip a bit in the input and verify that the signature is now
3759 * detected as invalid. Flip a bit at the beginning, not at the end,
3760 * because ECDSA may ignore the last few bits of the input. */
3761 input_data->x[0] ^= 1;
3762 TEST_EQUAL( psa_verify_message( key, alg,
3763 input_data->x, input_data->len,
3764 signature, signature_length ),
3765 PSA_ERROR_INVALID_SIGNATURE );
3766 }
3767
3768exit:
3769 psa_reset_key_attributes( &attributes );
3770
3771 psa_destroy_key( key );
3772 mbedtls_free( signature );
3773 PSA_DONE( );
3774}
3775/* END_CASE */
3776
3777/* BEGIN_CASE */
3778void verify_message( int key_type_arg,
3779 data_t *key_data,
3780 int alg_arg,
3781 data_t *input_data,
3782 data_t *signature_data )
3783{
3784 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3785 psa_key_type_t key_type = key_type_arg;
3786 psa_algorithm_t alg = alg_arg;
3787 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3788
3789 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3790
3791 PSA_ASSERT( psa_crypto_init( ) );
3792
3793 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3794 psa_set_key_algorithm( &attributes, alg );
3795 psa_set_key_type( &attributes, key_type );
3796
3797 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3798 &key ) );
3799
3800 PSA_ASSERT( psa_verify_message( key, alg,
3801 input_data->x, input_data->len,
3802 signature_data->x, signature_data->len ) );
3803
3804exit:
3805 psa_reset_key_attributes( &attributes );
3806 psa_destroy_key( key );
3807 PSA_DONE( );
3808}
3809/* END_CASE */
3810
3811/* BEGIN_CASE */
3812void verify_message_fail( int key_type_arg,
3813 data_t *key_data,
3814 int alg_arg,
3815 data_t *hash_data,
3816 data_t *signature_data,
3817 int expected_status_arg )
3818{
3819 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3820 psa_key_type_t key_type = key_type_arg;
3821 psa_algorithm_t alg = alg_arg;
3822 psa_status_t actual_status;
3823 psa_status_t expected_status = expected_status_arg;
3824 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3825
3826 PSA_ASSERT( psa_crypto_init( ) );
3827
3828 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3829 psa_set_key_algorithm( &attributes, alg );
3830 psa_set_key_type( &attributes, key_type );
3831
3832 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3833 &key ) );
3834
3835 actual_status = psa_verify_message( key, alg,
3836 hash_data->x, hash_data->len,
3837 signature_data->x,
3838 signature_data->len );
3839 TEST_EQUAL( actual_status, expected_status );
3840
3841exit:
3842 psa_reset_key_attributes( &attributes );
3843 psa_destroy_key( key );
3844 PSA_DONE( );
3845}
3846/* END_CASE */
3847
3848/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003849void asymmetric_encrypt( int key_type_arg,
3850 data_t *key_data,
3851 int alg_arg,
3852 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003853 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003854 int expected_output_length_arg,
3855 int expected_status_arg )
3856{
Ronald Cron5425a212020-08-04 14:58:35 +02003857 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003858 psa_key_type_t key_type = key_type_arg;
3859 psa_algorithm_t alg = alg_arg;
3860 size_t expected_output_length = expected_output_length_arg;
3861 size_t key_bits;
3862 unsigned char *output = NULL;
3863 size_t output_size;
3864 size_t output_length = ~0;
3865 psa_status_t actual_status;
3866 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003867 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003868
Gilles Peskine8817f612018-12-18 00:18:46 +01003869 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003870
Gilles Peskine656896e2018-06-29 19:12:28 +02003871 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003872 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3873 psa_set_key_algorithm( &attributes, alg );
3874 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003875 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003876 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003877
3878 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003879 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003880 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003881
Gilles Peskine656896e2018-06-29 19:12:28 +02003882 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003883 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003884 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003885
3886 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003887 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003888 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003889 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003890 output, output_size,
3891 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003892 TEST_EQUAL( actual_status, expected_status );
3893 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003894
Gilles Peskine68428122018-06-30 18:42:41 +02003895 /* If the label is empty, the test framework puts a non-null pointer
3896 * in label->x. Test that a null pointer works as well. */
3897 if( label->len == 0 )
3898 {
3899 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003900 if( output_size != 0 )
3901 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003902 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003903 input_data->x, input_data->len,
3904 NULL, label->len,
3905 output, output_size,
3906 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003907 TEST_EQUAL( actual_status, expected_status );
3908 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003909 }
3910
Gilles Peskine656896e2018-06-29 19:12:28 +02003911exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003912 /*
3913 * Key attributes may have been returned by psa_get_key_attributes()
3914 * thus reset them as required.
3915 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003916 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003917
Ronald Cron5425a212020-08-04 14:58:35 +02003918 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02003919 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003920 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003921}
3922/* END_CASE */
3923
3924/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003925void asymmetric_encrypt_decrypt( int key_type_arg,
3926 data_t *key_data,
3927 int alg_arg,
3928 data_t *input_data,
3929 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003930{
Ronald Cron5425a212020-08-04 14:58:35 +02003931 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003932 psa_key_type_t key_type = key_type_arg;
3933 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003934 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003935 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003936 size_t output_size;
3937 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003938 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003939 size_t output2_size;
3940 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003941 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003942
Gilles Peskine8817f612018-12-18 00:18:46 +01003943 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003944
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003945 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3946 psa_set_key_algorithm( &attributes, alg );
3947 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003948
Gilles Peskine049c7532019-05-15 20:22:09 +02003949 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003950 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003951
3952 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02003953 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003954 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003955
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003956 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003957 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003958 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01003959
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003960 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01003961 TEST_ASSERT( output2_size <=
3962 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
3963 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003964 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003965
Gilles Peskineeebd7382018-06-08 18:11:54 +02003966 /* We test encryption by checking that encrypt-then-decrypt gives back
3967 * the original plaintext because of the non-optional random
3968 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02003969 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003970 input_data->x, input_data->len,
3971 label->x, label->len,
3972 output, output_size,
3973 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003974 /* We don't know what ciphertext length to expect, but check that
3975 * it looks sensible. */
3976 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003977
Ronald Cron5425a212020-08-04 14:58:35 +02003978 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003979 output, output_length,
3980 label->x, label->len,
3981 output2, output2_size,
3982 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003983 ASSERT_COMPARE( input_data->x, input_data->len,
3984 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003985
3986exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003987 /*
3988 * Key attributes may have been returned by psa_get_key_attributes()
3989 * thus reset them as required.
3990 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003991 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003992
Ronald Cron5425a212020-08-04 14:58:35 +02003993 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003994 mbedtls_free( output );
3995 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003996 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003997}
3998/* END_CASE */
3999
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004000/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004001void asymmetric_decrypt( int key_type_arg,
4002 data_t *key_data,
4003 int alg_arg,
4004 data_t *input_data,
4005 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004006 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004007{
Ronald Cron5425a212020-08-04 14:58:35 +02004008 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004009 psa_key_type_t key_type = key_type_arg;
4010 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004011 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004012 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004013 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004014 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004015 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004016
Gilles Peskine8817f612018-12-18 00:18:46 +01004017 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004018
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004019 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4020 psa_set_key_algorithm( &attributes, alg );
4021 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004022
Gilles Peskine049c7532019-05-15 20:22:09 +02004023 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004024 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004025
gabor-mezei-armceface22021-01-21 12:26:17 +01004026 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4027 key_bits = psa_get_key_bits( &attributes );
4028
4029 /* Determine the maximum ciphertext length */
4030 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
4031 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
4032 ASSERT_ALLOC( output, output_size );
4033
Ronald Cron5425a212020-08-04 14:58:35 +02004034 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004035 input_data->x, input_data->len,
4036 label->x, label->len,
4037 output,
4038 output_size,
4039 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004040 ASSERT_COMPARE( expected_data->x, expected_data->len,
4041 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004042
Gilles Peskine68428122018-06-30 18:42:41 +02004043 /* If the label is empty, the test framework puts a non-null pointer
4044 * in label->x. Test that a null pointer works as well. */
4045 if( label->len == 0 )
4046 {
4047 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004048 if( output_size != 0 )
4049 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004050 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004051 input_data->x, input_data->len,
4052 NULL, label->len,
4053 output,
4054 output_size,
4055 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004056 ASSERT_COMPARE( expected_data->x, expected_data->len,
4057 output, output_length );
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 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004063 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004064 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004065}
4066/* END_CASE */
4067
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004068/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004069void asymmetric_decrypt_fail( int key_type_arg,
4070 data_t *key_data,
4071 int alg_arg,
4072 data_t *input_data,
4073 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004074 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004075 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004076{
Ronald Cron5425a212020-08-04 14:58:35 +02004077 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004078 psa_key_type_t key_type = key_type_arg;
4079 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004080 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004081 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004082 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004083 psa_status_t actual_status;
4084 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004085 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004086
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004087 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004088
Gilles Peskine8817f612018-12-18 00:18:46 +01004089 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004090
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004091 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4092 psa_set_key_algorithm( &attributes, alg );
4093 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004094
Gilles Peskine049c7532019-05-15 20:22:09 +02004095 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004096 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004097
Ronald Cron5425a212020-08-04 14:58:35 +02004098 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004099 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004100 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004101 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004102 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004103 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004104 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004105
Gilles Peskine68428122018-06-30 18:42:41 +02004106 /* If the label is empty, the test framework puts a non-null pointer
4107 * in label->x. Test that a null pointer works as well. */
4108 if( label->len == 0 )
4109 {
4110 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004111 if( output_size != 0 )
4112 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004113 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004114 input_data->x, input_data->len,
4115 NULL, label->len,
4116 output, output_size,
4117 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004118 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004119 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004120 }
4121
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004122exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004123 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004124 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004125 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004126 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004127}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004128/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004129
4130/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004131void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004132{
4133 /* Test each valid way of initializing the object, except for `= {0}`, as
4134 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4135 * though it's OK by the C standard. We could test for this, but we'd need
4136 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004137 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004138 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4139 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4140 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004141
4142 memset( &zero, 0, sizeof( zero ) );
4143
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004144 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004145 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004146 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004147 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004148 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004149 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004150 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004151
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004152 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004153 PSA_ASSERT( psa_key_derivation_abort(&func) );
4154 PSA_ASSERT( psa_key_derivation_abort(&init) );
4155 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004156}
4157/* END_CASE */
4158
Janos Follath16de4a42019-06-13 16:32:24 +01004159/* BEGIN_CASE */
4160void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004161{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004162 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004163 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004164 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004165
Gilles Peskine8817f612018-12-18 00:18:46 +01004166 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004167
Janos Follath16de4a42019-06-13 16:32:24 +01004168 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004169 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004170
4171exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004172 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004173 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004174}
4175/* END_CASE */
4176
Janos Follathaf3c2a02019-06-12 12:34:34 +01004177/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004178void derive_set_capacity( int alg_arg, int capacity_arg,
4179 int expected_status_arg )
4180{
4181 psa_algorithm_t alg = alg_arg;
4182 size_t capacity = capacity_arg;
4183 psa_status_t expected_status = expected_status_arg;
4184 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4185
4186 PSA_ASSERT( psa_crypto_init( ) );
4187
4188 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4189
4190 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4191 expected_status );
4192
4193exit:
4194 psa_key_derivation_abort( &operation );
4195 PSA_DONE( );
4196}
4197/* END_CASE */
4198
4199/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004200void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004201 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004202 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004203 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004204 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004205 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004206 int expected_status_arg3,
4207 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004208{
4209 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004210 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4211 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004212 psa_status_t expected_statuses[] = {expected_status_arg1,
4213 expected_status_arg2,
4214 expected_status_arg3};
4215 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004216 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4217 MBEDTLS_SVC_KEY_ID_INIT,
4218 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004219 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4220 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4221 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004222 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004223 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004224 psa_status_t expected_output_status = expected_output_status_arg;
4225 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004226
4227 PSA_ASSERT( psa_crypto_init( ) );
4228
4229 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4230 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004231
4232 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4233
4234 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4235 {
Gilles Peskinef6279312021-05-27 13:21:20 +02004236 mbedtls_test_set_step( i );
4237 if( steps[i] == 0 )
4238 {
4239 /* Skip this step */
4240 }
4241 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004242 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004243 psa_set_key_type( &attributes, key_types[i] );
4244 PSA_ASSERT( psa_import_key( &attributes,
4245 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004246 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004247 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4248 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4249 {
4250 // When taking a private key as secret input, use key agreement
4251 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004252 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4253 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004254 expected_statuses[i] );
4255 }
4256 else
4257 {
4258 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004259 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004260 expected_statuses[i] );
4261 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004262 }
4263 else
4264 {
4265 TEST_EQUAL( psa_key_derivation_input_bytes(
4266 &operation, steps[i],
4267 inputs[i]->x, inputs[i]->len ),
4268 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004269 }
4270 }
4271
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004272 if( output_key_type != PSA_KEY_TYPE_NONE )
4273 {
4274 psa_reset_key_attributes( &attributes );
4275 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4276 psa_set_key_bits( &attributes, 8 );
4277 actual_output_status =
4278 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004279 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004280 }
4281 else
4282 {
4283 uint8_t buffer[1];
4284 actual_output_status =
4285 psa_key_derivation_output_bytes( &operation,
4286 buffer, sizeof( buffer ) );
4287 }
4288 TEST_EQUAL( actual_output_status, expected_output_status );
4289
Janos Follathaf3c2a02019-06-12 12:34:34 +01004290exit:
4291 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004292 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4293 psa_destroy_key( keys[i] );
4294 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004295 PSA_DONE( );
4296}
4297/* END_CASE */
4298
Janos Follathd958bb72019-07-03 15:02:16 +01004299/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004300void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004301{
Janos Follathd958bb72019-07-03 15:02:16 +01004302 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004303 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004304 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004305 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004306 unsigned char input1[] = "Input 1";
4307 size_t input1_length = sizeof( input1 );
4308 unsigned char input2[] = "Input 2";
4309 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004310 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004311 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004312 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4313 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4314 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004315 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004316
Gilles Peskine8817f612018-12-18 00:18:46 +01004317 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004318
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004319 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4320 psa_set_key_algorithm( &attributes, alg );
4321 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004322
Gilles Peskine73676cb2019-05-15 20:15:10 +02004323 PSA_ASSERT( psa_import_key( &attributes,
4324 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004325 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004326
4327 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004328 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4329 input1, input1_length,
4330 input2, input2_length,
4331 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004332 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004333
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004334 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004335 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004336 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004337
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004338 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004339
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004340 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004341 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004342
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004343exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004344 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004345 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004346 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004347}
4348/* END_CASE */
4349
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004350/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004351void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004352{
4353 uint8_t output_buffer[16];
4354 size_t buffer_size = 16;
4355 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004356 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004357
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004358 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4359 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004360 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004361
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004362 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004363 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004364
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004365 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004366
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004367 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4368 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004369 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004370
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004371 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004372 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004373
4374exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004375 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004376}
4377/* END_CASE */
4378
4379/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004380void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004381 int step1_arg, data_t *input1,
4382 int step2_arg, data_t *input2,
4383 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004384 int requested_capacity_arg,
4385 data_t *expected_output1,
4386 data_t *expected_output2 )
4387{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004388 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004389 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4390 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004391 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4392 MBEDTLS_SVC_KEY_ID_INIT,
4393 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004394 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004395 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004396 uint8_t *expected_outputs[2] =
4397 {expected_output1->x, expected_output2->x};
4398 size_t output_sizes[2] =
4399 {expected_output1->len, expected_output2->len};
4400 size_t output_buffer_size = 0;
4401 uint8_t *output_buffer = NULL;
4402 size_t expected_capacity;
4403 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004404 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004405 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004406 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004407
4408 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4409 {
4410 if( output_sizes[i] > output_buffer_size )
4411 output_buffer_size = output_sizes[i];
4412 if( output_sizes[i] == 0 )
4413 expected_outputs[i] = NULL;
4414 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004415 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004416 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004417
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004418 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4419 psa_set_key_algorithm( &attributes, alg );
4420 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004421
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004422 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004423 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4424 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4425 requested_capacity ) );
4426 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004427 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004428 switch( steps[i] )
4429 {
4430 case 0:
4431 break;
4432 case PSA_KEY_DERIVATION_INPUT_SECRET:
4433 PSA_ASSERT( psa_import_key( &attributes,
4434 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004435 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004436
4437 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4438 {
4439 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4440 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4441 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4442 }
4443
Gilles Peskine1468da72019-05-29 17:35:49 +02004444 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004445 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004446 break;
4447 default:
4448 PSA_ASSERT( psa_key_derivation_input_bytes(
4449 &operation, steps[i],
4450 inputs[i]->x, inputs[i]->len ) );
4451 break;
4452 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004453 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004454
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004455 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004456 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004457 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004458 expected_capacity = requested_capacity;
4459
4460 /* Expansion phase. */
4461 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4462 {
4463 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004464 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004465 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004466 if( expected_capacity == 0 && output_sizes[i] == 0 )
4467 {
4468 /* Reading 0 bytes when 0 bytes are available can go either way. */
4469 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004470 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004471 continue;
4472 }
4473 else if( expected_capacity == 0 ||
4474 output_sizes[i] > expected_capacity )
4475 {
4476 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004477 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004478 expected_capacity = 0;
4479 continue;
4480 }
4481 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004482 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004483 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004484 ASSERT_COMPARE( output_buffer, output_sizes[i],
4485 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004486 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004487 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004488 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004489 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004490 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004491 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004492 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004493
4494exit:
4495 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004496 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004497 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4498 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004499 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004500}
4501/* END_CASE */
4502
4503/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004504void derive_full( int alg_arg,
4505 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004506 data_t *input1,
4507 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004508 int requested_capacity_arg )
4509{
Ronald Cron5425a212020-08-04 14:58:35 +02004510 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004511 psa_algorithm_t alg = alg_arg;
4512 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004513 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004514 unsigned char output_buffer[16];
4515 size_t expected_capacity = requested_capacity;
4516 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004517 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004518
Gilles Peskine8817f612018-12-18 00:18:46 +01004519 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004520
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004521 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4522 psa_set_key_algorithm( &attributes, alg );
4523 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004524
Gilles Peskine049c7532019-05-15 20:22:09 +02004525 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004526 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004527
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004528 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4529 input1->x, input1->len,
4530 input2->x, input2->len,
4531 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004532 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004533
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004534 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004535 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004536 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004537
4538 /* Expansion phase. */
4539 while( current_capacity > 0 )
4540 {
4541 size_t read_size = sizeof( output_buffer );
4542 if( read_size > current_capacity )
4543 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004544 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004545 output_buffer,
4546 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004547 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004548 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004549 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004550 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004551 }
4552
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004553 /* Check that the operation refuses to go over capacity. */
4554 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004555 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004556
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004557 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004558
4559exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004560 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004561 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004562 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004563}
4564/* END_CASE */
4565
Janos Follathe60c9052019-07-03 13:51:30 +01004566/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004567void derive_key_exercise( int alg_arg,
4568 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004569 data_t *input1,
4570 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004571 int derived_type_arg,
4572 int derived_bits_arg,
4573 int derived_usage_arg,
4574 int derived_alg_arg )
4575{
Ronald Cron5425a212020-08-04 14:58:35 +02004576 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4577 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004578 psa_algorithm_t alg = alg_arg;
4579 psa_key_type_t derived_type = derived_type_arg;
4580 size_t derived_bits = derived_bits_arg;
4581 psa_key_usage_t derived_usage = derived_usage_arg;
4582 psa_algorithm_t derived_alg = derived_alg_arg;
4583 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004584 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004585 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004586 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004587
Gilles Peskine8817f612018-12-18 00:18:46 +01004588 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004589
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004590 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4591 psa_set_key_algorithm( &attributes, alg );
4592 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004593 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004594 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004595
4596 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004597 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4598 input1->x, input1->len,
4599 input2->x, input2->len,
4600 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004601 goto exit;
4602
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004603 psa_set_key_usage_flags( &attributes, derived_usage );
4604 psa_set_key_algorithm( &attributes, derived_alg );
4605 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004606 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004607 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004608 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004609
4610 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004611 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004612 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4613 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004614
4615 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004616 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004617 goto exit;
4618
4619exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004620 /*
4621 * Key attributes may have been returned by psa_get_key_attributes()
4622 * thus reset them as required.
4623 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004624 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004625
4626 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004627 psa_destroy_key( base_key );
4628 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004629 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004630}
4631/* END_CASE */
4632
Janos Follath42fd8882019-07-03 14:17:09 +01004633/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004634void derive_key_export( int alg_arg,
4635 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004636 data_t *input1,
4637 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004638 int bytes1_arg,
4639 int bytes2_arg )
4640{
Ronald Cron5425a212020-08-04 14:58:35 +02004641 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4642 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004643 psa_algorithm_t alg = alg_arg;
4644 size_t bytes1 = bytes1_arg;
4645 size_t bytes2 = bytes2_arg;
4646 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004647 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004648 uint8_t *output_buffer = NULL;
4649 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004650 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4651 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004652 size_t length;
4653
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004654 ASSERT_ALLOC( output_buffer, capacity );
4655 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004656 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004657
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004658 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4659 psa_set_key_algorithm( &base_attributes, alg );
4660 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004661 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004662 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004663
4664 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004665 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4666 input1->x, input1->len,
4667 input2->x, input2->len,
4668 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004669 goto exit;
4670
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004671 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004672 output_buffer,
4673 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004674 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004675
4676 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004677 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4678 input1->x, input1->len,
4679 input2->x, input2->len,
4680 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004681 goto exit;
4682
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004683 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4684 psa_set_key_algorithm( &derived_attributes, 0 );
4685 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004686 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004687 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004688 &derived_key ) );
4689 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004690 export_buffer, bytes1,
4691 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004692 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004693 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004694 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004695 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004696 &derived_key ) );
4697 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004698 export_buffer + bytes1, bytes2,
4699 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004700 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004701
4702 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004703 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4704 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004705
4706exit:
4707 mbedtls_free( output_buffer );
4708 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004709 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004710 psa_destroy_key( base_key );
4711 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004712 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004713}
4714/* END_CASE */
4715
4716/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004717void derive_key( int alg_arg,
4718 data_t *key_data, data_t *input1, data_t *input2,
4719 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004720 int expected_status_arg,
4721 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004722{
Ronald Cron5425a212020-08-04 14:58:35 +02004723 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4724 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004725 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004726 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004727 size_t bits = bits_arg;
4728 psa_status_t expected_status = expected_status_arg;
4729 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4730 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4731 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4732
4733 PSA_ASSERT( psa_crypto_init( ) );
4734
4735 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4736 psa_set_key_algorithm( &base_attributes, alg );
4737 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4738 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004739 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004740
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004741 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4742 input1->x, input1->len,
4743 input2->x, input2->len,
4744 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004745 goto exit;
4746
4747 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4748 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004749 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004750 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004751
4752 psa_status_t status =
4753 psa_key_derivation_output_key( &derived_attributes,
4754 &operation,
4755 &derived_key );
4756 if( is_large_output > 0 )
4757 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4758 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004759
4760exit:
4761 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004762 psa_destroy_key( base_key );
4763 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004764 PSA_DONE( );
4765}
4766/* END_CASE */
4767
4768/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004769void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004770 int our_key_type_arg, int our_key_alg_arg,
4771 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004772 int expected_status_arg )
4773{
Ronald Cron5425a212020-08-04 14:58:35 +02004774 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004775 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004776 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004777 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004778 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004779 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004780 psa_status_t expected_status = expected_status_arg;
4781 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004782
Gilles Peskine8817f612018-12-18 00:18:46 +01004783 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004784
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004785 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004786 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004787 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004788 PSA_ASSERT( psa_import_key( &attributes,
4789 our_key_data->x, our_key_data->len,
4790 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004791
Gilles Peskine77f40d82019-04-11 21:27:06 +02004792 /* The tests currently include inputs that should fail at either step.
4793 * Test cases that fail at the setup step should be changed to call
4794 * key_derivation_setup instead, and this function should be renamed
4795 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004796 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004797 if( status == PSA_SUCCESS )
4798 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004799 TEST_EQUAL( psa_key_derivation_key_agreement(
4800 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4801 our_key,
4802 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004803 expected_status );
4804 }
4805 else
4806 {
4807 TEST_ASSERT( status == expected_status );
4808 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004809
4810exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004811 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004812 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004813 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004814}
4815/* END_CASE */
4816
4817/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004818void raw_key_agreement( int alg_arg,
4819 int our_key_type_arg, data_t *our_key_data,
4820 data_t *peer_key_data,
4821 data_t *expected_output )
4822{
Ronald Cron5425a212020-08-04 14:58:35 +02004823 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004824 psa_algorithm_t alg = alg_arg;
4825 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004826 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004827 unsigned char *output = NULL;
4828 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004829 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004830
4831 ASSERT_ALLOC( output, expected_output->len );
4832 PSA_ASSERT( psa_crypto_init( ) );
4833
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004834 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4835 psa_set_key_algorithm( &attributes, alg );
4836 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004837 PSA_ASSERT( psa_import_key( &attributes,
4838 our_key_data->x, our_key_data->len,
4839 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004840
gabor-mezei-armceface22021-01-21 12:26:17 +01004841 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4842 key_bits = psa_get_key_bits( &attributes );
4843
Gilles Peskinebe697d82019-05-16 18:00:41 +02004844 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4845 peer_key_data->x, peer_key_data->len,
4846 output, expected_output->len,
4847 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004848 ASSERT_COMPARE( output, output_length,
4849 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01004850 TEST_ASSERT( output_length <=
4851 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
4852 TEST_ASSERT( output_length <=
4853 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004854
4855exit:
4856 mbedtls_free( output );
4857 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004858 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004859}
4860/* END_CASE */
4861
4862/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004863void key_agreement_capacity( int alg_arg,
4864 int our_key_type_arg, data_t *our_key_data,
4865 data_t *peer_key_data,
4866 int expected_capacity_arg )
4867{
Ronald Cron5425a212020-08-04 14:58:35 +02004868 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004869 psa_algorithm_t alg = alg_arg;
4870 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004871 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004872 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004873 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004874 unsigned char output[16];
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 Peskinebf491972018-10-25 22:36:12 +02004898 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004899 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004900 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004901 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004902
Gilles Peskinebf491972018-10-25 22:36:12 +02004903 /* Test the actual capacity by reading the output. */
4904 while( actual_capacity > sizeof( output ) )
4905 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004906 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004907 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004908 actual_capacity -= sizeof( output );
4909 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004910 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004911 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004912 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004913 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004914
Gilles Peskine59685592018-09-18 12:11:34 +02004915exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004916 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004917 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004918 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004919}
4920/* END_CASE */
4921
4922/* BEGIN_CASE */
4923void key_agreement_output( int alg_arg,
4924 int our_key_type_arg, data_t *our_key_data,
4925 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004926 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004927{
Ronald Cron5425a212020-08-04 14:58:35 +02004928 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004929 psa_algorithm_t alg = alg_arg;
4930 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004931 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004932 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004933 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004934
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004935 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4936 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004937
Gilles Peskine8817f612018-12-18 00:18:46 +01004938 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004939
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004940 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4941 psa_set_key_algorithm( &attributes, alg );
4942 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004943 PSA_ASSERT( psa_import_key( &attributes,
4944 our_key_data->x, our_key_data->len,
4945 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004946
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004947 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004948 PSA_ASSERT( psa_key_derivation_key_agreement(
4949 &operation,
4950 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4951 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004952 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4953 {
4954 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004955 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004956 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004957 NULL, 0 ) );
4958 }
Gilles Peskine59685592018-09-18 12:11:34 +02004959
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004960 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004961 actual_output,
4962 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004963 ASSERT_COMPARE( actual_output, expected_output1->len,
4964 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004965 if( expected_output2->len != 0 )
4966 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004967 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004968 actual_output,
4969 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004970 ASSERT_COMPARE( actual_output, expected_output2->len,
4971 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004972 }
Gilles Peskine59685592018-09-18 12:11:34 +02004973
4974exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004975 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004976 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004977 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004978 mbedtls_free( actual_output );
4979}
4980/* END_CASE */
4981
4982/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004983void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004984{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004985 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004986 unsigned char *output = NULL;
4987 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004988 size_t i;
4989 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004990
Simon Butcher49f8e312020-03-03 15:51:50 +00004991 TEST_ASSERT( bytes_arg >= 0 );
4992
Gilles Peskine91892022021-02-08 19:50:26 +01004993 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004994 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02004995
Gilles Peskine8817f612018-12-18 00:18:46 +01004996 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004997
Gilles Peskinea50d7392018-06-21 10:22:13 +02004998 /* Run several times, to ensure that every output byte will be
4999 * nonzero at least once with overwhelming probability
5000 * (2^(-8*number_of_runs)). */
5001 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005002 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005003 if( bytes != 0 )
5004 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005005 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005006
Gilles Peskinea50d7392018-06-21 10:22:13 +02005007 for( i = 0; i < bytes; i++ )
5008 {
5009 if( output[i] != 0 )
5010 ++changed[i];
5011 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005012 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005013
5014 /* Check that every byte was changed to nonzero at least once. This
5015 * validates that psa_generate_random is overwriting every byte of
5016 * the output buffer. */
5017 for( i = 0; i < bytes; i++ )
5018 {
5019 TEST_ASSERT( changed[i] != 0 );
5020 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005021
5022exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005023 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005024 mbedtls_free( output );
5025 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005026}
5027/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005028
5029/* BEGIN_CASE */
5030void generate_key( int type_arg,
5031 int bits_arg,
5032 int usage_arg,
5033 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005034 int expected_status_arg,
5035 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005036{
Ronald Cron5425a212020-08-04 14:58:35 +02005037 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005038 psa_key_type_t type = type_arg;
5039 psa_key_usage_t usage = usage_arg;
5040 size_t bits = bits_arg;
5041 psa_algorithm_t alg = alg_arg;
5042 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005043 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005044 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005045
Gilles Peskine8817f612018-12-18 00:18:46 +01005046 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005047
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005048 psa_set_key_usage_flags( &attributes, usage );
5049 psa_set_key_algorithm( &attributes, alg );
5050 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005051 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005052
5053 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005054 psa_status_t status = psa_generate_key( &attributes, &key );
5055
5056 if( is_large_key > 0 )
5057 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5058 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005059 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005060 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005061
5062 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005063 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005064 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5065 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005066
Gilles Peskine818ca122018-06-20 18:16:48 +02005067 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005068 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005069 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005070
5071exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005072 /*
5073 * Key attributes may have been returned by psa_get_key_attributes()
5074 * thus reset them as required.
5075 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005076 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005077
Ronald Cron5425a212020-08-04 14:58:35 +02005078 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005079 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005080}
5081/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005082
Ronald Cronee414c72021-03-18 18:50:08 +01005083/* 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 +02005084void generate_key_rsa( int bits_arg,
5085 data_t *e_arg,
5086 int expected_status_arg )
5087{
Ronald Cron5425a212020-08-04 14:58:35 +02005088 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005089 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005090 size_t bits = bits_arg;
5091 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5092 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5093 psa_status_t expected_status = expected_status_arg;
5094 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5095 uint8_t *exported = NULL;
5096 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005097 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005098 size_t exported_length = SIZE_MAX;
5099 uint8_t *e_read_buffer = NULL;
5100 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005101 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005102 size_t e_read_length = SIZE_MAX;
5103
5104 if( e_arg->len == 0 ||
5105 ( e_arg->len == 3 &&
5106 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5107 {
5108 is_default_public_exponent = 1;
5109 e_read_size = 0;
5110 }
5111 ASSERT_ALLOC( e_read_buffer, e_read_size );
5112 ASSERT_ALLOC( exported, exported_size );
5113
5114 PSA_ASSERT( psa_crypto_init( ) );
5115
5116 psa_set_key_usage_flags( &attributes, usage );
5117 psa_set_key_algorithm( &attributes, alg );
5118 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5119 e_arg->x, e_arg->len ) );
5120 psa_set_key_bits( &attributes, bits );
5121
5122 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005123 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005124 if( expected_status != PSA_SUCCESS )
5125 goto exit;
5126
5127 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005128 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005129 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5130 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5131 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5132 e_read_buffer, e_read_size,
5133 &e_read_length ) );
5134 if( is_default_public_exponent )
5135 TEST_EQUAL( e_read_length, 0 );
5136 else
5137 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5138
5139 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005140 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005141 goto exit;
5142
5143 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005144 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005145 exported, exported_size,
5146 &exported_length ) );
5147 {
5148 uint8_t *p = exported;
5149 uint8_t *end = exported + exported_length;
5150 size_t len;
5151 /* RSAPublicKey ::= SEQUENCE {
5152 * modulus INTEGER, -- n
5153 * publicExponent INTEGER } -- e
5154 */
5155 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005156 MBEDTLS_ASN1_SEQUENCE |
5157 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005158 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005159 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5160 MBEDTLS_ASN1_INTEGER ) );
5161 if( len >= 1 && p[0] == 0 )
5162 {
5163 ++p;
5164 --len;
5165 }
5166 if( e_arg->len == 0 )
5167 {
5168 TEST_EQUAL( len, 3 );
5169 TEST_EQUAL( p[0], 1 );
5170 TEST_EQUAL( p[1], 0 );
5171 TEST_EQUAL( p[2], 1 );
5172 }
5173 else
5174 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5175 }
5176
5177exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005178 /*
5179 * Key attributes may have been returned by psa_get_key_attributes() or
5180 * set by psa_set_key_domain_parameters() thus reset them as required.
5181 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005182 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005183
Ronald Cron5425a212020-08-04 14:58:35 +02005184 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005185 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005186 mbedtls_free( e_read_buffer );
5187 mbedtls_free( exported );
5188}
5189/* END_CASE */
5190
Darryl Greend49a4992018-06-18 17:27:26 +01005191/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005192void persistent_key_load_key_from_storage( data_t *data,
5193 int type_arg, int bits_arg,
5194 int usage_flags_arg, int alg_arg,
5195 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005196{
Ronald Cron71016a92020-08-28 19:01:50 +02005197 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005198 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005199 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5200 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005201 psa_key_type_t type = type_arg;
5202 size_t bits = bits_arg;
5203 psa_key_usage_t usage_flags = usage_flags_arg;
5204 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005205 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005206 unsigned char *first_export = NULL;
5207 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005208 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005209 size_t first_exported_length;
5210 size_t second_exported_length;
5211
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005212 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5213 {
5214 ASSERT_ALLOC( first_export, export_size );
5215 ASSERT_ALLOC( second_export, export_size );
5216 }
Darryl Greend49a4992018-06-18 17:27:26 +01005217
Gilles Peskine8817f612018-12-18 00:18:46 +01005218 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005219
Gilles Peskinec87af662019-05-15 16:12:22 +02005220 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005221 psa_set_key_usage_flags( &attributes, usage_flags );
5222 psa_set_key_algorithm( &attributes, alg );
5223 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005224 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005225
Darryl Green0c6575a2018-11-07 16:05:30 +00005226 switch( generation_method )
5227 {
5228 case IMPORT_KEY:
5229 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005230 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005231 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005232 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005233
Darryl Green0c6575a2018-11-07 16:05:30 +00005234 case GENERATE_KEY:
5235 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005236 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005237 break;
5238
5239 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005240#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005241 {
5242 /* Create base key */
5243 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5244 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5245 psa_set_key_usage_flags( &base_attributes,
5246 PSA_KEY_USAGE_DERIVE );
5247 psa_set_key_algorithm( &base_attributes, derive_alg );
5248 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005249 PSA_ASSERT( psa_import_key( &base_attributes,
5250 data->x, data->len,
5251 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005252 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005253 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005254 PSA_ASSERT( psa_key_derivation_input_key(
5255 &operation,
5256 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005257 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005258 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005259 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005260 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5261 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005262 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005263 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005264 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005265 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005266 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005267#else
5268 TEST_ASSUME( ! "KDF not supported in this configuration" );
5269#endif
5270 break;
5271
5272 default:
5273 TEST_ASSERT( ! "generation_method not implemented in test" );
5274 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005275 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005276 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005277
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005278 /* Export the key if permitted by the key policy. */
5279 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5280 {
Ronald Cron5425a212020-08-04 14:58:35 +02005281 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005282 first_export, export_size,
5283 &first_exported_length ) );
5284 if( generation_method == IMPORT_KEY )
5285 ASSERT_COMPARE( data->x, data->len,
5286 first_export, first_exported_length );
5287 }
Darryl Greend49a4992018-06-18 17:27:26 +01005288
5289 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005290 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005291 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005292 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005293
Darryl Greend49a4992018-06-18 17:27:26 +01005294 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005295 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005296 TEST_ASSERT( mbedtls_svc_key_id_equal(
5297 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005298 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5299 PSA_KEY_LIFETIME_PERSISTENT );
5300 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5301 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4d9009e2021-05-13 12:05:01 +02005302 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-armff03fd62021-06-28 14:05:00 +02005303 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005304 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005305
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005306 /* Export the key again if permitted by the key policy. */
5307 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005308 {
Ronald Cron5425a212020-08-04 14:58:35 +02005309 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005310 second_export, export_size,
5311 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005312 ASSERT_COMPARE( first_export, first_exported_length,
5313 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005314 }
5315
5316 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005317 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005318 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005319
5320exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005321 /*
5322 * Key attributes may have been returned by psa_get_key_attributes()
5323 * thus reset them as required.
5324 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005325 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005326
Darryl Greend49a4992018-06-18 17:27:26 +01005327 mbedtls_free( first_export );
5328 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005329 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005330 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005331 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005332 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005333}
5334/* END_CASE */