blob: e6d03b5ad8b45d4cafe525d59a1828ea1a97496f [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010018
Gilles Peskinef6279312021-05-27 13:21:20 +020019/* If this comes up, it's a bug in the test code or in the test data. */
20#define UNUSED 0xdeadbeef
21
Jaeden Amerof24c7f82018-06-27 17:20:43 +010022/** An invalid export length that will never be set by psa_export_key(). */
23static const size_t INVALID_EXPORT_LENGTH = ~0U;
24
Gilles Peskinea7aa4422018-08-14 15:17:54 +020025/** Test if a buffer contains a constant byte value.
26 *
27 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020028 *
29 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020030 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020031 * \param size Size of the buffer in bytes.
32 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020033 * \return 1 if the buffer is all-bits-zero.
34 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020035 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020036static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037{
38 size_t i;
39 for( i = 0; i < size; i++ )
40 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020042 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020043 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020044 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020045}
Gilles Peskine818ca122018-06-20 18:16:48 +020046
Gilles Peskine0b352bc2018-06-28 00:16:11 +020047/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
48static int asn1_write_10x( unsigned char **p,
49 unsigned char *start,
50 size_t bits,
51 unsigned char x )
52{
53 int ret;
54 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020055 if( bits == 0 )
56 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
57 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020058 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030059 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020060 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
61 *p -= len;
62 ( *p )[len-1] = x;
63 if( bits % 8 == 0 )
64 ( *p )[1] |= 1;
65 else
66 ( *p )[0] |= 1 << ( bits % 8 );
67 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
68 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
69 MBEDTLS_ASN1_INTEGER ) );
70 return( len );
71}
72
73static int construct_fake_rsa_key( unsigned char *buffer,
74 size_t buffer_size,
75 unsigned char **p,
76 size_t bits,
77 int keypair )
78{
79 size_t half_bits = ( bits + 1 ) / 2;
80 int ret;
81 int len = 0;
82 /* Construct something that looks like a DER encoding of
83 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
84 * RSAPrivateKey ::= SEQUENCE {
85 * version Version,
86 * modulus INTEGER, -- n
87 * publicExponent INTEGER, -- e
88 * privateExponent INTEGER, -- d
89 * prime1 INTEGER, -- p
90 * prime2 INTEGER, -- q
91 * exponent1 INTEGER, -- d mod (p-1)
92 * exponent2 INTEGER, -- d mod (q-1)
93 * coefficient INTEGER, -- (inverse of q) mod p
94 * otherPrimeInfos OtherPrimeInfos OPTIONAL
95 * }
96 * Or, for a public key, the same structure with only
97 * version, modulus and publicExponent.
98 */
99 *p = buffer + buffer_size;
100 if( keypair )
101 {
102 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
103 asn1_write_10x( p, buffer, half_bits, 1 ) );
104 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
105 asn1_write_10x( p, buffer, half_bits, 1 ) );
106 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
107 asn1_write_10x( p, buffer, half_bits, 1 ) );
108 MBEDTLS_ASN1_CHK_ADD( len, /* q */
109 asn1_write_10x( p, buffer, half_bits, 1 ) );
110 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
111 asn1_write_10x( p, buffer, half_bits, 3 ) );
112 MBEDTLS_ASN1_CHK_ADD( len, /* d */
113 asn1_write_10x( p, buffer, bits, 1 ) );
114 }
115 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
116 asn1_write_10x( p, buffer, 17, 1 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* n */
118 asn1_write_10x( p, buffer, bits, 1 ) );
119 if( keypair )
120 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
121 mbedtls_asn1_write_int( p, buffer, 0 ) );
122 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
123 {
124 const unsigned char tag =
125 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
126 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
127 }
128 return( len );
129}
130
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100131int exercise_mac_setup( psa_key_type_t key_type,
132 const unsigned char *key_bytes,
133 size_t key_length,
134 psa_algorithm_t alg,
135 psa_mac_operation_t *operation,
136 psa_status_t *status )
137{
Ronald Cron5425a212020-08-04 14:58:35 +0200138 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200139 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100140
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100141 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200142 psa_set_key_algorithm( &attributes, alg );
143 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200144 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100145
Ronald Cron5425a212020-08-04 14:58:35 +0200146 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100147 /* Whether setup succeeded or failed, abort must succeed. */
148 PSA_ASSERT( psa_mac_abort( operation ) );
149 /* If setup failed, reproduce the failure, so that the caller can
150 * test the resulting state of the operation object. */
151 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100152 {
Ronald Cron5425a212020-08-04 14:58:35 +0200153 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100154 }
155
Ronald Cron5425a212020-08-04 14:58:35 +0200156 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100157 return( 1 );
158
159exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200160 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100161 return( 0 );
162}
163
164int exercise_cipher_setup( psa_key_type_t key_type,
165 const unsigned char *key_bytes,
166 size_t key_length,
167 psa_algorithm_t alg,
168 psa_cipher_operation_t *operation,
169 psa_status_t *status )
170{
Ronald Cron5425a212020-08-04 14:58:35 +0200171 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200172 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100173
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200174 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
175 psa_set_key_algorithm( &attributes, alg );
176 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200177 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100178
Ronald Cron5425a212020-08-04 14:58:35 +0200179 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100180 /* Whether setup succeeded or failed, abort must succeed. */
181 PSA_ASSERT( psa_cipher_abort( operation ) );
182 /* If setup failed, reproduce the failure, so that the caller can
183 * test the resulting state of the operation object. */
184 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185 {
Ronald Cron5425a212020-08-04 14:58:35 +0200186 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100187 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100188 }
189
Ronald Cron5425a212020-08-04 14:58:35 +0200190 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100191 return( 1 );
192
193exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200194 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100195 return( 0 );
196}
197
Ronald Cron5425a212020-08-04 14:58:35 +0200198static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200199{
200 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200201 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200202 uint8_t buffer[1];
203 size_t length;
204 int ok = 0;
205
Ronald Cronecfb2372020-07-23 17:13:42 +0200206 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200207 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
208 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
209 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200210 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000211 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200212 TEST_EQUAL(
213 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
214 TEST_EQUAL(
215 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200216 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200217 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
218 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
219 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
220 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
221
Ronald Cron5425a212020-08-04 14:58:35 +0200222 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000223 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200224 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200225 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000226 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200227
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200228 ok = 1;
229
230exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100231 /*
232 * Key attributes may have been returned by psa_get_key_attributes()
233 * thus reset them as required.
234 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200235 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100236
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200237 return( ok );
238}
239
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200240/* Assert that a key isn't reported as having a slot number. */
241#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
242#define ASSERT_NO_SLOT_NUMBER( attributes ) \
243 do \
244 { \
245 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
246 TEST_EQUAL( psa_get_key_slot_number( \
247 attributes, \
248 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
249 PSA_ERROR_INVALID_ARGUMENT ); \
250 } \
251 while( 0 )
252#else /* MBEDTLS_PSA_CRYPTO_SE_C */
253#define ASSERT_NO_SLOT_NUMBER( attributes ) \
254 ( (void) 0 )
255#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
256
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100257/* An overapproximation of the amount of storage needed for a key of the
258 * given type and with the given content. The API doesn't make it easy
259 * to find a good value for the size. The current implementation doesn't
260 * care about the value anyway. */
261#define KEY_BITS_FROM_DATA( type, data ) \
262 ( data )->len
263
Darryl Green0c6575a2018-11-07 16:05:30 +0000264typedef enum {
265 IMPORT_KEY = 0,
266 GENERATE_KEY = 1,
267 DERIVE_KEY = 2
268} generate_method;
269
Gilles Peskinee59236f2018-01-27 23:32:46 +0100270/* END_HEADER */
271
272/* BEGIN_DEPENDENCIES
273 * depends_on:MBEDTLS_PSA_CRYPTO_C
274 * END_DEPENDENCIES
275 */
276
277/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200278void static_checks( )
279{
280 size_t max_truncated_mac_size =
281 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
282
283 /* Check that the length for a truncated MAC always fits in the algorithm
284 * encoding. The shifted mask is the maximum truncated value. The
285 * untruncated algorithm may be one byte larger. */
286 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +0100287
288#if defined(MBEDTLS_TEST_DEPRECATED)
289 /* Check deprecated constants. */
290 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
291 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
292 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
293 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
294 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
295 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
296 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
297 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100298
Paul Elliott8ff510a2020-06-02 17:19:28 +0100299 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
300 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
301 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
302 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
303 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
304 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
305 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
306 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
307 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
308 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
309 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
310 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
311 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
312 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
313 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
314 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
315 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
316 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
317 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
318 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
319 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
320 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
321 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
322 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
323 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
324 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
325 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
326 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
327 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
328 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
329
330 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
331 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
332 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
333 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
334 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
335 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
336 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
337 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100338
Paul Elliott75e27032020-06-03 15:17:39 +0100339 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
340 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
341 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
342 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
343 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
344
345 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
346 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100347#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200348}
349/* END_CASE */
350
351/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200352void import_with_policy( int type_arg,
353 int usage_arg, int alg_arg,
354 int expected_status_arg )
355{
356 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
357 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200358 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200359 psa_key_type_t type = type_arg;
360 psa_key_usage_t usage = usage_arg;
361 psa_algorithm_t alg = alg_arg;
362 psa_status_t expected_status = expected_status_arg;
363 const uint8_t key_material[16] = {0};
364 psa_status_t status;
365
366 PSA_ASSERT( psa_crypto_init( ) );
367
368 psa_set_key_type( &attributes, type );
369 psa_set_key_usage_flags( &attributes, usage );
370 psa_set_key_algorithm( &attributes, alg );
371
372 status = psa_import_key( &attributes,
373 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200374 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200375 TEST_EQUAL( status, expected_status );
376 if( status != PSA_SUCCESS )
377 goto exit;
378
Ronald Cron5425a212020-08-04 14:58:35 +0200379 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200380 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4d9009e2021-05-13 12:05:01 +0200381 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
382 update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200383 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200384 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200385
Ronald Cron5425a212020-08-04 14:58:35 +0200386 PSA_ASSERT( psa_destroy_key( key ) );
387 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200388
389exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100390 /*
391 * Key attributes may have been returned by psa_get_key_attributes()
392 * thus reset them as required.
393 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200394 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100395
396 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200397 PSA_DONE( );
398}
399/* END_CASE */
400
401/* BEGIN_CASE */
402void import_with_data( data_t *data, int type_arg,
403 int attr_bits_arg,
404 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200405{
406 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
407 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200408 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200409 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200410 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200411 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100412 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100413
Gilles Peskine8817f612018-12-18 00:18:46 +0100414 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100415
Gilles Peskine4747d192019-04-17 15:05:45 +0200416 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200417 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200418
Ronald Cron5425a212020-08-04 14:58:35 +0200419 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100420 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200421 if( status != PSA_SUCCESS )
422 goto exit;
423
Ronald Cron5425a212020-08-04 14:58:35 +0200424 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200425 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200426 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200427 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200428 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200429
Ronald Cron5425a212020-08-04 14:58:35 +0200430 PSA_ASSERT( psa_destroy_key( key ) );
431 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100432
433exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100434 /*
435 * Key attributes may have been returned by psa_get_key_attributes()
436 * thus reset them as required.
437 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200438 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100439
440 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200441 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100442}
443/* END_CASE */
444
445/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200446void import_large_key( int type_arg, int byte_size_arg,
447 int expected_status_arg )
448{
449 psa_key_type_t type = type_arg;
450 size_t byte_size = byte_size_arg;
451 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
452 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200453 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200454 psa_status_t status;
455 uint8_t *buffer = NULL;
456 size_t buffer_size = byte_size + 1;
457 size_t n;
458
Steven Cooreman69967ce2021-01-18 18:01:08 +0100459 /* Skip the test case if the target running the test cannot
460 * accomodate large keys due to heap size constraints */
461 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200462 memset( buffer, 'K', byte_size );
463
464 PSA_ASSERT( psa_crypto_init( ) );
465
466 /* Try importing the key */
467 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
468 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200469 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100470 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200471 TEST_EQUAL( status, expected_status );
472
473 if( status == PSA_SUCCESS )
474 {
Ronald Cron5425a212020-08-04 14:58:35 +0200475 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200476 TEST_EQUAL( psa_get_key_type( &attributes ), type );
477 TEST_EQUAL( psa_get_key_bits( &attributes ),
478 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200479 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200480 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200481 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200482 for( n = 0; n < byte_size; n++ )
483 TEST_EQUAL( buffer[n], 'K' );
484 for( n = byte_size; n < buffer_size; n++ )
485 TEST_EQUAL( buffer[n], 0 );
486 }
487
488exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100489 /*
490 * Key attributes may have been returned by psa_get_key_attributes()
491 * thus reset them as required.
492 */
493 psa_reset_key_attributes( &attributes );
494
Ronald Cron5425a212020-08-04 14:58:35 +0200495 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200496 PSA_DONE( );
497 mbedtls_free( buffer );
498}
499/* END_CASE */
500
501/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200502void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
503{
Ronald Cron5425a212020-08-04 14:58:35 +0200504 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200505 size_t bits = bits_arg;
506 psa_status_t expected_status = expected_status_arg;
507 psa_status_t status;
508 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200509 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200510 size_t buffer_size = /* Slight overapproximations */
511 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200512 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200513 unsigned char *p;
514 int ret;
515 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200516 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200517
Gilles Peskine8817f612018-12-18 00:18:46 +0100518 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200519 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200520
521 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
522 bits, keypair ) ) >= 0 );
523 length = ret;
524
525 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200526 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200527 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100528 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200529
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200530 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200531 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200532
533exit:
534 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200535 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200536}
537/* END_CASE */
538
539/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300540void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300541 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200542 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100543 int expected_bits,
544 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200545 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100546 int canonical_input )
547{
Ronald Cron5425a212020-08-04 14:58:35 +0200548 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100549 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200550 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200551 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100552 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100553 unsigned char *exported = NULL;
554 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100555 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100556 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100557 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200558 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200559 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100560
Moran Pekercb088e72018-07-17 17:36:59 +0300561 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200562 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100563 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200564 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100565 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100566
Gilles Peskine4747d192019-04-17 15:05:45 +0200567 psa_set_key_usage_flags( &attributes, usage_arg );
568 psa_set_key_algorithm( &attributes, alg );
569 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700570
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100571 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200572 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100573
574 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200575 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200576 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
577 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200578 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100579
580 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200581 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100582 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100583
584 /* The exported length must be set by psa_export_key() to a value between 0
585 * and export_size. On errors, the exported length must be 0. */
586 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
587 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
588 TEST_ASSERT( exported_length <= export_size );
589
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200590 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200591 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100592 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200593 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100594 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100595 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200596 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100597
Gilles Peskineea38a922021-02-13 00:05:16 +0100598 /* Run sanity checks on the exported key. For non-canonical inputs,
599 * this validates the canonical representations. For canonical inputs,
600 * this doesn't directly validate the implementation, but it still helps
601 * by cross-validating the test data with the sanity check code. */
602 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200603 goto exit;
604
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100605 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200606 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100607 else
608 {
Ronald Cron5425a212020-08-04 14:58:35 +0200609 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200610 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200611 &key2 ) );
612 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100613 reexported,
614 export_size,
615 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200616 ASSERT_COMPARE( exported, exported_length,
617 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200618 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100619 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100620 TEST_ASSERT( exported_length <=
621 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
622 psa_get_key_bits( &got_attributes ) ) );
623 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100624
625destroy:
626 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200627 PSA_ASSERT( psa_destroy_key( key ) );
628 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100629
630exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100631 /*
632 * Key attributes may have been returned by psa_get_key_attributes()
633 * thus reset them as required.
634 */
635 psa_reset_key_attributes( &got_attributes );
636
itayzafrir3e02b3b2018-06-12 17:06:52 +0300637 mbedtls_free( exported );
638 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200639 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100640}
641/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100642
Moran Pekerf709f4a2018-06-06 17:26:04 +0300643/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300644void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200645 int type_arg,
646 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100647 int export_size_delta,
648 int expected_export_status_arg,
649 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300650{
Ronald Cron5425a212020-08-04 14:58:35 +0200651 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300652 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200653 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200654 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300655 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300656 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100657 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100658 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200659 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300660
Gilles Peskine8817f612018-12-18 00:18:46 +0100661 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300662
Gilles Peskine4747d192019-04-17 15:05:45 +0200663 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
664 psa_set_key_algorithm( &attributes, alg );
665 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300666
667 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200668 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300669
Gilles Peskine49c25912018-10-29 15:15:31 +0100670 /* Export the public key */
671 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200672 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200673 exported, export_size,
674 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100675 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100676 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100677 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200678 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100679 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200680 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200681 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100682 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100683 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100684 TEST_ASSERT( expected_public_key->len <=
685 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
686 TEST_ASSERT( expected_public_key->len <=
687 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100688 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
689 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100690 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300691
692exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100693 /*
694 * Key attributes may have been returned by psa_get_key_attributes()
695 * thus reset them as required.
696 */
697 psa_reset_key_attributes( &attributes );
698
itayzafrir3e02b3b2018-06-12 17:06:52 +0300699 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200700 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200701 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300702}
703/* END_CASE */
704
Gilles Peskine20035e32018-02-03 22:44:14 +0100705/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200706void import_and_exercise_key( data_t *data,
707 int type_arg,
708 int bits_arg,
709 int alg_arg )
710{
Ronald Cron5425a212020-08-04 14:58:35 +0200711 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200712 psa_key_type_t type = type_arg;
713 size_t bits = bits_arg;
714 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100715 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200716 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200717 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200718
Gilles Peskine8817f612018-12-18 00:18:46 +0100719 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200720
Gilles Peskine4747d192019-04-17 15:05:45 +0200721 psa_set_key_usage_flags( &attributes, usage );
722 psa_set_key_algorithm( &attributes, alg );
723 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200724
725 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200726 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200727
728 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200729 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200730 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
731 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200732
733 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100734 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200735 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200736
Ronald Cron5425a212020-08-04 14:58:35 +0200737 PSA_ASSERT( psa_destroy_key( key ) );
738 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200739
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200740exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100741 /*
742 * Key attributes may have been returned by psa_get_key_attributes()
743 * thus reset them as required.
744 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200745 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100746
747 psa_reset_key_attributes( &attributes );
748 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200749 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200750}
751/* END_CASE */
752
753/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100754void effective_key_attributes( int type_arg, int expected_type_arg,
755 int bits_arg, int expected_bits_arg,
756 int usage_arg, int expected_usage_arg,
757 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200758{
Ronald Cron5425a212020-08-04 14:58:35 +0200759 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100760 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100761 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100762 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100763 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200764 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100765 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200766 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100767 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200768 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200769
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200770 /* Check if all extended usage flags are deployed
771 in the expected usage flags. */
772 TEST_EQUAL( expected_usage, update_key_usage_flags( usage ) );
773
Gilles Peskine8817f612018-12-18 00:18:46 +0100774 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200775
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200776 psa_set_key_usage_flags( &attributes, usage );
777 psa_set_key_algorithm( &attributes, alg );
778 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +0100779 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +0200780
Ronald Cron5425a212020-08-04 14:58:35 +0200781 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +0100782 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +0200783
Ronald Cron5425a212020-08-04 14:58:35 +0200784 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +0100785 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
786 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
787 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
788 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200789
790exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100791 /*
792 * Key attributes may have been returned by psa_get_key_attributes()
793 * thus reset them as required.
794 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200795 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100796
797 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200798 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +0200799}
800/* END_CASE */
801
802/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100803void check_key_policy( int type_arg, int bits_arg,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200804 int usage_arg, int expected_usage_arg, int alg_arg )
Gilles Peskine06c28892019-11-26 18:07:46 +0100805{
806 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200807 usage_arg, expected_usage_arg,
808 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +0100809 goto exit;
810}
811/* END_CASE */
812
813/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200814void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +0000815{
816 /* Test each valid way of initializing the object, except for `= {0}`, as
817 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
818 * though it's OK by the C standard. We could test for this, but we'd need
819 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200820 psa_key_attributes_t func = psa_key_attributes_init( );
821 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
822 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000823
824 memset( &zero, 0, sizeof( zero ) );
825
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200826 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
827 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
828 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000829
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200830 TEST_EQUAL( psa_get_key_type( &func ), 0 );
831 TEST_EQUAL( psa_get_key_type( &init ), 0 );
832 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
833
834 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
835 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
836 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
837
838 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
839 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
840 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
841
842 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
843 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
844 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +0000845}
846/* END_CASE */
847
848/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200849void mac_key_policy( int policy_usage_arg,
850 int policy_alg_arg,
851 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200852 data_t *key_data,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200853 int exercise_alg_arg,
854 int expected_usage_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100855 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200856{
Ronald Cron5425a212020-08-04 14:58:35 +0200857 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200858 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000859 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200860 psa_key_type_t key_type = key_type_arg;
861 psa_algorithm_t policy_alg = policy_alg_arg;
862 psa_algorithm_t exercise_alg = exercise_alg_arg;
863 psa_key_usage_t policy_usage = policy_usage_arg;
864 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200865 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100866 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200867 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200868
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200869 /* Check if all extended usage flags are deployed
870 in the expected usage flags. */
871 TEST_EQUAL( expected_usage, update_key_usage_flags( policy_usage ) );
872
Gilles Peskine8817f612018-12-18 00:18:46 +0100873 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200874
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200875 psa_set_key_usage_flags( &attributes, policy_usage );
876 psa_set_key_algorithm( &attributes, policy_alg );
877 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200878
Gilles Peskine049c7532019-05-15 20:22:09 +0200879 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200880 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200881
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200882 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
883
Ronald Cron5425a212020-08-04 14:58:35 +0200884 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100885 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100886 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100887 else
888 TEST_EQUAL( status, expected_status );
889
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200890 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200891
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200892 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200893 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100894 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100895 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100896 else
897 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200898
899exit:
900 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200901 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200902 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200903}
904/* END_CASE */
905
906/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200907void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200908 int policy_alg,
909 int key_type,
910 data_t *key_data,
911 int exercise_alg )
912{
Ronald Cron5425a212020-08-04 14:58:35 +0200913 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200914 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000915 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200916 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200917 psa_status_t status;
918
Gilles Peskine8817f612018-12-18 00:18:46 +0100919 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200920
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200921 psa_set_key_usage_flags( &attributes, policy_usage );
922 psa_set_key_algorithm( &attributes, policy_alg );
923 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200924
Gilles Peskine049c7532019-05-15 20:22:09 +0200925 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200926 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200927
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200928 /* Check if no key usage extension is done */
929 TEST_EQUAL( policy_usage, update_key_usage_flags( policy_usage ) );
930
Ronald Cron5425a212020-08-04 14:58:35 +0200931 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200932 if( policy_alg == exercise_alg &&
933 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100934 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200935 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100936 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200937 psa_cipher_abort( &operation );
938
Ronald Cron5425a212020-08-04 14:58:35 +0200939 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200940 if( policy_alg == exercise_alg &&
941 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100942 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200943 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100944 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200945
946exit:
947 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200948 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200949 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200950}
951/* END_CASE */
952
953/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200954void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200955 int policy_alg,
956 int key_type,
957 data_t *key_data,
958 int nonce_length_arg,
959 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100960 int exercise_alg,
961 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200962{
Ronald Cron5425a212020-08-04 14:58:35 +0200963 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200964 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200965 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200966 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100967 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200968 unsigned char nonce[16] = {0};
969 size_t nonce_length = nonce_length_arg;
970 unsigned char tag[16];
971 size_t tag_length = tag_length_arg;
972 size_t output_length;
973
974 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
975 TEST_ASSERT( tag_length <= sizeof( tag ) );
976
Gilles Peskine8817f612018-12-18 00:18:46 +0100977 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200978
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200979 psa_set_key_usage_flags( &attributes, policy_usage );
980 psa_set_key_algorithm( &attributes, policy_alg );
981 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200982
Gilles Peskine049c7532019-05-15 20:22:09 +0200983 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200984 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200985
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200986 /* Check if no key usage extension is done */
987 TEST_EQUAL( policy_usage, update_key_usage_flags( policy_usage ) );
988
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-arm3e5f6cd2021-05-13 16:17:16 +02001045 /* Check if no key usage extension is done */
1046 TEST_EQUAL( policy_usage, update_key_usage_flags( policy_usage ) );
1047
Ronald Cron5425a212020-08-04 14:58:35 +02001048 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001049 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001050 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1051 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001052 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001053
Ronald Cron5425a212020-08-04 14:58:35 +02001054 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001055 NULL, 0,
1056 NULL, 0,
1057 buffer, buffer_length,
1058 &output_length );
1059 if( policy_alg == exercise_alg &&
1060 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001061 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001062 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001063 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001064
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001065 if( buffer_length != 0 )
1066 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001067 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001068 buffer, buffer_length,
1069 NULL, 0,
1070 buffer, buffer_length,
1071 &output_length );
1072 if( policy_alg == exercise_alg &&
1073 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001074 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001075 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001076 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001077
1078exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001079 /*
1080 * Key attributes may have been returned by psa_get_key_attributes()
1081 * thus reset them as required.
1082 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001083 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001084
1085 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001086 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001087 mbedtls_free( buffer );
1088}
1089/* END_CASE */
1090
1091/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001092void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001093 int policy_alg,
1094 int key_type,
1095 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001096 int exercise_alg,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001097 int payload_length_arg,
1098 int hashing_permitted,
1099 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001100{
Ronald Cron5425a212020-08-04 14:58:35 +02001101 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001102 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001103 psa_key_usage_t policy_usage = policy_usage_arg;
1104 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001105 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001106 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1107 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1108 * compatible with the policy and `payload_length_arg` is supposed to be
1109 * a valid input length to sign. If `payload_length_arg <= 0`,
1110 * `exercise_alg` is supposed to be forbidden by the policy. */
1111 int compatible_alg = payload_length_arg > 0;
1112 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001113 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001114 size_t signature_length;
1115
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001116 /* Check if all extended usage flags are deployed
1117 in the expected usage flags. */
1118 TEST_EQUAL( expected_usage, update_key_usage_flags( policy_usage ) );
1119
Gilles Peskine8817f612018-12-18 00:18:46 +01001120 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001121
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001122 psa_set_key_usage_flags( &attributes, policy_usage );
1123 psa_set_key_algorithm( &attributes, policy_alg );
1124 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001125
Gilles Peskine049c7532019-05-15 20:22:09 +02001126 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001127 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001128
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001129 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1130
Ronald Cron5425a212020-08-04 14:58:35 +02001131 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001132 payload, payload_length,
1133 signature, sizeof( signature ),
1134 &signature_length );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001135 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001136 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001137 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001138 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001139
1140 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001141 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001142 payload, payload_length,
1143 signature, sizeof( signature ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001144 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001145 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001146 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001147 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001148
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001149 if( hashing_permitted )
1150 {
1151 status = psa_sign_message( key, exercise_alg,
1152 payload, payload_length,
1153 signature, sizeof( signature ),
1154 &signature_length );
1155 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1156 PSA_ASSERT( status );
1157 else
1158 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1159
1160 memset( signature, 0, sizeof( signature ) );
1161 status = psa_verify_message( key, exercise_alg,
1162 payload, payload_length,
1163 signature, sizeof( signature ) );
1164 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1165 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1166 else
1167 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1168 }
1169
Gilles Peskined5b33222018-06-18 22:20:03 +02001170exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001171 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001172 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001173}
1174/* END_CASE */
1175
Janos Follathba3fab92019-06-11 14:50:16 +01001176/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001177void derive_key_policy( int policy_usage,
1178 int policy_alg,
1179 int key_type,
1180 data_t *key_data,
1181 int exercise_alg )
1182{
Ronald Cron5425a212020-08-04 14:58:35 +02001183 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001184 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001185 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001186 psa_status_t status;
1187
Gilles Peskine8817f612018-12-18 00:18:46 +01001188 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001189
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001190 psa_set_key_usage_flags( &attributes, policy_usage );
1191 psa_set_key_algorithm( &attributes, policy_alg );
1192 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001193
Gilles Peskine049c7532019-05-15 20:22:09 +02001194 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001195 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001196
Janos Follathba3fab92019-06-11 14:50:16 +01001197 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1198
1199 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1200 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001201 {
Janos Follathba3fab92019-06-11 14:50:16 +01001202 PSA_ASSERT( psa_key_derivation_input_bytes(
1203 &operation,
1204 PSA_KEY_DERIVATION_INPUT_SEED,
1205 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001206 }
Janos Follathba3fab92019-06-11 14:50:16 +01001207
1208 status = psa_key_derivation_input_key( &operation,
1209 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001210 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001211
Gilles Peskineea0fb492018-07-12 17:17:20 +02001212 if( policy_alg == exercise_alg &&
1213 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001214 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001215 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001216 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001217
1218exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001219 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001220 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001221 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001222}
1223/* END_CASE */
1224
1225/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001226void agreement_key_policy( int policy_usage,
1227 int policy_alg,
1228 int key_type_arg,
1229 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001230 int exercise_alg,
1231 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001232{
Ronald Cron5425a212020-08-04 14:58:35 +02001233 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001234 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001235 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001236 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001237 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001238 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001239
Gilles Peskine8817f612018-12-18 00:18:46 +01001240 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001241
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001242 psa_set_key_usage_flags( &attributes, policy_usage );
1243 psa_set_key_algorithm( &attributes, policy_alg );
1244 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001245
Gilles Peskine049c7532019-05-15 20:22:09 +02001246 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001247 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001248
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001249 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001250 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001251
Steven Cooremance48e852020-10-05 16:02:45 +02001252 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001253
1254exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001255 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001256 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001257 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001258}
1259/* END_CASE */
1260
1261/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001262void key_policy_alg2( int key_type_arg, data_t *key_data,
1263 int usage_arg, int alg_arg, int alg2_arg )
1264{
Ronald Cron5425a212020-08-04 14:58:35 +02001265 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001266 psa_key_type_t key_type = key_type_arg;
1267 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1268 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1269 psa_key_usage_t usage = usage_arg;
1270 psa_algorithm_t alg = alg_arg;
1271 psa_algorithm_t alg2 = alg2_arg;
1272
1273 PSA_ASSERT( psa_crypto_init( ) );
1274
1275 psa_set_key_usage_flags( &attributes, usage );
1276 psa_set_key_algorithm( &attributes, alg );
1277 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1278 psa_set_key_type( &attributes, key_type );
1279 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001280 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001281
gabor-mezei-arm4d9009e2021-05-13 12:05:01 +02001282 /* Update the usage flags to obtain extended usage flags */
1283 usage = update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001284 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001285 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1286 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1287 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1288
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001289 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001290 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001291 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001292 goto exit;
1293
1294exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001295 /*
1296 * Key attributes may have been returned by psa_get_key_attributes()
1297 * thus reset them as required.
1298 */
1299 psa_reset_key_attributes( &got_attributes );
1300
Ronald Cron5425a212020-08-04 14:58:35 +02001301 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001302 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001303}
1304/* END_CASE */
1305
1306/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001307void raw_agreement_key_policy( int policy_usage,
1308 int policy_alg,
1309 int key_type_arg,
1310 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001311 int exercise_alg,
1312 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001313{
Ronald Cron5425a212020-08-04 14:58:35 +02001314 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001315 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001316 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001317 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001318 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001319 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001320
1321 PSA_ASSERT( psa_crypto_init( ) );
1322
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001323 psa_set_key_usage_flags( &attributes, policy_usage );
1324 psa_set_key_algorithm( &attributes, policy_alg );
1325 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001326
Gilles Peskine049c7532019-05-15 20:22:09 +02001327 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001328 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001329
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001330 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001331
Steven Cooremance48e852020-10-05 16:02:45 +02001332 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001333
1334exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001335 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001336 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001337 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001338}
1339/* END_CASE */
1340
1341/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001342void copy_success( int source_usage_arg,
1343 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001344 int type_arg, data_t *material,
1345 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001346 int target_usage_arg,
1347 int target_alg_arg, int target_alg2_arg,
1348 int expected_usage_arg,
1349 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001350{
Gilles Peskineca25db92019-04-19 11:43:08 +02001351 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1352 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001353 psa_key_usage_t expected_usage = expected_usage_arg;
1354 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001355 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001356 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1357 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001358 uint8_t *export_buffer = NULL;
1359
Gilles Peskine57ab7212019-01-28 13:03:09 +01001360 PSA_ASSERT( psa_crypto_init( ) );
1361
Gilles Peskineca25db92019-04-19 11:43:08 +02001362 /* Prepare the source key. */
1363 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1364 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001365 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001366 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001367 PSA_ASSERT( psa_import_key( &source_attributes,
1368 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001369 &source_key ) );
1370 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001371
Gilles Peskineca25db92019-04-19 11:43:08 +02001372 /* Prepare the target attributes. */
1373 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001374 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001375 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001376 /* Set volatile lifetime to reset the key identifier to 0. */
1377 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1378 }
1379
Gilles Peskineca25db92019-04-19 11:43:08 +02001380 if( target_usage_arg != -1 )
1381 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1382 if( target_alg_arg != -1 )
1383 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001384 if( target_alg2_arg != -1 )
1385 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001386
1387 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001388 PSA_ASSERT( psa_copy_key( source_key,
1389 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001390
1391 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001392 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001393
1394 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001395 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001396 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1397 psa_get_key_type( &target_attributes ) );
1398 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1399 psa_get_key_bits( &target_attributes ) );
1400 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1401 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001402 TEST_EQUAL( expected_alg2,
1403 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001404 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1405 {
1406 size_t length;
1407 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001408 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001409 material->len, &length ) );
1410 ASSERT_COMPARE( material->x, material->len,
1411 export_buffer, length );
1412 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001413
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001414 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001415 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001416 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001417 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001418
Ronald Cron5425a212020-08-04 14:58:35 +02001419 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001420
1421exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001422 /*
1423 * Source and target key attributes may have been returned by
1424 * psa_get_key_attributes() thus reset them as required.
1425 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001426 psa_reset_key_attributes( &source_attributes );
1427 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001428
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001429 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001430 mbedtls_free( export_buffer );
1431}
1432/* END_CASE */
1433
1434/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001435void copy_fail( int source_usage_arg,
1436 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001437 int type_arg, data_t *material,
1438 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001439 int target_usage_arg,
1440 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001441 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001442 int expected_status_arg )
1443{
1444 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1445 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001446 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1447 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001448 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001449
1450 PSA_ASSERT( psa_crypto_init( ) );
1451
1452 /* Prepare the source key. */
1453 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1454 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001455 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001456 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001457 PSA_ASSERT( psa_import_key( &source_attributes,
1458 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001459 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001460
1461 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001462 psa_set_key_id( &target_attributes, key_id );
1463 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001464 psa_set_key_type( &target_attributes, target_type_arg );
1465 psa_set_key_bits( &target_attributes, target_bits_arg );
1466 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1467 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001468 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001469
1470 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001471 TEST_EQUAL( psa_copy_key( source_key,
1472 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001473 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001474
Ronald Cron5425a212020-08-04 14:58:35 +02001475 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001476
Gilles Peskine4a644642019-05-03 17:14:08 +02001477exit:
1478 psa_reset_key_attributes( &source_attributes );
1479 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001480 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001481}
1482/* END_CASE */
1483
1484/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001485void hash_operation_init( )
1486{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001487 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001488 /* Test each valid way of initializing the object, except for `= {0}`, as
1489 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1490 * though it's OK by the C standard. We could test for this, but we'd need
1491 * to supress the Clang warning for the test. */
1492 psa_hash_operation_t func = psa_hash_operation_init( );
1493 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1494 psa_hash_operation_t zero;
1495
1496 memset( &zero, 0, sizeof( zero ) );
1497
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001498 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001499 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1500 PSA_ERROR_BAD_STATE );
1501 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1502 PSA_ERROR_BAD_STATE );
1503 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1504 PSA_ERROR_BAD_STATE );
1505
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001506 /* A default hash operation should be abortable without error. */
1507 PSA_ASSERT( psa_hash_abort( &func ) );
1508 PSA_ASSERT( psa_hash_abort( &init ) );
1509 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001510}
1511/* END_CASE */
1512
1513/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001514void hash_setup( int alg_arg,
1515 int expected_status_arg )
1516{
1517 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001518 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001519 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001520 psa_status_t status;
1521
Gilles Peskine8817f612018-12-18 00:18:46 +01001522 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001523
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001524 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001525 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001526
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001527 /* Whether setup succeeded or failed, abort must succeed. */
1528 PSA_ASSERT( psa_hash_abort( &operation ) );
1529
1530 /* If setup failed, reproduce the failure, so as to
1531 * test the resulting state of the operation object. */
1532 if( status != PSA_SUCCESS )
1533 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1534
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001535 /* Now the operation object should be reusable. */
1536#if defined(KNOWN_SUPPORTED_HASH_ALG)
1537 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1538 PSA_ASSERT( psa_hash_abort( &operation ) );
1539#endif
1540
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001541exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001542 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001543}
1544/* END_CASE */
1545
1546/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001547void hash_compute_fail( int alg_arg, data_t *input,
1548 int output_size_arg, int expected_status_arg )
1549{
1550 psa_algorithm_t alg = alg_arg;
1551 uint8_t *output = NULL;
1552 size_t output_size = output_size_arg;
1553 size_t output_length = INVALID_EXPORT_LENGTH;
1554 psa_status_t expected_status = expected_status_arg;
1555 psa_status_t status;
1556
1557 ASSERT_ALLOC( output, output_size );
1558
1559 PSA_ASSERT( psa_crypto_init( ) );
1560
1561 status = psa_hash_compute( alg, input->x, input->len,
1562 output, output_size, &output_length );
1563 TEST_EQUAL( status, expected_status );
1564 TEST_ASSERT( output_length <= output_size );
1565
1566exit:
1567 mbedtls_free( output );
1568 PSA_DONE( );
1569}
1570/* END_CASE */
1571
1572/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001573void hash_compare_fail( int alg_arg, data_t *input,
1574 data_t *reference_hash,
1575 int expected_status_arg )
1576{
1577 psa_algorithm_t alg = alg_arg;
1578 psa_status_t expected_status = expected_status_arg;
1579 psa_status_t status;
1580
1581 PSA_ASSERT( psa_crypto_init( ) );
1582
1583 status = psa_hash_compare( alg, input->x, input->len,
1584 reference_hash->x, reference_hash->len );
1585 TEST_EQUAL( status, expected_status );
1586
1587exit:
1588 PSA_DONE( );
1589}
1590/* END_CASE */
1591
1592/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001593void hash_compute_compare( int alg_arg, data_t *input,
1594 data_t *expected_output )
1595{
1596 psa_algorithm_t alg = alg_arg;
1597 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1598 size_t output_length = INVALID_EXPORT_LENGTH;
1599 size_t i;
1600
1601 PSA_ASSERT( psa_crypto_init( ) );
1602
1603 /* Compute with tight buffer */
1604 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001605 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001606 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001607 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001608 ASSERT_COMPARE( output, output_length,
1609 expected_output->x, expected_output->len );
1610
1611 /* Compute with larger buffer */
1612 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1613 output, sizeof( output ),
1614 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001615 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001616 ASSERT_COMPARE( output, output_length,
1617 expected_output->x, expected_output->len );
1618
1619 /* Compare with correct hash */
1620 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1621 output, output_length ) );
1622
1623 /* Compare with trailing garbage */
1624 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1625 output, output_length + 1 ),
1626 PSA_ERROR_INVALID_SIGNATURE );
1627
1628 /* Compare with truncated hash */
1629 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1630 output, output_length - 1 ),
1631 PSA_ERROR_INVALID_SIGNATURE );
1632
1633 /* Compare with corrupted value */
1634 for( i = 0; i < output_length; i++ )
1635 {
Chris Jones9634bb12021-01-20 15:56:42 +00001636 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001637 output[i] ^= 1;
1638 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1639 output, output_length ),
1640 PSA_ERROR_INVALID_SIGNATURE );
1641 output[i] ^= 1;
1642 }
1643
1644exit:
1645 PSA_DONE( );
1646}
1647/* END_CASE */
1648
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001649/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001650void hash_bad_order( )
1651{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001652 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001653 unsigned char input[] = "";
1654 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001655 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001656 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1657 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1658 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001659 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001660 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001661 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001662
Gilles Peskine8817f612018-12-18 00:18:46 +01001663 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001664
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001665 /* Call setup twice in a row. */
1666 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1667 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1668 PSA_ERROR_BAD_STATE );
1669 PSA_ASSERT( psa_hash_abort( &operation ) );
1670
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001671 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001672 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001673 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001674 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001675
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001676 /* Call update after finish. */
1677 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1678 PSA_ASSERT( psa_hash_finish( &operation,
1679 hash, sizeof( hash ), &hash_len ) );
1680 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001681 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001682 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001683
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001684 /* Call verify without calling setup beforehand. */
1685 TEST_EQUAL( psa_hash_verify( &operation,
1686 valid_hash, sizeof( valid_hash ) ),
1687 PSA_ERROR_BAD_STATE );
1688 PSA_ASSERT( psa_hash_abort( &operation ) );
1689
1690 /* Call verify after finish. */
1691 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1692 PSA_ASSERT( psa_hash_finish( &operation,
1693 hash, sizeof( hash ), &hash_len ) );
1694 TEST_EQUAL( psa_hash_verify( &operation,
1695 valid_hash, sizeof( valid_hash ) ),
1696 PSA_ERROR_BAD_STATE );
1697 PSA_ASSERT( psa_hash_abort( &operation ) );
1698
1699 /* Call verify twice in a row. */
1700 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1701 PSA_ASSERT( psa_hash_verify( &operation,
1702 valid_hash, sizeof( valid_hash ) ) );
1703 TEST_EQUAL( psa_hash_verify( &operation,
1704 valid_hash, sizeof( valid_hash ) ),
1705 PSA_ERROR_BAD_STATE );
1706 PSA_ASSERT( psa_hash_abort( &operation ) );
1707
1708 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001709 TEST_EQUAL( psa_hash_finish( &operation,
1710 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001711 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001712 PSA_ASSERT( psa_hash_abort( &operation ) );
1713
1714 /* Call finish twice in a row. */
1715 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1716 PSA_ASSERT( psa_hash_finish( &operation,
1717 hash, sizeof( hash ), &hash_len ) );
1718 TEST_EQUAL( psa_hash_finish( &operation,
1719 hash, sizeof( hash ), &hash_len ),
1720 PSA_ERROR_BAD_STATE );
1721 PSA_ASSERT( psa_hash_abort( &operation ) );
1722
1723 /* Call finish after calling verify. */
1724 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1725 PSA_ASSERT( psa_hash_verify( &operation,
1726 valid_hash, sizeof( valid_hash ) ) );
1727 TEST_EQUAL( psa_hash_finish( &operation,
1728 hash, sizeof( hash ), &hash_len ),
1729 PSA_ERROR_BAD_STATE );
1730 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001731
1732exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001733 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001734}
1735/* END_CASE */
1736
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001737/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001738void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001739{
1740 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001741 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1742 * appended to it */
1743 unsigned char hash[] = {
1744 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1745 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1746 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001747 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001748 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001749
Gilles Peskine8817f612018-12-18 00:18:46 +01001750 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001751
itayzafrir27e69452018-11-01 14:26:34 +02001752 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001753 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001754 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001755 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001756
itayzafrir27e69452018-11-01 14:26:34 +02001757 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001758 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001759 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001760 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001761
itayzafrir27e69452018-11-01 14:26:34 +02001762 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001763 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001764 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001765 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001766
itayzafrirec93d302018-10-18 18:01:10 +03001767exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001768 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001769}
1770/* END_CASE */
1771
Ronald Cronee414c72021-03-18 18:50:08 +01001772/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001773void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001774{
1775 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001776 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001777 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001778 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001779 size_t hash_len;
1780
Gilles Peskine8817f612018-12-18 00:18:46 +01001781 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001782
itayzafrir58028322018-10-25 10:22:01 +03001783 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001784 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001785 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001786 hash, expected_size - 1, &hash_len ),
1787 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001788
1789exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001790 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001791}
1792/* END_CASE */
1793
Ronald Cronee414c72021-03-18 18:50:08 +01001794/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001795void hash_clone_source_state( )
1796{
1797 psa_algorithm_t alg = PSA_ALG_SHA_256;
1798 unsigned char hash[PSA_HASH_MAX_SIZE];
1799 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1800 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1801 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1802 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1803 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1804 size_t hash_len;
1805
1806 PSA_ASSERT( psa_crypto_init( ) );
1807 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1808
1809 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1810 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1811 PSA_ASSERT( psa_hash_finish( &op_finished,
1812 hash, sizeof( hash ), &hash_len ) );
1813 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1814 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1815
1816 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1817 PSA_ERROR_BAD_STATE );
1818
1819 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1820 PSA_ASSERT( psa_hash_finish( &op_init,
1821 hash, sizeof( hash ), &hash_len ) );
1822 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1823 PSA_ASSERT( psa_hash_finish( &op_finished,
1824 hash, sizeof( hash ), &hash_len ) );
1825 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1826 PSA_ASSERT( psa_hash_finish( &op_aborted,
1827 hash, sizeof( hash ), &hash_len ) );
1828
1829exit:
1830 psa_hash_abort( &op_source );
1831 psa_hash_abort( &op_init );
1832 psa_hash_abort( &op_setup );
1833 psa_hash_abort( &op_finished );
1834 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001835 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001836}
1837/* END_CASE */
1838
Ronald Cronee414c72021-03-18 18:50:08 +01001839/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001840void hash_clone_target_state( )
1841{
1842 psa_algorithm_t alg = PSA_ALG_SHA_256;
1843 unsigned char hash[PSA_HASH_MAX_SIZE];
1844 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1845 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1846 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1847 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1848 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1849 size_t hash_len;
1850
1851 PSA_ASSERT( psa_crypto_init( ) );
1852
1853 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1854 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1855 PSA_ASSERT( psa_hash_finish( &op_finished,
1856 hash, sizeof( hash ), &hash_len ) );
1857 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1858 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1859
1860 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1861 PSA_ASSERT( psa_hash_finish( &op_target,
1862 hash, sizeof( hash ), &hash_len ) );
1863
1864 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1865 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1866 PSA_ERROR_BAD_STATE );
1867 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1868 PSA_ERROR_BAD_STATE );
1869
1870exit:
1871 psa_hash_abort( &op_target );
1872 psa_hash_abort( &op_init );
1873 psa_hash_abort( &op_setup );
1874 psa_hash_abort( &op_finished );
1875 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001876 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001877}
1878/* END_CASE */
1879
itayzafrir58028322018-10-25 10:22:01 +03001880/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001881void mac_operation_init( )
1882{
Jaeden Amero252ef282019-02-15 14:05:35 +00001883 const uint8_t input[1] = { 0 };
1884
Jaeden Amero769ce272019-01-04 11:48:03 +00001885 /* Test each valid way of initializing the object, except for `= {0}`, as
1886 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1887 * though it's OK by the C standard. We could test for this, but we'd need
1888 * to supress the Clang warning for the test. */
1889 psa_mac_operation_t func = psa_mac_operation_init( );
1890 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1891 psa_mac_operation_t zero;
1892
1893 memset( &zero, 0, sizeof( zero ) );
1894
Jaeden Amero252ef282019-02-15 14:05:35 +00001895 /* A freshly-initialized MAC operation should not be usable. */
1896 TEST_EQUAL( psa_mac_update( &func,
1897 input, sizeof( input ) ),
1898 PSA_ERROR_BAD_STATE );
1899 TEST_EQUAL( psa_mac_update( &init,
1900 input, sizeof( input ) ),
1901 PSA_ERROR_BAD_STATE );
1902 TEST_EQUAL( psa_mac_update( &zero,
1903 input, sizeof( input ) ),
1904 PSA_ERROR_BAD_STATE );
1905
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001906 /* A default MAC operation should be abortable without error. */
1907 PSA_ASSERT( psa_mac_abort( &func ) );
1908 PSA_ASSERT( psa_mac_abort( &init ) );
1909 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001910}
1911/* END_CASE */
1912
1913/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001914void mac_setup( int key_type_arg,
1915 data_t *key,
1916 int alg_arg,
1917 int expected_status_arg )
1918{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001919 psa_key_type_t key_type = key_type_arg;
1920 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001921 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001922 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001923 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1924#if defined(KNOWN_SUPPORTED_MAC_ALG)
1925 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1926#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001927
Gilles Peskine8817f612018-12-18 00:18:46 +01001928 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001929
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001930 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1931 &operation, &status ) )
1932 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001933 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001934
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001935 /* The operation object should be reusable. */
1936#if defined(KNOWN_SUPPORTED_MAC_ALG)
1937 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1938 smoke_test_key_data,
1939 sizeof( smoke_test_key_data ),
1940 KNOWN_SUPPORTED_MAC_ALG,
1941 &operation, &status ) )
1942 goto exit;
1943 TEST_EQUAL( status, PSA_SUCCESS );
1944#endif
1945
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001946exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001947 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001948}
1949/* END_CASE */
1950
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001951/* 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 +00001952void mac_bad_order( )
1953{
Ronald Cron5425a212020-08-04 14:58:35 +02001954 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001955 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1956 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001957 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001958 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1959 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1960 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001961 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001962 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1963 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1964 size_t sign_mac_length = 0;
1965 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1966 const uint8_t verify_mac[] = {
1967 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1968 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1969 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1970
1971 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001972 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001973 psa_set_key_algorithm( &attributes, alg );
1974 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001975
Ronald Cron5425a212020-08-04 14:58:35 +02001976 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1977 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001978
Jaeden Amero252ef282019-02-15 14:05:35 +00001979 /* Call update without calling setup beforehand. */
1980 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1981 PSA_ERROR_BAD_STATE );
1982 PSA_ASSERT( psa_mac_abort( &operation ) );
1983
1984 /* Call sign finish without calling setup beforehand. */
1985 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1986 &sign_mac_length),
1987 PSA_ERROR_BAD_STATE );
1988 PSA_ASSERT( psa_mac_abort( &operation ) );
1989
1990 /* Call verify finish without calling setup beforehand. */
1991 TEST_EQUAL( psa_mac_verify_finish( &operation,
1992 verify_mac, sizeof( verify_mac ) ),
1993 PSA_ERROR_BAD_STATE );
1994 PSA_ASSERT( psa_mac_abort( &operation ) );
1995
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001996 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001997 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
1998 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001999 PSA_ERROR_BAD_STATE );
2000 PSA_ASSERT( psa_mac_abort( &operation ) );
2001
Jaeden Amero252ef282019-02-15 14:05:35 +00002002 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002003 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002004 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2005 PSA_ASSERT( psa_mac_sign_finish( &operation,
2006 sign_mac, sizeof( sign_mac ),
2007 &sign_mac_length ) );
2008 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2009 PSA_ERROR_BAD_STATE );
2010 PSA_ASSERT( psa_mac_abort( &operation ) );
2011
2012 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002013 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002014 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2015 PSA_ASSERT( psa_mac_verify_finish( &operation,
2016 verify_mac, sizeof( verify_mac ) ) );
2017 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2018 PSA_ERROR_BAD_STATE );
2019 PSA_ASSERT( psa_mac_abort( &operation ) );
2020
2021 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002022 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002023 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2024 PSA_ASSERT( psa_mac_sign_finish( &operation,
2025 sign_mac, sizeof( sign_mac ),
2026 &sign_mac_length ) );
2027 TEST_EQUAL( psa_mac_sign_finish( &operation,
2028 sign_mac, sizeof( sign_mac ),
2029 &sign_mac_length ),
2030 PSA_ERROR_BAD_STATE );
2031 PSA_ASSERT( psa_mac_abort( &operation ) );
2032
2033 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002034 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002035 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2036 PSA_ASSERT( psa_mac_verify_finish( &operation,
2037 verify_mac, sizeof( verify_mac ) ) );
2038 TEST_EQUAL( psa_mac_verify_finish( &operation,
2039 verify_mac, sizeof( verify_mac ) ),
2040 PSA_ERROR_BAD_STATE );
2041 PSA_ASSERT( psa_mac_abort( &operation ) );
2042
2043 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002044 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002045 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2046 TEST_EQUAL( psa_mac_verify_finish( &operation,
2047 verify_mac, sizeof( verify_mac ) ),
2048 PSA_ERROR_BAD_STATE );
2049 PSA_ASSERT( psa_mac_abort( &operation ) );
2050
2051 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002052 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002053 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2054 TEST_EQUAL( psa_mac_sign_finish( &operation,
2055 sign_mac, sizeof( sign_mac ),
2056 &sign_mac_length ),
2057 PSA_ERROR_BAD_STATE );
2058 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002059
Ronald Cron5425a212020-08-04 14:58:35 +02002060 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002061
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002062exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002063 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002064}
2065/* END_CASE */
2066
2067/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002068void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002069 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002070 int alg_arg,
2071 data_t *input,
2072 data_t *expected_mac )
2073{
Ronald Cron5425a212020-08-04 14:58:35 +02002074 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002075 psa_key_type_t key_type = key_type_arg;
2076 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002077 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002078 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002079 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002080 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002081 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002082 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002083 const size_t output_sizes_to_test[] = {
2084 0,
2085 1,
2086 expected_mac->len - 1,
2087 expected_mac->len,
2088 expected_mac->len + 1,
2089 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002090
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002091 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002092 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002093 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002094
Gilles Peskine8817f612018-12-18 00:18:46 +01002095 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002096
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002097 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002098 psa_set_key_algorithm( &attributes, alg );
2099 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002100
Ronald Cron5425a212020-08-04 14:58:35 +02002101 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2102 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002103
Gilles Peskine8b356b52020-08-25 23:44:59 +02002104 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2105 {
2106 const size_t output_size = output_sizes_to_test[i];
2107 psa_status_t expected_status =
2108 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2109 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002110
Chris Jones9634bb12021-01-20 15:56:42 +00002111 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002112 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002113
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002114 /* Calculate the MAC, one-shot case. */
2115 TEST_EQUAL( psa_mac_compute( key, alg,
2116 input->x, input->len,
2117 actual_mac, output_size, &mac_length ),
2118 expected_status );
2119 if( expected_status == PSA_SUCCESS )
2120 {
2121 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2122 actual_mac, mac_length );
2123 }
2124
2125 if( output_size > 0 )
2126 memset( actual_mac, 0, output_size );
2127
2128 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002129 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002130 PSA_ASSERT( psa_mac_update( &operation,
2131 input->x, input->len ) );
2132 TEST_EQUAL( psa_mac_sign_finish( &operation,
2133 actual_mac, output_size,
2134 &mac_length ),
2135 expected_status );
2136 PSA_ASSERT( psa_mac_abort( &operation ) );
2137
2138 if( expected_status == PSA_SUCCESS )
2139 {
2140 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2141 actual_mac, mac_length );
2142 }
2143 mbedtls_free( actual_mac );
2144 actual_mac = NULL;
2145 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002146
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002147exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002148 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002149 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002150 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002151 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002152}
2153/* END_CASE */
2154
2155/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002156void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002157 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002158 int alg_arg,
2159 data_t *input,
2160 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002161{
Ronald Cron5425a212020-08-04 14:58:35 +02002162 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002163 psa_key_type_t key_type = key_type_arg;
2164 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002165 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002166 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002167 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002168
Gilles Peskine69c12672018-06-28 00:07:19 +02002169 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2170
Gilles Peskine8817f612018-12-18 00:18:46 +01002171 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002172
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002173 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002174 psa_set_key_algorithm( &attributes, alg );
2175 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002176
Ronald Cron5425a212020-08-04 14:58:35 +02002177 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2178 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002179
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002180 /* Verify correct MAC, one-shot case. */
2181 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2182 expected_mac->x, expected_mac->len ) );
2183
2184 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002185 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002186 PSA_ASSERT( psa_mac_update( &operation,
2187 input->x, input->len ) );
2188 PSA_ASSERT( psa_mac_verify_finish( &operation,
2189 expected_mac->x,
2190 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002191
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002192 /* Test a MAC that's too short, one-shot case. */
2193 TEST_EQUAL( psa_mac_verify( key, alg,
2194 input->x, input->len,
2195 expected_mac->x,
2196 expected_mac->len - 1 ),
2197 PSA_ERROR_INVALID_SIGNATURE );
2198
2199 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002200 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002201 PSA_ASSERT( psa_mac_update( &operation,
2202 input->x, input->len ) );
2203 TEST_EQUAL( psa_mac_verify_finish( &operation,
2204 expected_mac->x,
2205 expected_mac->len - 1 ),
2206 PSA_ERROR_INVALID_SIGNATURE );
2207
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002208 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002209 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2210 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002211 TEST_EQUAL( psa_mac_verify( key, alg,
2212 input->x, input->len,
2213 perturbed_mac, expected_mac->len + 1 ),
2214 PSA_ERROR_INVALID_SIGNATURE );
2215
2216 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002217 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002218 PSA_ASSERT( psa_mac_update( &operation,
2219 input->x, input->len ) );
2220 TEST_EQUAL( psa_mac_verify_finish( &operation,
2221 perturbed_mac,
2222 expected_mac->len + 1 ),
2223 PSA_ERROR_INVALID_SIGNATURE );
2224
2225 /* Test changing one byte. */
2226 for( size_t i = 0; i < expected_mac->len; i++ )
2227 {
Chris Jones9634bb12021-01-20 15:56:42 +00002228 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002229 perturbed_mac[i] ^= 1;
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002230
2231 TEST_EQUAL( psa_mac_verify( key, alg,
2232 input->x, input->len,
2233 perturbed_mac, expected_mac->len ),
2234 PSA_ERROR_INVALID_SIGNATURE );
2235
Ronald Cron5425a212020-08-04 14:58:35 +02002236 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002237 PSA_ASSERT( psa_mac_update( &operation,
2238 input->x, input->len ) );
2239 TEST_EQUAL( psa_mac_verify_finish( &operation,
2240 perturbed_mac,
2241 expected_mac->len ),
2242 PSA_ERROR_INVALID_SIGNATURE );
2243 perturbed_mac[i] ^= 1;
2244 }
2245
Gilles Peskine8c9def32018-02-08 10:02:12 +01002246exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002247 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002248 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002249 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002250 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002251}
2252/* END_CASE */
2253
2254/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002255void cipher_operation_init( )
2256{
Jaeden Ameroab439972019-02-15 14:12:05 +00002257 const uint8_t input[1] = { 0 };
2258 unsigned char output[1] = { 0 };
2259 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002260 /* Test each valid way of initializing the object, except for `= {0}`, as
2261 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2262 * though it's OK by the C standard. We could test for this, but we'd need
2263 * to supress the Clang warning for the test. */
2264 psa_cipher_operation_t func = psa_cipher_operation_init( );
2265 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2266 psa_cipher_operation_t zero;
2267
2268 memset( &zero, 0, sizeof( zero ) );
2269
Jaeden Ameroab439972019-02-15 14:12:05 +00002270 /* A freshly-initialized cipher operation should not be usable. */
2271 TEST_EQUAL( psa_cipher_update( &func,
2272 input, sizeof( input ),
2273 output, sizeof( output ),
2274 &output_length ),
2275 PSA_ERROR_BAD_STATE );
2276 TEST_EQUAL( psa_cipher_update( &init,
2277 input, sizeof( input ),
2278 output, sizeof( output ),
2279 &output_length ),
2280 PSA_ERROR_BAD_STATE );
2281 TEST_EQUAL( psa_cipher_update( &zero,
2282 input, sizeof( input ),
2283 output, sizeof( output ),
2284 &output_length ),
2285 PSA_ERROR_BAD_STATE );
2286
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002287 /* A default cipher operation should be abortable without error. */
2288 PSA_ASSERT( psa_cipher_abort( &func ) );
2289 PSA_ASSERT( psa_cipher_abort( &init ) );
2290 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002291}
2292/* END_CASE */
2293
2294/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002295void cipher_setup( int key_type_arg,
2296 data_t *key,
2297 int alg_arg,
2298 int expected_status_arg )
2299{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002300 psa_key_type_t key_type = key_type_arg;
2301 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002302 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002303 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002304 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002305#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002306 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2307#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002308
Gilles Peskine8817f612018-12-18 00:18:46 +01002309 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002310
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002311 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2312 &operation, &status ) )
2313 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002314 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002315
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002316 /* The operation object should be reusable. */
2317#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2318 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2319 smoke_test_key_data,
2320 sizeof( smoke_test_key_data ),
2321 KNOWN_SUPPORTED_CIPHER_ALG,
2322 &operation, &status ) )
2323 goto exit;
2324 TEST_EQUAL( status, PSA_SUCCESS );
2325#endif
2326
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002327exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002328 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002329 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002330}
2331/* END_CASE */
2332
Ronald Cronee414c72021-03-18 18:50:08 +01002333/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002334void cipher_bad_order( )
2335{
Ronald Cron5425a212020-08-04 14:58:35 +02002336 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002337 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2338 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002339 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002340 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002341 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002342 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002343 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2344 0xaa, 0xaa, 0xaa, 0xaa };
2345 const uint8_t text[] = {
2346 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2347 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002348 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002349 size_t length = 0;
2350
2351 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002352 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2353 psa_set_key_algorithm( &attributes, alg );
2354 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002355 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2356 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002357
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002358 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002359 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2360 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002361 PSA_ERROR_BAD_STATE );
2362 PSA_ASSERT( psa_cipher_abort( &operation ) );
2363
2364 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002365 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2366 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002367 PSA_ERROR_BAD_STATE );
2368 PSA_ASSERT( psa_cipher_abort( &operation ) );
2369
Jaeden Ameroab439972019-02-15 14:12:05 +00002370 /* Generate an IV without calling setup beforehand. */
2371 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2372 buffer, sizeof( buffer ),
2373 &length ),
2374 PSA_ERROR_BAD_STATE );
2375 PSA_ASSERT( psa_cipher_abort( &operation ) );
2376
2377 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002378 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002379 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2380 buffer, sizeof( buffer ),
2381 &length ) );
2382 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2383 buffer, sizeof( buffer ),
2384 &length ),
2385 PSA_ERROR_BAD_STATE );
2386 PSA_ASSERT( psa_cipher_abort( &operation ) );
2387
2388 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002389 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002390 PSA_ASSERT( psa_cipher_set_iv( &operation,
2391 iv, sizeof( iv ) ) );
2392 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2393 buffer, sizeof( buffer ),
2394 &length ),
2395 PSA_ERROR_BAD_STATE );
2396 PSA_ASSERT( psa_cipher_abort( &operation ) );
2397
2398 /* Set an IV without calling setup beforehand. */
2399 TEST_EQUAL( psa_cipher_set_iv( &operation,
2400 iv, sizeof( iv ) ),
2401 PSA_ERROR_BAD_STATE );
2402 PSA_ASSERT( psa_cipher_abort( &operation ) );
2403
2404 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002405 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002406 PSA_ASSERT( psa_cipher_set_iv( &operation,
2407 iv, sizeof( iv ) ) );
2408 TEST_EQUAL( psa_cipher_set_iv( &operation,
2409 iv, sizeof( iv ) ),
2410 PSA_ERROR_BAD_STATE );
2411 PSA_ASSERT( psa_cipher_abort( &operation ) );
2412
2413 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002414 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002415 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2416 buffer, sizeof( buffer ),
2417 &length ) );
2418 TEST_EQUAL( psa_cipher_set_iv( &operation,
2419 iv, sizeof( iv ) ),
2420 PSA_ERROR_BAD_STATE );
2421 PSA_ASSERT( psa_cipher_abort( &operation ) );
2422
2423 /* Call update without calling setup beforehand. */
2424 TEST_EQUAL( psa_cipher_update( &operation,
2425 text, sizeof( text ),
2426 buffer, sizeof( buffer ),
2427 &length ),
2428 PSA_ERROR_BAD_STATE );
2429 PSA_ASSERT( psa_cipher_abort( &operation ) );
2430
2431 /* Call update without an IV where an IV is required. */
2432 TEST_EQUAL( psa_cipher_update( &operation,
2433 text, sizeof( text ),
2434 buffer, sizeof( buffer ),
2435 &length ),
2436 PSA_ERROR_BAD_STATE );
2437 PSA_ASSERT( psa_cipher_abort( &operation ) );
2438
2439 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002440 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002441 PSA_ASSERT( psa_cipher_set_iv( &operation,
2442 iv, sizeof( iv ) ) );
2443 PSA_ASSERT( psa_cipher_finish( &operation,
2444 buffer, sizeof( buffer ), &length ) );
2445 TEST_EQUAL( psa_cipher_update( &operation,
2446 text, sizeof( text ),
2447 buffer, sizeof( buffer ),
2448 &length ),
2449 PSA_ERROR_BAD_STATE );
2450 PSA_ASSERT( psa_cipher_abort( &operation ) );
2451
2452 /* Call finish without calling setup beforehand. */
2453 TEST_EQUAL( psa_cipher_finish( &operation,
2454 buffer, sizeof( buffer ), &length ),
2455 PSA_ERROR_BAD_STATE );
2456 PSA_ASSERT( psa_cipher_abort( &operation ) );
2457
2458 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002459 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002460 /* Not calling update means we are encrypting an empty buffer, which is OK
2461 * for cipher modes with padding. */
2462 TEST_EQUAL( psa_cipher_finish( &operation,
2463 buffer, sizeof( buffer ), &length ),
2464 PSA_ERROR_BAD_STATE );
2465 PSA_ASSERT( psa_cipher_abort( &operation ) );
2466
2467 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002468 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002469 PSA_ASSERT( psa_cipher_set_iv( &operation,
2470 iv, sizeof( iv ) ) );
2471 PSA_ASSERT( psa_cipher_finish( &operation,
2472 buffer, sizeof( buffer ), &length ) );
2473 TEST_EQUAL( psa_cipher_finish( &operation,
2474 buffer, sizeof( buffer ), &length ),
2475 PSA_ERROR_BAD_STATE );
2476 PSA_ASSERT( psa_cipher_abort( &operation ) );
2477
Ronald Cron5425a212020-08-04 14:58:35 +02002478 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002479
Jaeden Ameroab439972019-02-15 14:12:05 +00002480exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002481 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002482 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002483}
2484/* END_CASE */
2485
2486/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002487void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002488 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002489 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002490 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002491{
Ronald Cron5425a212020-08-04 14:58:35 +02002492 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002493 psa_status_t status;
2494 psa_key_type_t key_type = key_type_arg;
2495 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002496 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002497 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002498 size_t output_buffer_size = 0;
2499 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002500 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002501 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002502 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002503
Gilles Peskine8817f612018-12-18 00:18:46 +01002504 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002505
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002506 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2507 psa_set_key_algorithm( &attributes, alg );
2508 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002509
Ronald Cron5425a212020-08-04 14:58:35 +02002510 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2511 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002512
Ronald Cron5425a212020-08-04 14:58:35 +02002513 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002514
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002515 if( iv->len > 0 )
2516 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002517 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002518 }
2519
gabor-mezei-armceface22021-01-21 12:26:17 +01002520 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2521 TEST_ASSERT( output_buffer_size <=
2522 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002523 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002524
Gilles Peskine8817f612018-12-18 00:18:46 +01002525 PSA_ASSERT( psa_cipher_update( &operation,
2526 input->x, input->len,
2527 output, output_buffer_size,
2528 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002529 TEST_ASSERT( function_output_length <=
2530 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2531 TEST_ASSERT( function_output_length <=
2532 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002533 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002534
Gilles Peskine50e586b2018-06-08 14:28:46 +02002535 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002536 ( output_buffer_size == 0 ? NULL :
2537 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002538 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002539 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002540 TEST_ASSERT( function_output_length <=
2541 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2542 TEST_ASSERT( function_output_length <=
2543 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002544 total_output_length += function_output_length;
2545
Gilles Peskinefe11b722018-12-18 00:24:04 +01002546 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002547 if( expected_status == PSA_SUCCESS )
2548 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002549 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002550 ASSERT_COMPARE( expected_output->x, expected_output->len,
2551 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002552 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002553
Gilles Peskine50e586b2018-06-08 14:28:46 +02002554exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002555 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002556 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002557 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002558 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002559}
2560/* END_CASE */
2561
2562/* BEGIN_CASE */
2563void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002564 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002565 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002566 int first_part_size_arg,
2567 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002568 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002569{
Ronald Cron5425a212020-08-04 14:58:35 +02002570 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002571 psa_key_type_t key_type = key_type_arg;
2572 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002573 size_t first_part_size = first_part_size_arg;
2574 size_t output1_length = output1_length_arg;
2575 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002576 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002577 size_t output_buffer_size = 0;
2578 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002579 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002580 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002581 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002582
Gilles Peskine8817f612018-12-18 00:18:46 +01002583 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002584
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002585 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2586 psa_set_key_algorithm( &attributes, alg );
2587 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002588
Ronald Cron5425a212020-08-04 14:58:35 +02002589 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2590 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002591
Ronald Cron5425a212020-08-04 14:58:35 +02002592 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002593
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002594 if( iv->len > 0 )
2595 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002596 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002597 }
2598
gabor-mezei-armceface22021-01-21 12:26:17 +01002599 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2600 TEST_ASSERT( output_buffer_size <=
2601 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002602 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002603
Gilles Peskinee0866522019-02-19 19:44:00 +01002604 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002605 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2606 output, output_buffer_size,
2607 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002608 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002609 TEST_ASSERT( function_output_length <=
2610 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2611 TEST_ASSERT( function_output_length <=
2612 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002613 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002614
Gilles Peskine8817f612018-12-18 00:18:46 +01002615 PSA_ASSERT( psa_cipher_update( &operation,
2616 input->x + first_part_size,
2617 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002618 ( output_buffer_size == 0 ? NULL :
2619 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002620 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002621 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002622 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002623 TEST_ASSERT( function_output_length <=
2624 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2625 alg,
2626 input->len - first_part_size ) );
2627 TEST_ASSERT( function_output_length <=
2628 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002629 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002630
Gilles Peskine8817f612018-12-18 00:18:46 +01002631 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002632 ( output_buffer_size == 0 ? NULL :
2633 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002634 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002635 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002636 TEST_ASSERT( function_output_length <=
2637 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2638 TEST_ASSERT( function_output_length <=
2639 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002640 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002641 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002642
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002643 ASSERT_COMPARE( expected_output->x, expected_output->len,
2644 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002645
2646exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002647 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002648 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002649 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002650 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002651}
2652/* END_CASE */
2653
2654/* BEGIN_CASE */
2655void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002656 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002657 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002658 int first_part_size_arg,
2659 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002660 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002661{
Ronald Cron5425a212020-08-04 14:58:35 +02002662 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002663 psa_key_type_t key_type = key_type_arg;
2664 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002665 size_t first_part_size = first_part_size_arg;
2666 size_t output1_length = output1_length_arg;
2667 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002668 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002669 size_t output_buffer_size = 0;
2670 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002671 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002672 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002673 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002674
Gilles Peskine8817f612018-12-18 00:18:46 +01002675 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002676
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002677 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2678 psa_set_key_algorithm( &attributes, alg );
2679 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002680
Ronald Cron5425a212020-08-04 14:58:35 +02002681 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2682 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002683
Ronald Cron5425a212020-08-04 14:58:35 +02002684 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002685
Steven Cooreman177deba2020-09-07 17:14:14 +02002686 if( iv->len > 0 )
2687 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002688 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002689 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002690
gabor-mezei-armceface22021-01-21 12:26:17 +01002691 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2692 TEST_ASSERT( output_buffer_size <=
2693 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002694 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002695
Gilles Peskinee0866522019-02-19 19:44:00 +01002696 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002697 PSA_ASSERT( psa_cipher_update( &operation,
2698 input->x, first_part_size,
2699 output, output_buffer_size,
2700 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002701 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002702 TEST_ASSERT( function_output_length <=
2703 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2704 TEST_ASSERT( function_output_length <=
2705 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002706 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002707
Gilles Peskine8817f612018-12-18 00:18:46 +01002708 PSA_ASSERT( psa_cipher_update( &operation,
2709 input->x + first_part_size,
2710 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002711 ( output_buffer_size == 0 ? NULL :
2712 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002713 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002714 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002715 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002716 TEST_ASSERT( function_output_length <=
2717 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2718 alg,
2719 input->len - first_part_size ) );
2720 TEST_ASSERT( function_output_length <=
2721 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002722 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002723
Gilles Peskine8817f612018-12-18 00:18:46 +01002724 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002725 ( output_buffer_size == 0 ? NULL :
2726 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002727 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002728 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002729 TEST_ASSERT( function_output_length <=
2730 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2731 TEST_ASSERT( function_output_length <=
2732 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002733 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002734 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002735
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002736 ASSERT_COMPARE( expected_output->x, expected_output->len,
2737 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002738
2739exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002740 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002741 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002742 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002743 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002744}
2745/* END_CASE */
2746
Gilles Peskine50e586b2018-06-08 14:28:46 +02002747/* BEGIN_CASE */
2748void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002749 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002750 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002751 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002752{
Ronald Cron5425a212020-08-04 14:58:35 +02002753 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002754 psa_status_t status;
2755 psa_key_type_t key_type = key_type_arg;
2756 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002757 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002758 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002759 size_t output_buffer_size = 0;
2760 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002761 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002762 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002763 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002764
Gilles Peskine8817f612018-12-18 00:18:46 +01002765 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002766
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002767 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2768 psa_set_key_algorithm( &attributes, alg );
2769 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002770
Ronald Cron5425a212020-08-04 14:58:35 +02002771 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2772 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002773
Ronald Cron5425a212020-08-04 14:58:35 +02002774 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002775
Steven Cooreman177deba2020-09-07 17:14:14 +02002776 if( iv->len > 0 )
2777 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002778 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002779 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002780
gabor-mezei-armceface22021-01-21 12:26:17 +01002781 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2782 TEST_ASSERT( output_buffer_size <=
2783 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002784 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002785
Gilles Peskine8817f612018-12-18 00:18:46 +01002786 PSA_ASSERT( psa_cipher_update( &operation,
2787 input->x, input->len,
2788 output, output_buffer_size,
2789 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002790 TEST_ASSERT( function_output_length <=
2791 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2792 TEST_ASSERT( function_output_length <=
2793 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002794 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002795
Gilles Peskine50e586b2018-06-08 14:28:46 +02002796 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002797 ( output_buffer_size == 0 ? NULL :
2798 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002799 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002800 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002801 TEST_ASSERT( function_output_length <=
2802 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2803 TEST_ASSERT( function_output_length <=
2804 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002805 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002806 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002807
2808 if( expected_status == PSA_SUCCESS )
2809 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002810 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002811 ASSERT_COMPARE( expected_output->x, expected_output->len,
2812 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002813 }
2814
Gilles Peskine50e586b2018-06-08 14:28:46 +02002815exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002816 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002817 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002818 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002819 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002820}
2821/* END_CASE */
2822
Gilles Peskine50e586b2018-06-08 14:28:46 +02002823/* BEGIN_CASE */
2824void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002825 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002826 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002827{
Ronald Cron5425a212020-08-04 14:58:35 +02002828 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002829 psa_key_type_t key_type = key_type_arg;
2830 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002831 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002832 size_t iv_size = 16;
2833 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002834 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002835 size_t output1_size = 0;
2836 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002837 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002838 size_t output2_size = 0;
2839 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002840 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002841 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2842 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002843 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002844
Gilles Peskine8817f612018-12-18 00:18:46 +01002845 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002846
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002847 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2848 psa_set_key_algorithm( &attributes, alg );
2849 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002850
Ronald Cron5425a212020-08-04 14:58:35 +02002851 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2852 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002853
Ronald Cron5425a212020-08-04 14:58:35 +02002854 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2855 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002856
Steven Cooreman177deba2020-09-07 17:14:14 +02002857 if( alg != PSA_ALG_ECB_NO_PADDING )
2858 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002859 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2860 iv, iv_size,
2861 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002862 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002863 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2864 TEST_ASSERT( output1_size <=
2865 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002866 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002867
Gilles Peskine8817f612018-12-18 00:18:46 +01002868 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2869 output1, output1_size,
2870 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002871 TEST_ASSERT( output1_length <=
2872 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2873 TEST_ASSERT( output1_length <=
2874 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2875
Gilles Peskine8817f612018-12-18 00:18:46 +01002876 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002877 output1 + output1_length,
2878 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002879 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002880 TEST_ASSERT( function_output_length <=
2881 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2882 TEST_ASSERT( function_output_length <=
2883 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002884
Gilles Peskine048b7f02018-06-08 14:20:49 +02002885 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002886
Gilles Peskine8817f612018-12-18 00:18:46 +01002887 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002888
2889 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002890 TEST_ASSERT( output2_size <=
2891 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2892 TEST_ASSERT( output2_size <=
2893 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002894 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002895
Steven Cooreman177deba2020-09-07 17:14:14 +02002896 if( iv_length > 0 )
2897 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002898 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2899 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002900 }
2901
Gilles Peskine8817f612018-12-18 00:18:46 +01002902 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2903 output2, output2_size,
2904 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002905 TEST_ASSERT( output2_length <=
2906 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
2907 TEST_ASSERT( output2_length <=
2908 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
2909
Gilles Peskine048b7f02018-06-08 14:20:49 +02002910 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002911 PSA_ASSERT( psa_cipher_finish( &operation2,
2912 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002913 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002914 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002915 TEST_ASSERT( function_output_length <=
2916 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2917 TEST_ASSERT( function_output_length <=
2918 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03002919
Gilles Peskine048b7f02018-06-08 14:20:49 +02002920 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002921
Gilles Peskine8817f612018-12-18 00:18:46 +01002922 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002923
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002924 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002925
2926exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002927 psa_cipher_abort( &operation1 );
2928 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002929 mbedtls_free( output1 );
2930 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002931 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002932 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03002933}
2934/* END_CASE */
2935
2936/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002937void cipher_verify_output_multipart( int alg_arg,
2938 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002939 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002940 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002941 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03002942{
Ronald Cron5425a212020-08-04 14:58:35 +02002943 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002944 psa_key_type_t key_type = key_type_arg;
2945 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002946 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002947 unsigned char iv[16] = {0};
2948 size_t iv_size = 16;
2949 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002950 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002951 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002952 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002953 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002954 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002955 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002956 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002957 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2958 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002959 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002960
Gilles Peskine8817f612018-12-18 00:18:46 +01002961 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002962
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002963 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2964 psa_set_key_algorithm( &attributes, alg );
2965 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002966
Ronald Cron5425a212020-08-04 14:58:35 +02002967 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2968 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03002969
Ronald Cron5425a212020-08-04 14:58:35 +02002970 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2971 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002972
Steven Cooreman177deba2020-09-07 17:14:14 +02002973 if( alg != PSA_ALG_ECB_NO_PADDING )
2974 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002975 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2976 iv, iv_size,
2977 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002978 }
2979
gabor-mezei-armceface22021-01-21 12:26:17 +01002980 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2981 TEST_ASSERT( output1_buffer_size <=
2982 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002983 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002984
Gilles Peskinee0866522019-02-19 19:44:00 +01002985 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002986
Gilles Peskine8817f612018-12-18 00:18:46 +01002987 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2988 output1, output1_buffer_size,
2989 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002990 TEST_ASSERT( function_output_length <=
2991 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2992 TEST_ASSERT( function_output_length <=
2993 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002994 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002995
Gilles Peskine8817f612018-12-18 00:18:46 +01002996 PSA_ASSERT( psa_cipher_update( &operation1,
2997 input->x + first_part_size,
2998 input->len - first_part_size,
2999 output1, output1_buffer_size,
3000 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003001 TEST_ASSERT( function_output_length <=
3002 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3003 alg,
3004 input->len - first_part_size ) );
3005 TEST_ASSERT( function_output_length <=
3006 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003007 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003008
Gilles Peskine8817f612018-12-18 00:18:46 +01003009 PSA_ASSERT( psa_cipher_finish( &operation1,
3010 output1 + output1_length,
3011 output1_buffer_size - output1_length,
3012 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003013 TEST_ASSERT( function_output_length <=
3014 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3015 TEST_ASSERT( function_output_length <=
3016 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003017 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003018
Gilles Peskine8817f612018-12-18 00:18:46 +01003019 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003020
Gilles Peskine048b7f02018-06-08 14:20:49 +02003021 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003022 TEST_ASSERT( output2_buffer_size <=
3023 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3024 TEST_ASSERT( output2_buffer_size <=
3025 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003026 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003027
Steven Cooreman177deba2020-09-07 17:14:14 +02003028 if( iv_length > 0 )
3029 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003030 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3031 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003032 }
Moran Pekerded84402018-06-06 16:36:50 +03003033
Gilles Peskine8817f612018-12-18 00:18:46 +01003034 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3035 output2, output2_buffer_size,
3036 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003037 TEST_ASSERT( function_output_length <=
3038 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3039 TEST_ASSERT( function_output_length <=
3040 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003041 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003042
Gilles Peskine8817f612018-12-18 00:18:46 +01003043 PSA_ASSERT( psa_cipher_update( &operation2,
3044 output1 + first_part_size,
3045 output1_length - first_part_size,
3046 output2, output2_buffer_size,
3047 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003048 TEST_ASSERT( function_output_length <=
3049 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3050 alg,
3051 output1_length - first_part_size ) );
3052 TEST_ASSERT( function_output_length <=
3053 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003054 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003055
Gilles Peskine8817f612018-12-18 00:18:46 +01003056 PSA_ASSERT( psa_cipher_finish( &operation2,
3057 output2 + output2_length,
3058 output2_buffer_size - output2_length,
3059 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003060 TEST_ASSERT( function_output_length <=
3061 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3062 TEST_ASSERT( function_output_length <=
3063 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003064 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003065
Gilles Peskine8817f612018-12-18 00:18:46 +01003066 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003067
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003068 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003069
3070exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003071 psa_cipher_abort( &operation1 );
3072 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003073 mbedtls_free( output1 );
3074 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003075 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003076 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003077}
3078/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003079
Gilles Peskine20035e32018-02-03 22:44:14 +01003080/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003081void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003082 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003083 data_t *nonce,
3084 data_t *additional_data,
3085 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003086 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003087{
Ronald Cron5425a212020-08-04 14:58:35 +02003088 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003089 psa_key_type_t key_type = key_type_arg;
3090 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003091 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003092 unsigned char *output_data = NULL;
3093 size_t output_size = 0;
3094 size_t output_length = 0;
3095 unsigned char *output_data2 = NULL;
3096 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003097 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003098 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003099 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003100
Gilles Peskine8817f612018-12-18 00:18:46 +01003101 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003102
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003103 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3104 psa_set_key_algorithm( &attributes, alg );
3105 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003106
Gilles Peskine049c7532019-05-15 20:22:09 +02003107 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003108 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003109 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3110 key_bits = psa_get_key_bits( &attributes );
3111
3112 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3113 alg );
3114 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3115 * should be exact. */
3116 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3117 expected_result != PSA_ERROR_NOT_SUPPORTED )
3118 {
3119 TEST_EQUAL( output_size,
3120 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3121 TEST_ASSERT( output_size <=
3122 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3123 }
3124 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003125
Steven Cooremanf49478b2021-02-15 15:19:25 +01003126 status = psa_aead_encrypt( key, alg,
3127 nonce->x, nonce->len,
3128 additional_data->x,
3129 additional_data->len,
3130 input_data->x, input_data->len,
3131 output_data, output_size,
3132 &output_length );
3133
3134 /* If the operation is not supported, just skip and not fail in case the
3135 * encryption involves a common limitation of cryptography hardwares and
3136 * an alternative implementation. */
3137 if( status == PSA_ERROR_NOT_SUPPORTED )
3138 {
3139 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3140 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3141 }
3142
3143 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003144
3145 if( PSA_SUCCESS == expected_result )
3146 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003147 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003148
Gilles Peskine003a4a92019-05-14 16:09:40 +02003149 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3150 * should be exact. */
3151 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003152 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003153
gabor-mezei-armceface22021-01-21 12:26:17 +01003154 TEST_ASSERT( input_data->len <=
3155 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3156
Ronald Cron5425a212020-08-04 14:58:35 +02003157 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003158 nonce->x, nonce->len,
3159 additional_data->x,
3160 additional_data->len,
3161 output_data, output_length,
3162 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003163 &output_length2 ),
3164 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003165
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003166 ASSERT_COMPARE( input_data->x, input_data->len,
3167 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003168 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003169
Gilles Peskinea1cac842018-06-11 19:33:02 +02003170exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003171 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003172 mbedtls_free( output_data );
3173 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003174 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003175}
3176/* END_CASE */
3177
3178/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003179void aead_encrypt( int key_type_arg, data_t *key_data,
3180 int alg_arg,
3181 data_t *nonce,
3182 data_t *additional_data,
3183 data_t *input_data,
3184 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003185{
Ronald Cron5425a212020-08-04 14:58:35 +02003186 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003187 psa_key_type_t key_type = key_type_arg;
3188 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003189 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003190 unsigned char *output_data = NULL;
3191 size_t output_size = 0;
3192 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003193 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003194 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003195
Gilles Peskine8817f612018-12-18 00:18:46 +01003196 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003197
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003198 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3199 psa_set_key_algorithm( &attributes, alg );
3200 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003201
Gilles Peskine049c7532019-05-15 20:22:09 +02003202 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003203 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003204 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3205 key_bits = psa_get_key_bits( &attributes );
3206
3207 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3208 alg );
3209 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3210 * should be exact. */
3211 TEST_EQUAL( output_size,
3212 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3213 TEST_ASSERT( output_size <=
3214 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3215 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003216
Steven Cooremand588ea12021-01-11 19:36:04 +01003217 status = psa_aead_encrypt( key, alg,
3218 nonce->x, nonce->len,
3219 additional_data->x, additional_data->len,
3220 input_data->x, input_data->len,
3221 output_data, output_size,
3222 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003223
Ronald Cron28a45ed2021-02-09 20:35:42 +01003224 /* If the operation is not supported, just skip and not fail in case the
3225 * encryption involves a common limitation of cryptography hardwares and
3226 * an alternative implementation. */
3227 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003228 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003229 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3230 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003231 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003232
3233 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003234 ASSERT_COMPARE( expected_result->x, expected_result->len,
3235 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003236
Gilles Peskinea1cac842018-06-11 19:33:02 +02003237exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003238 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003239 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003240 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003241}
3242/* END_CASE */
3243
3244/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003245void aead_decrypt( int key_type_arg, data_t *key_data,
3246 int alg_arg,
3247 data_t *nonce,
3248 data_t *additional_data,
3249 data_t *input_data,
3250 data_t *expected_data,
3251 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003252{
Ronald Cron5425a212020-08-04 14:58:35 +02003253 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003254 psa_key_type_t key_type = key_type_arg;
3255 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003256 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003257 unsigned char *output_data = NULL;
3258 size_t output_size = 0;
3259 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003260 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003261 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003262 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003263
Gilles Peskine8817f612018-12-18 00:18:46 +01003264 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003265
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003266 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3267 psa_set_key_algorithm( &attributes, alg );
3268 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003269
Gilles Peskine049c7532019-05-15 20:22:09 +02003270 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003271 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003272 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3273 key_bits = psa_get_key_bits( &attributes );
3274
3275 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3276 alg );
3277 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3278 expected_result != PSA_ERROR_NOT_SUPPORTED )
3279 {
3280 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3281 * should be exact. */
3282 TEST_EQUAL( output_size,
3283 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3284 TEST_ASSERT( output_size <=
3285 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3286 }
3287 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003288
Steven Cooremand588ea12021-01-11 19:36:04 +01003289 status = psa_aead_decrypt( key, alg,
3290 nonce->x, nonce->len,
3291 additional_data->x,
3292 additional_data->len,
3293 input_data->x, input_data->len,
3294 output_data, output_size,
3295 &output_length );
3296
Ronald Cron28a45ed2021-02-09 20:35:42 +01003297 /* If the operation is not supported, just skip and not fail in case the
3298 * decryption involves a common limitation of cryptography hardwares and
3299 * an alternative implementation. */
3300 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003301 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003302 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3303 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003304 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003305
3306 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003307
Gilles Peskine2d277862018-06-18 15:41:12 +02003308 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003309 ASSERT_COMPARE( expected_data->x, expected_data->len,
3310 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003311
Gilles Peskinea1cac842018-06-11 19:33:02 +02003312exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003313 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003314 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003315 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003316}
3317/* END_CASE */
3318
3319/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003320void signature_size( int type_arg,
3321 int bits,
3322 int alg_arg,
3323 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003324{
3325 psa_key_type_t type = type_arg;
3326 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003327 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003328
Gilles Peskinefe11b722018-12-18 00:24:04 +01003329 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003330#if defined(MBEDTLS_TEST_DEPRECATED)
3331 TEST_EQUAL( actual_size,
3332 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3333#endif /* MBEDTLS_TEST_DEPRECATED */
3334
Gilles Peskinee59236f2018-01-27 23:32:46 +01003335exit:
3336 ;
3337}
3338/* END_CASE */
3339
3340/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003341void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3342 int alg_arg, data_t *input_data,
3343 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003344{
Ronald Cron5425a212020-08-04 14:58:35 +02003345 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003346 psa_key_type_t key_type = key_type_arg;
3347 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003348 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003349 unsigned char *signature = NULL;
3350 size_t signature_size;
3351 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003353
Gilles Peskine8817f612018-12-18 00:18:46 +01003354 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003355
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003356 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003357 psa_set_key_algorithm( &attributes, alg );
3358 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003359
Gilles Peskine049c7532019-05-15 20:22:09 +02003360 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003361 &key ) );
3362 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003363 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003364
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003365 /* Allocate a buffer which has the size advertized by the
3366 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003367 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003368 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003369 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003370 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003371 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003372
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003373 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003374 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003375 input_data->x, input_data->len,
3376 signature, signature_size,
3377 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003378 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003379 ASSERT_COMPARE( output_data->x, output_data->len,
3380 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003381
Gilles Peskine0627f982019-11-26 19:12:16 +01003382#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003383 memset( signature, 0, signature_size );
3384 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003385 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003386 input_data->x, input_data->len,
3387 signature, signature_size,
3388 &signature_length ) );
3389 ASSERT_COMPARE( output_data->x, output_data->len,
3390 signature, signature_length );
3391#endif /* MBEDTLS_TEST_DEPRECATED */
3392
Gilles Peskine20035e32018-02-03 22:44:14 +01003393exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003394 /*
3395 * Key attributes may have been returned by psa_get_key_attributes()
3396 * thus reset them as required.
3397 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003398 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003399
Ronald Cron5425a212020-08-04 14:58:35 +02003400 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003401 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003402 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003403}
3404/* END_CASE */
3405
3406/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003407void sign_hash_fail( int key_type_arg, data_t *key_data,
3408 int alg_arg, data_t *input_data,
3409 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003410{
Ronald Cron5425a212020-08-04 14:58:35 +02003411 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003412 psa_key_type_t key_type = key_type_arg;
3413 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003414 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003415 psa_status_t actual_status;
3416 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003417 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003418 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003419 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003420
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003421 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003422
Gilles Peskine8817f612018-12-18 00:18:46 +01003423 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003424
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003425 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003426 psa_set_key_algorithm( &attributes, alg );
3427 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003428
Gilles Peskine049c7532019-05-15 20:22:09 +02003429 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003430 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003431
Ronald Cron5425a212020-08-04 14:58:35 +02003432 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003433 input_data->x, input_data->len,
3434 signature, signature_size,
3435 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003436 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003437 /* The value of *signature_length is unspecified on error, but
3438 * whatever it is, it should be less than signature_size, so that
3439 * if the caller tries to read *signature_length bytes without
3440 * checking the error code then they don't overflow a buffer. */
3441 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003442
Gilles Peskine895242b2019-11-29 12:15:40 +01003443#if defined(MBEDTLS_TEST_DEPRECATED)
3444 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003445 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003446 input_data->x, input_data->len,
3447 signature, signature_size,
3448 &signature_length ),
3449 expected_status );
3450 TEST_ASSERT( signature_length <= signature_size );
3451#endif /* MBEDTLS_TEST_DEPRECATED */
3452
Gilles Peskine20035e32018-02-03 22:44:14 +01003453exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003454 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003455 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003456 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003457 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003458}
3459/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003460
3461/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003462void sign_verify_hash( int key_type_arg, data_t *key_data,
3463 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003464{
Ronald Cron5425a212020-08-04 14:58:35 +02003465 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003466 psa_key_type_t key_type = key_type_arg;
3467 psa_algorithm_t alg = alg_arg;
3468 size_t key_bits;
3469 unsigned char *signature = NULL;
3470 size_t signature_size;
3471 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003472 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003473
Gilles Peskine8817f612018-12-18 00:18:46 +01003474 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003475
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003476 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003477 psa_set_key_algorithm( &attributes, alg );
3478 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003479
Gilles Peskine049c7532019-05-15 20:22:09 +02003480 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003481 &key ) );
3482 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003483 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003484
3485 /* Allocate a buffer which has the size advertized by the
3486 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003487 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003488 key_bits, alg );
3489 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003490 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003491 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003492
3493 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003494 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003495 input_data->x, input_data->len,
3496 signature, signature_size,
3497 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003498 /* Check that the signature length looks sensible. */
3499 TEST_ASSERT( signature_length <= signature_size );
3500 TEST_ASSERT( signature_length > 0 );
3501
3502 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003503 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003504 input_data->x, input_data->len,
3505 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003506
3507 if( input_data->len != 0 )
3508 {
3509 /* Flip a bit in the input and verify that the signature is now
3510 * detected as invalid. Flip a bit at the beginning, not at the end,
3511 * because ECDSA may ignore the last few bits of the input. */
3512 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003513 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003514 input_data->x, input_data->len,
3515 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003516 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003517 }
3518
3519exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003520 /*
3521 * Key attributes may have been returned by psa_get_key_attributes()
3522 * thus reset them as required.
3523 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003524 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003525
Ronald Cron5425a212020-08-04 14:58:35 +02003526 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003527 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003528 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003529}
3530/* END_CASE */
3531
3532/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003533void verify_hash( int key_type_arg, data_t *key_data,
3534 int alg_arg, data_t *hash_data,
3535 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003536{
Ronald Cron5425a212020-08-04 14:58:35 +02003537 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003538 psa_key_type_t key_type = key_type_arg;
3539 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003540 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003541
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003542 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003543
Gilles Peskine8817f612018-12-18 00:18:46 +01003544 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003545
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003546 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003547 psa_set_key_algorithm( &attributes, alg );
3548 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003549
Gilles Peskine049c7532019-05-15 20:22:09 +02003550 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003551 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003552
Ronald Cron5425a212020-08-04 14:58:35 +02003553 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003554 hash_data->x, hash_data->len,
3555 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003556
3557#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003558 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003559 hash_data->x, hash_data->len,
3560 signature_data->x,
3561 signature_data->len ) );
3562
3563#endif /* MBEDTLS_TEST_DEPRECATED */
3564
itayzafrir5c753392018-05-08 11:18:38 +03003565exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003566 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003567 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003568 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003569}
3570/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003571
3572/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003573void verify_hash_fail( int key_type_arg, data_t *key_data,
3574 int alg_arg, data_t *hash_data,
3575 data_t *signature_data,
3576 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003577{
Ronald Cron5425a212020-08-04 14:58:35 +02003578 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003579 psa_key_type_t key_type = key_type_arg;
3580 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003581 psa_status_t actual_status;
3582 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003583 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003584
Gilles Peskine8817f612018-12-18 00:18:46 +01003585 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003586
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003587 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003588 psa_set_key_algorithm( &attributes, alg );
3589 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003590
Gilles Peskine049c7532019-05-15 20:22:09 +02003591 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003592 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003593
Ronald Cron5425a212020-08-04 14:58:35 +02003594 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003595 hash_data->x, hash_data->len,
3596 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003597 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003598
Gilles Peskine895242b2019-11-29 12:15:40 +01003599#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003600 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003601 hash_data->x, hash_data->len,
3602 signature_data->x, signature_data->len ),
3603 expected_status );
3604#endif /* MBEDTLS_TEST_DEPRECATED */
3605
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003606exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003607 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003608 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003609 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003610}
3611/* END_CASE */
3612
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003613/* BEGIN_CASE */
gabor-mezei-armabd72582021-04-15 18:19:50 +02003614void sign_message_deterministic( int key_type_arg,
3615 data_t *key_data,
3616 int alg_arg,
3617 data_t *input_data,
3618 data_t *output_data )
3619{
3620 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3621 psa_key_type_t key_type = key_type_arg;
3622 psa_algorithm_t alg = alg_arg;
3623 size_t key_bits;
3624 unsigned char *signature = NULL;
3625 size_t signature_size;
3626 size_t signature_length = 0xdeadbeef;
3627 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3628
3629 PSA_ASSERT( psa_crypto_init( ) );
3630
3631 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3632 psa_set_key_algorithm( &attributes, alg );
3633 psa_set_key_type( &attributes, key_type );
3634
3635 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3636 &key ) );
3637 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3638 key_bits = psa_get_key_bits( &attributes );
3639
3640 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3641 TEST_ASSERT( signature_size != 0 );
3642 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3643 ASSERT_ALLOC( signature, signature_size );
3644
3645 PSA_ASSERT( psa_sign_message( key, alg,
3646 input_data->x, input_data->len,
3647 signature, signature_size,
3648 &signature_length ) );
3649
3650 ASSERT_COMPARE( output_data->x, output_data->len,
3651 signature, signature_length );
3652
3653exit:
3654 psa_reset_key_attributes( &attributes );
3655
3656 psa_destroy_key( key );
3657 mbedtls_free( signature );
3658 PSA_DONE( );
3659
3660}
3661/* END_CASE */
3662
3663/* BEGIN_CASE */
3664void sign_message_fail( int key_type_arg,
3665 data_t *key_data,
3666 int alg_arg,
3667 data_t *input_data,
3668 int signature_size_arg,
3669 int expected_status_arg )
3670{
3671 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3672 psa_key_type_t key_type = key_type_arg;
3673 psa_algorithm_t alg = alg_arg;
3674 size_t signature_size = signature_size_arg;
3675 psa_status_t actual_status;
3676 psa_status_t expected_status = expected_status_arg;
3677 unsigned char *signature = NULL;
3678 size_t signature_length = 0xdeadbeef;
3679 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3680
3681 ASSERT_ALLOC( signature, signature_size );
3682
3683 PSA_ASSERT( psa_crypto_init( ) );
3684
3685 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3686 psa_set_key_algorithm( &attributes, alg );
3687 psa_set_key_type( &attributes, key_type );
3688
3689 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3690 &key ) );
3691
3692 actual_status = psa_sign_message( key, alg,
3693 input_data->x, input_data->len,
3694 signature, signature_size,
3695 &signature_length );
3696 TEST_EQUAL( actual_status, expected_status );
3697 /* The value of *signature_length is unspecified on error, but
3698 * whatever it is, it should be less than signature_size, so that
3699 * if the caller tries to read *signature_length bytes without
3700 * checking the error code then they don't overflow a buffer. */
3701 TEST_ASSERT( signature_length <= signature_size );
3702
3703exit:
3704 psa_reset_key_attributes( &attributes );
3705 psa_destroy_key( key );
3706 mbedtls_free( signature );
3707 PSA_DONE( );
3708}
3709/* END_CASE */
3710
3711/* BEGIN_CASE */
3712void sign_verify_message( int key_type_arg,
3713 data_t *key_data,
3714 int alg_arg,
3715 data_t *input_data )
3716{
3717 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3718 psa_key_type_t key_type = key_type_arg;
3719 psa_algorithm_t alg = alg_arg;
3720 size_t key_bits;
3721 unsigned char *signature = NULL;
3722 size_t signature_size;
3723 size_t signature_length = 0xdeadbeef;
3724 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3725
3726 PSA_ASSERT( psa_crypto_init( ) );
3727
3728 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3729 PSA_KEY_USAGE_VERIFY_MESSAGE );
3730 psa_set_key_algorithm( &attributes, alg );
3731 psa_set_key_type( &attributes, key_type );
3732
3733 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3734 &key ) );
3735 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3736 key_bits = psa_get_key_bits( &attributes );
3737
3738 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3739 TEST_ASSERT( signature_size != 0 );
3740 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3741 ASSERT_ALLOC( signature, signature_size );
3742
3743 PSA_ASSERT( psa_sign_message( key, alg,
3744 input_data->x, input_data->len,
3745 signature, signature_size,
3746 &signature_length ) );
3747 TEST_ASSERT( signature_length <= signature_size );
3748 TEST_ASSERT( signature_length > 0 );
3749
3750 PSA_ASSERT( psa_verify_message( key, alg,
3751 input_data->x, input_data->len,
3752 signature, signature_length ) );
3753
3754 if( input_data->len != 0 )
3755 {
3756 /* Flip a bit in the input and verify that the signature is now
3757 * detected as invalid. Flip a bit at the beginning, not at the end,
3758 * because ECDSA may ignore the last few bits of the input. */
3759 input_data->x[0] ^= 1;
3760 TEST_EQUAL( psa_verify_message( key, alg,
3761 input_data->x, input_data->len,
3762 signature, signature_length ),
3763 PSA_ERROR_INVALID_SIGNATURE );
3764 }
3765
3766exit:
3767 psa_reset_key_attributes( &attributes );
3768
3769 psa_destroy_key( key );
3770 mbedtls_free( signature );
3771 PSA_DONE( );
3772}
3773/* END_CASE */
3774
3775/* BEGIN_CASE */
3776void verify_message( int key_type_arg,
3777 data_t *key_data,
3778 int alg_arg,
3779 data_t *input_data,
3780 data_t *signature_data )
3781{
3782 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3783 psa_key_type_t key_type = key_type_arg;
3784 psa_algorithm_t alg = alg_arg;
3785 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3786
3787 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3788
3789 PSA_ASSERT( psa_crypto_init( ) );
3790
3791 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3792 psa_set_key_algorithm( &attributes, alg );
3793 psa_set_key_type( &attributes, key_type );
3794
3795 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3796 &key ) );
3797
3798 PSA_ASSERT( psa_verify_message( key, alg,
3799 input_data->x, input_data->len,
3800 signature_data->x, signature_data->len ) );
3801
3802exit:
3803 psa_reset_key_attributes( &attributes );
3804 psa_destroy_key( key );
3805 PSA_DONE( );
3806}
3807/* END_CASE */
3808
3809/* BEGIN_CASE */
3810void verify_message_fail( int key_type_arg,
3811 data_t *key_data,
3812 int alg_arg,
3813 data_t *hash_data,
3814 data_t *signature_data,
3815 int expected_status_arg )
3816{
3817 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3818 psa_key_type_t key_type = key_type_arg;
3819 psa_algorithm_t alg = alg_arg;
3820 psa_status_t actual_status;
3821 psa_status_t expected_status = expected_status_arg;
3822 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3823
3824 PSA_ASSERT( psa_crypto_init( ) );
3825
3826 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3827 psa_set_key_algorithm( &attributes, alg );
3828 psa_set_key_type( &attributes, key_type );
3829
3830 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3831 &key ) );
3832
3833 actual_status = psa_verify_message( key, alg,
3834 hash_data->x, hash_data->len,
3835 signature_data->x,
3836 signature_data->len );
3837 TEST_EQUAL( actual_status, expected_status );
3838
3839exit:
3840 psa_reset_key_attributes( &attributes );
3841 psa_destroy_key( key );
3842 PSA_DONE( );
3843}
3844/* END_CASE */
3845
3846/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003847void asymmetric_encrypt( int key_type_arg,
3848 data_t *key_data,
3849 int alg_arg,
3850 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003851 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003852 int expected_output_length_arg,
3853 int expected_status_arg )
3854{
Ronald Cron5425a212020-08-04 14:58:35 +02003855 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003856 psa_key_type_t key_type = key_type_arg;
3857 psa_algorithm_t alg = alg_arg;
3858 size_t expected_output_length = expected_output_length_arg;
3859 size_t key_bits;
3860 unsigned char *output = NULL;
3861 size_t output_size;
3862 size_t output_length = ~0;
3863 psa_status_t actual_status;
3864 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003865 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003866
Gilles Peskine8817f612018-12-18 00:18:46 +01003867 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003868
Gilles Peskine656896e2018-06-29 19:12:28 +02003869 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003870 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3871 psa_set_key_algorithm( &attributes, alg );
3872 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003873 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003874 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003875
3876 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003877 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003878 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003879
Gilles Peskine656896e2018-06-29 19:12:28 +02003880 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003881 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003882 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003883
3884 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003885 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003886 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003887 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003888 output, output_size,
3889 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003890 TEST_EQUAL( actual_status, expected_status );
3891 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003892
Gilles Peskine68428122018-06-30 18:42:41 +02003893 /* If the label is empty, the test framework puts a non-null pointer
3894 * in label->x. Test that a null pointer works as well. */
3895 if( label->len == 0 )
3896 {
3897 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003898 if( output_size != 0 )
3899 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003900 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003901 input_data->x, input_data->len,
3902 NULL, label->len,
3903 output, output_size,
3904 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003905 TEST_EQUAL( actual_status, expected_status );
3906 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003907 }
3908
Gilles Peskine656896e2018-06-29 19:12:28 +02003909exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003910 /*
3911 * Key attributes may have been returned by psa_get_key_attributes()
3912 * thus reset them as required.
3913 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003914 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003915
Ronald Cron5425a212020-08-04 14:58:35 +02003916 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02003917 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003918 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003919}
3920/* END_CASE */
3921
3922/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003923void asymmetric_encrypt_decrypt( int key_type_arg,
3924 data_t *key_data,
3925 int alg_arg,
3926 data_t *input_data,
3927 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003928{
Ronald Cron5425a212020-08-04 14:58:35 +02003929 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003930 psa_key_type_t key_type = key_type_arg;
3931 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003932 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003933 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003934 size_t output_size;
3935 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003936 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003937 size_t output2_size;
3938 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003939 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003940
Gilles Peskine8817f612018-12-18 00:18:46 +01003941 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003942
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003943 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3944 psa_set_key_algorithm( &attributes, alg );
3945 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003946
Gilles Peskine049c7532019-05-15 20:22:09 +02003947 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003948 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003949
3950 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02003951 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003952 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003953
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003954 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003955 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003956 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01003957
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003958 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01003959 TEST_ASSERT( output2_size <=
3960 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
3961 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003962 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003963
Gilles Peskineeebd7382018-06-08 18:11:54 +02003964 /* We test encryption by checking that encrypt-then-decrypt gives back
3965 * the original plaintext because of the non-optional random
3966 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02003967 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003968 input_data->x, input_data->len,
3969 label->x, label->len,
3970 output, output_size,
3971 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003972 /* We don't know what ciphertext length to expect, but check that
3973 * it looks sensible. */
3974 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003975
Ronald Cron5425a212020-08-04 14:58:35 +02003976 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003977 output, output_length,
3978 label->x, label->len,
3979 output2, output2_size,
3980 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003981 ASSERT_COMPARE( input_data->x, input_data->len,
3982 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003983
3984exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003985 /*
3986 * Key attributes may have been returned by psa_get_key_attributes()
3987 * thus reset them as required.
3988 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003989 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003990
Ronald Cron5425a212020-08-04 14:58:35 +02003991 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003992 mbedtls_free( output );
3993 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003994 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003995}
3996/* END_CASE */
3997
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003998/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003999void asymmetric_decrypt( int key_type_arg,
4000 data_t *key_data,
4001 int alg_arg,
4002 data_t *input_data,
4003 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004004 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004005{
Ronald Cron5425a212020-08-04 14:58:35 +02004006 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004007 psa_key_type_t key_type = key_type_arg;
4008 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004009 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004010 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004011 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004012 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004013 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004014
Gilles Peskine8817f612018-12-18 00:18:46 +01004015 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004016
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004017 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4018 psa_set_key_algorithm( &attributes, alg );
4019 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004020
Gilles Peskine049c7532019-05-15 20:22:09 +02004021 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004022 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004023
gabor-mezei-armceface22021-01-21 12:26:17 +01004024 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4025 key_bits = psa_get_key_bits( &attributes );
4026
4027 /* Determine the maximum ciphertext length */
4028 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
4029 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
4030 ASSERT_ALLOC( output, output_size );
4031
Ronald Cron5425a212020-08-04 14:58:35 +02004032 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004033 input_data->x, input_data->len,
4034 label->x, label->len,
4035 output,
4036 output_size,
4037 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004038 ASSERT_COMPARE( expected_data->x, expected_data->len,
4039 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004040
Gilles Peskine68428122018-06-30 18:42:41 +02004041 /* If the label is empty, the test framework puts a non-null pointer
4042 * in label->x. Test that a null pointer works as well. */
4043 if( label->len == 0 )
4044 {
4045 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004046 if( output_size != 0 )
4047 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004048 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004049 input_data->x, input_data->len,
4050 NULL, label->len,
4051 output,
4052 output_size,
4053 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004054 ASSERT_COMPARE( expected_data->x, expected_data->len,
4055 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004056 }
4057
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004058exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004059 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004060 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004061 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004062 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004063}
4064/* END_CASE */
4065
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004066/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004067void asymmetric_decrypt_fail( int key_type_arg,
4068 data_t *key_data,
4069 int alg_arg,
4070 data_t *input_data,
4071 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004072 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004073 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004074{
Ronald Cron5425a212020-08-04 14:58:35 +02004075 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004076 psa_key_type_t key_type = key_type_arg;
4077 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004078 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004079 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004080 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004081 psa_status_t actual_status;
4082 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004083 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004084
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004085 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004086
Gilles Peskine8817f612018-12-18 00:18:46 +01004087 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004088
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004089 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4090 psa_set_key_algorithm( &attributes, alg );
4091 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004092
Gilles Peskine049c7532019-05-15 20:22:09 +02004093 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004094 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004095
Ronald Cron5425a212020-08-04 14:58:35 +02004096 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004097 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004098 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004099 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004100 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004101 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004102 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004103
Gilles Peskine68428122018-06-30 18:42:41 +02004104 /* If the label is empty, the test framework puts a non-null pointer
4105 * in label->x. Test that a null pointer works as well. */
4106 if( label->len == 0 )
4107 {
4108 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004109 if( output_size != 0 )
4110 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004111 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004112 input_data->x, input_data->len,
4113 NULL, label->len,
4114 output, output_size,
4115 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004116 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004117 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004118 }
4119
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004120exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004121 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004122 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004123 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004124 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004125}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004126/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004127
4128/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004129void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004130{
4131 /* Test each valid way of initializing the object, except for `= {0}`, as
4132 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4133 * though it's OK by the C standard. We could test for this, but we'd need
4134 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004135 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004136 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4137 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4138 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004139
4140 memset( &zero, 0, sizeof( zero ) );
4141
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004142 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004143 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004144 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004145 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &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( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004148 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004149
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004150 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004151 PSA_ASSERT( psa_key_derivation_abort(&func) );
4152 PSA_ASSERT( psa_key_derivation_abort(&init) );
4153 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004154}
4155/* END_CASE */
4156
Janos Follath16de4a42019-06-13 16:32:24 +01004157/* BEGIN_CASE */
4158void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004159{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004160 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004161 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004162 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004163
Gilles Peskine8817f612018-12-18 00:18:46 +01004164 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004165
Janos Follath16de4a42019-06-13 16:32:24 +01004166 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004167 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004168
4169exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004170 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004171 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004172}
4173/* END_CASE */
4174
Janos Follathaf3c2a02019-06-12 12:34:34 +01004175/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004176void derive_set_capacity( int alg_arg, int capacity_arg,
4177 int expected_status_arg )
4178{
4179 psa_algorithm_t alg = alg_arg;
4180 size_t capacity = capacity_arg;
4181 psa_status_t expected_status = expected_status_arg;
4182 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4183
4184 PSA_ASSERT( psa_crypto_init( ) );
4185
4186 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4187
4188 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4189 expected_status );
4190
4191exit:
4192 psa_key_derivation_abort( &operation );
4193 PSA_DONE( );
4194}
4195/* END_CASE */
4196
4197/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004198void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004199 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004200 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004201 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004202 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004203 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004204 int expected_status_arg3,
4205 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004206{
4207 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004208 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4209 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004210 psa_status_t expected_statuses[] = {expected_status_arg1,
4211 expected_status_arg2,
4212 expected_status_arg3};
4213 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004214 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4215 MBEDTLS_SVC_KEY_ID_INIT,
4216 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004217 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4218 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4219 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004220 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004221 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004222 psa_status_t expected_output_status = expected_output_status_arg;
4223 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004224
4225 PSA_ASSERT( psa_crypto_init( ) );
4226
4227 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4228 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004229
4230 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4231
4232 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4233 {
Gilles Peskinef6279312021-05-27 13:21:20 +02004234 mbedtls_test_set_step( i );
4235 if( steps[i] == 0 )
4236 {
4237 /* Skip this step */
4238 }
4239 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004240 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004241 psa_set_key_type( &attributes, key_types[i] );
4242 PSA_ASSERT( psa_import_key( &attributes,
4243 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004244 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004245 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4246 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4247 {
4248 // When taking a private key as secret input, use key agreement
4249 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004250 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4251 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004252 expected_statuses[i] );
4253 }
4254 else
4255 {
4256 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004257 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004258 expected_statuses[i] );
4259 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004260 }
4261 else
4262 {
4263 TEST_EQUAL( psa_key_derivation_input_bytes(
4264 &operation, steps[i],
4265 inputs[i]->x, inputs[i]->len ),
4266 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004267 }
4268 }
4269
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004270 if( output_key_type != PSA_KEY_TYPE_NONE )
4271 {
4272 psa_reset_key_attributes( &attributes );
4273 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4274 psa_set_key_bits( &attributes, 8 );
4275 actual_output_status =
4276 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004277 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004278 }
4279 else
4280 {
4281 uint8_t buffer[1];
4282 actual_output_status =
4283 psa_key_derivation_output_bytes( &operation,
4284 buffer, sizeof( buffer ) );
4285 }
4286 TEST_EQUAL( actual_output_status, expected_output_status );
4287
Janos Follathaf3c2a02019-06-12 12:34:34 +01004288exit:
4289 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004290 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4291 psa_destroy_key( keys[i] );
4292 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004293 PSA_DONE( );
4294}
4295/* END_CASE */
4296
Janos Follathd958bb72019-07-03 15:02:16 +01004297/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004298void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004299{
Janos Follathd958bb72019-07-03 15:02:16 +01004300 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004301 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004302 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004303 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004304 unsigned char input1[] = "Input 1";
4305 size_t input1_length = sizeof( input1 );
4306 unsigned char input2[] = "Input 2";
4307 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004308 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004309 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004310 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4311 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4312 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004313 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004314
Gilles Peskine8817f612018-12-18 00:18:46 +01004315 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004316
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004317 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4318 psa_set_key_algorithm( &attributes, alg );
4319 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004320
Gilles Peskine73676cb2019-05-15 20:15:10 +02004321 PSA_ASSERT( psa_import_key( &attributes,
4322 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004323 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004324
4325 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004326 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4327 input1, input1_length,
4328 input2, input2_length,
4329 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004330 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004331
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004332 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004333 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004334 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004335
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004336 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004337
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004338 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004339 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004340
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004341exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004342 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004343 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004344 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004345}
4346/* END_CASE */
4347
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004348/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004349void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004350{
4351 uint8_t output_buffer[16];
4352 size_t buffer_size = 16;
4353 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004354 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004355
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004356 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4357 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004358 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004359
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004360 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004361 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004362
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004363 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004364
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004365 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4366 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004367 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004368
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004369 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004370 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004371
4372exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004373 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004374}
4375/* END_CASE */
4376
4377/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004378void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004379 int step1_arg, data_t *input1,
4380 int step2_arg, data_t *input2,
4381 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004382 int requested_capacity_arg,
4383 data_t *expected_output1,
4384 data_t *expected_output2 )
4385{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004386 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004387 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4388 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004389 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4390 MBEDTLS_SVC_KEY_ID_INIT,
4391 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004392 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004393 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004394 uint8_t *expected_outputs[2] =
4395 {expected_output1->x, expected_output2->x};
4396 size_t output_sizes[2] =
4397 {expected_output1->len, expected_output2->len};
4398 size_t output_buffer_size = 0;
4399 uint8_t *output_buffer = NULL;
4400 size_t expected_capacity;
4401 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004402 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004403 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004404 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004405
4406 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4407 {
4408 if( output_sizes[i] > output_buffer_size )
4409 output_buffer_size = output_sizes[i];
4410 if( output_sizes[i] == 0 )
4411 expected_outputs[i] = NULL;
4412 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004413 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004414 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004415
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004416 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4417 psa_set_key_algorithm( &attributes, alg );
4418 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004419
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004420 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004421 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4422 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4423 requested_capacity ) );
4424 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004425 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004426 switch( steps[i] )
4427 {
4428 case 0:
4429 break;
4430 case PSA_KEY_DERIVATION_INPUT_SECRET:
4431 PSA_ASSERT( psa_import_key( &attributes,
4432 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004433 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004434
4435 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4436 {
4437 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4438 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4439 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4440 }
4441
Gilles Peskine1468da72019-05-29 17:35:49 +02004442 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004443 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004444 break;
4445 default:
4446 PSA_ASSERT( psa_key_derivation_input_bytes(
4447 &operation, steps[i],
4448 inputs[i]->x, inputs[i]->len ) );
4449 break;
4450 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004451 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004452
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004453 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004454 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004455 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004456 expected_capacity = requested_capacity;
4457
4458 /* Expansion phase. */
4459 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4460 {
4461 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004462 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004463 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004464 if( expected_capacity == 0 && output_sizes[i] == 0 )
4465 {
4466 /* Reading 0 bytes when 0 bytes are available can go either way. */
4467 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004468 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004469 continue;
4470 }
4471 else if( expected_capacity == 0 ||
4472 output_sizes[i] > expected_capacity )
4473 {
4474 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004475 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004476 expected_capacity = 0;
4477 continue;
4478 }
4479 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004480 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004481 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004482 ASSERT_COMPARE( output_buffer, output_sizes[i],
4483 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004484 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004485 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004486 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004487 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004488 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004489 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004490 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004491
4492exit:
4493 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004494 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004495 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4496 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004497 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004498}
4499/* END_CASE */
4500
4501/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004502void derive_full( int alg_arg,
4503 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004504 data_t *input1,
4505 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004506 int requested_capacity_arg )
4507{
Ronald Cron5425a212020-08-04 14:58:35 +02004508 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004509 psa_algorithm_t alg = alg_arg;
4510 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004511 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004512 unsigned char output_buffer[16];
4513 size_t expected_capacity = requested_capacity;
4514 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004515 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004516
Gilles Peskine8817f612018-12-18 00:18:46 +01004517 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004518
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004519 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4520 psa_set_key_algorithm( &attributes, alg );
4521 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004522
Gilles Peskine049c7532019-05-15 20:22:09 +02004523 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004524 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004525
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004526 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4527 input1->x, input1->len,
4528 input2->x, input2->len,
4529 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004530 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004531
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004532 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004533 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004534 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004535
4536 /* Expansion phase. */
4537 while( current_capacity > 0 )
4538 {
4539 size_t read_size = sizeof( output_buffer );
4540 if( read_size > current_capacity )
4541 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004542 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004543 output_buffer,
4544 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004545 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004546 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004547 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004548 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004549 }
4550
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004551 /* Check that the operation refuses to go over capacity. */
4552 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004553 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004554
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004555 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004556
4557exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004558 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004559 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004560 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004561}
4562/* END_CASE */
4563
Janos Follathe60c9052019-07-03 13:51:30 +01004564/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004565void derive_key_exercise( int alg_arg,
4566 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004567 data_t *input1,
4568 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004569 int derived_type_arg,
4570 int derived_bits_arg,
4571 int derived_usage_arg,
4572 int derived_alg_arg )
4573{
Ronald Cron5425a212020-08-04 14:58:35 +02004574 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4575 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004576 psa_algorithm_t alg = alg_arg;
4577 psa_key_type_t derived_type = derived_type_arg;
4578 size_t derived_bits = derived_bits_arg;
4579 psa_key_usage_t derived_usage = derived_usage_arg;
4580 psa_algorithm_t derived_alg = derived_alg_arg;
4581 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004582 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004583 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004584 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004585
Gilles Peskine8817f612018-12-18 00:18:46 +01004586 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004587
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004588 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4589 psa_set_key_algorithm( &attributes, alg );
4590 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004591 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004592 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004593
4594 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004595 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4596 input1->x, input1->len,
4597 input2->x, input2->len,
4598 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004599 goto exit;
4600
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004601 psa_set_key_usage_flags( &attributes, derived_usage );
4602 psa_set_key_algorithm( &attributes, derived_alg );
4603 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004604 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004605 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004606 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004607
4608 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004609 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004610 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4611 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004612
4613 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004614 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004615 goto exit;
4616
4617exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004618 /*
4619 * Key attributes may have been returned by psa_get_key_attributes()
4620 * thus reset them as required.
4621 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004622 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004623
4624 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004625 psa_destroy_key( base_key );
4626 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004627 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004628}
4629/* END_CASE */
4630
Janos Follath42fd8882019-07-03 14:17:09 +01004631/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004632void derive_key_export( int alg_arg,
4633 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004634 data_t *input1,
4635 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004636 int bytes1_arg,
4637 int bytes2_arg )
4638{
Ronald Cron5425a212020-08-04 14:58:35 +02004639 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4640 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004641 psa_algorithm_t alg = alg_arg;
4642 size_t bytes1 = bytes1_arg;
4643 size_t bytes2 = bytes2_arg;
4644 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004645 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004646 uint8_t *output_buffer = NULL;
4647 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004648 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4649 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004650 size_t length;
4651
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004652 ASSERT_ALLOC( output_buffer, capacity );
4653 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004654 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004655
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004656 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4657 psa_set_key_algorithm( &base_attributes, alg );
4658 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004659 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004660 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004661
4662 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004663 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4664 input1->x, input1->len,
4665 input2->x, input2->len,
4666 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004667 goto exit;
4668
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004669 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004670 output_buffer,
4671 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004672 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004673
4674 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004675 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4676 input1->x, input1->len,
4677 input2->x, input2->len,
4678 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004679 goto exit;
4680
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004681 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4682 psa_set_key_algorithm( &derived_attributes, 0 );
4683 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004684 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004685 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004686 &derived_key ) );
4687 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004688 export_buffer, bytes1,
4689 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004690 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004691 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004692 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004693 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004694 &derived_key ) );
4695 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004696 export_buffer + bytes1, bytes2,
4697 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004698 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004699
4700 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004701 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4702 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004703
4704exit:
4705 mbedtls_free( output_buffer );
4706 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004707 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004708 psa_destroy_key( base_key );
4709 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004710 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004711}
4712/* END_CASE */
4713
4714/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004715void derive_key( int alg_arg,
4716 data_t *key_data, data_t *input1, data_t *input2,
4717 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004718 int expected_status_arg,
4719 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004720{
Ronald Cron5425a212020-08-04 14:58:35 +02004721 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4722 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004723 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004724 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004725 size_t bits = bits_arg;
4726 psa_status_t expected_status = expected_status_arg;
4727 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4728 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4729 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4730
4731 PSA_ASSERT( psa_crypto_init( ) );
4732
4733 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4734 psa_set_key_algorithm( &base_attributes, alg );
4735 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4736 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004737 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004738
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004739 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4740 input1->x, input1->len,
4741 input2->x, input2->len,
4742 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004743 goto exit;
4744
4745 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4746 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004747 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004748 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004749
4750 psa_status_t status =
4751 psa_key_derivation_output_key( &derived_attributes,
4752 &operation,
4753 &derived_key );
4754 if( is_large_output > 0 )
4755 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4756 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004757
4758exit:
4759 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004760 psa_destroy_key( base_key );
4761 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004762 PSA_DONE( );
4763}
4764/* END_CASE */
4765
4766/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004767void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004768 int our_key_type_arg, int our_key_alg_arg,
4769 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004770 int expected_status_arg )
4771{
Ronald Cron5425a212020-08-04 14:58:35 +02004772 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004773 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004774 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004775 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004776 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004777 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004778 psa_status_t expected_status = expected_status_arg;
4779 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004780
Gilles Peskine8817f612018-12-18 00:18:46 +01004781 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004782
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004783 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004784 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004785 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004786 PSA_ASSERT( psa_import_key( &attributes,
4787 our_key_data->x, our_key_data->len,
4788 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004789
Gilles Peskine77f40d82019-04-11 21:27:06 +02004790 /* The tests currently include inputs that should fail at either step.
4791 * Test cases that fail at the setup step should be changed to call
4792 * key_derivation_setup instead, and this function should be renamed
4793 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004794 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004795 if( status == PSA_SUCCESS )
4796 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004797 TEST_EQUAL( psa_key_derivation_key_agreement(
4798 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4799 our_key,
4800 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004801 expected_status );
4802 }
4803 else
4804 {
4805 TEST_ASSERT( status == expected_status );
4806 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004807
4808exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004809 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004810 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004811 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004812}
4813/* END_CASE */
4814
4815/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004816void raw_key_agreement( int alg_arg,
4817 int our_key_type_arg, data_t *our_key_data,
4818 data_t *peer_key_data,
4819 data_t *expected_output )
4820{
Ronald Cron5425a212020-08-04 14:58:35 +02004821 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004822 psa_algorithm_t alg = alg_arg;
4823 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004824 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004825 unsigned char *output = NULL;
4826 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004827 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004828
4829 ASSERT_ALLOC( output, expected_output->len );
4830 PSA_ASSERT( psa_crypto_init( ) );
4831
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004832 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4833 psa_set_key_algorithm( &attributes, alg );
4834 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004835 PSA_ASSERT( psa_import_key( &attributes,
4836 our_key_data->x, our_key_data->len,
4837 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004838
gabor-mezei-armceface22021-01-21 12:26:17 +01004839 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4840 key_bits = psa_get_key_bits( &attributes );
4841
Gilles Peskinebe697d82019-05-16 18:00:41 +02004842 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4843 peer_key_data->x, peer_key_data->len,
4844 output, expected_output->len,
4845 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004846 ASSERT_COMPARE( output, output_length,
4847 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01004848 TEST_ASSERT( output_length <=
4849 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
4850 TEST_ASSERT( output_length <=
4851 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004852
4853exit:
4854 mbedtls_free( output );
4855 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004856 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004857}
4858/* END_CASE */
4859
4860/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004861void key_agreement_capacity( int alg_arg,
4862 int our_key_type_arg, data_t *our_key_data,
4863 data_t *peer_key_data,
4864 int expected_capacity_arg )
4865{
Ronald Cron5425a212020-08-04 14:58:35 +02004866 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004867 psa_algorithm_t alg = alg_arg;
4868 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004869 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004870 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004871 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004872 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004873
Gilles Peskine8817f612018-12-18 00:18:46 +01004874 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004875
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004876 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4877 psa_set_key_algorithm( &attributes, alg );
4878 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004879 PSA_ASSERT( psa_import_key( &attributes,
4880 our_key_data->x, our_key_data->len,
4881 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004882
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004883 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004884 PSA_ASSERT( psa_key_derivation_key_agreement(
4885 &operation,
4886 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4887 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004888 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4889 {
4890 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004891 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004892 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004893 NULL, 0 ) );
4894 }
Gilles Peskine59685592018-09-18 12:11:34 +02004895
Gilles Peskinebf491972018-10-25 22:36:12 +02004896 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004897 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004898 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004899 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004900
Gilles Peskinebf491972018-10-25 22:36:12 +02004901 /* Test the actual capacity by reading the output. */
4902 while( actual_capacity > sizeof( output ) )
4903 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004904 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004905 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004906 actual_capacity -= sizeof( output );
4907 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004908 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004909 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004910 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004911 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004912
Gilles Peskine59685592018-09-18 12:11:34 +02004913exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004914 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004915 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004916 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004917}
4918/* END_CASE */
4919
4920/* BEGIN_CASE */
4921void key_agreement_output( int alg_arg,
4922 int our_key_type_arg, data_t *our_key_data,
4923 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004924 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004925{
Ronald Cron5425a212020-08-04 14:58:35 +02004926 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004927 psa_algorithm_t alg = alg_arg;
4928 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004929 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004930 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004931 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004932
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004933 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4934 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004935
Gilles Peskine8817f612018-12-18 00:18:46 +01004936 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004937
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004938 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4939 psa_set_key_algorithm( &attributes, alg );
4940 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004941 PSA_ASSERT( psa_import_key( &attributes,
4942 our_key_data->x, our_key_data->len,
4943 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004944
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004945 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004946 PSA_ASSERT( psa_key_derivation_key_agreement(
4947 &operation,
4948 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4949 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004950 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4951 {
4952 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004953 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004954 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004955 NULL, 0 ) );
4956 }
Gilles Peskine59685592018-09-18 12:11:34 +02004957
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004958 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004959 actual_output,
4960 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004961 ASSERT_COMPARE( actual_output, expected_output1->len,
4962 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004963 if( expected_output2->len != 0 )
4964 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004965 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004966 actual_output,
4967 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004968 ASSERT_COMPARE( actual_output, expected_output2->len,
4969 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004970 }
Gilles Peskine59685592018-09-18 12:11:34 +02004971
4972exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004973 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004974 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004975 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004976 mbedtls_free( actual_output );
4977}
4978/* END_CASE */
4979
4980/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004981void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004982{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004983 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004984 unsigned char *output = NULL;
4985 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004986 size_t i;
4987 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004988
Simon Butcher49f8e312020-03-03 15:51:50 +00004989 TEST_ASSERT( bytes_arg >= 0 );
4990
Gilles Peskine91892022021-02-08 19:50:26 +01004991 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004992 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02004993
Gilles Peskine8817f612018-12-18 00:18:46 +01004994 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004995
Gilles Peskinea50d7392018-06-21 10:22:13 +02004996 /* Run several times, to ensure that every output byte will be
4997 * nonzero at least once with overwhelming probability
4998 * (2^(-8*number_of_runs)). */
4999 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005000 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005001 if( bytes != 0 )
5002 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005003 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005004
Gilles Peskinea50d7392018-06-21 10:22:13 +02005005 for( i = 0; i < bytes; i++ )
5006 {
5007 if( output[i] != 0 )
5008 ++changed[i];
5009 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005010 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005011
5012 /* Check that every byte was changed to nonzero at least once. This
5013 * validates that psa_generate_random is overwriting every byte of
5014 * the output buffer. */
5015 for( i = 0; i < bytes; i++ )
5016 {
5017 TEST_ASSERT( changed[i] != 0 );
5018 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005019
5020exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005021 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005022 mbedtls_free( output );
5023 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005024}
5025/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005026
5027/* BEGIN_CASE */
5028void generate_key( int type_arg,
5029 int bits_arg,
5030 int usage_arg,
5031 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005032 int expected_status_arg,
5033 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005034{
Ronald Cron5425a212020-08-04 14:58:35 +02005035 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005036 psa_key_type_t type = type_arg;
5037 psa_key_usage_t usage = usage_arg;
5038 size_t bits = bits_arg;
5039 psa_algorithm_t alg = alg_arg;
5040 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005041 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005042 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005043
Gilles Peskine8817f612018-12-18 00:18:46 +01005044 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005045
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005046 psa_set_key_usage_flags( &attributes, usage );
5047 psa_set_key_algorithm( &attributes, alg );
5048 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005049 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005050
5051 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005052 psa_status_t status = psa_generate_key( &attributes, &key );
5053
5054 if( is_large_key > 0 )
5055 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5056 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005057 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005058 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005059
5060 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005061 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005062 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5063 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005064
Gilles Peskine818ca122018-06-20 18:16:48 +02005065 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005066 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005067 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005068
5069exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005070 /*
5071 * Key attributes may have been returned by psa_get_key_attributes()
5072 * thus reset them as required.
5073 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005074 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005075
Ronald Cron5425a212020-08-04 14:58:35 +02005076 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005077 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005078}
5079/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005080
Ronald Cronee414c72021-03-18 18:50:08 +01005081/* 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 +02005082void generate_key_rsa( int bits_arg,
5083 data_t *e_arg,
5084 int expected_status_arg )
5085{
Ronald Cron5425a212020-08-04 14:58:35 +02005086 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005087 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005088 size_t bits = bits_arg;
5089 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5090 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5091 psa_status_t expected_status = expected_status_arg;
5092 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5093 uint8_t *exported = NULL;
5094 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005095 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005096 size_t exported_length = SIZE_MAX;
5097 uint8_t *e_read_buffer = NULL;
5098 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005099 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005100 size_t e_read_length = SIZE_MAX;
5101
5102 if( e_arg->len == 0 ||
5103 ( e_arg->len == 3 &&
5104 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5105 {
5106 is_default_public_exponent = 1;
5107 e_read_size = 0;
5108 }
5109 ASSERT_ALLOC( e_read_buffer, e_read_size );
5110 ASSERT_ALLOC( exported, exported_size );
5111
5112 PSA_ASSERT( psa_crypto_init( ) );
5113
5114 psa_set_key_usage_flags( &attributes, usage );
5115 psa_set_key_algorithm( &attributes, alg );
5116 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5117 e_arg->x, e_arg->len ) );
5118 psa_set_key_bits( &attributes, bits );
5119
5120 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005121 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005122 if( expected_status != PSA_SUCCESS )
5123 goto exit;
5124
5125 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005126 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005127 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5128 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5129 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5130 e_read_buffer, e_read_size,
5131 &e_read_length ) );
5132 if( is_default_public_exponent )
5133 TEST_EQUAL( e_read_length, 0 );
5134 else
5135 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5136
5137 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005138 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005139 goto exit;
5140
5141 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005142 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005143 exported, exported_size,
5144 &exported_length ) );
5145 {
5146 uint8_t *p = exported;
5147 uint8_t *end = exported + exported_length;
5148 size_t len;
5149 /* RSAPublicKey ::= SEQUENCE {
5150 * modulus INTEGER, -- n
5151 * publicExponent INTEGER } -- e
5152 */
5153 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005154 MBEDTLS_ASN1_SEQUENCE |
5155 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005156 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005157 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5158 MBEDTLS_ASN1_INTEGER ) );
5159 if( len >= 1 && p[0] == 0 )
5160 {
5161 ++p;
5162 --len;
5163 }
5164 if( e_arg->len == 0 )
5165 {
5166 TEST_EQUAL( len, 3 );
5167 TEST_EQUAL( p[0], 1 );
5168 TEST_EQUAL( p[1], 0 );
5169 TEST_EQUAL( p[2], 1 );
5170 }
5171 else
5172 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5173 }
5174
5175exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005176 /*
5177 * Key attributes may have been returned by psa_get_key_attributes() or
5178 * set by psa_set_key_domain_parameters() thus reset them as required.
5179 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005180 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005181
Ronald Cron5425a212020-08-04 14:58:35 +02005182 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005183 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005184 mbedtls_free( e_read_buffer );
5185 mbedtls_free( exported );
5186}
5187/* END_CASE */
5188
Darryl Greend49a4992018-06-18 17:27:26 +01005189/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005190void persistent_key_load_key_from_storage( data_t *data,
5191 int type_arg, int bits_arg,
5192 int usage_flags_arg, int alg_arg,
5193 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005194{
Ronald Cron71016a92020-08-28 19:01:50 +02005195 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005196 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005197 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5198 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005199 psa_key_type_t type = type_arg;
5200 size_t bits = bits_arg;
5201 psa_key_usage_t usage_flags = usage_flags_arg;
5202 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005203 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005204 unsigned char *first_export = NULL;
5205 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005206 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005207 size_t first_exported_length;
5208 size_t second_exported_length;
5209
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005210 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5211 {
5212 ASSERT_ALLOC( first_export, export_size );
5213 ASSERT_ALLOC( second_export, export_size );
5214 }
Darryl Greend49a4992018-06-18 17:27:26 +01005215
Gilles Peskine8817f612018-12-18 00:18:46 +01005216 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005217
Gilles Peskinec87af662019-05-15 16:12:22 +02005218 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005219 psa_set_key_usage_flags( &attributes, usage_flags );
5220 psa_set_key_algorithm( &attributes, alg );
5221 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005222 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005223
Darryl Green0c6575a2018-11-07 16:05:30 +00005224 switch( generation_method )
5225 {
5226 case IMPORT_KEY:
5227 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005228 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005229 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005230 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005231
Darryl Green0c6575a2018-11-07 16:05:30 +00005232 case GENERATE_KEY:
5233 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005234 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005235 break;
5236
5237 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005238#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005239 {
5240 /* Create base key */
5241 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5242 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5243 psa_set_key_usage_flags( &base_attributes,
5244 PSA_KEY_USAGE_DERIVE );
5245 psa_set_key_algorithm( &base_attributes, derive_alg );
5246 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005247 PSA_ASSERT( psa_import_key( &base_attributes,
5248 data->x, data->len,
5249 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005250 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005251 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005252 PSA_ASSERT( psa_key_derivation_input_key(
5253 &operation,
5254 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005255 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005256 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005257 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005258 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5259 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005260 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005261 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005262 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005263 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005264 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005265#else
5266 TEST_ASSUME( ! "KDF not supported in this configuration" );
5267#endif
5268 break;
5269
5270 default:
5271 TEST_ASSERT( ! "generation_method not implemented in test" );
5272 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005273 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005274 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005275
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005276 /* Export the key if permitted by the key policy. */
5277 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5278 {
Ronald Cron5425a212020-08-04 14:58:35 +02005279 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005280 first_export, export_size,
5281 &first_exported_length ) );
5282 if( generation_method == IMPORT_KEY )
5283 ASSERT_COMPARE( data->x, data->len,
5284 first_export, first_exported_length );
5285 }
Darryl Greend49a4992018-06-18 17:27:26 +01005286
5287 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005288 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005289 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005290 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005291
Darryl Greend49a4992018-06-18 17:27:26 +01005292 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005293 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005294 TEST_ASSERT( mbedtls_svc_key_id_equal(
5295 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005296 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5297 PSA_KEY_LIFETIME_PERSISTENT );
5298 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5299 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4d9009e2021-05-13 12:05:01 +02005300 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
5301 update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005302 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005303
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005304 /* Export the key again if permitted by the key policy. */
5305 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005306 {
Ronald Cron5425a212020-08-04 14:58:35 +02005307 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005308 second_export, export_size,
5309 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005310 ASSERT_COMPARE( first_export, first_exported_length,
5311 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005312 }
5313
5314 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005315 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005316 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005317
5318exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005319 /*
5320 * Key attributes may have been returned by psa_get_key_attributes()
5321 * thus reset them as required.
5322 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005323 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005324
Darryl Greend49a4992018-06-18 17:27:26 +01005325 mbedtls_free( first_export );
5326 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005327 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005328 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005329 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005330 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005331}
5332/* END_CASE */