blob: dce53a5012def2139c5b5500fbdd67cfa5359f9a [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010018
Gilles Peskinef6279312021-05-27 13:21:20 +020019/* If this comes up, it's a bug in the test code or in the test data. */
20#define UNUSED 0xdeadbeef
21
Jaeden Amerof24c7f82018-06-27 17:20:43 +010022/** An invalid export length that will never be set by psa_export_key(). */
23static const size_t INVALID_EXPORT_LENGTH = ~0U;
24
Gilles Peskinea7aa4422018-08-14 15:17:54 +020025/** Test if a buffer contains a constant byte value.
26 *
27 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020028 *
29 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020030 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020031 * \param size Size of the buffer in bytes.
32 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020033 * \return 1 if the buffer is all-bits-zero.
34 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020035 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020036static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037{
38 size_t i;
39 for( i = 0; i < size; i++ )
40 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020042 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020043 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020044 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020045}
Gilles Peskine818ca122018-06-20 18:16:48 +020046
Gilles Peskine0b352bc2018-06-28 00:16:11 +020047/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
48static int asn1_write_10x( unsigned char **p,
49 unsigned char *start,
50 size_t bits,
51 unsigned char x )
52{
53 int ret;
54 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020055 if( bits == 0 )
56 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
57 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020058 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030059 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020060 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
61 *p -= len;
62 ( *p )[len-1] = x;
63 if( bits % 8 == 0 )
64 ( *p )[1] |= 1;
65 else
66 ( *p )[0] |= 1 << ( bits % 8 );
67 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
68 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
69 MBEDTLS_ASN1_INTEGER ) );
70 return( len );
71}
72
73static int construct_fake_rsa_key( unsigned char *buffer,
74 size_t buffer_size,
75 unsigned char **p,
76 size_t bits,
77 int keypair )
78{
79 size_t half_bits = ( bits + 1 ) / 2;
80 int ret;
81 int len = 0;
82 /* Construct something that looks like a DER encoding of
83 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
84 * RSAPrivateKey ::= SEQUENCE {
85 * version Version,
86 * modulus INTEGER, -- n
87 * publicExponent INTEGER, -- e
88 * privateExponent INTEGER, -- d
89 * prime1 INTEGER, -- p
90 * prime2 INTEGER, -- q
91 * exponent1 INTEGER, -- d mod (p-1)
92 * exponent2 INTEGER, -- d mod (q-1)
93 * coefficient INTEGER, -- (inverse of q) mod p
94 * otherPrimeInfos OtherPrimeInfos OPTIONAL
95 * }
96 * Or, for a public key, the same structure with only
97 * version, modulus and publicExponent.
98 */
99 *p = buffer + buffer_size;
100 if( keypair )
101 {
102 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
103 asn1_write_10x( p, buffer, half_bits, 1 ) );
104 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
105 asn1_write_10x( p, buffer, half_bits, 1 ) );
106 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
107 asn1_write_10x( p, buffer, half_bits, 1 ) );
108 MBEDTLS_ASN1_CHK_ADD( len, /* q */
109 asn1_write_10x( p, buffer, half_bits, 1 ) );
110 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
111 asn1_write_10x( p, buffer, half_bits, 3 ) );
112 MBEDTLS_ASN1_CHK_ADD( len, /* d */
113 asn1_write_10x( p, buffer, bits, 1 ) );
114 }
115 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
116 asn1_write_10x( p, buffer, 17, 1 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* n */
118 asn1_write_10x( p, buffer, bits, 1 ) );
119 if( keypair )
120 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
121 mbedtls_asn1_write_int( p, buffer, 0 ) );
122 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
123 {
124 const unsigned char tag =
125 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
126 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
127 }
128 return( len );
129}
130
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100131int exercise_mac_setup( psa_key_type_t key_type,
132 const unsigned char *key_bytes,
133 size_t key_length,
134 psa_algorithm_t alg,
135 psa_mac_operation_t *operation,
136 psa_status_t *status )
137{
Ronald Cron5425a212020-08-04 14:58:35 +0200138 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200139 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100140
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100141 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200142 psa_set_key_algorithm( &attributes, alg );
143 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200144 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100145
Ronald Cron5425a212020-08-04 14:58:35 +0200146 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100147 /* Whether setup succeeded or failed, abort must succeed. */
148 PSA_ASSERT( psa_mac_abort( operation ) );
149 /* If setup failed, reproduce the failure, so that the caller can
150 * test the resulting state of the operation object. */
151 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100152 {
Ronald Cron5425a212020-08-04 14:58:35 +0200153 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100154 }
155
Ronald Cron5425a212020-08-04 14:58:35 +0200156 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100157 return( 1 );
158
159exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200160 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100161 return( 0 );
162}
163
164int exercise_cipher_setup( psa_key_type_t key_type,
165 const unsigned char *key_bytes,
166 size_t key_length,
167 psa_algorithm_t alg,
168 psa_cipher_operation_t *operation,
169 psa_status_t *status )
170{
Ronald Cron5425a212020-08-04 14:58:35 +0200171 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200172 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100173
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200174 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
175 psa_set_key_algorithm( &attributes, alg );
176 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200177 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100178
Ronald Cron5425a212020-08-04 14:58:35 +0200179 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100180 /* Whether setup succeeded or failed, abort must succeed. */
181 PSA_ASSERT( psa_cipher_abort( operation ) );
182 /* If setup failed, reproduce the failure, so that the caller can
183 * test the resulting state of the operation object. */
184 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185 {
Ronald Cron5425a212020-08-04 14:58:35 +0200186 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100187 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100188 }
189
Ronald Cron5425a212020-08-04 14:58:35 +0200190 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100191 return( 1 );
192
193exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200194 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100195 return( 0 );
196}
197
Ronald Cron5425a212020-08-04 14:58:35 +0200198static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200199{
200 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200201 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200202 uint8_t buffer[1];
203 size_t length;
204 int ok = 0;
205
Ronald Cronecfb2372020-07-23 17:13:42 +0200206 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200207 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
208 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
209 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200210 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000211 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200212 TEST_EQUAL(
213 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
214 TEST_EQUAL(
215 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200216 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200217 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
218 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
219 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
220 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
221
Ronald Cron5425a212020-08-04 14:58:35 +0200222 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000223 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200224 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200225 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000226 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200227
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200228 ok = 1;
229
230exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100231 /*
232 * Key attributes may have been returned by psa_get_key_attributes()
233 * thus reset them as required.
234 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200235 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100236
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200237 return( ok );
238}
239
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200240/* Assert that a key isn't reported as having a slot number. */
241#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
242#define ASSERT_NO_SLOT_NUMBER( attributes ) \
243 do \
244 { \
245 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
246 TEST_EQUAL( psa_get_key_slot_number( \
247 attributes, \
248 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
249 PSA_ERROR_INVALID_ARGUMENT ); \
250 } \
251 while( 0 )
252#else /* MBEDTLS_PSA_CRYPTO_SE_C */
253#define ASSERT_NO_SLOT_NUMBER( attributes ) \
254 ( (void) 0 )
255#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
256
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100257/* An overapproximation of the amount of storage needed for a key of the
258 * given type and with the given content. The API doesn't make it easy
259 * to find a good value for the size. The current implementation doesn't
260 * care about the value anyway. */
261#define KEY_BITS_FROM_DATA( type, data ) \
262 ( data )->len
263
Darryl Green0c6575a2018-11-07 16:05:30 +0000264typedef enum {
265 IMPORT_KEY = 0,
266 GENERATE_KEY = 1,
267 DERIVE_KEY = 2
268} generate_method;
269
Gilles Peskinee59236f2018-01-27 23:32:46 +0100270/* END_HEADER */
271
272/* BEGIN_DEPENDENCIES
273 * depends_on:MBEDTLS_PSA_CRYPTO_C
274 * END_DEPENDENCIES
275 */
276
277/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200278void static_checks( )
279{
280 size_t max_truncated_mac_size =
281 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
282
283 /* Check that the length for a truncated MAC always fits in the algorithm
284 * encoding. The shifted mask is the maximum truncated value. The
285 * untruncated algorithm may be one byte larger. */
286 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +0100287
288#if defined(MBEDTLS_TEST_DEPRECATED)
289 /* Check deprecated constants. */
290 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
291 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
292 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
293 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
294 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
295 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
296 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
297 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100298
Paul Elliott8ff510a2020-06-02 17:19:28 +0100299 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
300 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
301 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
302 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
303 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
304 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
305 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
306 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
307 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
308 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
309 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
310 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
311 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
312 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
313 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
314 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
315 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
316 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
317 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
318 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
319 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
320 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
321 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
322 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
323 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
324 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
325 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
326 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
327 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
328 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
329
330 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
331 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
332 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
333 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
334 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
335 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
336 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
337 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100338
Paul Elliott75e27032020-06-03 15:17:39 +0100339 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
340 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
341 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
342 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
343 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
344
345 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
346 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100347#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200348}
349/* END_CASE */
350
351/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200352void import_with_policy( int type_arg,
353 int usage_arg, int alg_arg,
354 int expected_status_arg )
355{
356 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
357 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200358 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200359 psa_key_type_t type = type_arg;
360 psa_key_usage_t usage = usage_arg;
361 psa_algorithm_t alg = alg_arg;
362 psa_status_t expected_status = expected_status_arg;
363 const uint8_t key_material[16] = {0};
364 psa_status_t status;
365
366 PSA_ASSERT( psa_crypto_init( ) );
367
368 psa_set_key_type( &attributes, type );
369 psa_set_key_usage_flags( &attributes, usage );
370 psa_set_key_algorithm( &attributes, alg );
371
372 status = psa_import_key( &attributes,
373 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200374 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200375 TEST_EQUAL( status, expected_status );
376 if( status != PSA_SUCCESS )
377 goto exit;
378
Ronald Cron5425a212020-08-04 14:58:35 +0200379 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200380 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4d9009e2021-05-13 12:05:01 +0200381 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200382 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200383 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200384 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200385
Ronald Cron5425a212020-08-04 14:58:35 +0200386 PSA_ASSERT( psa_destroy_key( key ) );
387 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200388
389exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100390 /*
391 * Key attributes may have been returned by psa_get_key_attributes()
392 * thus reset them as required.
393 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200394 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100395
396 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200397 PSA_DONE( );
398}
399/* END_CASE */
400
401/* BEGIN_CASE */
402void import_with_data( data_t *data, int type_arg,
403 int attr_bits_arg,
404 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200405{
406 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
407 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200408 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200409 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200410 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200411 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100412 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100413
Gilles Peskine8817f612018-12-18 00:18:46 +0100414 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100415
Gilles Peskine4747d192019-04-17 15:05:45 +0200416 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200417 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200418
Ronald Cron5425a212020-08-04 14:58:35 +0200419 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100420 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200421 if( status != PSA_SUCCESS )
422 goto exit;
423
Ronald Cron5425a212020-08-04 14:58:35 +0200424 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200425 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200426 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200427 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200428 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200429
Ronald Cron5425a212020-08-04 14:58:35 +0200430 PSA_ASSERT( psa_destroy_key( key ) );
431 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100432
433exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100434 /*
435 * Key attributes may have been returned by psa_get_key_attributes()
436 * thus reset them as required.
437 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200438 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100439
440 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200441 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100442}
443/* END_CASE */
444
445/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200446void import_large_key( int type_arg, int byte_size_arg,
447 int expected_status_arg )
448{
449 psa_key_type_t type = type_arg;
450 size_t byte_size = byte_size_arg;
451 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
452 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200453 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200454 psa_status_t status;
455 uint8_t *buffer = NULL;
456 size_t buffer_size = byte_size + 1;
457 size_t n;
458
Steven Cooreman69967ce2021-01-18 18:01:08 +0100459 /* Skip the test case if the target running the test cannot
460 * accomodate large keys due to heap size constraints */
461 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200462 memset( buffer, 'K', byte_size );
463
464 PSA_ASSERT( psa_crypto_init( ) );
465
466 /* Try importing the key */
467 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
468 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200469 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100470 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200471 TEST_EQUAL( status, expected_status );
472
473 if( status == PSA_SUCCESS )
474 {
Ronald Cron5425a212020-08-04 14:58:35 +0200475 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200476 TEST_EQUAL( psa_get_key_type( &attributes ), type );
477 TEST_EQUAL( psa_get_key_bits( &attributes ),
478 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200479 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200480 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200481 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200482 for( n = 0; n < byte_size; n++ )
483 TEST_EQUAL( buffer[n], 'K' );
484 for( n = byte_size; n < buffer_size; n++ )
485 TEST_EQUAL( buffer[n], 0 );
486 }
487
488exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100489 /*
490 * Key attributes may have been returned by psa_get_key_attributes()
491 * thus reset them as required.
492 */
493 psa_reset_key_attributes( &attributes );
494
Ronald Cron5425a212020-08-04 14:58:35 +0200495 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200496 PSA_DONE( );
497 mbedtls_free( buffer );
498}
499/* END_CASE */
500
501/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200502void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
503{
Ronald Cron5425a212020-08-04 14:58:35 +0200504 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200505 size_t bits = bits_arg;
506 psa_status_t expected_status = expected_status_arg;
507 psa_status_t status;
508 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200509 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200510 size_t buffer_size = /* Slight overapproximations */
511 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200512 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200513 unsigned char *p;
514 int ret;
515 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200516 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200517
Gilles Peskine8817f612018-12-18 00:18:46 +0100518 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200519 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200520
521 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
522 bits, keypair ) ) >= 0 );
523 length = ret;
524
525 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200526 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200527 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100528 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200529
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200530 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200531 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200532
533exit:
534 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200535 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200536}
537/* END_CASE */
538
539/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300540void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300541 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200542 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100543 int expected_bits,
544 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200545 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100546 int canonical_input )
547{
Ronald Cron5425a212020-08-04 14:58:35 +0200548 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100549 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200550 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200551 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100552 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100553 unsigned char *exported = NULL;
554 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100555 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100556 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100557 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200558 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200559 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100560
Moran Pekercb088e72018-07-17 17:36:59 +0300561 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200562 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100563 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200564 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100565 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100566
Gilles Peskine4747d192019-04-17 15:05:45 +0200567 psa_set_key_usage_flags( &attributes, usage_arg );
568 psa_set_key_algorithm( &attributes, alg );
569 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700570
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100571 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200572 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100573
574 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200575 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200576 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
577 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200578 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100579
580 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200581 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100582 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100583
584 /* The exported length must be set by psa_export_key() to a value between 0
585 * and export_size. On errors, the exported length must be 0. */
586 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
587 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
588 TEST_ASSERT( exported_length <= export_size );
589
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200590 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200591 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100592 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200593 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100594 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100595 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200596 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100597
Gilles Peskineea38a922021-02-13 00:05:16 +0100598 /* Run sanity checks on the exported key. For non-canonical inputs,
599 * this validates the canonical representations. For canonical inputs,
600 * this doesn't directly validate the implementation, but it still helps
601 * by cross-validating the test data with the sanity check code. */
602 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200603 goto exit;
604
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100605 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200606 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100607 else
608 {
Ronald Cron5425a212020-08-04 14:58:35 +0200609 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200610 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200611 &key2 ) );
612 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100613 reexported,
614 export_size,
615 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200616 ASSERT_COMPARE( exported, exported_length,
617 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200618 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100619 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100620 TEST_ASSERT( exported_length <=
621 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
622 psa_get_key_bits( &got_attributes ) ) );
623 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100624
625destroy:
626 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200627 PSA_ASSERT( psa_destroy_key( key ) );
628 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100629
630exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100631 /*
632 * Key attributes may have been returned by psa_get_key_attributes()
633 * thus reset them as required.
634 */
635 psa_reset_key_attributes( &got_attributes );
636
itayzafrir3e02b3b2018-06-12 17:06:52 +0300637 mbedtls_free( exported );
638 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200639 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100640}
641/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100642
Moran Pekerf709f4a2018-06-06 17:26:04 +0300643/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300644void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200645 int type_arg,
646 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100647 int export_size_delta,
648 int expected_export_status_arg,
649 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300650{
Ronald Cron5425a212020-08-04 14:58:35 +0200651 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300652 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200653 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200654 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300655 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300656 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100657 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100658 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200659 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300660
Gilles Peskine8817f612018-12-18 00:18:46 +0100661 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300662
Gilles Peskine4747d192019-04-17 15:05:45 +0200663 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
664 psa_set_key_algorithm( &attributes, alg );
665 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300666
667 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200668 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300669
Gilles Peskine49c25912018-10-29 15:15:31 +0100670 /* Export the public key */
671 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200672 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200673 exported, export_size,
674 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100675 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100676 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100677 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200678 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100679 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200680 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200681 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100682 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100683 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100684 TEST_ASSERT( expected_public_key->len <=
685 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
686 TEST_ASSERT( expected_public_key->len <=
687 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100688 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
689 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100690 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300691
692exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100693 /*
694 * Key attributes may have been returned by psa_get_key_attributes()
695 * thus reset them as required.
696 */
697 psa_reset_key_attributes( &attributes );
698
itayzafrir3e02b3b2018-06-12 17:06:52 +0300699 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200700 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200701 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300702}
703/* END_CASE */
704
Gilles Peskine20035e32018-02-03 22:44:14 +0100705/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200706void import_and_exercise_key( data_t *data,
707 int type_arg,
708 int bits_arg,
709 int alg_arg )
710{
Ronald Cron5425a212020-08-04 14:58:35 +0200711 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200712 psa_key_type_t type = type_arg;
713 size_t bits = bits_arg;
714 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100715 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200716 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200717 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200718
Gilles Peskine8817f612018-12-18 00:18:46 +0100719 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200720
Gilles Peskine4747d192019-04-17 15:05:45 +0200721 psa_set_key_usage_flags( &attributes, usage );
722 psa_set_key_algorithm( &attributes, alg );
723 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200724
725 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200726 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200727
728 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200729 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200730 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
731 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200732
733 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100734 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200735 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200736
Ronald Cron5425a212020-08-04 14:58:35 +0200737 PSA_ASSERT( psa_destroy_key( key ) );
738 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200739
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200740exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100741 /*
742 * Key attributes may have been returned by psa_get_key_attributes()
743 * thus reset them as required.
744 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200745 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100746
747 psa_reset_key_attributes( &attributes );
748 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200749 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200750}
751/* END_CASE */
752
753/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100754void effective_key_attributes( int type_arg, int expected_type_arg,
755 int bits_arg, int expected_bits_arg,
756 int usage_arg, int expected_usage_arg,
757 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200758{
Ronald Cron5425a212020-08-04 14:58:35 +0200759 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100760 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100761 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100762 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100763 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200764 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100765 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200766 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100767 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200768 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200769
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200770 /* Check if all implicit usage flags are deployed
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200771 in the expected usage flags. */
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200772 TEST_EQUAL( expected_usage, mbedtls_test_update_key_usage_flags( usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200773
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-armff03fd62021-06-28 14:05:00 +0200869 /* Check if all implicit usage flags are deployed
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200870 in the expected usage flags. */
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200871 TEST_EQUAL( expected_usage,
872 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200873
Gilles Peskine8817f612018-12-18 00:18:46 +0100874 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200875
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200876 psa_set_key_usage_flags( &attributes, policy_usage );
877 psa_set_key_algorithm( &attributes, policy_alg );
878 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200879
Gilles Peskine049c7532019-05-15 20:22:09 +0200880 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200881 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200882
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200883 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
884
Ronald Cron5425a212020-08-04 14:58:35 +0200885 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100886 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100887 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100888 else
889 TEST_EQUAL( status, expected_status );
890
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200891 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200892
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200893 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200894 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100895 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100896 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100897 else
898 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200899
900exit:
901 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200902 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200903 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200904}
905/* END_CASE */
906
907/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200908void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200909 int policy_alg,
910 int key_type,
911 data_t *key_data,
912 int exercise_alg )
913{
Ronald Cron5425a212020-08-04 14:58:35 +0200914 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200915 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000916 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200917 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200918 psa_status_t status;
919
Gilles Peskine8817f612018-12-18 00:18:46 +0100920 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200921
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200922 psa_set_key_usage_flags( &attributes, policy_usage );
923 psa_set_key_algorithm( &attributes, policy_alg );
924 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200925
Gilles Peskine049c7532019-05-15 20:22:09 +0200926 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200927 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200928
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200929 /* Check if no key usage flag implication is done */
930 TEST_EQUAL( policy_usage,
931 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200932
Ronald Cron5425a212020-08-04 14:58:35 +0200933 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200934 if( policy_alg == exercise_alg &&
935 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100936 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200937 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100938 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200939 psa_cipher_abort( &operation );
940
Ronald Cron5425a212020-08-04 14:58:35 +0200941 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200942 if( policy_alg == exercise_alg &&
943 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100944 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200945 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100946 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200947
948exit:
949 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200950 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200951 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200952}
953/* END_CASE */
954
955/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200956void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200957 int policy_alg,
958 int key_type,
959 data_t *key_data,
960 int nonce_length_arg,
961 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100962 int exercise_alg,
963 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200964{
Ronald Cron5425a212020-08-04 14:58:35 +0200965 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200966 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200967 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200968 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100969 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200970 unsigned char nonce[16] = {0};
971 size_t nonce_length = nonce_length_arg;
972 unsigned char tag[16];
973 size_t tag_length = tag_length_arg;
974 size_t output_length;
975
976 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
977 TEST_ASSERT( tag_length <= sizeof( tag ) );
978
Gilles Peskine8817f612018-12-18 00:18:46 +0100979 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200980
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200981 psa_set_key_usage_flags( &attributes, policy_usage );
982 psa_set_key_algorithm( &attributes, policy_alg );
983 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200984
Gilles Peskine049c7532019-05-15 20:22:09 +0200985 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200986 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200987
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200988 /* Check if no key usage implication is done */
989 TEST_EQUAL( policy_usage,
990 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200991
Ronald Cron5425a212020-08-04 14:58:35 +0200992 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200993 nonce, nonce_length,
994 NULL, 0,
995 NULL, 0,
996 tag, tag_length,
997 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100998 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
999 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001000 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001001 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001002
1003 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001004 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001005 nonce, nonce_length,
1006 NULL, 0,
1007 tag, tag_length,
1008 NULL, 0,
1009 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001010 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1011 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1012 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001013 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001014 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001015 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001016
1017exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001018 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001019 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001020}
1021/* END_CASE */
1022
1023/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001024void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001025 int policy_alg,
1026 int key_type,
1027 data_t *key_data,
1028 int exercise_alg )
1029{
Ronald Cron5425a212020-08-04 14:58:35 +02001030 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001031 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001032 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001033 psa_status_t status;
1034 size_t key_bits;
1035 size_t buffer_length;
1036 unsigned char *buffer = NULL;
1037 size_t output_length;
1038
Gilles Peskine8817f612018-12-18 00:18:46 +01001039 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001040
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001041 psa_set_key_usage_flags( &attributes, policy_usage );
1042 psa_set_key_algorithm( &attributes, policy_alg );
1043 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001044
Gilles Peskine049c7532019-05-15 20:22:09 +02001045 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001046 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001047
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001048 /* Check if no key usage implication is done */
1049 TEST_EQUAL( policy_usage,
1050 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001051
Ronald Cron5425a212020-08-04 14:58:35 +02001052 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001053 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001054 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1055 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001056 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001057
Ronald Cron5425a212020-08-04 14:58:35 +02001058 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001059 NULL, 0,
1060 NULL, 0,
1061 buffer, buffer_length,
1062 &output_length );
1063 if( policy_alg == exercise_alg &&
1064 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001065 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001066 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001067 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001068
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001069 if( buffer_length != 0 )
1070 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001071 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001072 buffer, buffer_length,
1073 NULL, 0,
1074 buffer, buffer_length,
1075 &output_length );
1076 if( policy_alg == exercise_alg &&
1077 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001078 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001079 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001080 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001081
1082exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001083 /*
1084 * Key attributes may have been returned by psa_get_key_attributes()
1085 * thus reset them as required.
1086 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001087 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001088
1089 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001090 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001091 mbedtls_free( buffer );
1092}
1093/* END_CASE */
1094
1095/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001096void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001097 int policy_alg,
1098 int key_type,
1099 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001100 int exercise_alg,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001101 int payload_length_arg,
1102 int hashing_permitted,
1103 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001104{
Ronald Cron5425a212020-08-04 14:58:35 +02001105 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001106 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001107 psa_key_usage_t policy_usage = policy_usage_arg;
1108 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001109 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001110 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1111 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1112 * compatible with the policy and `payload_length_arg` is supposed to be
1113 * a valid input length to sign. If `payload_length_arg <= 0`,
1114 * `exercise_alg` is supposed to be forbidden by the policy. */
1115 int compatible_alg = payload_length_arg > 0;
1116 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001117 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001118 size_t signature_length;
1119
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001120 /* Check if all implicit usage flags are deployed
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001121 in the expected usage flags. */
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001122 TEST_EQUAL( expected_usage,
1123 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001124
Gilles Peskine8817f612018-12-18 00:18:46 +01001125 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001126
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001127 psa_set_key_usage_flags( &attributes, policy_usage );
1128 psa_set_key_algorithm( &attributes, policy_alg );
1129 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001130
Gilles Peskine049c7532019-05-15 20:22:09 +02001131 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001132 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001133
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001134 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1135
Ronald Cron5425a212020-08-04 14:58:35 +02001136 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001137 payload, payload_length,
1138 signature, sizeof( signature ),
1139 &signature_length );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001140 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001141 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001142 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001143 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001144
1145 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001146 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001147 payload, payload_length,
1148 signature, sizeof( signature ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001149 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001150 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001151 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001152 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001153
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001154 if( hashing_permitted )
1155 {
1156 status = psa_sign_message( key, exercise_alg,
1157 payload, payload_length,
1158 signature, sizeof( signature ),
1159 &signature_length );
1160 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1161 PSA_ASSERT( status );
1162 else
1163 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1164
1165 memset( signature, 0, sizeof( signature ) );
1166 status = psa_verify_message( key, exercise_alg,
1167 payload, payload_length,
1168 signature, sizeof( signature ) );
1169 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1170 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1171 else
1172 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1173 }
1174
Gilles Peskined5b33222018-06-18 22:20:03 +02001175exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001176 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001177 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001178}
1179/* END_CASE */
1180
Janos Follathba3fab92019-06-11 14:50:16 +01001181/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001182void derive_key_policy( int policy_usage,
1183 int policy_alg,
1184 int key_type,
1185 data_t *key_data,
1186 int exercise_alg )
1187{
Ronald Cron5425a212020-08-04 14:58:35 +02001188 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001189 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001190 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001191 psa_status_t status;
1192
Gilles Peskine8817f612018-12-18 00:18:46 +01001193 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001194
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001195 psa_set_key_usage_flags( &attributes, policy_usage );
1196 psa_set_key_algorithm( &attributes, policy_alg );
1197 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001198
Gilles Peskine049c7532019-05-15 20:22:09 +02001199 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001200 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001201
Janos Follathba3fab92019-06-11 14:50:16 +01001202 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1203
1204 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1205 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001206 {
Janos Follathba3fab92019-06-11 14:50:16 +01001207 PSA_ASSERT( psa_key_derivation_input_bytes(
1208 &operation,
1209 PSA_KEY_DERIVATION_INPUT_SEED,
1210 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001211 }
Janos Follathba3fab92019-06-11 14:50:16 +01001212
1213 status = psa_key_derivation_input_key( &operation,
1214 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001215 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001216
Gilles Peskineea0fb492018-07-12 17:17:20 +02001217 if( policy_alg == exercise_alg &&
1218 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001219 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001220 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001221 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001222
1223exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001224 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001225 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001226 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001227}
1228/* END_CASE */
1229
1230/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001231void agreement_key_policy( int policy_usage,
1232 int policy_alg,
1233 int key_type_arg,
1234 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001235 int exercise_alg,
1236 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001237{
Ronald Cron5425a212020-08-04 14:58:35 +02001238 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001239 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001240 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001241 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001242 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001243 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001244
Gilles Peskine8817f612018-12-18 00:18:46 +01001245 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001246
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001247 psa_set_key_usage_flags( &attributes, policy_usage );
1248 psa_set_key_algorithm( &attributes, policy_alg );
1249 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001250
Gilles Peskine049c7532019-05-15 20:22:09 +02001251 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001252 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001253
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001254 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001255 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001256
Steven Cooremance48e852020-10-05 16:02:45 +02001257 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001258
1259exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001260 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001261 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001262 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001263}
1264/* END_CASE */
1265
1266/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001267void key_policy_alg2( int key_type_arg, data_t *key_data,
1268 int usage_arg, int alg_arg, int alg2_arg )
1269{
Ronald Cron5425a212020-08-04 14:58:35 +02001270 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001271 psa_key_type_t key_type = key_type_arg;
1272 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1273 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1274 psa_key_usage_t usage = usage_arg;
1275 psa_algorithm_t alg = alg_arg;
1276 psa_algorithm_t alg2 = alg2_arg;
1277
1278 PSA_ASSERT( psa_crypto_init( ) );
1279
1280 psa_set_key_usage_flags( &attributes, usage );
1281 psa_set_key_algorithm( &attributes, alg );
1282 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1283 psa_set_key_type( &attributes, key_type );
1284 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001285 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001286
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001287 /* Update the usage flags to obtain implicit usage flags */
1288 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001289 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001290 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1291 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1292 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1293
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001294 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001295 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001296 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001297 goto exit;
1298
1299exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001300 /*
1301 * Key attributes may have been returned by psa_get_key_attributes()
1302 * thus reset them as required.
1303 */
1304 psa_reset_key_attributes( &got_attributes );
1305
Ronald Cron5425a212020-08-04 14:58:35 +02001306 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001307 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001308}
1309/* END_CASE */
1310
1311/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001312void raw_agreement_key_policy( int policy_usage,
1313 int policy_alg,
1314 int key_type_arg,
1315 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001316 int exercise_alg,
1317 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001318{
Ronald Cron5425a212020-08-04 14:58:35 +02001319 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001320 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001321 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001322 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001323 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001324 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001325
1326 PSA_ASSERT( psa_crypto_init( ) );
1327
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001328 psa_set_key_usage_flags( &attributes, policy_usage );
1329 psa_set_key_algorithm( &attributes, policy_alg );
1330 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001331
Gilles Peskine049c7532019-05-15 20:22:09 +02001332 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001333 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001334
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001335 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001336
Steven Cooremance48e852020-10-05 16:02:45 +02001337 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001338
1339exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001340 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001341 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001342 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001343}
1344/* END_CASE */
1345
1346/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001347void copy_success( int source_usage_arg,
1348 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001349 int type_arg, data_t *material,
1350 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001351 int target_usage_arg,
1352 int target_alg_arg, int target_alg2_arg,
1353 int expected_usage_arg,
1354 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001355{
Gilles Peskineca25db92019-04-19 11:43:08 +02001356 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1357 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001358 psa_key_usage_t expected_usage = expected_usage_arg;
1359 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001360 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001361 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1362 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001363 uint8_t *export_buffer = NULL;
1364
Gilles Peskine57ab7212019-01-28 13:03:09 +01001365 PSA_ASSERT( psa_crypto_init( ) );
1366
Gilles Peskineca25db92019-04-19 11:43:08 +02001367 /* Prepare the source key. */
1368 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1369 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001370 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001371 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001372 PSA_ASSERT( psa_import_key( &source_attributes,
1373 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001374 &source_key ) );
1375 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001376
Gilles Peskineca25db92019-04-19 11:43:08 +02001377 /* Prepare the target attributes. */
1378 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001379 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001380 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001381 /* Set volatile lifetime to reset the key identifier to 0. */
1382 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1383 }
1384
Gilles Peskineca25db92019-04-19 11:43:08 +02001385 if( target_usage_arg != -1 )
1386 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1387 if( target_alg_arg != -1 )
1388 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001389 if( target_alg2_arg != -1 )
1390 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001391
1392 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001393 PSA_ASSERT( psa_copy_key( source_key,
1394 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001395
1396 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001397 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001398
1399 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001400 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001401 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1402 psa_get_key_type( &target_attributes ) );
1403 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1404 psa_get_key_bits( &target_attributes ) );
1405 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1406 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001407 TEST_EQUAL( expected_alg2,
1408 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001409 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1410 {
1411 size_t length;
1412 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001413 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001414 material->len, &length ) );
1415 ASSERT_COMPARE( material->x, material->len,
1416 export_buffer, length );
1417 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001418
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001419 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001420 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001421 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001422 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001423
Ronald Cron5425a212020-08-04 14:58:35 +02001424 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001425
1426exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001427 /*
1428 * Source and target key attributes may have been returned by
1429 * psa_get_key_attributes() thus reset them as required.
1430 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001431 psa_reset_key_attributes( &source_attributes );
1432 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001433
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001434 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001435 mbedtls_free( export_buffer );
1436}
1437/* END_CASE */
1438
1439/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001440void copy_fail( int source_usage_arg,
1441 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001442 int type_arg, data_t *material,
1443 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001444 int target_usage_arg,
1445 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001446 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001447 int expected_status_arg )
1448{
1449 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1450 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001451 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1452 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001453 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001454
1455 PSA_ASSERT( psa_crypto_init( ) );
1456
1457 /* Prepare the source key. */
1458 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1459 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001460 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001461 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001462 PSA_ASSERT( psa_import_key( &source_attributes,
1463 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001464 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001465
1466 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001467 psa_set_key_id( &target_attributes, key_id );
1468 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001469 psa_set_key_type( &target_attributes, target_type_arg );
1470 psa_set_key_bits( &target_attributes, target_bits_arg );
1471 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1472 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001473 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001474
1475 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001476 TEST_EQUAL( psa_copy_key( source_key,
1477 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001478 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001479
Ronald Cron5425a212020-08-04 14:58:35 +02001480 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001481
Gilles Peskine4a644642019-05-03 17:14:08 +02001482exit:
1483 psa_reset_key_attributes( &source_attributes );
1484 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001485 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001486}
1487/* END_CASE */
1488
1489/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001490void hash_operation_init( )
1491{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001492 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001493 /* Test each valid way of initializing the object, except for `= {0}`, as
1494 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1495 * though it's OK by the C standard. We could test for this, but we'd need
1496 * to supress the Clang warning for the test. */
1497 psa_hash_operation_t func = psa_hash_operation_init( );
1498 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1499 psa_hash_operation_t zero;
1500
1501 memset( &zero, 0, sizeof( zero ) );
1502
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001503 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001504 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1505 PSA_ERROR_BAD_STATE );
1506 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1507 PSA_ERROR_BAD_STATE );
1508 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1509 PSA_ERROR_BAD_STATE );
1510
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001511 /* A default hash operation should be abortable without error. */
1512 PSA_ASSERT( psa_hash_abort( &func ) );
1513 PSA_ASSERT( psa_hash_abort( &init ) );
1514 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001515}
1516/* END_CASE */
1517
1518/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001519void hash_setup( int alg_arg,
1520 int expected_status_arg )
1521{
1522 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001523 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001524 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001525 psa_status_t status;
1526
Gilles Peskine8817f612018-12-18 00:18:46 +01001527 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001528
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001529 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001530 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001531
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001532 /* Whether setup succeeded or failed, abort must succeed. */
1533 PSA_ASSERT( psa_hash_abort( &operation ) );
1534
1535 /* If setup failed, reproduce the failure, so as to
1536 * test the resulting state of the operation object. */
1537 if( status != PSA_SUCCESS )
1538 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1539
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001540 /* Now the operation object should be reusable. */
1541#if defined(KNOWN_SUPPORTED_HASH_ALG)
1542 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1543 PSA_ASSERT( psa_hash_abort( &operation ) );
1544#endif
1545
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001546exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001547 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001548}
1549/* END_CASE */
1550
1551/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001552void hash_compute_fail( int alg_arg, data_t *input,
1553 int output_size_arg, int expected_status_arg )
1554{
1555 psa_algorithm_t alg = alg_arg;
1556 uint8_t *output = NULL;
1557 size_t output_size = output_size_arg;
1558 size_t output_length = INVALID_EXPORT_LENGTH;
1559 psa_status_t expected_status = expected_status_arg;
1560 psa_status_t status;
1561
1562 ASSERT_ALLOC( output, output_size );
1563
1564 PSA_ASSERT( psa_crypto_init( ) );
1565
1566 status = psa_hash_compute( alg, input->x, input->len,
1567 output, output_size, &output_length );
1568 TEST_EQUAL( status, expected_status );
1569 TEST_ASSERT( output_length <= output_size );
1570
1571exit:
1572 mbedtls_free( output );
1573 PSA_DONE( );
1574}
1575/* END_CASE */
1576
1577/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001578void hash_compare_fail( int alg_arg, data_t *input,
1579 data_t *reference_hash,
1580 int expected_status_arg )
1581{
1582 psa_algorithm_t alg = alg_arg;
1583 psa_status_t expected_status = expected_status_arg;
1584 psa_status_t status;
1585
1586 PSA_ASSERT( psa_crypto_init( ) );
1587
1588 status = psa_hash_compare( alg, input->x, input->len,
1589 reference_hash->x, reference_hash->len );
1590 TEST_EQUAL( status, expected_status );
1591
1592exit:
1593 PSA_DONE( );
1594}
1595/* END_CASE */
1596
1597/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001598void hash_compute_compare( int alg_arg, data_t *input,
1599 data_t *expected_output )
1600{
1601 psa_algorithm_t alg = alg_arg;
1602 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1603 size_t output_length = INVALID_EXPORT_LENGTH;
1604 size_t i;
1605
1606 PSA_ASSERT( psa_crypto_init( ) );
1607
1608 /* Compute with tight buffer */
1609 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001610 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001611 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001612 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001613 ASSERT_COMPARE( output, output_length,
1614 expected_output->x, expected_output->len );
1615
1616 /* Compute with larger buffer */
1617 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1618 output, sizeof( output ),
1619 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001620 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001621 ASSERT_COMPARE( output, output_length,
1622 expected_output->x, expected_output->len );
1623
1624 /* Compare with correct hash */
1625 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1626 output, output_length ) );
1627
1628 /* Compare with trailing garbage */
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 truncated hash */
1634 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1635 output, output_length - 1 ),
1636 PSA_ERROR_INVALID_SIGNATURE );
1637
1638 /* Compare with corrupted value */
1639 for( i = 0; i < output_length; i++ )
1640 {
Chris Jones9634bb12021-01-20 15:56:42 +00001641 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001642 output[i] ^= 1;
1643 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1644 output, output_length ),
1645 PSA_ERROR_INVALID_SIGNATURE );
1646 output[i] ^= 1;
1647 }
1648
1649exit:
1650 PSA_DONE( );
1651}
1652/* END_CASE */
1653
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001654/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001655void hash_bad_order( )
1656{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001657 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001658 unsigned char input[] = "";
1659 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001660 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001661 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1662 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1663 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001664 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001665 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001666 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001667
Gilles Peskine8817f612018-12-18 00:18:46 +01001668 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001669
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001670 /* Call setup twice in a row. */
1671 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1672 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1673 PSA_ERROR_BAD_STATE );
1674 PSA_ASSERT( psa_hash_abort( &operation ) );
1675
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001676 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001677 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001678 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001679 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001680
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001681 /* Call update after finish. */
1682 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1683 PSA_ASSERT( psa_hash_finish( &operation,
1684 hash, sizeof( hash ), &hash_len ) );
1685 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001686 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001687 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001688
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001689 /* Call verify without calling setup beforehand. */
1690 TEST_EQUAL( psa_hash_verify( &operation,
1691 valid_hash, sizeof( valid_hash ) ),
1692 PSA_ERROR_BAD_STATE );
1693 PSA_ASSERT( psa_hash_abort( &operation ) );
1694
1695 /* Call verify after finish. */
1696 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1697 PSA_ASSERT( psa_hash_finish( &operation,
1698 hash, sizeof( hash ), &hash_len ) );
1699 TEST_EQUAL( psa_hash_verify( &operation,
1700 valid_hash, sizeof( valid_hash ) ),
1701 PSA_ERROR_BAD_STATE );
1702 PSA_ASSERT( psa_hash_abort( &operation ) );
1703
1704 /* Call verify twice in a row. */
1705 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1706 PSA_ASSERT( psa_hash_verify( &operation,
1707 valid_hash, sizeof( valid_hash ) ) );
1708 TEST_EQUAL( psa_hash_verify( &operation,
1709 valid_hash, sizeof( valid_hash ) ),
1710 PSA_ERROR_BAD_STATE );
1711 PSA_ASSERT( psa_hash_abort( &operation ) );
1712
1713 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001714 TEST_EQUAL( psa_hash_finish( &operation,
1715 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001716 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001717 PSA_ASSERT( psa_hash_abort( &operation ) );
1718
1719 /* Call finish twice in a row. */
1720 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1721 PSA_ASSERT( psa_hash_finish( &operation,
1722 hash, sizeof( hash ), &hash_len ) );
1723 TEST_EQUAL( psa_hash_finish( &operation,
1724 hash, sizeof( hash ), &hash_len ),
1725 PSA_ERROR_BAD_STATE );
1726 PSA_ASSERT( psa_hash_abort( &operation ) );
1727
1728 /* Call finish after calling verify. */
1729 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1730 PSA_ASSERT( psa_hash_verify( &operation,
1731 valid_hash, sizeof( valid_hash ) ) );
1732 TEST_EQUAL( psa_hash_finish( &operation,
1733 hash, sizeof( hash ), &hash_len ),
1734 PSA_ERROR_BAD_STATE );
1735 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001736
1737exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001738 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001739}
1740/* END_CASE */
1741
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001742/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001743void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001744{
1745 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001746 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1747 * appended to it */
1748 unsigned char hash[] = {
1749 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1750 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1751 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001752 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001753 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001754
Gilles Peskine8817f612018-12-18 00:18:46 +01001755 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001756
itayzafrir27e69452018-11-01 14:26:34 +02001757 /* psa_hash_verify with a smaller hash than expected */
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, expected_size - 1 ),
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 non-matching hash */
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 + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001765 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001766
itayzafrir27e69452018-11-01 14:26:34 +02001767 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001768 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001769 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001770 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001771
itayzafrirec93d302018-10-18 18:01:10 +03001772exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001773 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001774}
1775/* END_CASE */
1776
Ronald Cronee414c72021-03-18 18:50:08 +01001777/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001778void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001779{
1780 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001781 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001782 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001783 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001784 size_t hash_len;
1785
Gilles Peskine8817f612018-12-18 00:18:46 +01001786 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001787
itayzafrir58028322018-10-25 10:22:01 +03001788 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001789 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001790 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001791 hash, expected_size - 1, &hash_len ),
1792 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001793
1794exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001795 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001796}
1797/* END_CASE */
1798
Ronald Cronee414c72021-03-18 18:50:08 +01001799/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001800void hash_clone_source_state( )
1801{
1802 psa_algorithm_t alg = PSA_ALG_SHA_256;
1803 unsigned char hash[PSA_HASH_MAX_SIZE];
1804 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1805 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1806 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1807 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1808 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1809 size_t hash_len;
1810
1811 PSA_ASSERT( psa_crypto_init( ) );
1812 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1813
1814 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1815 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1816 PSA_ASSERT( psa_hash_finish( &op_finished,
1817 hash, sizeof( hash ), &hash_len ) );
1818 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1819 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1820
1821 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1822 PSA_ERROR_BAD_STATE );
1823
1824 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1825 PSA_ASSERT( psa_hash_finish( &op_init,
1826 hash, sizeof( hash ), &hash_len ) );
1827 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1828 PSA_ASSERT( psa_hash_finish( &op_finished,
1829 hash, sizeof( hash ), &hash_len ) );
1830 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1831 PSA_ASSERT( psa_hash_finish( &op_aborted,
1832 hash, sizeof( hash ), &hash_len ) );
1833
1834exit:
1835 psa_hash_abort( &op_source );
1836 psa_hash_abort( &op_init );
1837 psa_hash_abort( &op_setup );
1838 psa_hash_abort( &op_finished );
1839 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001840 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001841}
1842/* END_CASE */
1843
Ronald Cronee414c72021-03-18 18:50:08 +01001844/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001845void hash_clone_target_state( )
1846{
1847 psa_algorithm_t alg = PSA_ALG_SHA_256;
1848 unsigned char hash[PSA_HASH_MAX_SIZE];
1849 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1850 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1851 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1852 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1853 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1854 size_t hash_len;
1855
1856 PSA_ASSERT( psa_crypto_init( ) );
1857
1858 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1859 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1860 PSA_ASSERT( psa_hash_finish( &op_finished,
1861 hash, sizeof( hash ), &hash_len ) );
1862 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1863 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1864
1865 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1866 PSA_ASSERT( psa_hash_finish( &op_target,
1867 hash, sizeof( hash ), &hash_len ) );
1868
1869 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1870 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1871 PSA_ERROR_BAD_STATE );
1872 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1873 PSA_ERROR_BAD_STATE );
1874
1875exit:
1876 psa_hash_abort( &op_target );
1877 psa_hash_abort( &op_init );
1878 psa_hash_abort( &op_setup );
1879 psa_hash_abort( &op_finished );
1880 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001881 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001882}
1883/* END_CASE */
1884
itayzafrir58028322018-10-25 10:22:01 +03001885/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001886void mac_operation_init( )
1887{
Jaeden Amero252ef282019-02-15 14:05:35 +00001888 const uint8_t input[1] = { 0 };
1889
Jaeden Amero769ce272019-01-04 11:48:03 +00001890 /* Test each valid way of initializing the object, except for `= {0}`, as
1891 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1892 * though it's OK by the C standard. We could test for this, but we'd need
1893 * to supress the Clang warning for the test. */
1894 psa_mac_operation_t func = psa_mac_operation_init( );
1895 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1896 psa_mac_operation_t zero;
1897
1898 memset( &zero, 0, sizeof( zero ) );
1899
Jaeden Amero252ef282019-02-15 14:05:35 +00001900 /* A freshly-initialized MAC operation should not be usable. */
1901 TEST_EQUAL( psa_mac_update( &func,
1902 input, sizeof( input ) ),
1903 PSA_ERROR_BAD_STATE );
1904 TEST_EQUAL( psa_mac_update( &init,
1905 input, sizeof( input ) ),
1906 PSA_ERROR_BAD_STATE );
1907 TEST_EQUAL( psa_mac_update( &zero,
1908 input, sizeof( input ) ),
1909 PSA_ERROR_BAD_STATE );
1910
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001911 /* A default MAC operation should be abortable without error. */
1912 PSA_ASSERT( psa_mac_abort( &func ) );
1913 PSA_ASSERT( psa_mac_abort( &init ) );
1914 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001915}
1916/* END_CASE */
1917
1918/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001919void mac_setup( int key_type_arg,
1920 data_t *key,
1921 int alg_arg,
1922 int expected_status_arg )
1923{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001924 psa_key_type_t key_type = key_type_arg;
1925 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001926 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001927 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001928 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1929#if defined(KNOWN_SUPPORTED_MAC_ALG)
1930 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1931#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001932
Gilles Peskine8817f612018-12-18 00:18:46 +01001933 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001934
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001935 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1936 &operation, &status ) )
1937 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001938 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001939
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001940 /* The operation object should be reusable. */
1941#if defined(KNOWN_SUPPORTED_MAC_ALG)
1942 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1943 smoke_test_key_data,
1944 sizeof( smoke_test_key_data ),
1945 KNOWN_SUPPORTED_MAC_ALG,
1946 &operation, &status ) )
1947 goto exit;
1948 TEST_EQUAL( status, PSA_SUCCESS );
1949#endif
1950
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001951exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001952 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001953}
1954/* END_CASE */
1955
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001956/* 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 +00001957void mac_bad_order( )
1958{
Ronald Cron5425a212020-08-04 14:58:35 +02001959 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001960 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1961 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001962 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001963 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1964 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1965 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001966 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001967 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1968 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1969 size_t sign_mac_length = 0;
1970 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1971 const uint8_t verify_mac[] = {
1972 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1973 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1974 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1975
1976 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001977 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001978 psa_set_key_algorithm( &attributes, alg );
1979 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001980
Ronald Cron5425a212020-08-04 14:58:35 +02001981 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1982 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001983
Jaeden Amero252ef282019-02-15 14:05:35 +00001984 /* Call update without calling setup beforehand. */
1985 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1986 PSA_ERROR_BAD_STATE );
1987 PSA_ASSERT( psa_mac_abort( &operation ) );
1988
1989 /* Call sign finish without calling setup beforehand. */
1990 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1991 &sign_mac_length),
1992 PSA_ERROR_BAD_STATE );
1993 PSA_ASSERT( psa_mac_abort( &operation ) );
1994
1995 /* Call verify finish without calling setup beforehand. */
1996 TEST_EQUAL( psa_mac_verify_finish( &operation,
1997 verify_mac, sizeof( verify_mac ) ),
1998 PSA_ERROR_BAD_STATE );
1999 PSA_ASSERT( psa_mac_abort( &operation ) );
2000
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002001 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002002 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2003 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002004 PSA_ERROR_BAD_STATE );
2005 PSA_ASSERT( psa_mac_abort( &operation ) );
2006
Jaeden Amero252ef282019-02-15 14:05:35 +00002007 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002008 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002009 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2010 PSA_ASSERT( psa_mac_sign_finish( &operation,
2011 sign_mac, sizeof( sign_mac ),
2012 &sign_mac_length ) );
2013 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2014 PSA_ERROR_BAD_STATE );
2015 PSA_ASSERT( psa_mac_abort( &operation ) );
2016
2017 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002018 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002019 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2020 PSA_ASSERT( psa_mac_verify_finish( &operation,
2021 verify_mac, sizeof( verify_mac ) ) );
2022 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2023 PSA_ERROR_BAD_STATE );
2024 PSA_ASSERT( psa_mac_abort( &operation ) );
2025
2026 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002027 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002028 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2029 PSA_ASSERT( psa_mac_sign_finish( &operation,
2030 sign_mac, sizeof( sign_mac ),
2031 &sign_mac_length ) );
2032 TEST_EQUAL( psa_mac_sign_finish( &operation,
2033 sign_mac, sizeof( sign_mac ),
2034 &sign_mac_length ),
2035 PSA_ERROR_BAD_STATE );
2036 PSA_ASSERT( psa_mac_abort( &operation ) );
2037
2038 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002039 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002040 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2041 PSA_ASSERT( psa_mac_verify_finish( &operation,
2042 verify_mac, sizeof( verify_mac ) ) );
2043 TEST_EQUAL( psa_mac_verify_finish( &operation,
2044 verify_mac, sizeof( verify_mac ) ),
2045 PSA_ERROR_BAD_STATE );
2046 PSA_ASSERT( psa_mac_abort( &operation ) );
2047
2048 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002049 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002050 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2051 TEST_EQUAL( psa_mac_verify_finish( &operation,
2052 verify_mac, sizeof( verify_mac ) ),
2053 PSA_ERROR_BAD_STATE );
2054 PSA_ASSERT( psa_mac_abort( &operation ) );
2055
2056 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002057 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002058 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2059 TEST_EQUAL( psa_mac_sign_finish( &operation,
2060 sign_mac, sizeof( sign_mac ),
2061 &sign_mac_length ),
2062 PSA_ERROR_BAD_STATE );
2063 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002064
Ronald Cron5425a212020-08-04 14:58:35 +02002065 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002066
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002067exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002068 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002069}
2070/* END_CASE */
2071
2072/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002073void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002074 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002075 int alg_arg,
2076 data_t *input,
2077 data_t *expected_mac )
2078{
Ronald Cron5425a212020-08-04 14:58:35 +02002079 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002080 psa_key_type_t key_type = key_type_arg;
2081 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002082 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002083 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002084 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002085 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002086 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002087 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002088 const size_t output_sizes_to_test[] = {
2089 0,
2090 1,
2091 expected_mac->len - 1,
2092 expected_mac->len,
2093 expected_mac->len + 1,
2094 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002095
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002096 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002097 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002098 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002099
Gilles Peskine8817f612018-12-18 00:18:46 +01002100 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002101
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002102 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002103 psa_set_key_algorithm( &attributes, alg );
2104 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002105
Ronald Cron5425a212020-08-04 14:58:35 +02002106 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2107 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002108
Gilles Peskine8b356b52020-08-25 23:44:59 +02002109 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2110 {
2111 const size_t output_size = output_sizes_to_test[i];
2112 psa_status_t expected_status =
2113 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2114 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002115
Chris Jones9634bb12021-01-20 15:56:42 +00002116 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002117 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002118
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002119 /* Calculate the MAC, one-shot case. */
2120 TEST_EQUAL( psa_mac_compute( key, alg,
2121 input->x, input->len,
2122 actual_mac, output_size, &mac_length ),
2123 expected_status );
2124 if( expected_status == PSA_SUCCESS )
2125 {
2126 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2127 actual_mac, mac_length );
2128 }
2129
2130 if( output_size > 0 )
2131 memset( actual_mac, 0, output_size );
2132
2133 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002134 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002135 PSA_ASSERT( psa_mac_update( &operation,
2136 input->x, input->len ) );
2137 TEST_EQUAL( psa_mac_sign_finish( &operation,
2138 actual_mac, output_size,
2139 &mac_length ),
2140 expected_status );
2141 PSA_ASSERT( psa_mac_abort( &operation ) );
2142
2143 if( expected_status == PSA_SUCCESS )
2144 {
2145 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2146 actual_mac, mac_length );
2147 }
2148 mbedtls_free( actual_mac );
2149 actual_mac = NULL;
2150 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002151
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002152exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002153 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002154 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002155 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002156 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002157}
2158/* END_CASE */
2159
2160/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002161void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002162 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002163 int alg_arg,
2164 data_t *input,
2165 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002166{
Ronald Cron5425a212020-08-04 14:58:35 +02002167 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002168 psa_key_type_t key_type = key_type_arg;
2169 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002170 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002171 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002172 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002173
Gilles Peskine69c12672018-06-28 00:07:19 +02002174 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2175
Gilles Peskine8817f612018-12-18 00:18:46 +01002176 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002177
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002178 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002179 psa_set_key_algorithm( &attributes, alg );
2180 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002181
Ronald Cron5425a212020-08-04 14:58:35 +02002182 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2183 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002184
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002185 /* Verify correct MAC, one-shot case. */
2186 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2187 expected_mac->x, expected_mac->len ) );
2188
2189 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002190 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002191 PSA_ASSERT( psa_mac_update( &operation,
2192 input->x, input->len ) );
2193 PSA_ASSERT( psa_mac_verify_finish( &operation,
2194 expected_mac->x,
2195 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002196
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002197 /* Test a MAC that's too short, one-shot case. */
2198 TEST_EQUAL( psa_mac_verify( key, alg,
2199 input->x, input->len,
2200 expected_mac->x,
2201 expected_mac->len - 1 ),
2202 PSA_ERROR_INVALID_SIGNATURE );
2203
2204 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002205 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002206 PSA_ASSERT( psa_mac_update( &operation,
2207 input->x, input->len ) );
2208 TEST_EQUAL( psa_mac_verify_finish( &operation,
2209 expected_mac->x,
2210 expected_mac->len - 1 ),
2211 PSA_ERROR_INVALID_SIGNATURE );
2212
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002213 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002214 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2215 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002216 TEST_EQUAL( psa_mac_verify( key, alg,
2217 input->x, input->len,
2218 perturbed_mac, expected_mac->len + 1 ),
2219 PSA_ERROR_INVALID_SIGNATURE );
2220
2221 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002222 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002223 PSA_ASSERT( psa_mac_update( &operation,
2224 input->x, input->len ) );
2225 TEST_EQUAL( psa_mac_verify_finish( &operation,
2226 perturbed_mac,
2227 expected_mac->len + 1 ),
2228 PSA_ERROR_INVALID_SIGNATURE );
2229
2230 /* Test changing one byte. */
2231 for( size_t i = 0; i < expected_mac->len; i++ )
2232 {
Chris Jones9634bb12021-01-20 15:56:42 +00002233 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002234 perturbed_mac[i] ^= 1;
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002235
2236 TEST_EQUAL( psa_mac_verify( key, alg,
2237 input->x, input->len,
2238 perturbed_mac, expected_mac->len ),
2239 PSA_ERROR_INVALID_SIGNATURE );
2240
Ronald Cron5425a212020-08-04 14:58:35 +02002241 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002242 PSA_ASSERT( psa_mac_update( &operation,
2243 input->x, input->len ) );
2244 TEST_EQUAL( psa_mac_verify_finish( &operation,
2245 perturbed_mac,
2246 expected_mac->len ),
2247 PSA_ERROR_INVALID_SIGNATURE );
2248 perturbed_mac[i] ^= 1;
2249 }
2250
Gilles Peskine8c9def32018-02-08 10:02:12 +01002251exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002252 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002253 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002254 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002255 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002256}
2257/* END_CASE */
2258
2259/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002260void cipher_operation_init( )
2261{
Jaeden Ameroab439972019-02-15 14:12:05 +00002262 const uint8_t input[1] = { 0 };
2263 unsigned char output[1] = { 0 };
2264 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002265 /* Test each valid way of initializing the object, except for `= {0}`, as
2266 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2267 * though it's OK by the C standard. We could test for this, but we'd need
2268 * to supress the Clang warning for the test. */
2269 psa_cipher_operation_t func = psa_cipher_operation_init( );
2270 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2271 psa_cipher_operation_t zero;
2272
2273 memset( &zero, 0, sizeof( zero ) );
2274
Jaeden Ameroab439972019-02-15 14:12:05 +00002275 /* A freshly-initialized cipher operation should not be usable. */
2276 TEST_EQUAL( psa_cipher_update( &func,
2277 input, sizeof( input ),
2278 output, sizeof( output ),
2279 &output_length ),
2280 PSA_ERROR_BAD_STATE );
2281 TEST_EQUAL( psa_cipher_update( &init,
2282 input, sizeof( input ),
2283 output, sizeof( output ),
2284 &output_length ),
2285 PSA_ERROR_BAD_STATE );
2286 TEST_EQUAL( psa_cipher_update( &zero,
2287 input, sizeof( input ),
2288 output, sizeof( output ),
2289 &output_length ),
2290 PSA_ERROR_BAD_STATE );
2291
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002292 /* A default cipher operation should be abortable without error. */
2293 PSA_ASSERT( psa_cipher_abort( &func ) );
2294 PSA_ASSERT( psa_cipher_abort( &init ) );
2295 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002296}
2297/* END_CASE */
2298
2299/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002300void cipher_setup( int key_type_arg,
2301 data_t *key,
2302 int alg_arg,
2303 int expected_status_arg )
2304{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002305 psa_key_type_t key_type = key_type_arg;
2306 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002307 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002308 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002309 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002310#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002311 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2312#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002313
Gilles Peskine8817f612018-12-18 00:18:46 +01002314 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002315
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002316 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2317 &operation, &status ) )
2318 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002319 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002320
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002321 /* The operation object should be reusable. */
2322#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2323 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2324 smoke_test_key_data,
2325 sizeof( smoke_test_key_data ),
2326 KNOWN_SUPPORTED_CIPHER_ALG,
2327 &operation, &status ) )
2328 goto exit;
2329 TEST_EQUAL( status, PSA_SUCCESS );
2330#endif
2331
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002332exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002333 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002334 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002335}
2336/* END_CASE */
2337
Ronald Cronee414c72021-03-18 18:50:08 +01002338/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002339void cipher_bad_order( )
2340{
Ronald Cron5425a212020-08-04 14:58:35 +02002341 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002342 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2343 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002344 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002345 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002346 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002347 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002348 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2349 0xaa, 0xaa, 0xaa, 0xaa };
2350 const uint8_t text[] = {
2351 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2352 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002353 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002354 size_t length = 0;
2355
2356 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002357 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2358 psa_set_key_algorithm( &attributes, alg );
2359 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002360 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2361 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002362
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002363 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002364 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2365 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002366 PSA_ERROR_BAD_STATE );
2367 PSA_ASSERT( psa_cipher_abort( &operation ) );
2368
2369 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002370 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2371 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002372 PSA_ERROR_BAD_STATE );
2373 PSA_ASSERT( psa_cipher_abort( &operation ) );
2374
Jaeden Ameroab439972019-02-15 14:12:05 +00002375 /* Generate an IV without calling setup beforehand. */
2376 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2377 buffer, sizeof( buffer ),
2378 &length ),
2379 PSA_ERROR_BAD_STATE );
2380 PSA_ASSERT( psa_cipher_abort( &operation ) );
2381
2382 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002383 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002384 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2385 buffer, sizeof( buffer ),
2386 &length ) );
2387 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2388 buffer, sizeof( buffer ),
2389 &length ),
2390 PSA_ERROR_BAD_STATE );
2391 PSA_ASSERT( psa_cipher_abort( &operation ) );
2392
2393 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002394 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002395 PSA_ASSERT( psa_cipher_set_iv( &operation,
2396 iv, sizeof( iv ) ) );
2397 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2398 buffer, sizeof( buffer ),
2399 &length ),
2400 PSA_ERROR_BAD_STATE );
2401 PSA_ASSERT( psa_cipher_abort( &operation ) );
2402
2403 /* Set an IV without calling setup beforehand. */
2404 TEST_EQUAL( psa_cipher_set_iv( &operation,
2405 iv, sizeof( iv ) ),
2406 PSA_ERROR_BAD_STATE );
2407 PSA_ASSERT( psa_cipher_abort( &operation ) );
2408
2409 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002410 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002411 PSA_ASSERT( psa_cipher_set_iv( &operation,
2412 iv, sizeof( iv ) ) );
2413 TEST_EQUAL( psa_cipher_set_iv( &operation,
2414 iv, sizeof( iv ) ),
2415 PSA_ERROR_BAD_STATE );
2416 PSA_ASSERT( psa_cipher_abort( &operation ) );
2417
2418 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002419 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002420 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2421 buffer, sizeof( buffer ),
2422 &length ) );
2423 TEST_EQUAL( psa_cipher_set_iv( &operation,
2424 iv, sizeof( iv ) ),
2425 PSA_ERROR_BAD_STATE );
2426 PSA_ASSERT( psa_cipher_abort( &operation ) );
2427
2428 /* Call update without calling setup beforehand. */
2429 TEST_EQUAL( psa_cipher_update( &operation,
2430 text, sizeof( text ),
2431 buffer, sizeof( buffer ),
2432 &length ),
2433 PSA_ERROR_BAD_STATE );
2434 PSA_ASSERT( psa_cipher_abort( &operation ) );
2435
2436 /* Call update without an IV where an IV is required. */
2437 TEST_EQUAL( psa_cipher_update( &operation,
2438 text, sizeof( text ),
2439 buffer, sizeof( buffer ),
2440 &length ),
2441 PSA_ERROR_BAD_STATE );
2442 PSA_ASSERT( psa_cipher_abort( &operation ) );
2443
2444 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002445 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002446 PSA_ASSERT( psa_cipher_set_iv( &operation,
2447 iv, sizeof( iv ) ) );
2448 PSA_ASSERT( psa_cipher_finish( &operation,
2449 buffer, sizeof( buffer ), &length ) );
2450 TEST_EQUAL( psa_cipher_update( &operation,
2451 text, sizeof( text ),
2452 buffer, sizeof( buffer ),
2453 &length ),
2454 PSA_ERROR_BAD_STATE );
2455 PSA_ASSERT( psa_cipher_abort( &operation ) );
2456
2457 /* Call finish without calling setup beforehand. */
2458 TEST_EQUAL( psa_cipher_finish( &operation,
2459 buffer, sizeof( buffer ), &length ),
2460 PSA_ERROR_BAD_STATE );
2461 PSA_ASSERT( psa_cipher_abort( &operation ) );
2462
2463 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002464 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002465 /* Not calling update means we are encrypting an empty buffer, which is OK
2466 * for cipher modes with padding. */
2467 TEST_EQUAL( psa_cipher_finish( &operation,
2468 buffer, sizeof( buffer ), &length ),
2469 PSA_ERROR_BAD_STATE );
2470 PSA_ASSERT( psa_cipher_abort( &operation ) );
2471
2472 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002473 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002474 PSA_ASSERT( psa_cipher_set_iv( &operation,
2475 iv, sizeof( iv ) ) );
2476 PSA_ASSERT( psa_cipher_finish( &operation,
2477 buffer, sizeof( buffer ), &length ) );
2478 TEST_EQUAL( psa_cipher_finish( &operation,
2479 buffer, sizeof( buffer ), &length ),
2480 PSA_ERROR_BAD_STATE );
2481 PSA_ASSERT( psa_cipher_abort( &operation ) );
2482
Ronald Cron5425a212020-08-04 14:58:35 +02002483 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002484
Jaeden Ameroab439972019-02-15 14:12:05 +00002485exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002486 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002487 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002488}
2489/* END_CASE */
2490
2491/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002492void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002493 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002494 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002495 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002496{
Ronald Cron5425a212020-08-04 14:58:35 +02002497 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002498 psa_status_t status;
2499 psa_key_type_t key_type = key_type_arg;
2500 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002501 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002502 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002503 size_t output_buffer_size = 0;
2504 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002505 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002506 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002507 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002508
Gilles Peskine8817f612018-12-18 00:18:46 +01002509 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002510
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002511 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2512 psa_set_key_algorithm( &attributes, alg );
2513 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002514
Ronald Cron5425a212020-08-04 14:58:35 +02002515 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2516 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002517
Ronald Cron5425a212020-08-04 14:58:35 +02002518 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002519
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002520 if( iv->len > 0 )
2521 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002522 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002523 }
2524
gabor-mezei-armceface22021-01-21 12:26:17 +01002525 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2526 TEST_ASSERT( output_buffer_size <=
2527 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002528 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002529
Gilles Peskine8817f612018-12-18 00:18:46 +01002530 PSA_ASSERT( psa_cipher_update( &operation,
2531 input->x, input->len,
2532 output, output_buffer_size,
2533 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002534 TEST_ASSERT( function_output_length <=
2535 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2536 TEST_ASSERT( function_output_length <=
2537 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002538 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002539
Gilles Peskine50e586b2018-06-08 14:28:46 +02002540 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002541 ( output_buffer_size == 0 ? NULL :
2542 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002543 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002544 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002545 TEST_ASSERT( function_output_length <=
2546 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2547 TEST_ASSERT( function_output_length <=
2548 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002549 total_output_length += function_output_length;
2550
Gilles Peskinefe11b722018-12-18 00:24:04 +01002551 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002552 if( expected_status == PSA_SUCCESS )
2553 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002554 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002555 ASSERT_COMPARE( expected_output->x, expected_output->len,
2556 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002557 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002558
Gilles Peskine50e586b2018-06-08 14:28:46 +02002559exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002560 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002561 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002562 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002563 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002564}
2565/* END_CASE */
2566
2567/* BEGIN_CASE */
2568void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002569 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002570 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002571 int first_part_size_arg,
2572 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002573 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002574{
Ronald Cron5425a212020-08-04 14:58:35 +02002575 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002576 psa_key_type_t key_type = key_type_arg;
2577 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002578 size_t first_part_size = first_part_size_arg;
2579 size_t output1_length = output1_length_arg;
2580 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002581 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002582 size_t output_buffer_size = 0;
2583 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002584 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002585 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002586 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002587
Gilles Peskine8817f612018-12-18 00:18:46 +01002588 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002589
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002590 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2591 psa_set_key_algorithm( &attributes, alg );
2592 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002593
Ronald Cron5425a212020-08-04 14:58:35 +02002594 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2595 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002596
Ronald Cron5425a212020-08-04 14:58:35 +02002597 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002598
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002599 if( iv->len > 0 )
2600 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002601 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002602 }
2603
gabor-mezei-armceface22021-01-21 12:26:17 +01002604 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2605 TEST_ASSERT( output_buffer_size <=
2606 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002607 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002608
Gilles Peskinee0866522019-02-19 19:44:00 +01002609 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002610 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2611 output, output_buffer_size,
2612 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002613 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002614 TEST_ASSERT( function_output_length <=
2615 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2616 TEST_ASSERT( function_output_length <=
2617 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002618 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002619
Gilles Peskine8817f612018-12-18 00:18:46 +01002620 PSA_ASSERT( psa_cipher_update( &operation,
2621 input->x + first_part_size,
2622 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002623 ( output_buffer_size == 0 ? NULL :
2624 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002625 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002626 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002627 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002628 TEST_ASSERT( function_output_length <=
2629 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2630 alg,
2631 input->len - first_part_size ) );
2632 TEST_ASSERT( function_output_length <=
2633 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002634 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002635
Gilles Peskine8817f612018-12-18 00:18:46 +01002636 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002637 ( output_buffer_size == 0 ? NULL :
2638 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002639 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002640 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002641 TEST_ASSERT( function_output_length <=
2642 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2643 TEST_ASSERT( function_output_length <=
2644 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002645 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002646 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002647
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002648 ASSERT_COMPARE( expected_output->x, expected_output->len,
2649 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002650
2651exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002652 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002653 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002654 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002655 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002656}
2657/* END_CASE */
2658
2659/* BEGIN_CASE */
2660void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002661 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002662 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002663 int first_part_size_arg,
2664 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002665 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002666{
Ronald Cron5425a212020-08-04 14:58:35 +02002667 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002668 psa_key_type_t key_type = key_type_arg;
2669 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002670 size_t first_part_size = first_part_size_arg;
2671 size_t output1_length = output1_length_arg;
2672 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002673 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002674 size_t output_buffer_size = 0;
2675 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002676 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002677 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002678 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002679
Gilles Peskine8817f612018-12-18 00:18:46 +01002680 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002681
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002682 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2683 psa_set_key_algorithm( &attributes, alg );
2684 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002685
Ronald Cron5425a212020-08-04 14:58:35 +02002686 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2687 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002688
Ronald Cron5425a212020-08-04 14:58:35 +02002689 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002690
Steven Cooreman177deba2020-09-07 17:14:14 +02002691 if( iv->len > 0 )
2692 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002693 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002694 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002695
gabor-mezei-armceface22021-01-21 12:26:17 +01002696 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2697 TEST_ASSERT( output_buffer_size <=
2698 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002699 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002700
Gilles Peskinee0866522019-02-19 19:44:00 +01002701 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002702 PSA_ASSERT( psa_cipher_update( &operation,
2703 input->x, first_part_size,
2704 output, output_buffer_size,
2705 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002706 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002707 TEST_ASSERT( function_output_length <=
2708 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2709 TEST_ASSERT( function_output_length <=
2710 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002711 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002712
Gilles Peskine8817f612018-12-18 00:18:46 +01002713 PSA_ASSERT( psa_cipher_update( &operation,
2714 input->x + first_part_size,
2715 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002716 ( output_buffer_size == 0 ? NULL :
2717 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002718 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002719 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002720 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002721 TEST_ASSERT( function_output_length <=
2722 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2723 alg,
2724 input->len - first_part_size ) );
2725 TEST_ASSERT( function_output_length <=
2726 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002727 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002728
Gilles Peskine8817f612018-12-18 00:18:46 +01002729 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002730 ( output_buffer_size == 0 ? NULL :
2731 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002732 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002733 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002734 TEST_ASSERT( function_output_length <=
2735 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2736 TEST_ASSERT( function_output_length <=
2737 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002738 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002739 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002740
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002741 ASSERT_COMPARE( expected_output->x, expected_output->len,
2742 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002743
2744exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002745 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002746 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002747 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002748 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002749}
2750/* END_CASE */
2751
Gilles Peskine50e586b2018-06-08 14:28:46 +02002752/* BEGIN_CASE */
2753void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002754 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002755 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002756 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002757{
Ronald Cron5425a212020-08-04 14:58:35 +02002758 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002759 psa_status_t status;
2760 psa_key_type_t key_type = key_type_arg;
2761 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002762 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002763 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002764 size_t output_buffer_size = 0;
2765 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002766 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002767 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002768 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002769
Gilles Peskine8817f612018-12-18 00:18:46 +01002770 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002771
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002772 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2773 psa_set_key_algorithm( &attributes, alg );
2774 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002775
Ronald Cron5425a212020-08-04 14:58:35 +02002776 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2777 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002778
Ronald Cron5425a212020-08-04 14:58:35 +02002779 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002780
Steven Cooreman177deba2020-09-07 17:14:14 +02002781 if( iv->len > 0 )
2782 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002783 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002784 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002785
gabor-mezei-armceface22021-01-21 12:26:17 +01002786 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2787 TEST_ASSERT( output_buffer_size <=
2788 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002789 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002790
Gilles Peskine8817f612018-12-18 00:18:46 +01002791 PSA_ASSERT( psa_cipher_update( &operation,
2792 input->x, input->len,
2793 output, output_buffer_size,
2794 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002795 TEST_ASSERT( function_output_length <=
2796 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2797 TEST_ASSERT( function_output_length <=
2798 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002799 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002800
Gilles Peskine50e586b2018-06-08 14:28:46 +02002801 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002802 ( output_buffer_size == 0 ? NULL :
2803 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002804 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002805 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002806 TEST_ASSERT( function_output_length <=
2807 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2808 TEST_ASSERT( function_output_length <=
2809 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002810 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002811 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002812
2813 if( expected_status == PSA_SUCCESS )
2814 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002815 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002816 ASSERT_COMPARE( expected_output->x, expected_output->len,
2817 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002818 }
2819
Gilles Peskine50e586b2018-06-08 14:28:46 +02002820exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002821 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002822 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002823 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002824 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002825}
2826/* END_CASE */
2827
Gilles Peskine50e586b2018-06-08 14:28:46 +02002828/* BEGIN_CASE */
2829void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002830 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002831 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002832{
Ronald Cron5425a212020-08-04 14:58:35 +02002833 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002834 psa_key_type_t key_type = key_type_arg;
2835 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002836 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002837 size_t iv_size = 16;
2838 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002839 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002840 size_t output1_size = 0;
2841 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002842 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002843 size_t output2_size = 0;
2844 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002845 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002846 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2847 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002848 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002849
Gilles Peskine8817f612018-12-18 00:18:46 +01002850 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002851
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002852 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2853 psa_set_key_algorithm( &attributes, alg );
2854 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002855
Ronald Cron5425a212020-08-04 14:58:35 +02002856 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2857 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002858
Ronald Cron5425a212020-08-04 14:58:35 +02002859 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2860 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002861
Steven Cooreman177deba2020-09-07 17:14:14 +02002862 if( alg != PSA_ALG_ECB_NO_PADDING )
2863 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002864 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2865 iv, iv_size,
2866 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002867 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002868 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2869 TEST_ASSERT( output1_size <=
2870 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002871 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002872
Gilles Peskine8817f612018-12-18 00:18:46 +01002873 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2874 output1, output1_size,
2875 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002876 TEST_ASSERT( output1_length <=
2877 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2878 TEST_ASSERT( output1_length <=
2879 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2880
Gilles Peskine8817f612018-12-18 00:18:46 +01002881 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002882 output1 + output1_length,
2883 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002884 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002885 TEST_ASSERT( function_output_length <=
2886 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2887 TEST_ASSERT( function_output_length <=
2888 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002889
Gilles Peskine048b7f02018-06-08 14:20:49 +02002890 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002891
Gilles Peskine8817f612018-12-18 00:18:46 +01002892 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002893
2894 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002895 TEST_ASSERT( output2_size <=
2896 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2897 TEST_ASSERT( output2_size <=
2898 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002899 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002900
Steven Cooreman177deba2020-09-07 17:14:14 +02002901 if( iv_length > 0 )
2902 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002903 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2904 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002905 }
2906
Gilles Peskine8817f612018-12-18 00:18:46 +01002907 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2908 output2, output2_size,
2909 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002910 TEST_ASSERT( output2_length <=
2911 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
2912 TEST_ASSERT( output2_length <=
2913 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
2914
Gilles Peskine048b7f02018-06-08 14:20:49 +02002915 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002916 PSA_ASSERT( psa_cipher_finish( &operation2,
2917 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002918 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002919 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002920 TEST_ASSERT( function_output_length <=
2921 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2922 TEST_ASSERT( function_output_length <=
2923 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03002924
Gilles Peskine048b7f02018-06-08 14:20:49 +02002925 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002926
Gilles Peskine8817f612018-12-18 00:18:46 +01002927 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002928
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002929 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002930
2931exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002932 psa_cipher_abort( &operation1 );
2933 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002934 mbedtls_free( output1 );
2935 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002936 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002937 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03002938}
2939/* END_CASE */
2940
2941/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002942void cipher_verify_output_multipart( int alg_arg,
2943 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002944 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002945 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002946 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03002947{
Ronald Cron5425a212020-08-04 14:58:35 +02002948 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002949 psa_key_type_t key_type = key_type_arg;
2950 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002951 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002952 unsigned char iv[16] = {0};
2953 size_t iv_size = 16;
2954 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002955 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002956 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002957 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002958 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002959 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002960 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002961 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002962 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2963 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002964 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002965
Gilles Peskine8817f612018-12-18 00:18:46 +01002966 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002967
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002968 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2969 psa_set_key_algorithm( &attributes, alg );
2970 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002971
Ronald Cron5425a212020-08-04 14:58:35 +02002972 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2973 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03002974
Ronald Cron5425a212020-08-04 14:58:35 +02002975 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2976 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002977
Steven Cooreman177deba2020-09-07 17:14:14 +02002978 if( alg != PSA_ALG_ECB_NO_PADDING )
2979 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002980 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2981 iv, iv_size,
2982 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002983 }
2984
gabor-mezei-armceface22021-01-21 12:26:17 +01002985 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2986 TEST_ASSERT( output1_buffer_size <=
2987 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002988 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002989
Gilles Peskinee0866522019-02-19 19:44:00 +01002990 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002991
Gilles Peskine8817f612018-12-18 00:18:46 +01002992 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2993 output1, output1_buffer_size,
2994 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002995 TEST_ASSERT( function_output_length <=
2996 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2997 TEST_ASSERT( function_output_length <=
2998 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002999 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003000
Gilles Peskine8817f612018-12-18 00:18:46 +01003001 PSA_ASSERT( psa_cipher_update( &operation1,
3002 input->x + first_part_size,
3003 input->len - first_part_size,
3004 output1, output1_buffer_size,
3005 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003006 TEST_ASSERT( function_output_length <=
3007 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3008 alg,
3009 input->len - first_part_size ) );
3010 TEST_ASSERT( function_output_length <=
3011 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003012 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003013
Gilles Peskine8817f612018-12-18 00:18:46 +01003014 PSA_ASSERT( psa_cipher_finish( &operation1,
3015 output1 + output1_length,
3016 output1_buffer_size - output1_length,
3017 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003018 TEST_ASSERT( function_output_length <=
3019 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3020 TEST_ASSERT( function_output_length <=
3021 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003022 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003023
Gilles Peskine8817f612018-12-18 00:18:46 +01003024 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003025
Gilles Peskine048b7f02018-06-08 14:20:49 +02003026 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003027 TEST_ASSERT( output2_buffer_size <=
3028 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3029 TEST_ASSERT( output2_buffer_size <=
3030 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003031 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003032
Steven Cooreman177deba2020-09-07 17:14:14 +02003033 if( iv_length > 0 )
3034 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003035 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3036 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003037 }
Moran Pekerded84402018-06-06 16:36:50 +03003038
Gilles Peskine8817f612018-12-18 00:18:46 +01003039 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3040 output2, output2_buffer_size,
3041 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003042 TEST_ASSERT( function_output_length <=
3043 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3044 TEST_ASSERT( function_output_length <=
3045 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003046 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003047
Gilles Peskine8817f612018-12-18 00:18:46 +01003048 PSA_ASSERT( psa_cipher_update( &operation2,
3049 output1 + first_part_size,
3050 output1_length - first_part_size,
3051 output2, output2_buffer_size,
3052 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003053 TEST_ASSERT( function_output_length <=
3054 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3055 alg,
3056 output1_length - first_part_size ) );
3057 TEST_ASSERT( function_output_length <=
3058 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003059 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003060
Gilles Peskine8817f612018-12-18 00:18:46 +01003061 PSA_ASSERT( psa_cipher_finish( &operation2,
3062 output2 + output2_length,
3063 output2_buffer_size - output2_length,
3064 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003065 TEST_ASSERT( function_output_length <=
3066 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3067 TEST_ASSERT( function_output_length <=
3068 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003069 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003070
Gilles Peskine8817f612018-12-18 00:18:46 +01003071 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003072
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003073 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003074
3075exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003076 psa_cipher_abort( &operation1 );
3077 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003078 mbedtls_free( output1 );
3079 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003080 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003081 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003082}
3083/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003084
Gilles Peskine20035e32018-02-03 22:44:14 +01003085/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003086void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003087 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003088 data_t *nonce,
3089 data_t *additional_data,
3090 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003091 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003092{
Ronald Cron5425a212020-08-04 14:58:35 +02003093 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003094 psa_key_type_t key_type = key_type_arg;
3095 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003096 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003097 unsigned char *output_data = NULL;
3098 size_t output_size = 0;
3099 size_t output_length = 0;
3100 unsigned char *output_data2 = NULL;
3101 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003102 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003103 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003104 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003105
Gilles Peskine8817f612018-12-18 00:18:46 +01003106 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003107
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003108 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3109 psa_set_key_algorithm( &attributes, alg );
3110 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003111
Gilles Peskine049c7532019-05-15 20:22:09 +02003112 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003113 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003114 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3115 key_bits = psa_get_key_bits( &attributes );
3116
3117 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3118 alg );
3119 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3120 * should be exact. */
3121 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3122 expected_result != PSA_ERROR_NOT_SUPPORTED )
3123 {
3124 TEST_EQUAL( output_size,
3125 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3126 TEST_ASSERT( output_size <=
3127 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3128 }
3129 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003130
Steven Cooremanf49478b2021-02-15 15:19:25 +01003131 status = psa_aead_encrypt( key, alg,
3132 nonce->x, nonce->len,
3133 additional_data->x,
3134 additional_data->len,
3135 input_data->x, input_data->len,
3136 output_data, output_size,
3137 &output_length );
3138
3139 /* If the operation is not supported, just skip and not fail in case the
3140 * encryption involves a common limitation of cryptography hardwares and
3141 * an alternative implementation. */
3142 if( status == PSA_ERROR_NOT_SUPPORTED )
3143 {
3144 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3145 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3146 }
3147
3148 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003149
3150 if( PSA_SUCCESS == expected_result )
3151 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003152 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003153
Gilles Peskine003a4a92019-05-14 16:09:40 +02003154 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3155 * should be exact. */
3156 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003157 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003158
gabor-mezei-armceface22021-01-21 12:26:17 +01003159 TEST_ASSERT( input_data->len <=
3160 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3161
Ronald Cron5425a212020-08-04 14:58:35 +02003162 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003163 nonce->x, nonce->len,
3164 additional_data->x,
3165 additional_data->len,
3166 output_data, output_length,
3167 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003168 &output_length2 ),
3169 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003170
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003171 ASSERT_COMPARE( input_data->x, input_data->len,
3172 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003173 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003174
Gilles Peskinea1cac842018-06-11 19:33:02 +02003175exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003176 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003177 mbedtls_free( output_data );
3178 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003179 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003180}
3181/* END_CASE */
3182
3183/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003184void aead_encrypt( int key_type_arg, data_t *key_data,
3185 int alg_arg,
3186 data_t *nonce,
3187 data_t *additional_data,
3188 data_t *input_data,
3189 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003190{
Ronald Cron5425a212020-08-04 14:58:35 +02003191 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003192 psa_key_type_t key_type = key_type_arg;
3193 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003194 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003195 unsigned char *output_data = NULL;
3196 size_t output_size = 0;
3197 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003198 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003199 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003200
Gilles Peskine8817f612018-12-18 00:18:46 +01003201 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003202
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003203 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3204 psa_set_key_algorithm( &attributes, alg );
3205 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003206
Gilles Peskine049c7532019-05-15 20:22:09 +02003207 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003208 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003209 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3210 key_bits = psa_get_key_bits( &attributes );
3211
3212 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3213 alg );
3214 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3215 * should be exact. */
3216 TEST_EQUAL( output_size,
3217 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3218 TEST_ASSERT( output_size <=
3219 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3220 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003221
Steven Cooremand588ea12021-01-11 19:36:04 +01003222 status = psa_aead_encrypt( key, alg,
3223 nonce->x, nonce->len,
3224 additional_data->x, additional_data->len,
3225 input_data->x, input_data->len,
3226 output_data, output_size,
3227 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003228
Ronald Cron28a45ed2021-02-09 20:35:42 +01003229 /* If the operation is not supported, just skip and not fail in case the
3230 * encryption involves a common limitation of cryptography hardwares and
3231 * an alternative implementation. */
3232 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003233 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003234 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3235 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003236 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003237
3238 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003239 ASSERT_COMPARE( expected_result->x, expected_result->len,
3240 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003241
Gilles Peskinea1cac842018-06-11 19:33:02 +02003242exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003243 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003244 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003245 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003246}
3247/* END_CASE */
3248
3249/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003250void aead_decrypt( int key_type_arg, data_t *key_data,
3251 int alg_arg,
3252 data_t *nonce,
3253 data_t *additional_data,
3254 data_t *input_data,
3255 data_t *expected_data,
3256 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003257{
Ronald Cron5425a212020-08-04 14:58:35 +02003258 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003259 psa_key_type_t key_type = key_type_arg;
3260 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003261 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003262 unsigned char *output_data = NULL;
3263 size_t output_size = 0;
3264 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003265 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003266 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003267 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003268
Gilles Peskine8817f612018-12-18 00:18:46 +01003269 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003270
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003271 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3272 psa_set_key_algorithm( &attributes, alg );
3273 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003274
Gilles Peskine049c7532019-05-15 20:22:09 +02003275 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003276 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003277 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3278 key_bits = psa_get_key_bits( &attributes );
3279
3280 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3281 alg );
3282 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3283 expected_result != PSA_ERROR_NOT_SUPPORTED )
3284 {
3285 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3286 * should be exact. */
3287 TEST_EQUAL( output_size,
3288 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3289 TEST_ASSERT( output_size <=
3290 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3291 }
3292 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003293
Steven Cooremand588ea12021-01-11 19:36:04 +01003294 status = psa_aead_decrypt( key, alg,
3295 nonce->x, nonce->len,
3296 additional_data->x,
3297 additional_data->len,
3298 input_data->x, input_data->len,
3299 output_data, output_size,
3300 &output_length );
3301
Ronald Cron28a45ed2021-02-09 20:35:42 +01003302 /* If the operation is not supported, just skip and not fail in case the
3303 * decryption involves a common limitation of cryptography hardwares and
3304 * an alternative implementation. */
3305 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003306 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003307 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3308 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003309 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003310
3311 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003312
Gilles Peskine2d277862018-06-18 15:41:12 +02003313 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003314 ASSERT_COMPARE( expected_data->x, expected_data->len,
3315 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003316
Gilles Peskinea1cac842018-06-11 19:33:02 +02003317exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003318 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003319 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003320 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003321}
3322/* END_CASE */
3323
3324/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003325void signature_size( int type_arg,
3326 int bits,
3327 int alg_arg,
3328 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003329{
3330 psa_key_type_t type = type_arg;
3331 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003332 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003333
Gilles Peskinefe11b722018-12-18 00:24:04 +01003334 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003335#if defined(MBEDTLS_TEST_DEPRECATED)
3336 TEST_EQUAL( actual_size,
3337 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3338#endif /* MBEDTLS_TEST_DEPRECATED */
3339
Gilles Peskinee59236f2018-01-27 23:32:46 +01003340exit:
3341 ;
3342}
3343/* END_CASE */
3344
3345/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003346void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3347 int alg_arg, data_t *input_data,
3348 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003349{
Ronald Cron5425a212020-08-04 14:58:35 +02003350 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003351 psa_key_type_t key_type = key_type_arg;
3352 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003353 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003354 unsigned char *signature = NULL;
3355 size_t signature_size;
3356 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003357 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003358
Gilles Peskine8817f612018-12-18 00:18:46 +01003359 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003360
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003361 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003362 psa_set_key_algorithm( &attributes, alg );
3363 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003364
Gilles Peskine049c7532019-05-15 20:22:09 +02003365 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003366 &key ) );
3367 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003368 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003369
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003370 /* Allocate a buffer which has the size advertized by the
3371 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003372 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003373 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003374 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003375 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003376 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003377
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003378 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003379 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003380 input_data->x, input_data->len,
3381 signature, signature_size,
3382 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003383 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003384 ASSERT_COMPARE( output_data->x, output_data->len,
3385 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003386
Gilles Peskine0627f982019-11-26 19:12:16 +01003387#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003388 memset( signature, 0, signature_size );
3389 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003390 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003391 input_data->x, input_data->len,
3392 signature, signature_size,
3393 &signature_length ) );
3394 ASSERT_COMPARE( output_data->x, output_data->len,
3395 signature, signature_length );
3396#endif /* MBEDTLS_TEST_DEPRECATED */
3397
Gilles Peskine20035e32018-02-03 22:44:14 +01003398exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003399 /*
3400 * Key attributes may have been returned by psa_get_key_attributes()
3401 * thus reset them as required.
3402 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003403 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003404
Ronald Cron5425a212020-08-04 14:58:35 +02003405 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003406 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003407 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003408}
3409/* END_CASE */
3410
3411/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003412void sign_hash_fail( int key_type_arg, data_t *key_data,
3413 int alg_arg, data_t *input_data,
3414 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003415{
Ronald Cron5425a212020-08-04 14:58:35 +02003416 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003417 psa_key_type_t key_type = key_type_arg;
3418 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003419 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003420 psa_status_t actual_status;
3421 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003422 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003423 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003424 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003425
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003426 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003427
Gilles Peskine8817f612018-12-18 00:18:46 +01003428 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003429
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003430 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003431 psa_set_key_algorithm( &attributes, alg );
3432 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003433
Gilles Peskine049c7532019-05-15 20:22:09 +02003434 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003435 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003436
Ronald Cron5425a212020-08-04 14:58:35 +02003437 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003438 input_data->x, input_data->len,
3439 signature, signature_size,
3440 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003441 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003442 /* The value of *signature_length is unspecified on error, but
3443 * whatever it is, it should be less than signature_size, so that
3444 * if the caller tries to read *signature_length bytes without
3445 * checking the error code then they don't overflow a buffer. */
3446 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003447
Gilles Peskine895242b2019-11-29 12:15:40 +01003448#if defined(MBEDTLS_TEST_DEPRECATED)
3449 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003450 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003451 input_data->x, input_data->len,
3452 signature, signature_size,
3453 &signature_length ),
3454 expected_status );
3455 TEST_ASSERT( signature_length <= signature_size );
3456#endif /* MBEDTLS_TEST_DEPRECATED */
3457
Gilles Peskine20035e32018-02-03 22:44:14 +01003458exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003459 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003460 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003461 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003462 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003463}
3464/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003465
3466/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003467void sign_verify_hash( int key_type_arg, data_t *key_data,
3468 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003469{
Ronald Cron5425a212020-08-04 14:58:35 +02003470 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003471 psa_key_type_t key_type = key_type_arg;
3472 psa_algorithm_t alg = alg_arg;
3473 size_t key_bits;
3474 unsigned char *signature = NULL;
3475 size_t signature_size;
3476 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003477 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003478
Gilles Peskine8817f612018-12-18 00:18:46 +01003479 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003480
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003481 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003482 psa_set_key_algorithm( &attributes, alg );
3483 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003484
Gilles Peskine049c7532019-05-15 20:22:09 +02003485 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003486 &key ) );
3487 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003488 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003489
3490 /* Allocate a buffer which has the size advertized by the
3491 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003492 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003493 key_bits, alg );
3494 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003495 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003496 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003497
3498 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003499 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003500 input_data->x, input_data->len,
3501 signature, signature_size,
3502 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003503 /* Check that the signature length looks sensible. */
3504 TEST_ASSERT( signature_length <= signature_size );
3505 TEST_ASSERT( signature_length > 0 );
3506
3507 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003508 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003509 input_data->x, input_data->len,
3510 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003511
3512 if( input_data->len != 0 )
3513 {
3514 /* Flip a bit in the input and verify that the signature is now
3515 * detected as invalid. Flip a bit at the beginning, not at the end,
3516 * because ECDSA may ignore the last few bits of the input. */
3517 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003518 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003519 input_data->x, input_data->len,
3520 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003521 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003522 }
3523
3524exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003525 /*
3526 * Key attributes may have been returned by psa_get_key_attributes()
3527 * thus reset them as required.
3528 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003529 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003530
Ronald Cron5425a212020-08-04 14:58:35 +02003531 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003532 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003533 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003534}
3535/* END_CASE */
3536
3537/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003538void verify_hash( int key_type_arg, data_t *key_data,
3539 int alg_arg, data_t *hash_data,
3540 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003541{
Ronald Cron5425a212020-08-04 14:58:35 +02003542 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003543 psa_key_type_t key_type = key_type_arg;
3544 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003545 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003546
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003547 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003548
Gilles Peskine8817f612018-12-18 00:18:46 +01003549 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003550
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003551 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003552 psa_set_key_algorithm( &attributes, alg );
3553 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003554
Gilles Peskine049c7532019-05-15 20:22:09 +02003555 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003556 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003557
Ronald Cron5425a212020-08-04 14:58:35 +02003558 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003559 hash_data->x, hash_data->len,
3560 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003561
3562#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003563 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003564 hash_data->x, hash_data->len,
3565 signature_data->x,
3566 signature_data->len ) );
3567
3568#endif /* MBEDTLS_TEST_DEPRECATED */
3569
itayzafrir5c753392018-05-08 11:18:38 +03003570exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003571 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003572 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003573 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003574}
3575/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003576
3577/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003578void verify_hash_fail( int key_type_arg, data_t *key_data,
3579 int alg_arg, data_t *hash_data,
3580 data_t *signature_data,
3581 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003582{
Ronald Cron5425a212020-08-04 14:58:35 +02003583 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003584 psa_key_type_t key_type = key_type_arg;
3585 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003586 psa_status_t actual_status;
3587 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003588 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003589
Gilles Peskine8817f612018-12-18 00:18:46 +01003590 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003591
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003592 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003593 psa_set_key_algorithm( &attributes, alg );
3594 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003595
Gilles Peskine049c7532019-05-15 20:22:09 +02003596 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003597 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003598
Ronald Cron5425a212020-08-04 14:58:35 +02003599 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003600 hash_data->x, hash_data->len,
3601 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003602 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003603
Gilles Peskine895242b2019-11-29 12:15:40 +01003604#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003605 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003606 hash_data->x, hash_data->len,
3607 signature_data->x, signature_data->len ),
3608 expected_status );
3609#endif /* MBEDTLS_TEST_DEPRECATED */
3610
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003611exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003612 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003613 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003614 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003615}
3616/* END_CASE */
3617
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003618/* BEGIN_CASE */
gabor-mezei-armabd72582021-04-15 18:19:50 +02003619void sign_message_deterministic( int key_type_arg,
3620 data_t *key_data,
3621 int alg_arg,
3622 data_t *input_data,
3623 data_t *output_data )
3624{
3625 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3626 psa_key_type_t key_type = key_type_arg;
3627 psa_algorithm_t alg = alg_arg;
3628 size_t key_bits;
3629 unsigned char *signature = NULL;
3630 size_t signature_size;
3631 size_t signature_length = 0xdeadbeef;
3632 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3633
3634 PSA_ASSERT( psa_crypto_init( ) );
3635
3636 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3637 psa_set_key_algorithm( &attributes, alg );
3638 psa_set_key_type( &attributes, key_type );
3639
3640 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3641 &key ) );
3642 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3643 key_bits = psa_get_key_bits( &attributes );
3644
3645 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3646 TEST_ASSERT( signature_size != 0 );
3647 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3648 ASSERT_ALLOC( signature, signature_size );
3649
3650 PSA_ASSERT( psa_sign_message( key, alg,
3651 input_data->x, input_data->len,
3652 signature, signature_size,
3653 &signature_length ) );
3654
3655 ASSERT_COMPARE( output_data->x, output_data->len,
3656 signature, signature_length );
3657
3658exit:
3659 psa_reset_key_attributes( &attributes );
3660
3661 psa_destroy_key( key );
3662 mbedtls_free( signature );
3663 PSA_DONE( );
3664
3665}
3666/* END_CASE */
3667
3668/* BEGIN_CASE */
3669void sign_message_fail( int key_type_arg,
3670 data_t *key_data,
3671 int alg_arg,
3672 data_t *input_data,
3673 int signature_size_arg,
3674 int expected_status_arg )
3675{
3676 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3677 psa_key_type_t key_type = key_type_arg;
3678 psa_algorithm_t alg = alg_arg;
3679 size_t signature_size = signature_size_arg;
3680 psa_status_t actual_status;
3681 psa_status_t expected_status = expected_status_arg;
3682 unsigned char *signature = NULL;
3683 size_t signature_length = 0xdeadbeef;
3684 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3685
3686 ASSERT_ALLOC( signature, signature_size );
3687
3688 PSA_ASSERT( psa_crypto_init( ) );
3689
3690 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3691 psa_set_key_algorithm( &attributes, alg );
3692 psa_set_key_type( &attributes, key_type );
3693
3694 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3695 &key ) );
3696
3697 actual_status = psa_sign_message( key, alg,
3698 input_data->x, input_data->len,
3699 signature, signature_size,
3700 &signature_length );
3701 TEST_EQUAL( actual_status, expected_status );
3702 /* The value of *signature_length is unspecified on error, but
3703 * whatever it is, it should be less than signature_size, so that
3704 * if the caller tries to read *signature_length bytes without
3705 * checking the error code then they don't overflow a buffer. */
3706 TEST_ASSERT( signature_length <= signature_size );
3707
3708exit:
3709 psa_reset_key_attributes( &attributes );
3710 psa_destroy_key( key );
3711 mbedtls_free( signature );
3712 PSA_DONE( );
3713}
3714/* END_CASE */
3715
3716/* BEGIN_CASE */
3717void sign_verify_message( int key_type_arg,
3718 data_t *key_data,
3719 int alg_arg,
3720 data_t *input_data )
3721{
3722 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3723 psa_key_type_t key_type = key_type_arg;
3724 psa_algorithm_t alg = alg_arg;
3725 size_t key_bits;
3726 unsigned char *signature = NULL;
3727 size_t signature_size;
3728 size_t signature_length = 0xdeadbeef;
3729 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3730
3731 PSA_ASSERT( psa_crypto_init( ) );
3732
3733 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3734 PSA_KEY_USAGE_VERIFY_MESSAGE );
3735 psa_set_key_algorithm( &attributes, alg );
3736 psa_set_key_type( &attributes, key_type );
3737
3738 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3739 &key ) );
3740 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3741 key_bits = psa_get_key_bits( &attributes );
3742
3743 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3744 TEST_ASSERT( signature_size != 0 );
3745 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3746 ASSERT_ALLOC( signature, signature_size );
3747
3748 PSA_ASSERT( psa_sign_message( key, alg,
3749 input_data->x, input_data->len,
3750 signature, signature_size,
3751 &signature_length ) );
3752 TEST_ASSERT( signature_length <= signature_size );
3753 TEST_ASSERT( signature_length > 0 );
3754
3755 PSA_ASSERT( psa_verify_message( key, alg,
3756 input_data->x, input_data->len,
3757 signature, signature_length ) );
3758
3759 if( input_data->len != 0 )
3760 {
3761 /* Flip a bit in the input and verify that the signature is now
3762 * detected as invalid. Flip a bit at the beginning, not at the end,
3763 * because ECDSA may ignore the last few bits of the input. */
3764 input_data->x[0] ^= 1;
3765 TEST_EQUAL( psa_verify_message( key, alg,
3766 input_data->x, input_data->len,
3767 signature, signature_length ),
3768 PSA_ERROR_INVALID_SIGNATURE );
3769 }
3770
3771exit:
3772 psa_reset_key_attributes( &attributes );
3773
3774 psa_destroy_key( key );
3775 mbedtls_free( signature );
3776 PSA_DONE( );
3777}
3778/* END_CASE */
3779
3780/* BEGIN_CASE */
3781void verify_message( int key_type_arg,
3782 data_t *key_data,
3783 int alg_arg,
3784 data_t *input_data,
3785 data_t *signature_data )
3786{
3787 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3788 psa_key_type_t key_type = key_type_arg;
3789 psa_algorithm_t alg = alg_arg;
3790 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3791
3792 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3793
3794 PSA_ASSERT( psa_crypto_init( ) );
3795
3796 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3797 psa_set_key_algorithm( &attributes, alg );
3798 psa_set_key_type( &attributes, key_type );
3799
3800 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3801 &key ) );
3802
3803 PSA_ASSERT( psa_verify_message( key, alg,
3804 input_data->x, input_data->len,
3805 signature_data->x, signature_data->len ) );
3806
3807exit:
3808 psa_reset_key_attributes( &attributes );
3809 psa_destroy_key( key );
3810 PSA_DONE( );
3811}
3812/* END_CASE */
3813
3814/* BEGIN_CASE */
3815void verify_message_fail( int key_type_arg,
3816 data_t *key_data,
3817 int alg_arg,
3818 data_t *hash_data,
3819 data_t *signature_data,
3820 int expected_status_arg )
3821{
3822 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3823 psa_key_type_t key_type = key_type_arg;
3824 psa_algorithm_t alg = alg_arg;
3825 psa_status_t actual_status;
3826 psa_status_t expected_status = expected_status_arg;
3827 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3828
3829 PSA_ASSERT( psa_crypto_init( ) );
3830
3831 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3832 psa_set_key_algorithm( &attributes, alg );
3833 psa_set_key_type( &attributes, key_type );
3834
3835 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3836 &key ) );
3837
3838 actual_status = psa_verify_message( key, alg,
3839 hash_data->x, hash_data->len,
3840 signature_data->x,
3841 signature_data->len );
3842 TEST_EQUAL( actual_status, expected_status );
3843
3844exit:
3845 psa_reset_key_attributes( &attributes );
3846 psa_destroy_key( key );
3847 PSA_DONE( );
3848}
3849/* END_CASE */
3850
3851/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003852void asymmetric_encrypt( int key_type_arg,
3853 data_t *key_data,
3854 int alg_arg,
3855 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003856 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003857 int expected_output_length_arg,
3858 int expected_status_arg )
3859{
Ronald Cron5425a212020-08-04 14:58:35 +02003860 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003861 psa_key_type_t key_type = key_type_arg;
3862 psa_algorithm_t alg = alg_arg;
3863 size_t expected_output_length = expected_output_length_arg;
3864 size_t key_bits;
3865 unsigned char *output = NULL;
3866 size_t output_size;
3867 size_t output_length = ~0;
3868 psa_status_t actual_status;
3869 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003870 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003871
Gilles Peskine8817f612018-12-18 00:18:46 +01003872 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003873
Gilles Peskine656896e2018-06-29 19:12:28 +02003874 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003875 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3876 psa_set_key_algorithm( &attributes, alg );
3877 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003878 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003879 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003880
3881 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003882 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003883 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003884
Gilles Peskine656896e2018-06-29 19:12:28 +02003885 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003886 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003887 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003888
3889 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003890 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003891 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003892 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003893 output, output_size,
3894 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003895 TEST_EQUAL( actual_status, expected_status );
3896 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003897
Gilles Peskine68428122018-06-30 18:42:41 +02003898 /* If the label is empty, the test framework puts a non-null pointer
3899 * in label->x. Test that a null pointer works as well. */
3900 if( label->len == 0 )
3901 {
3902 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003903 if( output_size != 0 )
3904 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003905 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003906 input_data->x, input_data->len,
3907 NULL, label->len,
3908 output, output_size,
3909 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003910 TEST_EQUAL( actual_status, expected_status );
3911 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003912 }
3913
Gilles Peskine656896e2018-06-29 19:12:28 +02003914exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003915 /*
3916 * Key attributes may have been returned by psa_get_key_attributes()
3917 * thus reset them as required.
3918 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003919 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003920
Ronald Cron5425a212020-08-04 14:58:35 +02003921 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02003922 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003923 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003924}
3925/* END_CASE */
3926
3927/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003928void asymmetric_encrypt_decrypt( int key_type_arg,
3929 data_t *key_data,
3930 int alg_arg,
3931 data_t *input_data,
3932 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003933{
Ronald Cron5425a212020-08-04 14:58:35 +02003934 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003935 psa_key_type_t key_type = key_type_arg;
3936 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003937 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003938 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003939 size_t output_size;
3940 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003941 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003942 size_t output2_size;
3943 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003944 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003945
Gilles Peskine8817f612018-12-18 00:18:46 +01003946 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003947
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003948 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3949 psa_set_key_algorithm( &attributes, alg );
3950 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003951
Gilles Peskine049c7532019-05-15 20:22:09 +02003952 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003953 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003954
3955 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02003956 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003957 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003958
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003959 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003960 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003961 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01003962
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003963 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01003964 TEST_ASSERT( output2_size <=
3965 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
3966 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003967 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003968
Gilles Peskineeebd7382018-06-08 18:11:54 +02003969 /* We test encryption by checking that encrypt-then-decrypt gives back
3970 * the original plaintext because of the non-optional random
3971 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02003972 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003973 input_data->x, input_data->len,
3974 label->x, label->len,
3975 output, output_size,
3976 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003977 /* We don't know what ciphertext length to expect, but check that
3978 * it looks sensible. */
3979 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003980
Ronald Cron5425a212020-08-04 14:58:35 +02003981 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003982 output, output_length,
3983 label->x, label->len,
3984 output2, output2_size,
3985 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003986 ASSERT_COMPARE( input_data->x, input_data->len,
3987 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003988
3989exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003990 /*
3991 * Key attributes may have been returned by psa_get_key_attributes()
3992 * thus reset them as required.
3993 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003994 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003995
Ronald Cron5425a212020-08-04 14:58:35 +02003996 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003997 mbedtls_free( output );
3998 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003999 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004000}
4001/* END_CASE */
4002
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004003/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004004void asymmetric_decrypt( int key_type_arg,
4005 data_t *key_data,
4006 int alg_arg,
4007 data_t *input_data,
4008 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004009 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004010{
Ronald Cron5425a212020-08-04 14:58:35 +02004011 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004012 psa_key_type_t key_type = key_type_arg;
4013 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004014 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004015 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004016 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004017 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004018 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004019
Gilles Peskine8817f612018-12-18 00:18:46 +01004020 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004021
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004022 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4023 psa_set_key_algorithm( &attributes, alg );
4024 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004025
Gilles Peskine049c7532019-05-15 20:22:09 +02004026 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004027 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004028
gabor-mezei-armceface22021-01-21 12:26:17 +01004029 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4030 key_bits = psa_get_key_bits( &attributes );
4031
4032 /* Determine the maximum ciphertext length */
4033 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
4034 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
4035 ASSERT_ALLOC( output, output_size );
4036
Ronald Cron5425a212020-08-04 14:58:35 +02004037 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004038 input_data->x, input_data->len,
4039 label->x, label->len,
4040 output,
4041 output_size,
4042 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004043 ASSERT_COMPARE( expected_data->x, expected_data->len,
4044 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004045
Gilles Peskine68428122018-06-30 18:42:41 +02004046 /* If the label is empty, the test framework puts a non-null pointer
4047 * in label->x. Test that a null pointer works as well. */
4048 if( label->len == 0 )
4049 {
4050 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004051 if( output_size != 0 )
4052 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004053 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004054 input_data->x, input_data->len,
4055 NULL, label->len,
4056 output,
4057 output_size,
4058 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004059 ASSERT_COMPARE( expected_data->x, expected_data->len,
4060 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004061 }
4062
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004063exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004064 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004065 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004066 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004067 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004068}
4069/* END_CASE */
4070
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004071/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004072void asymmetric_decrypt_fail( int key_type_arg,
4073 data_t *key_data,
4074 int alg_arg,
4075 data_t *input_data,
4076 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004077 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004078 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004079{
Ronald Cron5425a212020-08-04 14:58:35 +02004080 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004081 psa_key_type_t key_type = key_type_arg;
4082 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004083 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004084 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004085 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004086 psa_status_t actual_status;
4087 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004088 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004089
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004090 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004091
Gilles Peskine8817f612018-12-18 00:18:46 +01004092 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004093
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004094 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4095 psa_set_key_algorithm( &attributes, alg );
4096 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004097
Gilles Peskine049c7532019-05-15 20:22:09 +02004098 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004099 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004100
Ronald Cron5425a212020-08-04 14:58:35 +02004101 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004102 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004103 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004104 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004105 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004106 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004107 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004108
Gilles Peskine68428122018-06-30 18:42:41 +02004109 /* If the label is empty, the test framework puts a non-null pointer
4110 * in label->x. Test that a null pointer works as well. */
4111 if( label->len == 0 )
4112 {
4113 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004114 if( output_size != 0 )
4115 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004116 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004117 input_data->x, input_data->len,
4118 NULL, label->len,
4119 output, output_size,
4120 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004121 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004122 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004123 }
4124
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004125exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004126 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004127 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004128 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004129 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004130}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004131/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004132
4133/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004134void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004135{
4136 /* Test each valid way of initializing the object, except for `= {0}`, as
4137 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4138 * though it's OK by the C standard. We could test for this, but we'd need
4139 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004140 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004141 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4142 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4143 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004144
4145 memset( &zero, 0, sizeof( zero ) );
4146
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004147 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004148 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004149 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004150 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004151 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004152 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004153 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004154
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004155 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004156 PSA_ASSERT( psa_key_derivation_abort(&func) );
4157 PSA_ASSERT( psa_key_derivation_abort(&init) );
4158 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004159}
4160/* END_CASE */
4161
Janos Follath16de4a42019-06-13 16:32:24 +01004162/* BEGIN_CASE */
4163void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004164{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004165 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004166 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004167 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004168
Gilles Peskine8817f612018-12-18 00:18:46 +01004169 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004170
Janos Follath16de4a42019-06-13 16:32:24 +01004171 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004172 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004173
4174exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004175 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004176 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004177}
4178/* END_CASE */
4179
Janos Follathaf3c2a02019-06-12 12:34:34 +01004180/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004181void derive_set_capacity( int alg_arg, int capacity_arg,
4182 int expected_status_arg )
4183{
4184 psa_algorithm_t alg = alg_arg;
4185 size_t capacity = capacity_arg;
4186 psa_status_t expected_status = expected_status_arg;
4187 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4188
4189 PSA_ASSERT( psa_crypto_init( ) );
4190
4191 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4192
4193 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4194 expected_status );
4195
4196exit:
4197 psa_key_derivation_abort( &operation );
4198 PSA_DONE( );
4199}
4200/* END_CASE */
4201
4202/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004203void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004204 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004205 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004206 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004207 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004208 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004209 int expected_status_arg3,
4210 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004211{
4212 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004213 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4214 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004215 psa_status_t expected_statuses[] = {expected_status_arg1,
4216 expected_status_arg2,
4217 expected_status_arg3};
4218 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004219 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4220 MBEDTLS_SVC_KEY_ID_INIT,
4221 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004222 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4223 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4224 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004225 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004226 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004227 psa_status_t expected_output_status = expected_output_status_arg;
4228 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004229
4230 PSA_ASSERT( psa_crypto_init( ) );
4231
4232 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4233 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004234
4235 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4236
4237 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4238 {
Gilles Peskinef6279312021-05-27 13:21:20 +02004239 mbedtls_test_set_step( i );
4240 if( steps[i] == 0 )
4241 {
4242 /* Skip this step */
4243 }
4244 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004245 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004246 psa_set_key_type( &attributes, key_types[i] );
4247 PSA_ASSERT( psa_import_key( &attributes,
4248 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004249 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004250 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4251 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4252 {
4253 // When taking a private key as secret input, use key agreement
4254 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004255 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4256 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004257 expected_statuses[i] );
4258 }
4259 else
4260 {
4261 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004262 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004263 expected_statuses[i] );
4264 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004265 }
4266 else
4267 {
4268 TEST_EQUAL( psa_key_derivation_input_bytes(
4269 &operation, steps[i],
4270 inputs[i]->x, inputs[i]->len ),
4271 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004272 }
4273 }
4274
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004275 if( output_key_type != PSA_KEY_TYPE_NONE )
4276 {
4277 psa_reset_key_attributes( &attributes );
4278 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4279 psa_set_key_bits( &attributes, 8 );
4280 actual_output_status =
4281 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004282 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004283 }
4284 else
4285 {
4286 uint8_t buffer[1];
4287 actual_output_status =
4288 psa_key_derivation_output_bytes( &operation,
4289 buffer, sizeof( buffer ) );
4290 }
4291 TEST_EQUAL( actual_output_status, expected_output_status );
4292
Janos Follathaf3c2a02019-06-12 12:34:34 +01004293exit:
4294 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004295 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4296 psa_destroy_key( keys[i] );
4297 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004298 PSA_DONE( );
4299}
4300/* END_CASE */
4301
Janos Follathd958bb72019-07-03 15:02:16 +01004302/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004303void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004304{
Janos Follathd958bb72019-07-03 15:02:16 +01004305 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004306 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004307 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004308 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004309 unsigned char input1[] = "Input 1";
4310 size_t input1_length = sizeof( input1 );
4311 unsigned char input2[] = "Input 2";
4312 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004313 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004314 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004315 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4316 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4317 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004318 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004319
Gilles Peskine8817f612018-12-18 00:18:46 +01004320 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004321
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004322 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4323 psa_set_key_algorithm( &attributes, alg );
4324 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004325
Gilles Peskine73676cb2019-05-15 20:15:10 +02004326 PSA_ASSERT( psa_import_key( &attributes,
4327 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004328 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004329
4330 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004331 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4332 input1, input1_length,
4333 input2, input2_length,
4334 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004335 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004336
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004337 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004338 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004339 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004340
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004341 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004342
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004343 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004344 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004345
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004346exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004347 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004348 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004349 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004350}
4351/* END_CASE */
4352
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004353/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004354void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004355{
4356 uint8_t output_buffer[16];
4357 size_t buffer_size = 16;
4358 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004359 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004360
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004361 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4362 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004363 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004364
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004365 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004366 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004367
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004368 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004369
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004370 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4371 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004372 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004373
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004374 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004375 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004376
4377exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004378 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004379}
4380/* END_CASE */
4381
4382/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004383void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004384 int step1_arg, data_t *input1,
4385 int step2_arg, data_t *input2,
4386 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004387 int requested_capacity_arg,
4388 data_t *expected_output1,
4389 data_t *expected_output2 )
4390{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004391 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004392 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4393 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004394 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4395 MBEDTLS_SVC_KEY_ID_INIT,
4396 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004397 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004398 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004399 uint8_t *expected_outputs[2] =
4400 {expected_output1->x, expected_output2->x};
4401 size_t output_sizes[2] =
4402 {expected_output1->len, expected_output2->len};
4403 size_t output_buffer_size = 0;
4404 uint8_t *output_buffer = NULL;
4405 size_t expected_capacity;
4406 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004407 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004408 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004409 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004410
4411 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4412 {
4413 if( output_sizes[i] > output_buffer_size )
4414 output_buffer_size = output_sizes[i];
4415 if( output_sizes[i] == 0 )
4416 expected_outputs[i] = NULL;
4417 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004418 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004419 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004420
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004421 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4422 psa_set_key_algorithm( &attributes, alg );
4423 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004424
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004425 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004426 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4427 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4428 requested_capacity ) );
4429 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004430 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004431 switch( steps[i] )
4432 {
4433 case 0:
4434 break;
4435 case PSA_KEY_DERIVATION_INPUT_SECRET:
4436 PSA_ASSERT( psa_import_key( &attributes,
4437 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004438 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004439
4440 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4441 {
4442 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4443 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4444 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4445 }
4446
Gilles Peskine1468da72019-05-29 17:35:49 +02004447 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004448 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004449 break;
4450 default:
4451 PSA_ASSERT( psa_key_derivation_input_bytes(
4452 &operation, steps[i],
4453 inputs[i]->x, inputs[i]->len ) );
4454 break;
4455 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004456 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004457
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004458 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004459 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004460 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004461 expected_capacity = requested_capacity;
4462
4463 /* Expansion phase. */
4464 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4465 {
4466 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004467 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004468 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004469 if( expected_capacity == 0 && output_sizes[i] == 0 )
4470 {
4471 /* Reading 0 bytes when 0 bytes are available can go either way. */
4472 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004473 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004474 continue;
4475 }
4476 else if( expected_capacity == 0 ||
4477 output_sizes[i] > expected_capacity )
4478 {
4479 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004480 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004481 expected_capacity = 0;
4482 continue;
4483 }
4484 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004485 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004486 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004487 ASSERT_COMPARE( output_buffer, output_sizes[i],
4488 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004489 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004490 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004491 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004492 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004493 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004494 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004495 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004496
4497exit:
4498 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004499 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004500 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4501 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004502 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004503}
4504/* END_CASE */
4505
4506/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004507void derive_full( int alg_arg,
4508 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004509 data_t *input1,
4510 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004511 int requested_capacity_arg )
4512{
Ronald Cron5425a212020-08-04 14:58:35 +02004513 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004514 psa_algorithm_t alg = alg_arg;
4515 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004516 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004517 unsigned char output_buffer[16];
4518 size_t expected_capacity = requested_capacity;
4519 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004520 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004521
Gilles Peskine8817f612018-12-18 00:18:46 +01004522 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004523
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004524 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4525 psa_set_key_algorithm( &attributes, alg );
4526 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004527
Gilles Peskine049c7532019-05-15 20:22:09 +02004528 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004529 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004530
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004531 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4532 input1->x, input1->len,
4533 input2->x, input2->len,
4534 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004535 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004536
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004537 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004538 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004539 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004540
4541 /* Expansion phase. */
4542 while( current_capacity > 0 )
4543 {
4544 size_t read_size = sizeof( output_buffer );
4545 if( read_size > current_capacity )
4546 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004547 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004548 output_buffer,
4549 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004550 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004551 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004552 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004553 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004554 }
4555
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004556 /* Check that the operation refuses to go over capacity. */
4557 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004558 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004559
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004560 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004561
4562exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004563 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004564 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004565 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004566}
4567/* END_CASE */
4568
Janos Follathe60c9052019-07-03 13:51:30 +01004569/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004570void derive_key_exercise( int alg_arg,
4571 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004572 data_t *input1,
4573 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004574 int derived_type_arg,
4575 int derived_bits_arg,
4576 int derived_usage_arg,
4577 int derived_alg_arg )
4578{
Ronald Cron5425a212020-08-04 14:58:35 +02004579 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4580 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004581 psa_algorithm_t alg = alg_arg;
4582 psa_key_type_t derived_type = derived_type_arg;
4583 size_t derived_bits = derived_bits_arg;
4584 psa_key_usage_t derived_usage = derived_usage_arg;
4585 psa_algorithm_t derived_alg = derived_alg_arg;
4586 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004587 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004588 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004589 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004590
Gilles Peskine8817f612018-12-18 00:18:46 +01004591 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004592
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004593 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4594 psa_set_key_algorithm( &attributes, alg );
4595 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004596 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004597 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004598
4599 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004600 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4601 input1->x, input1->len,
4602 input2->x, input2->len,
4603 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004604 goto exit;
4605
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004606 psa_set_key_usage_flags( &attributes, derived_usage );
4607 psa_set_key_algorithm( &attributes, derived_alg );
4608 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004609 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004610 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004611 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004612
4613 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004614 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004615 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4616 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004617
4618 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004619 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004620 goto exit;
4621
4622exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004623 /*
4624 * Key attributes may have been returned by psa_get_key_attributes()
4625 * thus reset them as required.
4626 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004627 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004628
4629 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004630 psa_destroy_key( base_key );
4631 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004632 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004633}
4634/* END_CASE */
4635
Janos Follath42fd8882019-07-03 14:17:09 +01004636/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004637void derive_key_export( int alg_arg,
4638 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004639 data_t *input1,
4640 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004641 int bytes1_arg,
4642 int bytes2_arg )
4643{
Ronald Cron5425a212020-08-04 14:58:35 +02004644 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4645 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004646 psa_algorithm_t alg = alg_arg;
4647 size_t bytes1 = bytes1_arg;
4648 size_t bytes2 = bytes2_arg;
4649 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004650 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004651 uint8_t *output_buffer = NULL;
4652 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004653 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4654 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004655 size_t length;
4656
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004657 ASSERT_ALLOC( output_buffer, capacity );
4658 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004659 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004660
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004661 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4662 psa_set_key_algorithm( &base_attributes, alg );
4663 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004664 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004665 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004666
4667 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004668 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4669 input1->x, input1->len,
4670 input2->x, input2->len,
4671 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004672 goto exit;
4673
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004674 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004675 output_buffer,
4676 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004677 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004678
4679 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004680 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4681 input1->x, input1->len,
4682 input2->x, input2->len,
4683 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004684 goto exit;
4685
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004686 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4687 psa_set_key_algorithm( &derived_attributes, 0 );
4688 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004689 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004690 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004691 &derived_key ) );
4692 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004693 export_buffer, bytes1,
4694 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004695 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004696 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004697 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004698 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004699 &derived_key ) );
4700 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004701 export_buffer + bytes1, bytes2,
4702 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004703 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004704
4705 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004706 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4707 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004708
4709exit:
4710 mbedtls_free( output_buffer );
4711 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004712 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004713 psa_destroy_key( base_key );
4714 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004715 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004716}
4717/* END_CASE */
4718
4719/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004720void derive_key( int alg_arg,
4721 data_t *key_data, data_t *input1, data_t *input2,
4722 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004723 int expected_status_arg,
4724 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004725{
Ronald Cron5425a212020-08-04 14:58:35 +02004726 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4727 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004728 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004729 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004730 size_t bits = bits_arg;
4731 psa_status_t expected_status = expected_status_arg;
4732 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4733 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4734 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4735
4736 PSA_ASSERT( psa_crypto_init( ) );
4737
4738 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4739 psa_set_key_algorithm( &base_attributes, alg );
4740 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4741 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004742 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004743
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004744 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4745 input1->x, input1->len,
4746 input2->x, input2->len,
4747 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004748 goto exit;
4749
4750 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4751 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004752 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004753 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004754
4755 psa_status_t status =
4756 psa_key_derivation_output_key( &derived_attributes,
4757 &operation,
4758 &derived_key );
4759 if( is_large_output > 0 )
4760 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4761 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004762
4763exit:
4764 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004765 psa_destroy_key( base_key );
4766 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004767 PSA_DONE( );
4768}
4769/* END_CASE */
4770
4771/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004772void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004773 int our_key_type_arg, int our_key_alg_arg,
4774 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004775 int expected_status_arg )
4776{
Ronald Cron5425a212020-08-04 14:58:35 +02004777 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004778 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004779 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004780 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004781 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004782 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004783 psa_status_t expected_status = expected_status_arg;
4784 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004785
Gilles Peskine8817f612018-12-18 00:18:46 +01004786 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004787
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004788 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004789 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004790 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004791 PSA_ASSERT( psa_import_key( &attributes,
4792 our_key_data->x, our_key_data->len,
4793 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004794
Gilles Peskine77f40d82019-04-11 21:27:06 +02004795 /* The tests currently include inputs that should fail at either step.
4796 * Test cases that fail at the setup step should be changed to call
4797 * key_derivation_setup instead, and this function should be renamed
4798 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004799 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004800 if( status == PSA_SUCCESS )
4801 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004802 TEST_EQUAL( psa_key_derivation_key_agreement(
4803 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4804 our_key,
4805 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004806 expected_status );
4807 }
4808 else
4809 {
4810 TEST_ASSERT( status == expected_status );
4811 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004812
4813exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004814 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004815 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004816 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004817}
4818/* END_CASE */
4819
4820/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004821void raw_key_agreement( int alg_arg,
4822 int our_key_type_arg, data_t *our_key_data,
4823 data_t *peer_key_data,
4824 data_t *expected_output )
4825{
Ronald Cron5425a212020-08-04 14:58:35 +02004826 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004827 psa_algorithm_t alg = alg_arg;
4828 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004829 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004830 unsigned char *output = NULL;
4831 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004832 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004833
4834 ASSERT_ALLOC( output, expected_output->len );
4835 PSA_ASSERT( psa_crypto_init( ) );
4836
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004837 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4838 psa_set_key_algorithm( &attributes, alg );
4839 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004840 PSA_ASSERT( psa_import_key( &attributes,
4841 our_key_data->x, our_key_data->len,
4842 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004843
gabor-mezei-armceface22021-01-21 12:26:17 +01004844 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4845 key_bits = psa_get_key_bits( &attributes );
4846
Gilles Peskinebe697d82019-05-16 18:00:41 +02004847 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4848 peer_key_data->x, peer_key_data->len,
4849 output, expected_output->len,
4850 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004851 ASSERT_COMPARE( output, output_length,
4852 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01004853 TEST_ASSERT( output_length <=
4854 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
4855 TEST_ASSERT( output_length <=
4856 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004857
4858exit:
4859 mbedtls_free( output );
4860 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004861 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004862}
4863/* END_CASE */
4864
4865/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004866void key_agreement_capacity( int alg_arg,
4867 int our_key_type_arg, data_t *our_key_data,
4868 data_t *peer_key_data,
4869 int expected_capacity_arg )
4870{
Ronald Cron5425a212020-08-04 14:58:35 +02004871 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004872 psa_algorithm_t alg = alg_arg;
4873 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004874 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004875 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004876 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004877 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004878
Gilles Peskine8817f612018-12-18 00:18:46 +01004879 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004880
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004881 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4882 psa_set_key_algorithm( &attributes, alg );
4883 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004884 PSA_ASSERT( psa_import_key( &attributes,
4885 our_key_data->x, our_key_data->len,
4886 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004887
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004888 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004889 PSA_ASSERT( psa_key_derivation_key_agreement(
4890 &operation,
4891 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4892 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004893 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4894 {
4895 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004896 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004897 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004898 NULL, 0 ) );
4899 }
Gilles Peskine59685592018-09-18 12:11:34 +02004900
Gilles Peskinebf491972018-10-25 22:36:12 +02004901 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004902 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004903 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004904 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004905
Gilles Peskinebf491972018-10-25 22:36:12 +02004906 /* Test the actual capacity by reading the output. */
4907 while( actual_capacity > sizeof( output ) )
4908 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004909 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004910 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004911 actual_capacity -= sizeof( output );
4912 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004913 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004914 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004915 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004916 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004917
Gilles Peskine59685592018-09-18 12:11:34 +02004918exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004919 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004920 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004921 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004922}
4923/* END_CASE */
4924
4925/* BEGIN_CASE */
4926void key_agreement_output( int alg_arg,
4927 int our_key_type_arg, data_t *our_key_data,
4928 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004929 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004930{
Ronald Cron5425a212020-08-04 14:58:35 +02004931 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004932 psa_algorithm_t alg = alg_arg;
4933 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004934 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004935 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004936 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004937
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004938 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4939 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004940
Gilles Peskine8817f612018-12-18 00:18:46 +01004941 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004942
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004943 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4944 psa_set_key_algorithm( &attributes, alg );
4945 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004946 PSA_ASSERT( psa_import_key( &attributes,
4947 our_key_data->x, our_key_data->len,
4948 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004949
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004950 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004951 PSA_ASSERT( psa_key_derivation_key_agreement(
4952 &operation,
4953 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4954 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004955 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4956 {
4957 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004958 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004959 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004960 NULL, 0 ) );
4961 }
Gilles Peskine59685592018-09-18 12:11:34 +02004962
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004963 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004964 actual_output,
4965 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004966 ASSERT_COMPARE( actual_output, expected_output1->len,
4967 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004968 if( expected_output2->len != 0 )
4969 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004970 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004971 actual_output,
4972 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004973 ASSERT_COMPARE( actual_output, expected_output2->len,
4974 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004975 }
Gilles Peskine59685592018-09-18 12:11:34 +02004976
4977exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004978 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004979 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004980 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004981 mbedtls_free( actual_output );
4982}
4983/* END_CASE */
4984
4985/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004986void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004987{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004988 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004989 unsigned char *output = NULL;
4990 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004991 size_t i;
4992 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004993
Simon Butcher49f8e312020-03-03 15:51:50 +00004994 TEST_ASSERT( bytes_arg >= 0 );
4995
Gilles Peskine91892022021-02-08 19:50:26 +01004996 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004997 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02004998
Gilles Peskine8817f612018-12-18 00:18:46 +01004999 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005000
Gilles Peskinea50d7392018-06-21 10:22:13 +02005001 /* Run several times, to ensure that every output byte will be
5002 * nonzero at least once with overwhelming probability
5003 * (2^(-8*number_of_runs)). */
5004 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005005 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005006 if( bytes != 0 )
5007 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005008 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005009
Gilles Peskinea50d7392018-06-21 10:22:13 +02005010 for( i = 0; i < bytes; i++ )
5011 {
5012 if( output[i] != 0 )
5013 ++changed[i];
5014 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005015 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005016
5017 /* Check that every byte was changed to nonzero at least once. This
5018 * validates that psa_generate_random is overwriting every byte of
5019 * the output buffer. */
5020 for( i = 0; i < bytes; i++ )
5021 {
5022 TEST_ASSERT( changed[i] != 0 );
5023 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005024
5025exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005026 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005027 mbedtls_free( output );
5028 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005029}
5030/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005031
5032/* BEGIN_CASE */
5033void generate_key( int type_arg,
5034 int bits_arg,
5035 int usage_arg,
5036 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005037 int expected_status_arg,
5038 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005039{
Ronald Cron5425a212020-08-04 14:58:35 +02005040 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005041 psa_key_type_t type = type_arg;
5042 psa_key_usage_t usage = usage_arg;
5043 size_t bits = bits_arg;
5044 psa_algorithm_t alg = alg_arg;
5045 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005046 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005047 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005048
Gilles Peskine8817f612018-12-18 00:18:46 +01005049 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005050
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005051 psa_set_key_usage_flags( &attributes, usage );
5052 psa_set_key_algorithm( &attributes, alg );
5053 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005054 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005055
5056 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005057 psa_status_t status = psa_generate_key( &attributes, &key );
5058
5059 if( is_large_key > 0 )
5060 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5061 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005062 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005063 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005064
5065 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005066 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005067 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5068 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005069
Gilles Peskine818ca122018-06-20 18:16:48 +02005070 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005071 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005072 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005073
5074exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005075 /*
5076 * Key attributes may have been returned by psa_get_key_attributes()
5077 * thus reset them as required.
5078 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005079 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005080
Ronald Cron5425a212020-08-04 14:58:35 +02005081 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005082 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005083}
5084/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005085
Ronald Cronee414c72021-03-18 18:50:08 +01005086/* 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 +02005087void generate_key_rsa( int bits_arg,
5088 data_t *e_arg,
5089 int expected_status_arg )
5090{
Ronald Cron5425a212020-08-04 14:58:35 +02005091 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005092 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005093 size_t bits = bits_arg;
5094 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5095 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5096 psa_status_t expected_status = expected_status_arg;
5097 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5098 uint8_t *exported = NULL;
5099 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005100 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005101 size_t exported_length = SIZE_MAX;
5102 uint8_t *e_read_buffer = NULL;
5103 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005104 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005105 size_t e_read_length = SIZE_MAX;
5106
5107 if( e_arg->len == 0 ||
5108 ( e_arg->len == 3 &&
5109 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5110 {
5111 is_default_public_exponent = 1;
5112 e_read_size = 0;
5113 }
5114 ASSERT_ALLOC( e_read_buffer, e_read_size );
5115 ASSERT_ALLOC( exported, exported_size );
5116
5117 PSA_ASSERT( psa_crypto_init( ) );
5118
5119 psa_set_key_usage_flags( &attributes, usage );
5120 psa_set_key_algorithm( &attributes, alg );
5121 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5122 e_arg->x, e_arg->len ) );
5123 psa_set_key_bits( &attributes, bits );
5124
5125 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005126 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005127 if( expected_status != PSA_SUCCESS )
5128 goto exit;
5129
5130 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005131 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005132 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5133 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5134 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5135 e_read_buffer, e_read_size,
5136 &e_read_length ) );
5137 if( is_default_public_exponent )
5138 TEST_EQUAL( e_read_length, 0 );
5139 else
5140 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5141
5142 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005143 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005144 goto exit;
5145
5146 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005147 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005148 exported, exported_size,
5149 &exported_length ) );
5150 {
5151 uint8_t *p = exported;
5152 uint8_t *end = exported + exported_length;
5153 size_t len;
5154 /* RSAPublicKey ::= SEQUENCE {
5155 * modulus INTEGER, -- n
5156 * publicExponent INTEGER } -- e
5157 */
5158 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005159 MBEDTLS_ASN1_SEQUENCE |
5160 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005161 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005162 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5163 MBEDTLS_ASN1_INTEGER ) );
5164 if( len >= 1 && p[0] == 0 )
5165 {
5166 ++p;
5167 --len;
5168 }
5169 if( e_arg->len == 0 )
5170 {
5171 TEST_EQUAL( len, 3 );
5172 TEST_EQUAL( p[0], 1 );
5173 TEST_EQUAL( p[1], 0 );
5174 TEST_EQUAL( p[2], 1 );
5175 }
5176 else
5177 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5178 }
5179
5180exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005181 /*
5182 * Key attributes may have been returned by psa_get_key_attributes() or
5183 * set by psa_set_key_domain_parameters() thus reset them as required.
5184 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005185 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005186
Ronald Cron5425a212020-08-04 14:58:35 +02005187 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005188 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005189 mbedtls_free( e_read_buffer );
5190 mbedtls_free( exported );
5191}
5192/* END_CASE */
5193
Darryl Greend49a4992018-06-18 17:27:26 +01005194/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005195void persistent_key_load_key_from_storage( data_t *data,
5196 int type_arg, int bits_arg,
5197 int usage_flags_arg, int alg_arg,
5198 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005199{
Ronald Cron71016a92020-08-28 19:01:50 +02005200 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005201 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005202 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5203 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005204 psa_key_type_t type = type_arg;
5205 size_t bits = bits_arg;
5206 psa_key_usage_t usage_flags = usage_flags_arg;
5207 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005208 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005209 unsigned char *first_export = NULL;
5210 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005211 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005212 size_t first_exported_length;
5213 size_t second_exported_length;
5214
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005215 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5216 {
5217 ASSERT_ALLOC( first_export, export_size );
5218 ASSERT_ALLOC( second_export, export_size );
5219 }
Darryl Greend49a4992018-06-18 17:27:26 +01005220
Gilles Peskine8817f612018-12-18 00:18:46 +01005221 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005222
Gilles Peskinec87af662019-05-15 16:12:22 +02005223 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005224 psa_set_key_usage_flags( &attributes, usage_flags );
5225 psa_set_key_algorithm( &attributes, alg );
5226 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005227 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005228
Darryl Green0c6575a2018-11-07 16:05:30 +00005229 switch( generation_method )
5230 {
5231 case IMPORT_KEY:
5232 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005233 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005234 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005235 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005236
Darryl Green0c6575a2018-11-07 16:05:30 +00005237 case GENERATE_KEY:
5238 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005239 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005240 break;
5241
5242 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005243#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005244 {
5245 /* Create base key */
5246 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5247 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5248 psa_set_key_usage_flags( &base_attributes,
5249 PSA_KEY_USAGE_DERIVE );
5250 psa_set_key_algorithm( &base_attributes, derive_alg );
5251 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005252 PSA_ASSERT( psa_import_key( &base_attributes,
5253 data->x, data->len,
5254 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005255 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005256 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005257 PSA_ASSERT( psa_key_derivation_input_key(
5258 &operation,
5259 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005260 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005261 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005262 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005263 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5264 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005265 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005266 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005267 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005268 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005269 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005270#else
5271 TEST_ASSUME( ! "KDF not supported in this configuration" );
5272#endif
5273 break;
5274
5275 default:
5276 TEST_ASSERT( ! "generation_method not implemented in test" );
5277 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005278 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005279 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005280
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005281 /* Export the key if permitted by the key policy. */
5282 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5283 {
Ronald Cron5425a212020-08-04 14:58:35 +02005284 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005285 first_export, export_size,
5286 &first_exported_length ) );
5287 if( generation_method == IMPORT_KEY )
5288 ASSERT_COMPARE( data->x, data->len,
5289 first_export, first_exported_length );
5290 }
Darryl Greend49a4992018-06-18 17:27:26 +01005291
5292 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005293 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005294 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005295 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005296
Darryl Greend49a4992018-06-18 17:27:26 +01005297 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005298 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005299 TEST_ASSERT( mbedtls_svc_key_id_equal(
5300 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005301 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5302 PSA_KEY_LIFETIME_PERSISTENT );
5303 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5304 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4d9009e2021-05-13 12:05:01 +02005305 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-armff03fd62021-06-28 14:05:00 +02005306 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005307 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005308
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005309 /* Export the key again if permitted by the key policy. */
5310 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005311 {
Ronald Cron5425a212020-08-04 14:58:35 +02005312 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005313 second_export, export_size,
5314 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005315 ASSERT_COMPARE( first_export, first_exported_length,
5316 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005317 }
5318
5319 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005320 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005321 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005322
5323exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005324 /*
5325 * Key attributes may have been returned by psa_get_key_attributes()
5326 * thus reset them as required.
5327 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005328 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005329
Darryl Greend49a4992018-06-18 17:27:26 +01005330 mbedtls_free( first_export );
5331 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005332 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005333 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005334 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005335 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005336}
5337/* END_CASE */