blob: b73e5f6771533d1e8b288f083dec46d8a82a728b [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010018
Gilles Peskinef6279312021-05-27 13:21:20 +020019/* If this comes up, it's a bug in the test code or in the test data. */
20#define UNUSED 0xdeadbeef
21
Jaeden Amerof24c7f82018-06-27 17:20:43 +010022/** An invalid export length that will never be set by psa_export_key(). */
23static const size_t INVALID_EXPORT_LENGTH = ~0U;
24
Gilles Peskinea7aa4422018-08-14 15:17:54 +020025/** Test if a buffer contains a constant byte value.
26 *
27 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020028 *
29 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020030 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020031 * \param size Size of the buffer in bytes.
32 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020033 * \return 1 if the buffer is all-bits-zero.
34 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020035 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020036static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037{
38 size_t i;
39 for( i = 0; i < size; i++ )
40 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020042 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020043 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020044 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020045}
Gilles Peskine818ca122018-06-20 18:16:48 +020046
Gilles Peskine0b352bc2018-06-28 00:16:11 +020047/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
48static int asn1_write_10x( unsigned char **p,
49 unsigned char *start,
50 size_t bits,
51 unsigned char x )
52{
53 int ret;
54 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020055 if( bits == 0 )
56 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
57 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020058 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030059 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020060 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
61 *p -= len;
62 ( *p )[len-1] = x;
63 if( bits % 8 == 0 )
64 ( *p )[1] |= 1;
65 else
66 ( *p )[0] |= 1 << ( bits % 8 );
67 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
68 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
69 MBEDTLS_ASN1_INTEGER ) );
70 return( len );
71}
72
73static int construct_fake_rsa_key( unsigned char *buffer,
74 size_t buffer_size,
75 unsigned char **p,
76 size_t bits,
77 int keypair )
78{
79 size_t half_bits = ( bits + 1 ) / 2;
80 int ret;
81 int len = 0;
82 /* Construct something that looks like a DER encoding of
83 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
84 * RSAPrivateKey ::= SEQUENCE {
85 * version Version,
86 * modulus INTEGER, -- n
87 * publicExponent INTEGER, -- e
88 * privateExponent INTEGER, -- d
89 * prime1 INTEGER, -- p
90 * prime2 INTEGER, -- q
91 * exponent1 INTEGER, -- d mod (p-1)
92 * exponent2 INTEGER, -- d mod (q-1)
93 * coefficient INTEGER, -- (inverse of q) mod p
94 * otherPrimeInfos OtherPrimeInfos OPTIONAL
95 * }
96 * Or, for a public key, the same structure with only
97 * version, modulus and publicExponent.
98 */
99 *p = buffer + buffer_size;
100 if( keypair )
101 {
102 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
103 asn1_write_10x( p, buffer, half_bits, 1 ) );
104 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
105 asn1_write_10x( p, buffer, half_bits, 1 ) );
106 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
107 asn1_write_10x( p, buffer, half_bits, 1 ) );
108 MBEDTLS_ASN1_CHK_ADD( len, /* q */
109 asn1_write_10x( p, buffer, half_bits, 1 ) );
110 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
111 asn1_write_10x( p, buffer, half_bits, 3 ) );
112 MBEDTLS_ASN1_CHK_ADD( len, /* d */
113 asn1_write_10x( p, buffer, bits, 1 ) );
114 }
115 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
116 asn1_write_10x( p, buffer, 17, 1 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* n */
118 asn1_write_10x( p, buffer, bits, 1 ) );
119 if( keypair )
120 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
121 mbedtls_asn1_write_int( p, buffer, 0 ) );
122 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
123 {
124 const unsigned char tag =
125 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
126 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
127 }
128 return( len );
129}
130
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100131int exercise_mac_setup( psa_key_type_t key_type,
132 const unsigned char *key_bytes,
133 size_t key_length,
134 psa_algorithm_t alg,
135 psa_mac_operation_t *operation,
136 psa_status_t *status )
137{
Ronald Cron5425a212020-08-04 14:58:35 +0200138 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200139 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100140
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100141 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200142 psa_set_key_algorithm( &attributes, alg );
143 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200144 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100145
Ronald Cron5425a212020-08-04 14:58:35 +0200146 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100147 /* Whether setup succeeded or failed, abort must succeed. */
148 PSA_ASSERT( psa_mac_abort( operation ) );
149 /* If setup failed, reproduce the failure, so that the caller can
150 * test the resulting state of the operation object. */
151 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100152 {
Ronald Cron5425a212020-08-04 14:58:35 +0200153 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100154 }
155
Ronald Cron5425a212020-08-04 14:58:35 +0200156 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100157 return( 1 );
158
159exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200160 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100161 return( 0 );
162}
163
164int exercise_cipher_setup( psa_key_type_t key_type,
165 const unsigned char *key_bytes,
166 size_t key_length,
167 psa_algorithm_t alg,
168 psa_cipher_operation_t *operation,
169 psa_status_t *status )
170{
Ronald Cron5425a212020-08-04 14:58:35 +0200171 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200172 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100173
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200174 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
175 psa_set_key_algorithm( &attributes, alg );
176 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200177 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100178
Ronald Cron5425a212020-08-04 14:58:35 +0200179 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100180 /* Whether setup succeeded or failed, abort must succeed. */
181 PSA_ASSERT( psa_cipher_abort( operation ) );
182 /* If setup failed, reproduce the failure, so that the caller can
183 * test the resulting state of the operation object. */
184 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185 {
Ronald Cron5425a212020-08-04 14:58:35 +0200186 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100187 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100188 }
189
Ronald Cron5425a212020-08-04 14:58:35 +0200190 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100191 return( 1 );
192
193exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200194 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100195 return( 0 );
196}
197
Ronald Cron5425a212020-08-04 14:58:35 +0200198static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200199{
200 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200201 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200202 uint8_t buffer[1];
203 size_t length;
204 int ok = 0;
205
Ronald Cronecfb2372020-07-23 17:13:42 +0200206 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200207 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
208 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
209 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200210 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000211 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200212 TEST_EQUAL(
213 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
214 TEST_EQUAL(
215 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200216 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200217 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
218 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
219 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
220 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
221
Ronald Cron5425a212020-08-04 14:58:35 +0200222 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000223 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200224 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200225 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000226 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200227
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200228 ok = 1;
229
230exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100231 /*
232 * Key attributes may have been returned by psa_get_key_attributes()
233 * thus reset them as required.
234 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200235 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100236
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200237 return( ok );
238}
239
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200240/* Assert that a key isn't reported as having a slot number. */
241#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
242#define ASSERT_NO_SLOT_NUMBER( attributes ) \
243 do \
244 { \
245 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
246 TEST_EQUAL( psa_get_key_slot_number( \
247 attributes, \
248 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
249 PSA_ERROR_INVALID_ARGUMENT ); \
250 } \
251 while( 0 )
252#else /* MBEDTLS_PSA_CRYPTO_SE_C */
253#define ASSERT_NO_SLOT_NUMBER( attributes ) \
254 ( (void) 0 )
255#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
256
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100257/* An overapproximation of the amount of storage needed for a key of the
258 * given type and with the given content. The API doesn't make it easy
259 * to find a good value for the size. The current implementation doesn't
260 * care about the value anyway. */
261#define KEY_BITS_FROM_DATA( type, data ) \
262 ( data )->len
263
Darryl Green0c6575a2018-11-07 16:05:30 +0000264typedef enum {
265 IMPORT_KEY = 0,
266 GENERATE_KEY = 1,
267 DERIVE_KEY = 2
268} generate_method;
269
Gilles Peskinee59236f2018-01-27 23:32:46 +0100270/* END_HEADER */
271
272/* BEGIN_DEPENDENCIES
273 * depends_on:MBEDTLS_PSA_CRYPTO_C
274 * END_DEPENDENCIES
275 */
276
277/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200278void static_checks( )
279{
280 size_t max_truncated_mac_size =
281 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
282
283 /* Check that the length for a truncated MAC always fits in the algorithm
284 * encoding. The shifted mask is the maximum truncated value. The
285 * untruncated algorithm may be one byte larger. */
286 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +0100287
288#if defined(MBEDTLS_TEST_DEPRECATED)
289 /* Check deprecated constants. */
290 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
291 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
292 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
293 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
294 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
295 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
296 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
297 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100298
Paul Elliott8ff510a2020-06-02 17:19:28 +0100299 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
300 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
301 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
302 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
303 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
304 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
305 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
306 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
307 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
308 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
309 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
310 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
311 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
312 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
313 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
314 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
315 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
316 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
317 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
318 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
319 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
320 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
321 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
322 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
323 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
324 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
325 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
326 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
327 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
328 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
329
330 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
331 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
332 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
333 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
334 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
335 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
336 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
337 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100338
Paul Elliott75e27032020-06-03 15:17:39 +0100339 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
340 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
341 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
342 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
343 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
344
345 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
346 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100347#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200348}
349/* END_CASE */
350
351/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200352void import_with_policy( int type_arg,
353 int usage_arg, int alg_arg,
354 int expected_status_arg )
355{
356 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
357 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200358 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200359 psa_key_type_t type = type_arg;
360 psa_key_usage_t usage = usage_arg;
361 psa_algorithm_t alg = alg_arg;
362 psa_status_t expected_status = expected_status_arg;
363 const uint8_t key_material[16] = {0};
364 psa_status_t status;
365
366 PSA_ASSERT( psa_crypto_init( ) );
367
368 psa_set_key_type( &attributes, type );
369 psa_set_key_usage_flags( &attributes, usage );
370 psa_set_key_algorithm( &attributes, alg );
371
372 status = psa_import_key( &attributes,
373 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200374 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200375 TEST_EQUAL( status, expected_status );
376 if( status != PSA_SUCCESS )
377 goto exit;
378
Ronald Cron5425a212020-08-04 14:58:35 +0200379 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200380 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4d9009e2021-05-13 12:05:01 +0200381 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200382 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200383 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200384 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200385
Ronald Cron5425a212020-08-04 14:58:35 +0200386 PSA_ASSERT( psa_destroy_key( key ) );
387 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200388
389exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100390 /*
391 * Key attributes may have been returned by psa_get_key_attributes()
392 * thus reset them as required.
393 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200394 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100395
396 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200397 PSA_DONE( );
398}
399/* END_CASE */
400
401/* BEGIN_CASE */
402void import_with_data( data_t *data, int type_arg,
403 int attr_bits_arg,
404 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200405{
406 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
407 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200408 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200409 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200410 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200411 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100412 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100413
Gilles Peskine8817f612018-12-18 00:18:46 +0100414 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100415
Gilles Peskine4747d192019-04-17 15:05:45 +0200416 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200417 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200418
Ronald Cron5425a212020-08-04 14:58:35 +0200419 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100420 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200421 if( status != PSA_SUCCESS )
422 goto exit;
423
Ronald Cron5425a212020-08-04 14:58:35 +0200424 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200425 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200426 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200427 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200428 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200429
Ronald Cron5425a212020-08-04 14:58:35 +0200430 PSA_ASSERT( psa_destroy_key( key ) );
431 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100432
433exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100434 /*
435 * Key attributes may have been returned by psa_get_key_attributes()
436 * thus reset them as required.
437 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200438 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100439
440 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200441 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100442}
443/* END_CASE */
444
445/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200446void import_large_key( int type_arg, int byte_size_arg,
447 int expected_status_arg )
448{
449 psa_key_type_t type = type_arg;
450 size_t byte_size = byte_size_arg;
451 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
452 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200453 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200454 psa_status_t status;
455 uint8_t *buffer = NULL;
456 size_t buffer_size = byte_size + 1;
457 size_t n;
458
Steven Cooreman69967ce2021-01-18 18:01:08 +0100459 /* Skip the test case if the target running the test cannot
460 * accomodate large keys due to heap size constraints */
461 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200462 memset( buffer, 'K', byte_size );
463
464 PSA_ASSERT( psa_crypto_init( ) );
465
466 /* Try importing the key */
467 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
468 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200469 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100470 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200471 TEST_EQUAL( status, expected_status );
472
473 if( status == PSA_SUCCESS )
474 {
Ronald Cron5425a212020-08-04 14:58:35 +0200475 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200476 TEST_EQUAL( psa_get_key_type( &attributes ), type );
477 TEST_EQUAL( psa_get_key_bits( &attributes ),
478 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200479 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200480 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200481 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200482 for( n = 0; n < byte_size; n++ )
483 TEST_EQUAL( buffer[n], 'K' );
484 for( n = byte_size; n < buffer_size; n++ )
485 TEST_EQUAL( buffer[n], 0 );
486 }
487
488exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100489 /*
490 * Key attributes may have been returned by psa_get_key_attributes()
491 * thus reset them as required.
492 */
493 psa_reset_key_attributes( &attributes );
494
Ronald Cron5425a212020-08-04 14:58:35 +0200495 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200496 PSA_DONE( );
497 mbedtls_free( buffer );
498}
499/* END_CASE */
500
501/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200502void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
503{
Ronald Cron5425a212020-08-04 14:58:35 +0200504 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200505 size_t bits = bits_arg;
506 psa_status_t expected_status = expected_status_arg;
507 psa_status_t status;
508 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200509 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200510 size_t buffer_size = /* Slight overapproximations */
511 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200512 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200513 unsigned char *p;
514 int ret;
515 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200516 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200517
Gilles Peskine8817f612018-12-18 00:18:46 +0100518 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200519 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200520
521 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
522 bits, keypair ) ) >= 0 );
523 length = ret;
524
525 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200526 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200527 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100528 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200529
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200530 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200531 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200532
533exit:
534 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200535 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200536}
537/* END_CASE */
538
539/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300540void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300541 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200542 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100543 int expected_bits,
544 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200545 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100546 int canonical_input )
547{
Ronald Cron5425a212020-08-04 14:58:35 +0200548 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100549 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200550 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200551 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100552 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100553 unsigned char *exported = NULL;
554 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100555 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100556 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100557 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200558 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200559 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100560
Moran Pekercb088e72018-07-17 17:36:59 +0300561 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200562 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100563 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200564 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100565 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100566
Gilles Peskine4747d192019-04-17 15:05:45 +0200567 psa_set_key_usage_flags( &attributes, usage_arg );
568 psa_set_key_algorithm( &attributes, alg );
569 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700570
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100571 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200572 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100573
574 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200575 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200576 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
577 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200578 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100579
580 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200581 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100582 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100583
584 /* The exported length must be set by psa_export_key() to a value between 0
585 * and export_size. On errors, the exported length must be 0. */
586 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
587 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
588 TEST_ASSERT( exported_length <= export_size );
589
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200590 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200591 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100592 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200593 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100594 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100595 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200596 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100597
Gilles Peskineea38a922021-02-13 00:05:16 +0100598 /* Run sanity checks on the exported key. For non-canonical inputs,
599 * this validates the canonical representations. For canonical inputs,
600 * this doesn't directly validate the implementation, but it still helps
601 * by cross-validating the test data with the sanity check code. */
602 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200603 goto exit;
604
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100605 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200606 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100607 else
608 {
Ronald Cron5425a212020-08-04 14:58:35 +0200609 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200610 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200611 &key2 ) );
612 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100613 reexported,
614 export_size,
615 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200616 ASSERT_COMPARE( exported, exported_length,
617 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200618 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100619 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100620 TEST_ASSERT( exported_length <=
621 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
622 psa_get_key_bits( &got_attributes ) ) );
623 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100624
625destroy:
626 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200627 PSA_ASSERT( psa_destroy_key( key ) );
628 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100629
630exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100631 /*
632 * Key attributes may have been returned by psa_get_key_attributes()
633 * thus reset them as required.
634 */
635 psa_reset_key_attributes( &got_attributes );
636
itayzafrir3e02b3b2018-06-12 17:06:52 +0300637 mbedtls_free( exported );
638 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200639 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100640}
641/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100642
Moran Pekerf709f4a2018-06-06 17:26:04 +0300643/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300644void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200645 int type_arg,
646 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100647 int export_size_delta,
648 int expected_export_status_arg,
649 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300650{
Ronald Cron5425a212020-08-04 14:58:35 +0200651 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300652 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200653 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200654 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300655 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300656 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100657 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100658 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200659 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300660
Gilles Peskine8817f612018-12-18 00:18:46 +0100661 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300662
Gilles Peskine4747d192019-04-17 15:05:45 +0200663 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
664 psa_set_key_algorithm( &attributes, alg );
665 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300666
667 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200668 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300669
Gilles Peskine49c25912018-10-29 15:15:31 +0100670 /* Export the public key */
671 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200672 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200673 exported, export_size,
674 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100675 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100676 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100677 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200678 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100679 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200680 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200681 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100682 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100683 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100684 TEST_ASSERT( expected_public_key->len <=
685 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
686 TEST_ASSERT( expected_public_key->len <=
687 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100688 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
689 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100690 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300691
692exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100693 /*
694 * Key attributes may have been returned by psa_get_key_attributes()
695 * thus reset them as required.
696 */
697 psa_reset_key_attributes( &attributes );
698
itayzafrir3e02b3b2018-06-12 17:06:52 +0300699 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200700 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200701 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300702}
703/* END_CASE */
704
Gilles Peskine20035e32018-02-03 22:44:14 +0100705/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200706void import_and_exercise_key( data_t *data,
707 int type_arg,
708 int bits_arg,
709 int alg_arg )
710{
Ronald Cron5425a212020-08-04 14:58:35 +0200711 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200712 psa_key_type_t type = type_arg;
713 size_t bits = bits_arg;
714 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100715 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200716 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200717 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200718
Gilles Peskine8817f612018-12-18 00:18:46 +0100719 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200720
Gilles Peskine4747d192019-04-17 15:05:45 +0200721 psa_set_key_usage_flags( &attributes, usage );
722 psa_set_key_algorithm( &attributes, alg );
723 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200724
725 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200726 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200727
728 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200729 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200730 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
731 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200732
733 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100734 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200735 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200736
Ronald Cron5425a212020-08-04 14:58:35 +0200737 PSA_ASSERT( psa_destroy_key( key ) );
738 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200739
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200740exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100741 /*
742 * Key attributes may have been returned by psa_get_key_attributes()
743 * thus reset them as required.
744 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200745 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100746
747 psa_reset_key_attributes( &attributes );
748 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200749 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200750}
751/* END_CASE */
752
753/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100754void effective_key_attributes( int type_arg, int expected_type_arg,
755 int bits_arg, int expected_bits_arg,
756 int usage_arg, int expected_usage_arg,
757 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200758{
Ronald Cron5425a212020-08-04 14:58:35 +0200759 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100760 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100761 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100762 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100763 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200764 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100765 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200766 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100767 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200768 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200769
Gilles Peskine8817f612018-12-18 00:18:46 +0100770 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200771
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200772 psa_set_key_usage_flags( &attributes, usage );
773 psa_set_key_algorithm( &attributes, alg );
774 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +0100775 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +0200776
Ronald Cron5425a212020-08-04 14:58:35 +0200777 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +0100778 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +0200779
Ronald Cron5425a212020-08-04 14:58:35 +0200780 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +0100781 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
782 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
783 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
784 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200785
786exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100787 /*
788 * Key attributes may have been returned by psa_get_key_attributes()
789 * thus reset them as required.
790 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200791 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100792
793 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200794 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +0200795}
796/* END_CASE */
797
798/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100799void check_key_policy( int type_arg, int bits_arg,
gabor-mezei-arm58e510f2021-06-28 14:36:03 +0200800 int usage_arg, int alg_arg )
Gilles Peskine06c28892019-11-26 18:07:46 +0100801{
802 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-arm58e510f2021-06-28 14:36:03 +0200803 usage_arg,
804 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200805 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +0100806 goto exit;
807}
808/* END_CASE */
809
810/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200811void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +0000812{
813 /* Test each valid way of initializing the object, except for `= {0}`, as
814 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
815 * though it's OK by the C standard. We could test for this, but we'd need
816 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200817 psa_key_attributes_t func = psa_key_attributes_init( );
818 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
819 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000820
821 memset( &zero, 0, sizeof( zero ) );
822
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200823 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
824 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
825 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000826
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200827 TEST_EQUAL( psa_get_key_type( &func ), 0 );
828 TEST_EQUAL( psa_get_key_type( &init ), 0 );
829 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
830
831 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
832 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
833 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
834
835 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
836 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
837 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
838
839 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
840 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
841 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +0000842}
843/* END_CASE */
844
845/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200846void mac_key_policy( int policy_usage_arg,
847 int policy_alg_arg,
848 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200849 data_t *key_data,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200850 int exercise_alg_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100851 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200852{
Ronald Cron5425a212020-08-04 14:58:35 +0200853 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200854 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000855 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200856 psa_key_type_t key_type = key_type_arg;
857 psa_algorithm_t policy_alg = policy_alg_arg;
858 psa_algorithm_t exercise_alg = exercise_alg_arg;
859 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200860 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100861 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200862 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200863
Gilles Peskine8817f612018-12-18 00:18:46 +0100864 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200865
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200866 psa_set_key_usage_flags( &attributes, policy_usage );
867 psa_set_key_algorithm( &attributes, policy_alg );
868 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200869
Gilles Peskine049c7532019-05-15 20:22:09 +0200870 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200871 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200872
gabor-mezei-arm659af9e2021-06-29 11:06:16 +0200873 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
874 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200875
Ronald Cron5425a212020-08-04 14:58:35 +0200876 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100877 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100878 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100879 else
880 TEST_EQUAL( status, expected_status );
881
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200882 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200883
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200884 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200885 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100886 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_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 );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200890
891exit:
892 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200893 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200894 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200895}
896/* END_CASE */
897
898/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200899void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200900 int policy_alg,
901 int key_type,
902 data_t *key_data,
903 int exercise_alg )
904{
Ronald Cron5425a212020-08-04 14:58:35 +0200905 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200906 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000907 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200908 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200909 psa_status_t status;
910
Gilles Peskine8817f612018-12-18 00:18:46 +0100911 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200912
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200913 psa_set_key_usage_flags( &attributes, policy_usage );
914 psa_set_key_algorithm( &attributes, policy_alg );
915 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200916
Gilles Peskine049c7532019-05-15 20:22:09 +0200917 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200918 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200919
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200920 /* Check if no key usage flag implication is done */
921 TEST_EQUAL( policy_usage,
922 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200923
Ronald Cron5425a212020-08-04 14:58:35 +0200924 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200925 if( policy_alg == exercise_alg &&
926 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100927 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200928 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100929 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200930 psa_cipher_abort( &operation );
931
Ronald Cron5425a212020-08-04 14:58:35 +0200932 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200933 if( policy_alg == exercise_alg &&
934 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100935 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200936 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100937 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200938
939exit:
940 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200941 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200942 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200943}
944/* END_CASE */
945
946/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200947void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200948 int policy_alg,
949 int key_type,
950 data_t *key_data,
951 int nonce_length_arg,
952 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100953 int exercise_alg,
954 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200955{
Ronald Cron5425a212020-08-04 14:58:35 +0200956 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200957 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200958 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200959 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100960 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200961 unsigned char nonce[16] = {0};
962 size_t nonce_length = nonce_length_arg;
963 unsigned char tag[16];
964 size_t tag_length = tag_length_arg;
965 size_t output_length;
966
967 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
968 TEST_ASSERT( tag_length <= sizeof( tag ) );
969
Gilles Peskine8817f612018-12-18 00:18:46 +0100970 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200971
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200972 psa_set_key_usage_flags( &attributes, policy_usage );
973 psa_set_key_algorithm( &attributes, policy_alg );
974 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200975
Gilles Peskine049c7532019-05-15 20:22:09 +0200976 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200977 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200978
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200979 /* Check if no key usage implication is done */
980 TEST_EQUAL( policy_usage,
981 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200982
Ronald Cron5425a212020-08-04 14:58:35 +0200983 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200984 nonce, nonce_length,
985 NULL, 0,
986 NULL, 0,
987 tag, tag_length,
988 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100989 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
990 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200991 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100992 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200993
994 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200995 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200996 nonce, nonce_length,
997 NULL, 0,
998 tag, tag_length,
999 NULL, 0,
1000 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001001 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1002 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1003 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001004 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001005 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001006 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001007
1008exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001009 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001010 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001011}
1012/* END_CASE */
1013
1014/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001015void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001016 int policy_alg,
1017 int key_type,
1018 data_t *key_data,
1019 int exercise_alg )
1020{
Ronald Cron5425a212020-08-04 14:58:35 +02001021 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001022 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001023 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001024 psa_status_t status;
1025 size_t key_bits;
1026 size_t buffer_length;
1027 unsigned char *buffer = NULL;
1028 size_t output_length;
1029
Gilles Peskine8817f612018-12-18 00:18:46 +01001030 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001031
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001032 psa_set_key_usage_flags( &attributes, policy_usage );
1033 psa_set_key_algorithm( &attributes, policy_alg );
1034 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001035
Gilles Peskine049c7532019-05-15 20:22:09 +02001036 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001037 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001038
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001039 /* Check if no key usage implication is done */
1040 TEST_EQUAL( policy_usage,
1041 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001042
Ronald Cron5425a212020-08-04 14:58:35 +02001043 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001044 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001045 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1046 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001047 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001048
Ronald Cron5425a212020-08-04 14:58:35 +02001049 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001050 NULL, 0,
1051 NULL, 0,
1052 buffer, buffer_length,
1053 &output_length );
1054 if( policy_alg == exercise_alg &&
1055 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001056 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001057 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001058 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001059
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001060 if( buffer_length != 0 )
1061 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001062 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001063 buffer, buffer_length,
1064 NULL, 0,
1065 buffer, buffer_length,
1066 &output_length );
1067 if( policy_alg == exercise_alg &&
1068 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001069 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001070 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001071 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001072
1073exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001074 /*
1075 * Key attributes may have been returned by psa_get_key_attributes()
1076 * thus reset them as required.
1077 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001078 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001079
1080 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001081 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001082 mbedtls_free( buffer );
1083}
1084/* END_CASE */
1085
1086/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001087void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001088 int policy_alg,
1089 int key_type,
1090 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001091 int exercise_alg,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001092 int payload_length_arg,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001093 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001094{
Ronald Cron5425a212020-08-04 14:58:35 +02001095 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001096 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001097 psa_key_usage_t policy_usage = policy_usage_arg;
1098 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001099 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001100 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1101 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1102 * compatible with the policy and `payload_length_arg` is supposed to be
1103 * a valid input length to sign. If `payload_length_arg <= 0`,
1104 * `exercise_alg` is supposed to be forbidden by the policy. */
1105 int compatible_alg = payload_length_arg > 0;
1106 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001107 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001108 size_t signature_length;
1109
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001110 /* Check if all implicit usage flags are deployed
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001111 in the expected usage flags. */
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001112 TEST_EQUAL( expected_usage,
1113 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001114
Gilles Peskine8817f612018-12-18 00:18:46 +01001115 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001116
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001117 psa_set_key_usage_flags( &attributes, policy_usage );
1118 psa_set_key_algorithm( &attributes, policy_alg );
1119 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001120
Gilles Peskine049c7532019-05-15 20:22:09 +02001121 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001122 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001123
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001124 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1125
Ronald Cron5425a212020-08-04 14:58:35 +02001126 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001127 payload, payload_length,
1128 signature, sizeof( signature ),
1129 &signature_length );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001130 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001131 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001132 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001133 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001134
1135 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001136 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001137 payload, payload_length,
1138 signature, sizeof( signature ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001139 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001140 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001141 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001142 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001143
gabor-mezei-arm79df41d2021-06-28 14:53:49 +02001144 if( PSA_ALG_IS_HASH_AND_SIGN( exercise_alg ) &&
1145 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001146 {
1147 status = psa_sign_message( key, exercise_alg,
1148 payload, payload_length,
1149 signature, sizeof( signature ),
1150 &signature_length );
1151 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1152 PSA_ASSERT( status );
1153 else
1154 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1155
1156 memset( signature, 0, sizeof( signature ) );
1157 status = psa_verify_message( key, exercise_alg,
1158 payload, payload_length,
1159 signature, sizeof( signature ) );
1160 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1161 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1162 else
1163 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1164 }
1165
Gilles Peskined5b33222018-06-18 22:20:03 +02001166exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001167 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001168 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001169}
1170/* END_CASE */
1171
Janos Follathba3fab92019-06-11 14:50:16 +01001172/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001173void derive_key_policy( int policy_usage,
1174 int policy_alg,
1175 int key_type,
1176 data_t *key_data,
1177 int exercise_alg )
1178{
Ronald Cron5425a212020-08-04 14:58:35 +02001179 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001180 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001181 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001182 psa_status_t status;
1183
Gilles Peskine8817f612018-12-18 00:18:46 +01001184 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001185
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001186 psa_set_key_usage_flags( &attributes, policy_usage );
1187 psa_set_key_algorithm( &attributes, policy_alg );
1188 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001189
Gilles Peskine049c7532019-05-15 20:22:09 +02001190 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001191 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001192
Janos Follathba3fab92019-06-11 14:50:16 +01001193 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1194
1195 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1196 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001197 {
Janos Follathba3fab92019-06-11 14:50:16 +01001198 PSA_ASSERT( psa_key_derivation_input_bytes(
1199 &operation,
1200 PSA_KEY_DERIVATION_INPUT_SEED,
1201 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001202 }
Janos Follathba3fab92019-06-11 14:50:16 +01001203
1204 status = psa_key_derivation_input_key( &operation,
1205 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001206 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001207
Gilles Peskineea0fb492018-07-12 17:17:20 +02001208 if( policy_alg == exercise_alg &&
1209 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001210 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001211 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001212 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001213
1214exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001215 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001216 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001217 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001218}
1219/* END_CASE */
1220
1221/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001222void agreement_key_policy( int policy_usage,
1223 int policy_alg,
1224 int key_type_arg,
1225 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001226 int exercise_alg,
1227 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001228{
Ronald Cron5425a212020-08-04 14:58:35 +02001229 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001230 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001231 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001232 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001233 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001234 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001235
Gilles Peskine8817f612018-12-18 00:18:46 +01001236 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001237
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001238 psa_set_key_usage_flags( &attributes, policy_usage );
1239 psa_set_key_algorithm( &attributes, policy_alg );
1240 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001241
Gilles Peskine049c7532019-05-15 20:22:09 +02001242 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001243 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001244
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001245 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001246 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001247
Steven Cooremance48e852020-10-05 16:02:45 +02001248 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001249
1250exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001251 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001252 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001253 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001254}
1255/* END_CASE */
1256
1257/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001258void key_policy_alg2( int key_type_arg, data_t *key_data,
1259 int usage_arg, int alg_arg, int alg2_arg )
1260{
Ronald Cron5425a212020-08-04 14:58:35 +02001261 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001262 psa_key_type_t key_type = key_type_arg;
1263 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1264 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1265 psa_key_usage_t usage = usage_arg;
1266 psa_algorithm_t alg = alg_arg;
1267 psa_algorithm_t alg2 = alg2_arg;
1268
1269 PSA_ASSERT( psa_crypto_init( ) );
1270
1271 psa_set_key_usage_flags( &attributes, usage );
1272 psa_set_key_algorithm( &attributes, alg );
1273 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1274 psa_set_key_type( &attributes, key_type );
1275 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001276 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001277
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001278 /* Update the usage flags to obtain implicit usage flags */
1279 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001280 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001281 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1282 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1283 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1284
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001285 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001286 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001287 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001288 goto exit;
1289
1290exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001291 /*
1292 * Key attributes may have been returned by psa_get_key_attributes()
1293 * thus reset them as required.
1294 */
1295 psa_reset_key_attributes( &got_attributes );
1296
Ronald Cron5425a212020-08-04 14:58:35 +02001297 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001298 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001299}
1300/* END_CASE */
1301
1302/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001303void raw_agreement_key_policy( int policy_usage,
1304 int policy_alg,
1305 int key_type_arg,
1306 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001307 int exercise_alg,
1308 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001309{
Ronald Cron5425a212020-08-04 14:58:35 +02001310 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001311 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001312 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001313 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001314 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001315 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001316
1317 PSA_ASSERT( psa_crypto_init( ) );
1318
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001319 psa_set_key_usage_flags( &attributes, policy_usage );
1320 psa_set_key_algorithm( &attributes, policy_alg );
1321 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001322
Gilles Peskine049c7532019-05-15 20:22:09 +02001323 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001324 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001325
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001326 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001327
Steven Cooremance48e852020-10-05 16:02:45 +02001328 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001329
1330exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001331 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001332 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001333 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001334}
1335/* END_CASE */
1336
1337/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001338void copy_success( int source_usage_arg,
1339 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001340 int type_arg, data_t *material,
1341 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001342 int target_usage_arg,
1343 int target_alg_arg, int target_alg2_arg,
1344 int expected_usage_arg,
1345 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001346{
Gilles Peskineca25db92019-04-19 11:43:08 +02001347 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1348 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001349 psa_key_usage_t expected_usage = expected_usage_arg;
1350 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001351 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001352 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1353 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001354 uint8_t *export_buffer = NULL;
1355
Gilles Peskine57ab7212019-01-28 13:03:09 +01001356 PSA_ASSERT( psa_crypto_init( ) );
1357
Gilles Peskineca25db92019-04-19 11:43:08 +02001358 /* Prepare the source key. */
1359 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1360 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001361 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001362 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001363 PSA_ASSERT( psa_import_key( &source_attributes,
1364 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001365 &source_key ) );
1366 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001367
Gilles Peskineca25db92019-04-19 11:43:08 +02001368 /* Prepare the target attributes. */
1369 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001370 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001371 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001372 /* Set volatile lifetime to reset the key identifier to 0. */
1373 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1374 }
1375
Gilles Peskineca25db92019-04-19 11:43:08 +02001376 if( target_usage_arg != -1 )
1377 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1378 if( target_alg_arg != -1 )
1379 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001380 if( target_alg2_arg != -1 )
1381 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001382
1383 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001384 PSA_ASSERT( psa_copy_key( source_key,
1385 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001386
1387 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001388 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001389
1390 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001391 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001392 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1393 psa_get_key_type( &target_attributes ) );
1394 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1395 psa_get_key_bits( &target_attributes ) );
1396 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1397 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001398 TEST_EQUAL( expected_alg2,
1399 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001400 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1401 {
1402 size_t length;
1403 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001404 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001405 material->len, &length ) );
1406 ASSERT_COMPARE( material->x, material->len,
1407 export_buffer, length );
1408 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001409
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001410 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001411 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001412 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001413 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001414
Ronald Cron5425a212020-08-04 14:58:35 +02001415 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001416
1417exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001418 /*
1419 * Source and target key attributes may have been returned by
1420 * psa_get_key_attributes() thus reset them as required.
1421 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001422 psa_reset_key_attributes( &source_attributes );
1423 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001424
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001425 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001426 mbedtls_free( export_buffer );
1427}
1428/* END_CASE */
1429
1430/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001431void copy_fail( int source_usage_arg,
1432 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001433 int type_arg, data_t *material,
1434 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001435 int target_usage_arg,
1436 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001437 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001438 int expected_status_arg )
1439{
1440 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1441 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001442 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1443 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001444 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001445
1446 PSA_ASSERT( psa_crypto_init( ) );
1447
1448 /* Prepare the source key. */
1449 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1450 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001451 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001452 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001453 PSA_ASSERT( psa_import_key( &source_attributes,
1454 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001455 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001456
1457 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001458 psa_set_key_id( &target_attributes, key_id );
1459 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001460 psa_set_key_type( &target_attributes, target_type_arg );
1461 psa_set_key_bits( &target_attributes, target_bits_arg );
1462 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1463 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001464 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001465
1466 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001467 TEST_EQUAL( psa_copy_key( source_key,
1468 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001469 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001470
Ronald Cron5425a212020-08-04 14:58:35 +02001471 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001472
Gilles Peskine4a644642019-05-03 17:14:08 +02001473exit:
1474 psa_reset_key_attributes( &source_attributes );
1475 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001476 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001477}
1478/* END_CASE */
1479
1480/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001481void hash_operation_init( )
1482{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001483 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001484 /* Test each valid way of initializing the object, except for `= {0}`, as
1485 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1486 * though it's OK by the C standard. We could test for this, but we'd need
1487 * to supress the Clang warning for the test. */
1488 psa_hash_operation_t func = psa_hash_operation_init( );
1489 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1490 psa_hash_operation_t zero;
1491
1492 memset( &zero, 0, sizeof( zero ) );
1493
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001494 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001495 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1496 PSA_ERROR_BAD_STATE );
1497 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1498 PSA_ERROR_BAD_STATE );
1499 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1500 PSA_ERROR_BAD_STATE );
1501
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001502 /* A default hash operation should be abortable without error. */
1503 PSA_ASSERT( psa_hash_abort( &func ) );
1504 PSA_ASSERT( psa_hash_abort( &init ) );
1505 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001506}
1507/* END_CASE */
1508
1509/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001510void hash_setup( int alg_arg,
1511 int expected_status_arg )
1512{
1513 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001514 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001515 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001516 psa_status_t status;
1517
Gilles Peskine8817f612018-12-18 00:18:46 +01001518 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001519
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001520 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001521 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001522
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001523 /* Whether setup succeeded or failed, abort must succeed. */
1524 PSA_ASSERT( psa_hash_abort( &operation ) );
1525
1526 /* If setup failed, reproduce the failure, so as to
1527 * test the resulting state of the operation object. */
1528 if( status != PSA_SUCCESS )
1529 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1530
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001531 /* Now the operation object should be reusable. */
1532#if defined(KNOWN_SUPPORTED_HASH_ALG)
1533 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1534 PSA_ASSERT( psa_hash_abort( &operation ) );
1535#endif
1536
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001537exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001538 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001539}
1540/* END_CASE */
1541
1542/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001543void hash_compute_fail( int alg_arg, data_t *input,
1544 int output_size_arg, int expected_status_arg )
1545{
1546 psa_algorithm_t alg = alg_arg;
1547 uint8_t *output = NULL;
1548 size_t output_size = output_size_arg;
1549 size_t output_length = INVALID_EXPORT_LENGTH;
1550 psa_status_t expected_status = expected_status_arg;
1551 psa_status_t status;
1552
1553 ASSERT_ALLOC( output, output_size );
1554
1555 PSA_ASSERT( psa_crypto_init( ) );
1556
1557 status = psa_hash_compute( alg, input->x, input->len,
1558 output, output_size, &output_length );
1559 TEST_EQUAL( status, expected_status );
1560 TEST_ASSERT( output_length <= output_size );
1561
1562exit:
1563 mbedtls_free( output );
1564 PSA_DONE( );
1565}
1566/* END_CASE */
1567
1568/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001569void hash_compare_fail( int alg_arg, data_t *input,
1570 data_t *reference_hash,
1571 int expected_status_arg )
1572{
1573 psa_algorithm_t alg = alg_arg;
1574 psa_status_t expected_status = expected_status_arg;
1575 psa_status_t status;
1576
1577 PSA_ASSERT( psa_crypto_init( ) );
1578
1579 status = psa_hash_compare( alg, input->x, input->len,
1580 reference_hash->x, reference_hash->len );
1581 TEST_EQUAL( status, expected_status );
1582
1583exit:
1584 PSA_DONE( );
1585}
1586/* END_CASE */
1587
1588/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001589void hash_compute_compare( int alg_arg, data_t *input,
1590 data_t *expected_output )
1591{
1592 psa_algorithm_t alg = alg_arg;
1593 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1594 size_t output_length = INVALID_EXPORT_LENGTH;
1595 size_t i;
1596
1597 PSA_ASSERT( psa_crypto_init( ) );
1598
1599 /* Compute with tight buffer */
1600 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001601 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001602 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001603 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001604 ASSERT_COMPARE( output, output_length,
1605 expected_output->x, expected_output->len );
1606
1607 /* Compute with larger buffer */
1608 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1609 output, sizeof( output ),
1610 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001611 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001612 ASSERT_COMPARE( output, output_length,
1613 expected_output->x, expected_output->len );
1614
1615 /* Compare with correct hash */
1616 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1617 output, output_length ) );
1618
1619 /* Compare with trailing garbage */
1620 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1621 output, output_length + 1 ),
1622 PSA_ERROR_INVALID_SIGNATURE );
1623
1624 /* Compare with truncated hash */
1625 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1626 output, output_length - 1 ),
1627 PSA_ERROR_INVALID_SIGNATURE );
1628
1629 /* Compare with corrupted value */
1630 for( i = 0; i < output_length; i++ )
1631 {
Chris Jones9634bb12021-01-20 15:56:42 +00001632 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001633 output[i] ^= 1;
1634 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1635 output, output_length ),
1636 PSA_ERROR_INVALID_SIGNATURE );
1637 output[i] ^= 1;
1638 }
1639
1640exit:
1641 PSA_DONE( );
1642}
1643/* END_CASE */
1644
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001645/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001646void hash_bad_order( )
1647{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001648 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001649 unsigned char input[] = "";
1650 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001651 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001652 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1653 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1654 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001655 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001656 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001657 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001658
Gilles Peskine8817f612018-12-18 00:18:46 +01001659 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001660
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001661 /* Call setup twice in a row. */
1662 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1663 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1664 PSA_ERROR_BAD_STATE );
1665 PSA_ASSERT( psa_hash_abort( &operation ) );
1666
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001667 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001668 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001669 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001670 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001671
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001672 /* Call update after finish. */
1673 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1674 PSA_ASSERT( psa_hash_finish( &operation,
1675 hash, sizeof( hash ), &hash_len ) );
1676 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001677 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001678 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001679
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001680 /* Call verify without calling setup beforehand. */
1681 TEST_EQUAL( psa_hash_verify( &operation,
1682 valid_hash, sizeof( valid_hash ) ),
1683 PSA_ERROR_BAD_STATE );
1684 PSA_ASSERT( psa_hash_abort( &operation ) );
1685
1686 /* Call verify after finish. */
1687 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1688 PSA_ASSERT( psa_hash_finish( &operation,
1689 hash, sizeof( hash ), &hash_len ) );
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 twice in a row. */
1696 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1697 PSA_ASSERT( psa_hash_verify( &operation,
1698 valid_hash, sizeof( valid_hash ) ) );
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 finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001705 TEST_EQUAL( psa_hash_finish( &operation,
1706 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001707 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001708 PSA_ASSERT( psa_hash_abort( &operation ) );
1709
1710 /* Call finish twice in a row. */
1711 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1712 PSA_ASSERT( psa_hash_finish( &operation,
1713 hash, sizeof( hash ), &hash_len ) );
1714 TEST_EQUAL( psa_hash_finish( &operation,
1715 hash, sizeof( hash ), &hash_len ),
1716 PSA_ERROR_BAD_STATE );
1717 PSA_ASSERT( psa_hash_abort( &operation ) );
1718
1719 /* Call finish after calling verify. */
1720 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1721 PSA_ASSERT( psa_hash_verify( &operation,
1722 valid_hash, sizeof( valid_hash ) ) );
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 ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001727
1728exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001729 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001730}
1731/* END_CASE */
1732
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001733/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001734void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001735{
1736 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001737 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1738 * appended to it */
1739 unsigned char hash[] = {
1740 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1741 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1742 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001743 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001744 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001745
Gilles Peskine8817f612018-12-18 00:18:46 +01001746 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001747
itayzafrir27e69452018-11-01 14:26:34 +02001748 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001749 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001750 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001751 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001752
itayzafrir27e69452018-11-01 14:26:34 +02001753 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001754 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001755 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001756 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001757
itayzafrir27e69452018-11-01 14:26:34 +02001758 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001759 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001760 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001761 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001762
itayzafrirec93d302018-10-18 18:01:10 +03001763exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001764 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001765}
1766/* END_CASE */
1767
Ronald Cronee414c72021-03-18 18:50:08 +01001768/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001769void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001770{
1771 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001772 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001773 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001774 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001775 size_t hash_len;
1776
Gilles Peskine8817f612018-12-18 00:18:46 +01001777 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001778
itayzafrir58028322018-10-25 10:22:01 +03001779 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001780 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001781 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001782 hash, expected_size - 1, &hash_len ),
1783 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001784
1785exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001786 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001787}
1788/* END_CASE */
1789
Ronald Cronee414c72021-03-18 18:50:08 +01001790/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001791void hash_clone_source_state( )
1792{
1793 psa_algorithm_t alg = PSA_ALG_SHA_256;
1794 unsigned char hash[PSA_HASH_MAX_SIZE];
1795 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1796 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1797 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1798 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1799 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1800 size_t hash_len;
1801
1802 PSA_ASSERT( psa_crypto_init( ) );
1803 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1804
1805 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1806 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1807 PSA_ASSERT( psa_hash_finish( &op_finished,
1808 hash, sizeof( hash ), &hash_len ) );
1809 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1810 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1811
1812 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1813 PSA_ERROR_BAD_STATE );
1814
1815 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1816 PSA_ASSERT( psa_hash_finish( &op_init,
1817 hash, sizeof( hash ), &hash_len ) );
1818 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1819 PSA_ASSERT( psa_hash_finish( &op_finished,
1820 hash, sizeof( hash ), &hash_len ) );
1821 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1822 PSA_ASSERT( psa_hash_finish( &op_aborted,
1823 hash, sizeof( hash ), &hash_len ) );
1824
1825exit:
1826 psa_hash_abort( &op_source );
1827 psa_hash_abort( &op_init );
1828 psa_hash_abort( &op_setup );
1829 psa_hash_abort( &op_finished );
1830 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001831 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001832}
1833/* END_CASE */
1834
Ronald Cronee414c72021-03-18 18:50:08 +01001835/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001836void hash_clone_target_state( )
1837{
1838 psa_algorithm_t alg = PSA_ALG_SHA_256;
1839 unsigned char hash[PSA_HASH_MAX_SIZE];
1840 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1841 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1842 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1843 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1844 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1845 size_t hash_len;
1846
1847 PSA_ASSERT( psa_crypto_init( ) );
1848
1849 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1850 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1851 PSA_ASSERT( psa_hash_finish( &op_finished,
1852 hash, sizeof( hash ), &hash_len ) );
1853 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1854 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1855
1856 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1857 PSA_ASSERT( psa_hash_finish( &op_target,
1858 hash, sizeof( hash ), &hash_len ) );
1859
1860 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1861 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1862 PSA_ERROR_BAD_STATE );
1863 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1864 PSA_ERROR_BAD_STATE );
1865
1866exit:
1867 psa_hash_abort( &op_target );
1868 psa_hash_abort( &op_init );
1869 psa_hash_abort( &op_setup );
1870 psa_hash_abort( &op_finished );
1871 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001872 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001873}
1874/* END_CASE */
1875
itayzafrir58028322018-10-25 10:22:01 +03001876/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001877void mac_operation_init( )
1878{
Jaeden Amero252ef282019-02-15 14:05:35 +00001879 const uint8_t input[1] = { 0 };
1880
Jaeden Amero769ce272019-01-04 11:48:03 +00001881 /* Test each valid way of initializing the object, except for `= {0}`, as
1882 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1883 * though it's OK by the C standard. We could test for this, but we'd need
1884 * to supress the Clang warning for the test. */
1885 psa_mac_operation_t func = psa_mac_operation_init( );
1886 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1887 psa_mac_operation_t zero;
1888
1889 memset( &zero, 0, sizeof( zero ) );
1890
Jaeden Amero252ef282019-02-15 14:05:35 +00001891 /* A freshly-initialized MAC operation should not be usable. */
1892 TEST_EQUAL( psa_mac_update( &func,
1893 input, sizeof( input ) ),
1894 PSA_ERROR_BAD_STATE );
1895 TEST_EQUAL( psa_mac_update( &init,
1896 input, sizeof( input ) ),
1897 PSA_ERROR_BAD_STATE );
1898 TEST_EQUAL( psa_mac_update( &zero,
1899 input, sizeof( input ) ),
1900 PSA_ERROR_BAD_STATE );
1901
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001902 /* A default MAC operation should be abortable without error. */
1903 PSA_ASSERT( psa_mac_abort( &func ) );
1904 PSA_ASSERT( psa_mac_abort( &init ) );
1905 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001906}
1907/* END_CASE */
1908
1909/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001910void mac_setup( int key_type_arg,
1911 data_t *key,
1912 int alg_arg,
1913 int expected_status_arg )
1914{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001915 psa_key_type_t key_type = key_type_arg;
1916 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001917 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001918 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001919 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1920#if defined(KNOWN_SUPPORTED_MAC_ALG)
1921 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1922#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001923
Gilles Peskine8817f612018-12-18 00:18:46 +01001924 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001925
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001926 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1927 &operation, &status ) )
1928 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001929 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001930
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001931 /* The operation object should be reusable. */
1932#if defined(KNOWN_SUPPORTED_MAC_ALG)
1933 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1934 smoke_test_key_data,
1935 sizeof( smoke_test_key_data ),
1936 KNOWN_SUPPORTED_MAC_ALG,
1937 &operation, &status ) )
1938 goto exit;
1939 TEST_EQUAL( status, PSA_SUCCESS );
1940#endif
1941
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001942exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001943 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001944}
1945/* END_CASE */
1946
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001947/* 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 +00001948void mac_bad_order( )
1949{
Ronald Cron5425a212020-08-04 14:58:35 +02001950 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001951 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1952 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001953 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001954 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1955 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1956 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001957 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001958 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1959 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1960 size_t sign_mac_length = 0;
1961 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1962 const uint8_t verify_mac[] = {
1963 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1964 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1965 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1966
1967 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001968 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001969 psa_set_key_algorithm( &attributes, alg );
1970 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001971
Ronald Cron5425a212020-08-04 14:58:35 +02001972 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1973 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001974
Jaeden Amero252ef282019-02-15 14:05:35 +00001975 /* Call update without calling setup beforehand. */
1976 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1977 PSA_ERROR_BAD_STATE );
1978 PSA_ASSERT( psa_mac_abort( &operation ) );
1979
1980 /* Call sign finish without calling setup beforehand. */
1981 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1982 &sign_mac_length),
1983 PSA_ERROR_BAD_STATE );
1984 PSA_ASSERT( psa_mac_abort( &operation ) );
1985
1986 /* Call verify finish without calling setup beforehand. */
1987 TEST_EQUAL( psa_mac_verify_finish( &operation,
1988 verify_mac, sizeof( verify_mac ) ),
1989 PSA_ERROR_BAD_STATE );
1990 PSA_ASSERT( psa_mac_abort( &operation ) );
1991
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001992 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001993 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
1994 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001995 PSA_ERROR_BAD_STATE );
1996 PSA_ASSERT( psa_mac_abort( &operation ) );
1997
Jaeden Amero252ef282019-02-15 14:05:35 +00001998 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001999 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002000 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2001 PSA_ASSERT( psa_mac_sign_finish( &operation,
2002 sign_mac, sizeof( sign_mac ),
2003 &sign_mac_length ) );
2004 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2005 PSA_ERROR_BAD_STATE );
2006 PSA_ASSERT( psa_mac_abort( &operation ) );
2007
2008 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002009 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002010 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2011 PSA_ASSERT( psa_mac_verify_finish( &operation,
2012 verify_mac, sizeof( verify_mac ) ) );
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 sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002018 PSA_ASSERT( psa_mac_sign_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_sign_finish( &operation,
2021 sign_mac, sizeof( sign_mac ),
2022 &sign_mac_length ) );
2023 TEST_EQUAL( psa_mac_sign_finish( &operation,
2024 sign_mac, sizeof( sign_mac ),
2025 &sign_mac_length ),
2026 PSA_ERROR_BAD_STATE );
2027 PSA_ASSERT( psa_mac_abort( &operation ) );
2028
2029 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002030 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002031 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2032 PSA_ASSERT( psa_mac_verify_finish( &operation,
2033 verify_mac, sizeof( verify_mac ) ) );
2034 TEST_EQUAL( psa_mac_verify_finish( &operation,
2035 verify_mac, sizeof( verify_mac ) ),
2036 PSA_ERROR_BAD_STATE );
2037 PSA_ASSERT( psa_mac_abort( &operation ) );
2038
2039 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002040 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002041 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2042 TEST_EQUAL( psa_mac_verify_finish( &operation,
2043 verify_mac, sizeof( verify_mac ) ),
2044 PSA_ERROR_BAD_STATE );
2045 PSA_ASSERT( psa_mac_abort( &operation ) );
2046
2047 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002048 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002049 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2050 TEST_EQUAL( psa_mac_sign_finish( &operation,
2051 sign_mac, sizeof( sign_mac ),
2052 &sign_mac_length ),
2053 PSA_ERROR_BAD_STATE );
2054 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002055
Ronald Cron5425a212020-08-04 14:58:35 +02002056 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002057
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002058exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002059 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002060}
2061/* END_CASE */
2062
2063/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002064void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002065 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002066 int alg_arg,
2067 data_t *input,
2068 data_t *expected_mac )
2069{
Ronald Cron5425a212020-08-04 14:58:35 +02002070 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002071 psa_key_type_t key_type = key_type_arg;
2072 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002073 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002074 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002075 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002076 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002077 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002078 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002079 const size_t output_sizes_to_test[] = {
2080 0,
2081 1,
2082 expected_mac->len - 1,
2083 expected_mac->len,
2084 expected_mac->len + 1,
2085 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002086
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002087 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002088 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002089 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002090
Gilles Peskine8817f612018-12-18 00:18:46 +01002091 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002092
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002093 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002094 psa_set_key_algorithm( &attributes, alg );
2095 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002096
Ronald Cron5425a212020-08-04 14:58:35 +02002097 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2098 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002099
Gilles Peskine8b356b52020-08-25 23:44:59 +02002100 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2101 {
2102 const size_t output_size = output_sizes_to_test[i];
2103 psa_status_t expected_status =
2104 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2105 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002106
Chris Jones9634bb12021-01-20 15:56:42 +00002107 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002108 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002109
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002110 /* Calculate the MAC, one-shot case. */
2111 TEST_EQUAL( psa_mac_compute( key, alg,
2112 input->x, input->len,
2113 actual_mac, output_size, &mac_length ),
2114 expected_status );
2115 if( expected_status == PSA_SUCCESS )
2116 {
2117 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2118 actual_mac, mac_length );
2119 }
2120
2121 if( output_size > 0 )
2122 memset( actual_mac, 0, output_size );
2123
2124 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002125 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002126 PSA_ASSERT( psa_mac_update( &operation,
2127 input->x, input->len ) );
2128 TEST_EQUAL( psa_mac_sign_finish( &operation,
2129 actual_mac, output_size,
2130 &mac_length ),
2131 expected_status );
2132 PSA_ASSERT( psa_mac_abort( &operation ) );
2133
2134 if( expected_status == PSA_SUCCESS )
2135 {
2136 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2137 actual_mac, mac_length );
2138 }
2139 mbedtls_free( actual_mac );
2140 actual_mac = NULL;
2141 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002142
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002143exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002144 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002145 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002146 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002147 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002148}
2149/* END_CASE */
2150
2151/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002152void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002153 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002154 int alg_arg,
2155 data_t *input,
2156 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002157{
Ronald Cron5425a212020-08-04 14:58:35 +02002158 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002159 psa_key_type_t key_type = key_type_arg;
2160 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002161 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002162 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002163 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002164
Gilles Peskine69c12672018-06-28 00:07:19 +02002165 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2166
Gilles Peskine8817f612018-12-18 00:18:46 +01002167 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002168
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002169 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002170 psa_set_key_algorithm( &attributes, alg );
2171 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002172
Ronald Cron5425a212020-08-04 14:58:35 +02002173 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2174 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002175
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002176 /* Verify correct MAC, one-shot case. */
2177 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2178 expected_mac->x, expected_mac->len ) );
2179
2180 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002181 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002182 PSA_ASSERT( psa_mac_update( &operation,
2183 input->x, input->len ) );
2184 PSA_ASSERT( psa_mac_verify_finish( &operation,
2185 expected_mac->x,
2186 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002187
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002188 /* Test a MAC that's too short, one-shot case. */
2189 TEST_EQUAL( psa_mac_verify( key, alg,
2190 input->x, input->len,
2191 expected_mac->x,
2192 expected_mac->len - 1 ),
2193 PSA_ERROR_INVALID_SIGNATURE );
2194
2195 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002196 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002197 PSA_ASSERT( psa_mac_update( &operation,
2198 input->x, input->len ) );
2199 TEST_EQUAL( psa_mac_verify_finish( &operation,
2200 expected_mac->x,
2201 expected_mac->len - 1 ),
2202 PSA_ERROR_INVALID_SIGNATURE );
2203
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002204 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002205 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2206 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002207 TEST_EQUAL( psa_mac_verify( key, alg,
2208 input->x, input->len,
2209 perturbed_mac, expected_mac->len + 1 ),
2210 PSA_ERROR_INVALID_SIGNATURE );
2211
2212 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002213 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002214 PSA_ASSERT( psa_mac_update( &operation,
2215 input->x, input->len ) );
2216 TEST_EQUAL( psa_mac_verify_finish( &operation,
2217 perturbed_mac,
2218 expected_mac->len + 1 ),
2219 PSA_ERROR_INVALID_SIGNATURE );
2220
2221 /* Test changing one byte. */
2222 for( size_t i = 0; i < expected_mac->len; i++ )
2223 {
Chris Jones9634bb12021-01-20 15:56:42 +00002224 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002225 perturbed_mac[i] ^= 1;
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002226
2227 TEST_EQUAL( psa_mac_verify( key, alg,
2228 input->x, input->len,
2229 perturbed_mac, expected_mac->len ),
2230 PSA_ERROR_INVALID_SIGNATURE );
2231
Ronald Cron5425a212020-08-04 14:58:35 +02002232 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002233 PSA_ASSERT( psa_mac_update( &operation,
2234 input->x, input->len ) );
2235 TEST_EQUAL( psa_mac_verify_finish( &operation,
2236 perturbed_mac,
2237 expected_mac->len ),
2238 PSA_ERROR_INVALID_SIGNATURE );
2239 perturbed_mac[i] ^= 1;
2240 }
2241
Gilles Peskine8c9def32018-02-08 10:02:12 +01002242exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002243 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002244 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002245 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002246 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002247}
2248/* END_CASE */
2249
2250/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002251void cipher_operation_init( )
2252{
Jaeden Ameroab439972019-02-15 14:12:05 +00002253 const uint8_t input[1] = { 0 };
2254 unsigned char output[1] = { 0 };
2255 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002256 /* Test each valid way of initializing the object, except for `= {0}`, as
2257 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2258 * though it's OK by the C standard. We could test for this, but we'd need
2259 * to supress the Clang warning for the test. */
2260 psa_cipher_operation_t func = psa_cipher_operation_init( );
2261 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2262 psa_cipher_operation_t zero;
2263
2264 memset( &zero, 0, sizeof( zero ) );
2265
Jaeden Ameroab439972019-02-15 14:12:05 +00002266 /* A freshly-initialized cipher operation should not be usable. */
2267 TEST_EQUAL( psa_cipher_update( &func,
2268 input, sizeof( input ),
2269 output, sizeof( output ),
2270 &output_length ),
2271 PSA_ERROR_BAD_STATE );
2272 TEST_EQUAL( psa_cipher_update( &init,
2273 input, sizeof( input ),
2274 output, sizeof( output ),
2275 &output_length ),
2276 PSA_ERROR_BAD_STATE );
2277 TEST_EQUAL( psa_cipher_update( &zero,
2278 input, sizeof( input ),
2279 output, sizeof( output ),
2280 &output_length ),
2281 PSA_ERROR_BAD_STATE );
2282
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002283 /* A default cipher operation should be abortable without error. */
2284 PSA_ASSERT( psa_cipher_abort( &func ) );
2285 PSA_ASSERT( psa_cipher_abort( &init ) );
2286 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002287}
2288/* END_CASE */
2289
2290/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002291void cipher_setup( int key_type_arg,
2292 data_t *key,
2293 int alg_arg,
2294 int expected_status_arg )
2295{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002296 psa_key_type_t key_type = key_type_arg;
2297 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002298 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002299 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002300 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002301#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002302 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2303#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002304
Gilles Peskine8817f612018-12-18 00:18:46 +01002305 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002306
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002307 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2308 &operation, &status ) )
2309 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002310 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002311
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002312 /* The operation object should be reusable. */
2313#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2314 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2315 smoke_test_key_data,
2316 sizeof( smoke_test_key_data ),
2317 KNOWN_SUPPORTED_CIPHER_ALG,
2318 &operation, &status ) )
2319 goto exit;
2320 TEST_EQUAL( status, PSA_SUCCESS );
2321#endif
2322
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002323exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002324 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002325 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002326}
2327/* END_CASE */
2328
Ronald Cronee414c72021-03-18 18:50:08 +01002329/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002330void cipher_bad_order( )
2331{
Ronald Cron5425a212020-08-04 14:58:35 +02002332 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002333 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2334 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002335 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002336 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002337 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002338 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002339 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2340 0xaa, 0xaa, 0xaa, 0xaa };
2341 const uint8_t text[] = {
2342 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2343 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002344 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002345 size_t length = 0;
2346
2347 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002348 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2349 psa_set_key_algorithm( &attributes, alg );
2350 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002351 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2352 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002353
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002354 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002355 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2356 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002357 PSA_ERROR_BAD_STATE );
2358 PSA_ASSERT( psa_cipher_abort( &operation ) );
2359
2360 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002361 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2362 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002363 PSA_ERROR_BAD_STATE );
2364 PSA_ASSERT( psa_cipher_abort( &operation ) );
2365
Jaeden Ameroab439972019-02-15 14:12:05 +00002366 /* Generate an IV without calling setup beforehand. */
2367 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2368 buffer, sizeof( buffer ),
2369 &length ),
2370 PSA_ERROR_BAD_STATE );
2371 PSA_ASSERT( psa_cipher_abort( &operation ) );
2372
2373 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002374 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002375 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2376 buffer, sizeof( buffer ),
2377 &length ) );
2378 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2379 buffer, sizeof( buffer ),
2380 &length ),
2381 PSA_ERROR_BAD_STATE );
2382 PSA_ASSERT( psa_cipher_abort( &operation ) );
2383
2384 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002385 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002386 PSA_ASSERT( psa_cipher_set_iv( &operation,
2387 iv, sizeof( iv ) ) );
2388 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2389 buffer, sizeof( buffer ),
2390 &length ),
2391 PSA_ERROR_BAD_STATE );
2392 PSA_ASSERT( psa_cipher_abort( &operation ) );
2393
2394 /* Set an IV without calling setup beforehand. */
2395 TEST_EQUAL( psa_cipher_set_iv( &operation,
2396 iv, sizeof( iv ) ),
2397 PSA_ERROR_BAD_STATE );
2398 PSA_ASSERT( psa_cipher_abort( &operation ) );
2399
2400 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002401 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002402 PSA_ASSERT( psa_cipher_set_iv( &operation,
2403 iv, sizeof( iv ) ) );
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 generated. */
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_generate_iv( &operation,
2412 buffer, sizeof( buffer ),
2413 &length ) );
2414 TEST_EQUAL( psa_cipher_set_iv( &operation,
2415 iv, sizeof( iv ) ),
2416 PSA_ERROR_BAD_STATE );
2417 PSA_ASSERT( psa_cipher_abort( &operation ) );
2418
2419 /* Call update without calling setup beforehand. */
2420 TEST_EQUAL( psa_cipher_update( &operation,
2421 text, sizeof( text ),
2422 buffer, sizeof( buffer ),
2423 &length ),
2424 PSA_ERROR_BAD_STATE );
2425 PSA_ASSERT( psa_cipher_abort( &operation ) );
2426
2427 /* Call update without an IV where an IV is required. */
2428 TEST_EQUAL( psa_cipher_update( &operation,
2429 text, sizeof( text ),
2430 buffer, sizeof( buffer ),
2431 &length ),
2432 PSA_ERROR_BAD_STATE );
2433 PSA_ASSERT( psa_cipher_abort( &operation ) );
2434
2435 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002436 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002437 PSA_ASSERT( psa_cipher_set_iv( &operation,
2438 iv, sizeof( iv ) ) );
2439 PSA_ASSERT( psa_cipher_finish( &operation,
2440 buffer, sizeof( buffer ), &length ) );
2441 TEST_EQUAL( psa_cipher_update( &operation,
2442 text, sizeof( text ),
2443 buffer, sizeof( buffer ),
2444 &length ),
2445 PSA_ERROR_BAD_STATE );
2446 PSA_ASSERT( psa_cipher_abort( &operation ) );
2447
2448 /* Call finish without calling setup beforehand. */
2449 TEST_EQUAL( psa_cipher_finish( &operation,
2450 buffer, sizeof( buffer ), &length ),
2451 PSA_ERROR_BAD_STATE );
2452 PSA_ASSERT( psa_cipher_abort( &operation ) );
2453
2454 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002455 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002456 /* Not calling update means we are encrypting an empty buffer, which is OK
2457 * for cipher modes with padding. */
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 twice in a row. */
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 PSA_ASSERT( psa_cipher_set_iv( &operation,
2466 iv, sizeof( iv ) ) );
2467 PSA_ASSERT( psa_cipher_finish( &operation,
2468 buffer, sizeof( buffer ), &length ) );
2469 TEST_EQUAL( psa_cipher_finish( &operation,
2470 buffer, sizeof( buffer ), &length ),
2471 PSA_ERROR_BAD_STATE );
2472 PSA_ASSERT( psa_cipher_abort( &operation ) );
2473
Ronald Cron5425a212020-08-04 14:58:35 +02002474 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002475
Jaeden Ameroab439972019-02-15 14:12:05 +00002476exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002477 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002478 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002479}
2480/* END_CASE */
2481
2482/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002483void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002484 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002485 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002486 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002487{
Ronald Cron5425a212020-08-04 14:58:35 +02002488 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002489 psa_status_t status;
2490 psa_key_type_t key_type = key_type_arg;
2491 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002492 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002493 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002494 size_t output_buffer_size = 0;
2495 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002496 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002497 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002498 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002499
Gilles Peskine8817f612018-12-18 00:18:46 +01002500 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002501
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002502 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2503 psa_set_key_algorithm( &attributes, alg );
2504 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002505
Ronald Cron5425a212020-08-04 14:58:35 +02002506 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2507 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002508
Ronald Cron5425a212020-08-04 14:58:35 +02002509 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002510
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002511 if( iv->len > 0 )
2512 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002513 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002514 }
2515
gabor-mezei-armceface22021-01-21 12:26:17 +01002516 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2517 TEST_ASSERT( output_buffer_size <=
2518 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002519 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002520
Gilles Peskine8817f612018-12-18 00:18:46 +01002521 PSA_ASSERT( psa_cipher_update( &operation,
2522 input->x, input->len,
2523 output, output_buffer_size,
2524 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002525 TEST_ASSERT( function_output_length <=
2526 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2527 TEST_ASSERT( function_output_length <=
2528 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002529 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002530
Gilles Peskine50e586b2018-06-08 14:28:46 +02002531 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002532 ( output_buffer_size == 0 ? NULL :
2533 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002534 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002535 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002536 TEST_ASSERT( function_output_length <=
2537 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2538 TEST_ASSERT( function_output_length <=
2539 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002540 total_output_length += function_output_length;
2541
Gilles Peskinefe11b722018-12-18 00:24:04 +01002542 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002543 if( expected_status == PSA_SUCCESS )
2544 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002545 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002546 ASSERT_COMPARE( expected_output->x, expected_output->len,
2547 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002548 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002549
Gilles Peskine50e586b2018-06-08 14:28:46 +02002550exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002551 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002552 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002553 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002554 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002555}
2556/* END_CASE */
2557
2558/* BEGIN_CASE */
2559void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002560 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002561 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002562 int first_part_size_arg,
2563 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002564 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002565{
Ronald Cron5425a212020-08-04 14:58:35 +02002566 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002567 psa_key_type_t key_type = key_type_arg;
2568 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002569 size_t first_part_size = first_part_size_arg;
2570 size_t output1_length = output1_length_arg;
2571 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002572 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002573 size_t output_buffer_size = 0;
2574 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002575 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002576 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002577 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002578
Gilles Peskine8817f612018-12-18 00:18:46 +01002579 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002580
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002581 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2582 psa_set_key_algorithm( &attributes, alg );
2583 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002584
Ronald Cron5425a212020-08-04 14:58:35 +02002585 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2586 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002587
Ronald Cron5425a212020-08-04 14:58:35 +02002588 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002589
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002590 if( iv->len > 0 )
2591 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002592 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002593 }
2594
gabor-mezei-armceface22021-01-21 12:26:17 +01002595 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2596 TEST_ASSERT( output_buffer_size <=
2597 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002598 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002599
Gilles Peskinee0866522019-02-19 19:44:00 +01002600 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002601 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2602 output, output_buffer_size,
2603 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002604 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002605 TEST_ASSERT( function_output_length <=
2606 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2607 TEST_ASSERT( function_output_length <=
2608 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002609 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002610
Gilles Peskine8817f612018-12-18 00:18:46 +01002611 PSA_ASSERT( psa_cipher_update( &operation,
2612 input->x + first_part_size,
2613 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002614 ( output_buffer_size == 0 ? NULL :
2615 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002616 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002617 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002618 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002619 TEST_ASSERT( function_output_length <=
2620 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2621 alg,
2622 input->len - first_part_size ) );
2623 TEST_ASSERT( function_output_length <=
2624 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002625 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002626
Gilles Peskine8817f612018-12-18 00:18:46 +01002627 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002628 ( output_buffer_size == 0 ? NULL :
2629 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002630 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002631 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002632 TEST_ASSERT( function_output_length <=
2633 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2634 TEST_ASSERT( function_output_length <=
2635 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002636 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002637 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002638
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002639 ASSERT_COMPARE( expected_output->x, expected_output->len,
2640 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002641
2642exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002643 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002644 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002645 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002646 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002647}
2648/* END_CASE */
2649
2650/* BEGIN_CASE */
2651void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002652 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002653 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002654 int first_part_size_arg,
2655 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002656 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002657{
Ronald Cron5425a212020-08-04 14:58:35 +02002658 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002659 psa_key_type_t key_type = key_type_arg;
2660 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002661 size_t first_part_size = first_part_size_arg;
2662 size_t output1_length = output1_length_arg;
2663 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002664 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002665 size_t output_buffer_size = 0;
2666 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002667 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002668 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002669 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002670
Gilles Peskine8817f612018-12-18 00:18:46 +01002671 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002672
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002673 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2674 psa_set_key_algorithm( &attributes, alg );
2675 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002676
Ronald Cron5425a212020-08-04 14:58:35 +02002677 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2678 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002679
Ronald Cron5425a212020-08-04 14:58:35 +02002680 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002681
Steven Cooreman177deba2020-09-07 17:14:14 +02002682 if( iv->len > 0 )
2683 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002684 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002685 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002686
gabor-mezei-armceface22021-01-21 12:26:17 +01002687 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2688 TEST_ASSERT( output_buffer_size <=
2689 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002690 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002691
Gilles Peskinee0866522019-02-19 19:44:00 +01002692 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002693 PSA_ASSERT( psa_cipher_update( &operation,
2694 input->x, first_part_size,
2695 output, output_buffer_size,
2696 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002697 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002698 TEST_ASSERT( function_output_length <=
2699 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2700 TEST_ASSERT( function_output_length <=
2701 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002702 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002703
Gilles Peskine8817f612018-12-18 00:18:46 +01002704 PSA_ASSERT( psa_cipher_update( &operation,
2705 input->x + first_part_size,
2706 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002707 ( output_buffer_size == 0 ? NULL :
2708 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002709 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002710 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002711 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002712 TEST_ASSERT( function_output_length <=
2713 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2714 alg,
2715 input->len - first_part_size ) );
2716 TEST_ASSERT( function_output_length <=
2717 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002718 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002719
Gilles Peskine8817f612018-12-18 00:18:46 +01002720 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002721 ( output_buffer_size == 0 ? NULL :
2722 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002723 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002724 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002725 TEST_ASSERT( function_output_length <=
2726 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2727 TEST_ASSERT( function_output_length <=
2728 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002729 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002730 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002731
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002732 ASSERT_COMPARE( expected_output->x, expected_output->len,
2733 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002734
2735exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002736 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002737 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002738 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002739 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002740}
2741/* END_CASE */
2742
Gilles Peskine50e586b2018-06-08 14:28:46 +02002743/* BEGIN_CASE */
2744void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002745 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002746 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002747 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002748{
Ronald Cron5425a212020-08-04 14:58:35 +02002749 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002750 psa_status_t status;
2751 psa_key_type_t key_type = key_type_arg;
2752 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002753 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002754 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002755 size_t output_buffer_size = 0;
2756 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002757 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002758 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002759 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002760
Gilles Peskine8817f612018-12-18 00:18:46 +01002761 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002762
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002763 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2764 psa_set_key_algorithm( &attributes, alg );
2765 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002766
Ronald Cron5425a212020-08-04 14:58:35 +02002767 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2768 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002769
Ronald Cron5425a212020-08-04 14:58:35 +02002770 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002771
Steven Cooreman177deba2020-09-07 17:14:14 +02002772 if( iv->len > 0 )
2773 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002774 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002775 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002776
gabor-mezei-armceface22021-01-21 12:26:17 +01002777 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2778 TEST_ASSERT( output_buffer_size <=
2779 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002780 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002781
Gilles Peskine8817f612018-12-18 00:18:46 +01002782 PSA_ASSERT( psa_cipher_update( &operation,
2783 input->x, input->len,
2784 output, output_buffer_size,
2785 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002786 TEST_ASSERT( function_output_length <=
2787 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2788 TEST_ASSERT( function_output_length <=
2789 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002790 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002791
Gilles Peskine50e586b2018-06-08 14:28:46 +02002792 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002793 ( output_buffer_size == 0 ? NULL :
2794 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002795 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002796 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002797 TEST_ASSERT( function_output_length <=
2798 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2799 TEST_ASSERT( function_output_length <=
2800 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002801 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002802 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002803
2804 if( expected_status == PSA_SUCCESS )
2805 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002806 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002807 ASSERT_COMPARE( expected_output->x, expected_output->len,
2808 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002809 }
2810
Gilles Peskine50e586b2018-06-08 14:28:46 +02002811exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002812 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002813 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002814 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002815 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002816}
2817/* END_CASE */
2818
Gilles Peskine50e586b2018-06-08 14:28:46 +02002819/* BEGIN_CASE */
2820void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002821 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002822 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002823{
Ronald Cron5425a212020-08-04 14:58:35 +02002824 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002825 psa_key_type_t key_type = key_type_arg;
2826 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002827 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002828 size_t iv_size = 16;
2829 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002830 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002831 size_t output1_size = 0;
2832 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002833 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002834 size_t output2_size = 0;
2835 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002836 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002837 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2838 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002839 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002840
Gilles Peskine8817f612018-12-18 00:18:46 +01002841 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002842
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002843 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2844 psa_set_key_algorithm( &attributes, alg );
2845 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002846
Ronald Cron5425a212020-08-04 14:58:35 +02002847 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2848 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002849
Ronald Cron5425a212020-08-04 14:58:35 +02002850 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2851 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002852
Steven Cooreman177deba2020-09-07 17:14:14 +02002853 if( alg != PSA_ALG_ECB_NO_PADDING )
2854 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002855 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2856 iv, iv_size,
2857 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002858 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002859 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2860 TEST_ASSERT( output1_size <=
2861 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002862 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002863
Gilles Peskine8817f612018-12-18 00:18:46 +01002864 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2865 output1, output1_size,
2866 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002867 TEST_ASSERT( output1_length <=
2868 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2869 TEST_ASSERT( output1_length <=
2870 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2871
Gilles Peskine8817f612018-12-18 00:18:46 +01002872 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002873 output1 + output1_length,
2874 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002875 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002876 TEST_ASSERT( function_output_length <=
2877 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2878 TEST_ASSERT( function_output_length <=
2879 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002880
Gilles Peskine048b7f02018-06-08 14:20:49 +02002881 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002882
Gilles Peskine8817f612018-12-18 00:18:46 +01002883 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002884
2885 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002886 TEST_ASSERT( output2_size <=
2887 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2888 TEST_ASSERT( output2_size <=
2889 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002890 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002891
Steven Cooreman177deba2020-09-07 17:14:14 +02002892 if( iv_length > 0 )
2893 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002894 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2895 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002896 }
2897
Gilles Peskine8817f612018-12-18 00:18:46 +01002898 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2899 output2, output2_size,
2900 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002901 TEST_ASSERT( output2_length <=
2902 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
2903 TEST_ASSERT( output2_length <=
2904 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
2905
Gilles Peskine048b7f02018-06-08 14:20:49 +02002906 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002907 PSA_ASSERT( psa_cipher_finish( &operation2,
2908 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002909 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002910 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002911 TEST_ASSERT( function_output_length <=
2912 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2913 TEST_ASSERT( function_output_length <=
2914 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03002915
Gilles Peskine048b7f02018-06-08 14:20:49 +02002916 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002917
Gilles Peskine8817f612018-12-18 00:18:46 +01002918 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002919
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002920 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002921
2922exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002923 psa_cipher_abort( &operation1 );
2924 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002925 mbedtls_free( output1 );
2926 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002927 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002928 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03002929}
2930/* END_CASE */
2931
2932/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002933void cipher_verify_output_multipart( int alg_arg,
2934 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002935 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002936 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002937 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03002938{
Ronald Cron5425a212020-08-04 14:58:35 +02002939 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002940 psa_key_type_t key_type = key_type_arg;
2941 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002942 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002943 unsigned char iv[16] = {0};
2944 size_t iv_size = 16;
2945 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002946 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002947 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002948 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002949 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002950 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002951 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002952 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002953 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2954 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002955 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002956
Gilles Peskine8817f612018-12-18 00:18:46 +01002957 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002958
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002959 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2960 psa_set_key_algorithm( &attributes, alg );
2961 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002962
Ronald Cron5425a212020-08-04 14:58:35 +02002963 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2964 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03002965
Ronald Cron5425a212020-08-04 14:58:35 +02002966 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2967 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002968
Steven Cooreman177deba2020-09-07 17:14:14 +02002969 if( alg != PSA_ALG_ECB_NO_PADDING )
2970 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002971 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2972 iv, iv_size,
2973 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002974 }
2975
gabor-mezei-armceface22021-01-21 12:26:17 +01002976 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2977 TEST_ASSERT( output1_buffer_size <=
2978 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002979 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002980
Gilles Peskinee0866522019-02-19 19:44:00 +01002981 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002982
Gilles Peskine8817f612018-12-18 00:18:46 +01002983 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2984 output1, output1_buffer_size,
2985 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002986 TEST_ASSERT( function_output_length <=
2987 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2988 TEST_ASSERT( function_output_length <=
2989 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002990 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002991
Gilles Peskine8817f612018-12-18 00:18:46 +01002992 PSA_ASSERT( psa_cipher_update( &operation1,
2993 input->x + first_part_size,
2994 input->len - first_part_size,
2995 output1, output1_buffer_size,
2996 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002997 TEST_ASSERT( function_output_length <=
2998 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2999 alg,
3000 input->len - first_part_size ) );
3001 TEST_ASSERT( function_output_length <=
3002 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003003 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003004
Gilles Peskine8817f612018-12-18 00:18:46 +01003005 PSA_ASSERT( psa_cipher_finish( &operation1,
3006 output1 + output1_length,
3007 output1_buffer_size - output1_length,
3008 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003009 TEST_ASSERT( function_output_length <=
3010 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3011 TEST_ASSERT( function_output_length <=
3012 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003013 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003014
Gilles Peskine8817f612018-12-18 00:18:46 +01003015 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003016
Gilles Peskine048b7f02018-06-08 14:20:49 +02003017 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003018 TEST_ASSERT( output2_buffer_size <=
3019 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3020 TEST_ASSERT( output2_buffer_size <=
3021 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003022 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003023
Steven Cooreman177deba2020-09-07 17:14:14 +02003024 if( iv_length > 0 )
3025 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003026 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3027 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003028 }
Moran Pekerded84402018-06-06 16:36:50 +03003029
Gilles Peskine8817f612018-12-18 00:18:46 +01003030 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3031 output2, output2_buffer_size,
3032 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003033 TEST_ASSERT( function_output_length <=
3034 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3035 TEST_ASSERT( function_output_length <=
3036 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003037 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003038
Gilles Peskine8817f612018-12-18 00:18:46 +01003039 PSA_ASSERT( psa_cipher_update( &operation2,
3040 output1 + first_part_size,
3041 output1_length - first_part_size,
3042 output2, output2_buffer_size,
3043 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003044 TEST_ASSERT( function_output_length <=
3045 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3046 alg,
3047 output1_length - first_part_size ) );
3048 TEST_ASSERT( function_output_length <=
3049 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003050 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003051
Gilles Peskine8817f612018-12-18 00:18:46 +01003052 PSA_ASSERT( psa_cipher_finish( &operation2,
3053 output2 + output2_length,
3054 output2_buffer_size - output2_length,
3055 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003056 TEST_ASSERT( function_output_length <=
3057 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3058 TEST_ASSERT( function_output_length <=
3059 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003060 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003061
Gilles Peskine8817f612018-12-18 00:18:46 +01003062 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003063
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003064 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003065
3066exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003067 psa_cipher_abort( &operation1 );
3068 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003069 mbedtls_free( output1 );
3070 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003071 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003072 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003073}
3074/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003075
Gilles Peskine20035e32018-02-03 22:44:14 +01003076/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003077void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003078 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003079 data_t *nonce,
3080 data_t *additional_data,
3081 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003082 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003083{
Ronald Cron5425a212020-08-04 14:58:35 +02003084 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003085 psa_key_type_t key_type = key_type_arg;
3086 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003087 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003088 unsigned char *output_data = NULL;
3089 size_t output_size = 0;
3090 size_t output_length = 0;
3091 unsigned char *output_data2 = NULL;
3092 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003093 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003094 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003095 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003096
Gilles Peskine8817f612018-12-18 00:18:46 +01003097 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003098
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003099 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3100 psa_set_key_algorithm( &attributes, alg );
3101 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003102
Gilles Peskine049c7532019-05-15 20:22:09 +02003103 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003104 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003105 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3106 key_bits = psa_get_key_bits( &attributes );
3107
3108 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3109 alg );
3110 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3111 * should be exact. */
3112 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3113 expected_result != PSA_ERROR_NOT_SUPPORTED )
3114 {
3115 TEST_EQUAL( output_size,
3116 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3117 TEST_ASSERT( output_size <=
3118 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3119 }
3120 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003121
Steven Cooremanf49478b2021-02-15 15:19:25 +01003122 status = psa_aead_encrypt( key, alg,
3123 nonce->x, nonce->len,
3124 additional_data->x,
3125 additional_data->len,
3126 input_data->x, input_data->len,
3127 output_data, output_size,
3128 &output_length );
3129
3130 /* If the operation is not supported, just skip and not fail in case the
3131 * encryption involves a common limitation of cryptography hardwares and
3132 * an alternative implementation. */
3133 if( status == PSA_ERROR_NOT_SUPPORTED )
3134 {
3135 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3136 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3137 }
3138
3139 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003140
3141 if( PSA_SUCCESS == expected_result )
3142 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003143 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003144
Gilles Peskine003a4a92019-05-14 16:09:40 +02003145 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3146 * should be exact. */
3147 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003148 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003149
gabor-mezei-armceface22021-01-21 12:26:17 +01003150 TEST_ASSERT( input_data->len <=
3151 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3152
Ronald Cron5425a212020-08-04 14:58:35 +02003153 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003154 nonce->x, nonce->len,
3155 additional_data->x,
3156 additional_data->len,
3157 output_data, output_length,
3158 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003159 &output_length2 ),
3160 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003161
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003162 ASSERT_COMPARE( input_data->x, input_data->len,
3163 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003164 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003165
Gilles Peskinea1cac842018-06-11 19:33:02 +02003166exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003167 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003168 mbedtls_free( output_data );
3169 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003170 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003171}
3172/* END_CASE */
3173
3174/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003175void aead_encrypt( int key_type_arg, data_t *key_data,
3176 int alg_arg,
3177 data_t *nonce,
3178 data_t *additional_data,
3179 data_t *input_data,
3180 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003181{
Ronald Cron5425a212020-08-04 14:58:35 +02003182 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003183 psa_key_type_t key_type = key_type_arg;
3184 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003185 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003186 unsigned char *output_data = NULL;
3187 size_t output_size = 0;
3188 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003189 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003190 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003191
Gilles Peskine8817f612018-12-18 00:18:46 +01003192 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003193
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003194 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3195 psa_set_key_algorithm( &attributes, alg );
3196 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003197
Gilles Peskine049c7532019-05-15 20:22:09 +02003198 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003199 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003200 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3201 key_bits = psa_get_key_bits( &attributes );
3202
3203 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3204 alg );
3205 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3206 * should be exact. */
3207 TEST_EQUAL( output_size,
3208 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3209 TEST_ASSERT( output_size <=
3210 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3211 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003212
Steven Cooremand588ea12021-01-11 19:36:04 +01003213 status = psa_aead_encrypt( key, alg,
3214 nonce->x, nonce->len,
3215 additional_data->x, additional_data->len,
3216 input_data->x, input_data->len,
3217 output_data, output_size,
3218 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003219
Ronald Cron28a45ed2021-02-09 20:35:42 +01003220 /* If the operation is not supported, just skip and not fail in case the
3221 * encryption involves a common limitation of cryptography hardwares and
3222 * an alternative implementation. */
3223 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003224 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003225 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3226 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003227 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003228
3229 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003230 ASSERT_COMPARE( expected_result->x, expected_result->len,
3231 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003232
Gilles Peskinea1cac842018-06-11 19:33:02 +02003233exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003234 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003235 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003236 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003237}
3238/* END_CASE */
3239
3240/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003241void aead_decrypt( int key_type_arg, data_t *key_data,
3242 int alg_arg,
3243 data_t *nonce,
3244 data_t *additional_data,
3245 data_t *input_data,
3246 data_t *expected_data,
3247 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003248{
Ronald Cron5425a212020-08-04 14:58:35 +02003249 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003250 psa_key_type_t key_type = key_type_arg;
3251 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003252 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003253 unsigned char *output_data = NULL;
3254 size_t output_size = 0;
3255 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003256 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003257 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003258 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003259
Gilles Peskine8817f612018-12-18 00:18:46 +01003260 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003261
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003262 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3263 psa_set_key_algorithm( &attributes, alg );
3264 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003265
Gilles Peskine049c7532019-05-15 20:22:09 +02003266 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003267 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003268 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3269 key_bits = psa_get_key_bits( &attributes );
3270
3271 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3272 alg );
3273 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3274 expected_result != PSA_ERROR_NOT_SUPPORTED )
3275 {
3276 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3277 * should be exact. */
3278 TEST_EQUAL( output_size,
3279 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3280 TEST_ASSERT( output_size <=
3281 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3282 }
3283 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003284
Steven Cooremand588ea12021-01-11 19:36:04 +01003285 status = psa_aead_decrypt( key, alg,
3286 nonce->x, nonce->len,
3287 additional_data->x,
3288 additional_data->len,
3289 input_data->x, input_data->len,
3290 output_data, output_size,
3291 &output_length );
3292
Ronald Cron28a45ed2021-02-09 20:35:42 +01003293 /* If the operation is not supported, just skip and not fail in case the
3294 * decryption involves a common limitation of cryptography hardwares and
3295 * an alternative implementation. */
3296 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003297 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003298 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3299 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003300 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003301
3302 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003303
Gilles Peskine2d277862018-06-18 15:41:12 +02003304 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003305 ASSERT_COMPARE( expected_data->x, expected_data->len,
3306 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003307
Gilles Peskinea1cac842018-06-11 19:33:02 +02003308exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003309 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003310 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003311 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003312}
3313/* END_CASE */
3314
3315/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003316void signature_size( int type_arg,
3317 int bits,
3318 int alg_arg,
3319 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003320{
3321 psa_key_type_t type = type_arg;
3322 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003323 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003324
Gilles Peskinefe11b722018-12-18 00:24:04 +01003325 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003326#if defined(MBEDTLS_TEST_DEPRECATED)
3327 TEST_EQUAL( actual_size,
3328 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3329#endif /* MBEDTLS_TEST_DEPRECATED */
3330
Gilles Peskinee59236f2018-01-27 23:32:46 +01003331exit:
3332 ;
3333}
3334/* END_CASE */
3335
3336/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003337void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3338 int alg_arg, data_t *input_data,
3339 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003340{
Ronald Cron5425a212020-08-04 14:58:35 +02003341 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003342 psa_key_type_t key_type = key_type_arg;
3343 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003344 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003345 unsigned char *signature = NULL;
3346 size_t signature_size;
3347 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003348 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003349
Gilles Peskine8817f612018-12-18 00:18:46 +01003350 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003351
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003352 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003353 psa_set_key_algorithm( &attributes, alg );
3354 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003355
Gilles Peskine049c7532019-05-15 20:22:09 +02003356 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003357 &key ) );
3358 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003359 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003360
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003361 /* Allocate a buffer which has the size advertized by the
3362 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003363 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003364 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003365 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003366 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003367 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003368
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003369 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003370 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003371 input_data->x, input_data->len,
3372 signature, signature_size,
3373 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003374 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003375 ASSERT_COMPARE( output_data->x, output_data->len,
3376 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003377
Gilles Peskine0627f982019-11-26 19:12:16 +01003378#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003379 memset( signature, 0, signature_size );
3380 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003381 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003382 input_data->x, input_data->len,
3383 signature, signature_size,
3384 &signature_length ) );
3385 ASSERT_COMPARE( output_data->x, output_data->len,
3386 signature, signature_length );
3387#endif /* MBEDTLS_TEST_DEPRECATED */
3388
Gilles Peskine20035e32018-02-03 22:44:14 +01003389exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003390 /*
3391 * Key attributes may have been returned by psa_get_key_attributes()
3392 * thus reset them as required.
3393 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003394 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003395
Ronald Cron5425a212020-08-04 14:58:35 +02003396 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003397 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003398 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003399}
3400/* END_CASE */
3401
3402/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003403void sign_hash_fail( int key_type_arg, data_t *key_data,
3404 int alg_arg, data_t *input_data,
3405 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003406{
Ronald Cron5425a212020-08-04 14:58:35 +02003407 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003408 psa_key_type_t key_type = key_type_arg;
3409 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003410 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003411 psa_status_t actual_status;
3412 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003413 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003414 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003415 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003416
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003417 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003418
Gilles Peskine8817f612018-12-18 00:18:46 +01003419 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003420
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003421 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003422 psa_set_key_algorithm( &attributes, alg );
3423 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003424
Gilles Peskine049c7532019-05-15 20:22:09 +02003425 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003426 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003427
Ronald Cron5425a212020-08-04 14:58:35 +02003428 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003429 input_data->x, input_data->len,
3430 signature, signature_size,
3431 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003432 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003433 /* The value of *signature_length is unspecified on error, but
3434 * whatever it is, it should be less than signature_size, so that
3435 * if the caller tries to read *signature_length bytes without
3436 * checking the error code then they don't overflow a buffer. */
3437 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003438
Gilles Peskine895242b2019-11-29 12:15:40 +01003439#if defined(MBEDTLS_TEST_DEPRECATED)
3440 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003441 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003442 input_data->x, input_data->len,
3443 signature, signature_size,
3444 &signature_length ),
3445 expected_status );
3446 TEST_ASSERT( signature_length <= signature_size );
3447#endif /* MBEDTLS_TEST_DEPRECATED */
3448
Gilles Peskine20035e32018-02-03 22:44:14 +01003449exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003450 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003451 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003452 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003453 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003454}
3455/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003456
3457/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003458void sign_verify_hash( int key_type_arg, data_t *key_data,
3459 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003460{
Ronald Cron5425a212020-08-04 14:58:35 +02003461 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003462 psa_key_type_t key_type = key_type_arg;
3463 psa_algorithm_t alg = alg_arg;
3464 size_t key_bits;
3465 unsigned char *signature = NULL;
3466 size_t signature_size;
3467 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003468 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003469
Gilles Peskine8817f612018-12-18 00:18:46 +01003470 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003471
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003472 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003473 psa_set_key_algorithm( &attributes, alg );
3474 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003475
Gilles Peskine049c7532019-05-15 20:22:09 +02003476 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003477 &key ) );
3478 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003479 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003480
3481 /* Allocate a buffer which has the size advertized by the
3482 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003483 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003484 key_bits, alg );
3485 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003486 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003487 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003488
3489 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003490 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003491 input_data->x, input_data->len,
3492 signature, signature_size,
3493 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003494 /* Check that the signature length looks sensible. */
3495 TEST_ASSERT( signature_length <= signature_size );
3496 TEST_ASSERT( signature_length > 0 );
3497
3498 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003499 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003500 input_data->x, input_data->len,
3501 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003502
3503 if( input_data->len != 0 )
3504 {
3505 /* Flip a bit in the input and verify that the signature is now
3506 * detected as invalid. Flip a bit at the beginning, not at the end,
3507 * because ECDSA may ignore the last few bits of the input. */
3508 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003509 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003510 input_data->x, input_data->len,
3511 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003512 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003513 }
3514
3515exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003516 /*
3517 * Key attributes may have been returned by psa_get_key_attributes()
3518 * thus reset them as required.
3519 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003520 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003521
Ronald Cron5425a212020-08-04 14:58:35 +02003522 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003523 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003524 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003525}
3526/* END_CASE */
3527
3528/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003529void verify_hash( int key_type_arg, data_t *key_data,
3530 int alg_arg, data_t *hash_data,
3531 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003532{
Ronald Cron5425a212020-08-04 14:58:35 +02003533 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003534 psa_key_type_t key_type = key_type_arg;
3535 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003536 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003537
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003538 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003539
Gilles Peskine8817f612018-12-18 00:18:46 +01003540 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003541
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003542 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003543 psa_set_key_algorithm( &attributes, alg );
3544 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003545
Gilles Peskine049c7532019-05-15 20:22:09 +02003546 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003547 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003548
Ronald Cron5425a212020-08-04 14:58:35 +02003549 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003550 hash_data->x, hash_data->len,
3551 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003552
3553#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003554 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003555 hash_data->x, hash_data->len,
3556 signature_data->x,
3557 signature_data->len ) );
3558
3559#endif /* MBEDTLS_TEST_DEPRECATED */
3560
itayzafrir5c753392018-05-08 11:18:38 +03003561exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003562 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003563 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003564 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003565}
3566/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003567
3568/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003569void verify_hash_fail( int key_type_arg, data_t *key_data,
3570 int alg_arg, data_t *hash_data,
3571 data_t *signature_data,
3572 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003573{
Ronald Cron5425a212020-08-04 14:58:35 +02003574 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003575 psa_key_type_t key_type = key_type_arg;
3576 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003577 psa_status_t actual_status;
3578 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003579 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003580
Gilles Peskine8817f612018-12-18 00:18:46 +01003581 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003582
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003583 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003584 psa_set_key_algorithm( &attributes, alg );
3585 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003586
Gilles Peskine049c7532019-05-15 20:22:09 +02003587 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003588 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003589
Ronald Cron5425a212020-08-04 14:58:35 +02003590 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003591 hash_data->x, hash_data->len,
3592 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003593 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003594
Gilles Peskine895242b2019-11-29 12:15:40 +01003595#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003596 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003597 hash_data->x, hash_data->len,
3598 signature_data->x, signature_data->len ),
3599 expected_status );
3600#endif /* MBEDTLS_TEST_DEPRECATED */
3601
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003602exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003603 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003604 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003605 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003606}
3607/* END_CASE */
3608
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003609/* BEGIN_CASE */
gabor-mezei-armabd72582021-04-15 18:19:50 +02003610void sign_message_deterministic( int key_type_arg,
3611 data_t *key_data,
3612 int alg_arg,
3613 data_t *input_data,
3614 data_t *output_data )
3615{
3616 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3617 psa_key_type_t key_type = key_type_arg;
3618 psa_algorithm_t alg = alg_arg;
3619 size_t key_bits;
3620 unsigned char *signature = NULL;
3621 size_t signature_size;
3622 size_t signature_length = 0xdeadbeef;
3623 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3624
3625 PSA_ASSERT( psa_crypto_init( ) );
3626
3627 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3628 psa_set_key_algorithm( &attributes, alg );
3629 psa_set_key_type( &attributes, key_type );
3630
3631 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3632 &key ) );
3633 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3634 key_bits = psa_get_key_bits( &attributes );
3635
3636 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3637 TEST_ASSERT( signature_size != 0 );
3638 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3639 ASSERT_ALLOC( signature, signature_size );
3640
3641 PSA_ASSERT( psa_sign_message( key, alg,
3642 input_data->x, input_data->len,
3643 signature, signature_size,
3644 &signature_length ) );
3645
3646 ASSERT_COMPARE( output_data->x, output_data->len,
3647 signature, signature_length );
3648
3649exit:
3650 psa_reset_key_attributes( &attributes );
3651
3652 psa_destroy_key( key );
3653 mbedtls_free( signature );
3654 PSA_DONE( );
3655
3656}
3657/* END_CASE */
3658
3659/* BEGIN_CASE */
3660void sign_message_fail( int key_type_arg,
3661 data_t *key_data,
3662 int alg_arg,
3663 data_t *input_data,
3664 int signature_size_arg,
3665 int expected_status_arg )
3666{
3667 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3668 psa_key_type_t key_type = key_type_arg;
3669 psa_algorithm_t alg = alg_arg;
3670 size_t signature_size = signature_size_arg;
3671 psa_status_t actual_status;
3672 psa_status_t expected_status = expected_status_arg;
3673 unsigned char *signature = NULL;
3674 size_t signature_length = 0xdeadbeef;
3675 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3676
3677 ASSERT_ALLOC( signature, signature_size );
3678
3679 PSA_ASSERT( psa_crypto_init( ) );
3680
3681 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3682 psa_set_key_algorithm( &attributes, alg );
3683 psa_set_key_type( &attributes, key_type );
3684
3685 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3686 &key ) );
3687
3688 actual_status = psa_sign_message( key, alg,
3689 input_data->x, input_data->len,
3690 signature, signature_size,
3691 &signature_length );
3692 TEST_EQUAL( actual_status, expected_status );
3693 /* The value of *signature_length is unspecified on error, but
3694 * whatever it is, it should be less than signature_size, so that
3695 * if the caller tries to read *signature_length bytes without
3696 * checking the error code then they don't overflow a buffer. */
3697 TEST_ASSERT( signature_length <= signature_size );
3698
3699exit:
3700 psa_reset_key_attributes( &attributes );
3701 psa_destroy_key( key );
3702 mbedtls_free( signature );
3703 PSA_DONE( );
3704}
3705/* END_CASE */
3706
3707/* BEGIN_CASE */
3708void sign_verify_message( int key_type_arg,
3709 data_t *key_data,
3710 int alg_arg,
3711 data_t *input_data )
3712{
3713 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3714 psa_key_type_t key_type = key_type_arg;
3715 psa_algorithm_t alg = alg_arg;
3716 size_t key_bits;
3717 unsigned char *signature = NULL;
3718 size_t signature_size;
3719 size_t signature_length = 0xdeadbeef;
3720 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3721
3722 PSA_ASSERT( psa_crypto_init( ) );
3723
3724 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3725 PSA_KEY_USAGE_VERIFY_MESSAGE );
3726 psa_set_key_algorithm( &attributes, alg );
3727 psa_set_key_type( &attributes, key_type );
3728
3729 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3730 &key ) );
3731 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3732 key_bits = psa_get_key_bits( &attributes );
3733
3734 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3735 TEST_ASSERT( signature_size != 0 );
3736 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3737 ASSERT_ALLOC( signature, signature_size );
3738
3739 PSA_ASSERT( psa_sign_message( key, alg,
3740 input_data->x, input_data->len,
3741 signature, signature_size,
3742 &signature_length ) );
3743 TEST_ASSERT( signature_length <= signature_size );
3744 TEST_ASSERT( signature_length > 0 );
3745
3746 PSA_ASSERT( psa_verify_message( key, alg,
3747 input_data->x, input_data->len,
3748 signature, signature_length ) );
3749
3750 if( input_data->len != 0 )
3751 {
3752 /* Flip a bit in the input and verify that the signature is now
3753 * detected as invalid. Flip a bit at the beginning, not at the end,
3754 * because ECDSA may ignore the last few bits of the input. */
3755 input_data->x[0] ^= 1;
3756 TEST_EQUAL( psa_verify_message( key, alg,
3757 input_data->x, input_data->len,
3758 signature, signature_length ),
3759 PSA_ERROR_INVALID_SIGNATURE );
3760 }
3761
3762exit:
3763 psa_reset_key_attributes( &attributes );
3764
3765 psa_destroy_key( key );
3766 mbedtls_free( signature );
3767 PSA_DONE( );
3768}
3769/* END_CASE */
3770
3771/* BEGIN_CASE */
3772void verify_message( int key_type_arg,
3773 data_t *key_data,
3774 int alg_arg,
3775 data_t *input_data,
3776 data_t *signature_data )
3777{
3778 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3779 psa_key_type_t key_type = key_type_arg;
3780 psa_algorithm_t alg = alg_arg;
3781 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3782
3783 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3784
3785 PSA_ASSERT( psa_crypto_init( ) );
3786
3787 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3788 psa_set_key_algorithm( &attributes, alg );
3789 psa_set_key_type( &attributes, key_type );
3790
3791 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3792 &key ) );
3793
3794 PSA_ASSERT( psa_verify_message( key, alg,
3795 input_data->x, input_data->len,
3796 signature_data->x, signature_data->len ) );
3797
3798exit:
3799 psa_reset_key_attributes( &attributes );
3800 psa_destroy_key( key );
3801 PSA_DONE( );
3802}
3803/* END_CASE */
3804
3805/* BEGIN_CASE */
3806void verify_message_fail( int key_type_arg,
3807 data_t *key_data,
3808 int alg_arg,
3809 data_t *hash_data,
3810 data_t *signature_data,
3811 int expected_status_arg )
3812{
3813 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3814 psa_key_type_t key_type = key_type_arg;
3815 psa_algorithm_t alg = alg_arg;
3816 psa_status_t actual_status;
3817 psa_status_t expected_status = expected_status_arg;
3818 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3819
3820 PSA_ASSERT( psa_crypto_init( ) );
3821
3822 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3823 psa_set_key_algorithm( &attributes, alg );
3824 psa_set_key_type( &attributes, key_type );
3825
3826 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3827 &key ) );
3828
3829 actual_status = psa_verify_message( key, alg,
3830 hash_data->x, hash_data->len,
3831 signature_data->x,
3832 signature_data->len );
3833 TEST_EQUAL( actual_status, expected_status );
3834
3835exit:
3836 psa_reset_key_attributes( &attributes );
3837 psa_destroy_key( key );
3838 PSA_DONE( );
3839}
3840/* END_CASE */
3841
3842/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003843void asymmetric_encrypt( int key_type_arg,
3844 data_t *key_data,
3845 int alg_arg,
3846 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003847 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003848 int expected_output_length_arg,
3849 int expected_status_arg )
3850{
Ronald Cron5425a212020-08-04 14:58:35 +02003851 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003852 psa_key_type_t key_type = key_type_arg;
3853 psa_algorithm_t alg = alg_arg;
3854 size_t expected_output_length = expected_output_length_arg;
3855 size_t key_bits;
3856 unsigned char *output = NULL;
3857 size_t output_size;
3858 size_t output_length = ~0;
3859 psa_status_t actual_status;
3860 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003861 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003862
Gilles Peskine8817f612018-12-18 00:18:46 +01003863 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003864
Gilles Peskine656896e2018-06-29 19:12:28 +02003865 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003866 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3867 psa_set_key_algorithm( &attributes, alg );
3868 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003869 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003870 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003871
3872 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003873 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003874 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003875
Gilles Peskine656896e2018-06-29 19:12:28 +02003876 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003877 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003878 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003879
3880 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003881 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003882 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003883 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003884 output, output_size,
3885 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003886 TEST_EQUAL( actual_status, expected_status );
3887 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003888
Gilles Peskine68428122018-06-30 18:42:41 +02003889 /* If the label is empty, the test framework puts a non-null pointer
3890 * in label->x. Test that a null pointer works as well. */
3891 if( label->len == 0 )
3892 {
3893 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003894 if( output_size != 0 )
3895 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003896 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003897 input_data->x, input_data->len,
3898 NULL, label->len,
3899 output, output_size,
3900 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003901 TEST_EQUAL( actual_status, expected_status );
3902 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003903 }
3904
Gilles Peskine656896e2018-06-29 19:12:28 +02003905exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003906 /*
3907 * Key attributes may have been returned by psa_get_key_attributes()
3908 * thus reset them as required.
3909 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003910 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003911
Ronald Cron5425a212020-08-04 14:58:35 +02003912 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02003913 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003914 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003915}
3916/* END_CASE */
3917
3918/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003919void asymmetric_encrypt_decrypt( int key_type_arg,
3920 data_t *key_data,
3921 int alg_arg,
3922 data_t *input_data,
3923 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003924{
Ronald Cron5425a212020-08-04 14:58:35 +02003925 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003926 psa_key_type_t key_type = key_type_arg;
3927 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003928 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003929 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003930 size_t output_size;
3931 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003932 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003933 size_t output2_size;
3934 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003935 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003936
Gilles Peskine8817f612018-12-18 00:18:46 +01003937 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003938
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003939 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3940 psa_set_key_algorithm( &attributes, alg );
3941 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003942
Gilles Peskine049c7532019-05-15 20:22:09 +02003943 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003944 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003945
3946 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02003947 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003948 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003949
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003950 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003951 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003952 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01003953
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003954 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01003955 TEST_ASSERT( output2_size <=
3956 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
3957 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003958 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003959
Gilles Peskineeebd7382018-06-08 18:11:54 +02003960 /* We test encryption by checking that encrypt-then-decrypt gives back
3961 * the original plaintext because of the non-optional random
3962 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02003963 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003964 input_data->x, input_data->len,
3965 label->x, label->len,
3966 output, output_size,
3967 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003968 /* We don't know what ciphertext length to expect, but check that
3969 * it looks sensible. */
3970 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003971
Ronald Cron5425a212020-08-04 14:58:35 +02003972 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003973 output, output_length,
3974 label->x, label->len,
3975 output2, output2_size,
3976 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003977 ASSERT_COMPARE( input_data->x, input_data->len,
3978 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003979
3980exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003981 /*
3982 * Key attributes may have been returned by psa_get_key_attributes()
3983 * thus reset them as required.
3984 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003985 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003986
Ronald Cron5425a212020-08-04 14:58:35 +02003987 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003988 mbedtls_free( output );
3989 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003990 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003991}
3992/* END_CASE */
3993
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003994/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003995void asymmetric_decrypt( int key_type_arg,
3996 data_t *key_data,
3997 int alg_arg,
3998 data_t *input_data,
3999 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004000 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004001{
Ronald Cron5425a212020-08-04 14:58:35 +02004002 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004003 psa_key_type_t key_type = key_type_arg;
4004 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004005 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004006 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004007 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004008 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004009 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004010
Gilles Peskine8817f612018-12-18 00:18:46 +01004011 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004012
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004013 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4014 psa_set_key_algorithm( &attributes, alg );
4015 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004016
Gilles Peskine049c7532019-05-15 20:22:09 +02004017 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004018 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004019
gabor-mezei-armceface22021-01-21 12:26:17 +01004020 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4021 key_bits = psa_get_key_bits( &attributes );
4022
4023 /* Determine the maximum ciphertext length */
4024 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
4025 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
4026 ASSERT_ALLOC( output, output_size );
4027
Ronald Cron5425a212020-08-04 14:58:35 +02004028 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004029 input_data->x, input_data->len,
4030 label->x, label->len,
4031 output,
4032 output_size,
4033 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004034 ASSERT_COMPARE( expected_data->x, expected_data->len,
4035 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004036
Gilles Peskine68428122018-06-30 18:42:41 +02004037 /* If the label is empty, the test framework puts a non-null pointer
4038 * in label->x. Test that a null pointer works as well. */
4039 if( label->len == 0 )
4040 {
4041 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004042 if( output_size != 0 )
4043 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004044 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004045 input_data->x, input_data->len,
4046 NULL, label->len,
4047 output,
4048 output_size,
4049 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004050 ASSERT_COMPARE( expected_data->x, expected_data->len,
4051 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004052 }
4053
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004054exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004055 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004056 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004057 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004058 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004059}
4060/* END_CASE */
4061
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004062/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004063void asymmetric_decrypt_fail( int key_type_arg,
4064 data_t *key_data,
4065 int alg_arg,
4066 data_t *input_data,
4067 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004068 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004069 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004070{
Ronald Cron5425a212020-08-04 14:58:35 +02004071 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004072 psa_key_type_t key_type = key_type_arg;
4073 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004074 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004075 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004076 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004077 psa_status_t actual_status;
4078 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004079 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004080
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004081 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004082
Gilles Peskine8817f612018-12-18 00:18:46 +01004083 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004084
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004085 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4086 psa_set_key_algorithm( &attributes, alg );
4087 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004088
Gilles Peskine049c7532019-05-15 20:22:09 +02004089 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004090 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004091
Ronald Cron5425a212020-08-04 14:58:35 +02004092 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004093 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004094 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004095 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004096 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004097 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004098 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004099
Gilles Peskine68428122018-06-30 18:42:41 +02004100 /* If the label is empty, the test framework puts a non-null pointer
4101 * in label->x. Test that a null pointer works as well. */
4102 if( label->len == 0 )
4103 {
4104 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004105 if( output_size != 0 )
4106 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004107 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004108 input_data->x, input_data->len,
4109 NULL, label->len,
4110 output, output_size,
4111 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004112 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004113 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004114 }
4115
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004116exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004117 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004118 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004119 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004120 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004121}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004122/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004123
4124/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004125void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004126{
4127 /* Test each valid way of initializing the object, except for `= {0}`, as
4128 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4129 * though it's OK by the C standard. We could test for this, but we'd need
4130 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004131 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004132 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4133 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4134 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004135
4136 memset( &zero, 0, sizeof( zero ) );
4137
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004138 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004139 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004140 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004141 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004142 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004143 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004144 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004145
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004146 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004147 PSA_ASSERT( psa_key_derivation_abort(&func) );
4148 PSA_ASSERT( psa_key_derivation_abort(&init) );
4149 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004150}
4151/* END_CASE */
4152
Janos Follath16de4a42019-06-13 16:32:24 +01004153/* BEGIN_CASE */
4154void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004155{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004156 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004157 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004158 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004159
Gilles Peskine8817f612018-12-18 00:18:46 +01004160 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004161
Janos Follath16de4a42019-06-13 16:32:24 +01004162 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004163 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004164
4165exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004166 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004167 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004168}
4169/* END_CASE */
4170
Janos Follathaf3c2a02019-06-12 12:34:34 +01004171/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004172void derive_set_capacity( int alg_arg, int capacity_arg,
4173 int expected_status_arg )
4174{
4175 psa_algorithm_t alg = alg_arg;
4176 size_t capacity = capacity_arg;
4177 psa_status_t expected_status = expected_status_arg;
4178 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4179
4180 PSA_ASSERT( psa_crypto_init( ) );
4181
4182 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4183
4184 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4185 expected_status );
4186
4187exit:
4188 psa_key_derivation_abort( &operation );
4189 PSA_DONE( );
4190}
4191/* END_CASE */
4192
4193/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004194void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004195 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004196 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004197 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004198 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004199 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004200 int expected_status_arg3,
4201 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004202{
4203 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004204 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4205 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004206 psa_status_t expected_statuses[] = {expected_status_arg1,
4207 expected_status_arg2,
4208 expected_status_arg3};
4209 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004210 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4211 MBEDTLS_SVC_KEY_ID_INIT,
4212 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004213 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4214 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4215 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004216 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004217 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004218 psa_status_t expected_output_status = expected_output_status_arg;
4219 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004220
4221 PSA_ASSERT( psa_crypto_init( ) );
4222
4223 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4224 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004225
4226 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4227
4228 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4229 {
Gilles Peskinef6279312021-05-27 13:21:20 +02004230 mbedtls_test_set_step( i );
4231 if( steps[i] == 0 )
4232 {
4233 /* Skip this step */
4234 }
4235 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004236 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004237 psa_set_key_type( &attributes, key_types[i] );
4238 PSA_ASSERT( psa_import_key( &attributes,
4239 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004240 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004241 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4242 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4243 {
4244 // When taking a private key as secret input, use key agreement
4245 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004246 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4247 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004248 expected_statuses[i] );
4249 }
4250 else
4251 {
4252 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004253 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004254 expected_statuses[i] );
4255 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004256 }
4257 else
4258 {
4259 TEST_EQUAL( psa_key_derivation_input_bytes(
4260 &operation, steps[i],
4261 inputs[i]->x, inputs[i]->len ),
4262 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004263 }
4264 }
4265
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004266 if( output_key_type != PSA_KEY_TYPE_NONE )
4267 {
4268 psa_reset_key_attributes( &attributes );
4269 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4270 psa_set_key_bits( &attributes, 8 );
4271 actual_output_status =
4272 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004273 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004274 }
4275 else
4276 {
4277 uint8_t buffer[1];
4278 actual_output_status =
4279 psa_key_derivation_output_bytes( &operation,
4280 buffer, sizeof( buffer ) );
4281 }
4282 TEST_EQUAL( actual_output_status, expected_output_status );
4283
Janos Follathaf3c2a02019-06-12 12:34:34 +01004284exit:
4285 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004286 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4287 psa_destroy_key( keys[i] );
4288 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004289 PSA_DONE( );
4290}
4291/* END_CASE */
4292
Janos Follathd958bb72019-07-03 15:02:16 +01004293/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004294void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004295{
Janos Follathd958bb72019-07-03 15:02:16 +01004296 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004297 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004298 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004299 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004300 unsigned char input1[] = "Input 1";
4301 size_t input1_length = sizeof( input1 );
4302 unsigned char input2[] = "Input 2";
4303 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004304 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004305 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004306 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4307 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4308 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004309 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004310
Gilles Peskine8817f612018-12-18 00:18:46 +01004311 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004312
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004313 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4314 psa_set_key_algorithm( &attributes, alg );
4315 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004316
Gilles Peskine73676cb2019-05-15 20:15:10 +02004317 PSA_ASSERT( psa_import_key( &attributes,
4318 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004319 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004320
4321 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004322 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4323 input1, input1_length,
4324 input2, input2_length,
4325 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004326 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004327
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004328 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004329 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004330 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004331
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004332 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004333
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004334 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004335 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004336
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004337exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004338 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004339 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004340 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004341}
4342/* END_CASE */
4343
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004344/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004345void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004346{
4347 uint8_t output_buffer[16];
4348 size_t buffer_size = 16;
4349 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004350 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004351
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004352 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4353 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004354 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004355
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004356 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004357 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004358
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004359 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
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
4368exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004369 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004370}
4371/* END_CASE */
4372
4373/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004374void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004375 int step1_arg, data_t *input1,
4376 int step2_arg, data_t *input2,
4377 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004378 int requested_capacity_arg,
4379 data_t *expected_output1,
4380 data_t *expected_output2 )
4381{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004382 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004383 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4384 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004385 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4386 MBEDTLS_SVC_KEY_ID_INIT,
4387 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004388 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004389 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004390 uint8_t *expected_outputs[2] =
4391 {expected_output1->x, expected_output2->x};
4392 size_t output_sizes[2] =
4393 {expected_output1->len, expected_output2->len};
4394 size_t output_buffer_size = 0;
4395 uint8_t *output_buffer = NULL;
4396 size_t expected_capacity;
4397 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004398 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004399 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004400 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004401
4402 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4403 {
4404 if( output_sizes[i] > output_buffer_size )
4405 output_buffer_size = output_sizes[i];
4406 if( output_sizes[i] == 0 )
4407 expected_outputs[i] = NULL;
4408 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004409 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004410 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004411
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004412 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4413 psa_set_key_algorithm( &attributes, alg );
4414 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004415
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004416 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004417 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4418 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4419 requested_capacity ) );
4420 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004421 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004422 switch( steps[i] )
4423 {
4424 case 0:
4425 break;
4426 case PSA_KEY_DERIVATION_INPUT_SECRET:
4427 PSA_ASSERT( psa_import_key( &attributes,
4428 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004429 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004430
4431 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4432 {
4433 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4434 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4435 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4436 }
4437
Gilles Peskine1468da72019-05-29 17:35:49 +02004438 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004439 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004440 break;
4441 default:
4442 PSA_ASSERT( psa_key_derivation_input_bytes(
4443 &operation, steps[i],
4444 inputs[i]->x, inputs[i]->len ) );
4445 break;
4446 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004447 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004448
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004449 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004450 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004451 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004452 expected_capacity = requested_capacity;
4453
4454 /* Expansion phase. */
4455 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4456 {
4457 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004458 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004459 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004460 if( expected_capacity == 0 && output_sizes[i] == 0 )
4461 {
4462 /* Reading 0 bytes when 0 bytes are available can go either way. */
4463 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004464 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004465 continue;
4466 }
4467 else if( expected_capacity == 0 ||
4468 output_sizes[i] > expected_capacity )
4469 {
4470 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004471 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004472 expected_capacity = 0;
4473 continue;
4474 }
4475 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004476 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004477 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004478 ASSERT_COMPARE( output_buffer, output_sizes[i],
4479 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004480 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004481 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004482 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004483 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004484 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004485 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004486 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004487
4488exit:
4489 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004490 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004491 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4492 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004493 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004494}
4495/* END_CASE */
4496
4497/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004498void derive_full( int alg_arg,
4499 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004500 data_t *input1,
4501 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004502 int requested_capacity_arg )
4503{
Ronald Cron5425a212020-08-04 14:58:35 +02004504 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004505 psa_algorithm_t alg = alg_arg;
4506 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004507 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004508 unsigned char output_buffer[16];
4509 size_t expected_capacity = requested_capacity;
4510 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004511 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004512
Gilles Peskine8817f612018-12-18 00:18:46 +01004513 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004514
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004515 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4516 psa_set_key_algorithm( &attributes, alg );
4517 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004518
Gilles Peskine049c7532019-05-15 20:22:09 +02004519 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004520 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004521
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004522 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4523 input1->x, input1->len,
4524 input2->x, input2->len,
4525 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004526 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004527
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004528 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004529 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004530 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004531
4532 /* Expansion phase. */
4533 while( current_capacity > 0 )
4534 {
4535 size_t read_size = sizeof( output_buffer );
4536 if( read_size > current_capacity )
4537 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004538 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004539 output_buffer,
4540 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004541 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004542 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004543 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004544 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004545 }
4546
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004547 /* Check that the operation refuses to go over capacity. */
4548 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004549 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004550
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004551 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004552
4553exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004554 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004555 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004556 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004557}
4558/* END_CASE */
4559
Janos Follathe60c9052019-07-03 13:51:30 +01004560/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004561void derive_key_exercise( int alg_arg,
4562 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004563 data_t *input1,
4564 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004565 int derived_type_arg,
4566 int derived_bits_arg,
4567 int derived_usage_arg,
4568 int derived_alg_arg )
4569{
Ronald Cron5425a212020-08-04 14:58:35 +02004570 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4571 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004572 psa_algorithm_t alg = alg_arg;
4573 psa_key_type_t derived_type = derived_type_arg;
4574 size_t derived_bits = derived_bits_arg;
4575 psa_key_usage_t derived_usage = derived_usage_arg;
4576 psa_algorithm_t derived_alg = derived_alg_arg;
4577 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004578 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004579 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004580 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004581
Gilles Peskine8817f612018-12-18 00:18:46 +01004582 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004583
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004584 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4585 psa_set_key_algorithm( &attributes, alg );
4586 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004587 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004588 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004589
4590 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004591 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4592 input1->x, input1->len,
4593 input2->x, input2->len,
4594 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004595 goto exit;
4596
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004597 psa_set_key_usage_flags( &attributes, derived_usage );
4598 psa_set_key_algorithm( &attributes, derived_alg );
4599 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004600 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004601 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004602 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004603
4604 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004605 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004606 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4607 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004608
4609 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004610 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004611 goto exit;
4612
4613exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004614 /*
4615 * Key attributes may have been returned by psa_get_key_attributes()
4616 * thus reset them as required.
4617 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004618 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004619
4620 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004621 psa_destroy_key( base_key );
4622 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004623 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004624}
4625/* END_CASE */
4626
Janos Follath42fd8882019-07-03 14:17:09 +01004627/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004628void derive_key_export( int alg_arg,
4629 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004630 data_t *input1,
4631 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004632 int bytes1_arg,
4633 int bytes2_arg )
4634{
Ronald Cron5425a212020-08-04 14:58:35 +02004635 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4636 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004637 psa_algorithm_t alg = alg_arg;
4638 size_t bytes1 = bytes1_arg;
4639 size_t bytes2 = bytes2_arg;
4640 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004641 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004642 uint8_t *output_buffer = NULL;
4643 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004644 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4645 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004646 size_t length;
4647
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004648 ASSERT_ALLOC( output_buffer, capacity );
4649 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004650 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004651
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004652 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4653 psa_set_key_algorithm( &base_attributes, alg );
4654 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004655 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004656 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004657
4658 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004659 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4660 input1->x, input1->len,
4661 input2->x, input2->len,
4662 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004663 goto exit;
4664
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004665 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004666 output_buffer,
4667 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004668 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004669
4670 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004671 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4672 input1->x, input1->len,
4673 input2->x, input2->len,
4674 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004675 goto exit;
4676
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004677 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4678 psa_set_key_algorithm( &derived_attributes, 0 );
4679 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004680 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004681 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004682 &derived_key ) );
4683 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004684 export_buffer, bytes1,
4685 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004686 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004687 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004688 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004689 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004690 &derived_key ) );
4691 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004692 export_buffer + bytes1, bytes2,
4693 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004694 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004695
4696 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004697 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4698 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004699
4700exit:
4701 mbedtls_free( output_buffer );
4702 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004703 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004704 psa_destroy_key( base_key );
4705 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004706 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004707}
4708/* END_CASE */
4709
4710/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004711void derive_key( int alg_arg,
4712 data_t *key_data, data_t *input1, data_t *input2,
4713 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004714 int expected_status_arg,
4715 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004716{
Ronald Cron5425a212020-08-04 14:58:35 +02004717 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4718 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004719 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004720 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004721 size_t bits = bits_arg;
4722 psa_status_t expected_status = expected_status_arg;
4723 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4724 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4725 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4726
4727 PSA_ASSERT( psa_crypto_init( ) );
4728
4729 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4730 psa_set_key_algorithm( &base_attributes, alg );
4731 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4732 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004733 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004734
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004735 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4736 input1->x, input1->len,
4737 input2->x, input2->len,
4738 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004739 goto exit;
4740
4741 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4742 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004743 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004744 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004745
4746 psa_status_t status =
4747 psa_key_derivation_output_key( &derived_attributes,
4748 &operation,
4749 &derived_key );
4750 if( is_large_output > 0 )
4751 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4752 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004753
4754exit:
4755 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004756 psa_destroy_key( base_key );
4757 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004758 PSA_DONE( );
4759}
4760/* END_CASE */
4761
4762/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004763void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004764 int our_key_type_arg, int our_key_alg_arg,
4765 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004766 int expected_status_arg )
4767{
Ronald Cron5425a212020-08-04 14:58:35 +02004768 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004769 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004770 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004771 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004772 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004773 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004774 psa_status_t expected_status = expected_status_arg;
4775 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004776
Gilles Peskine8817f612018-12-18 00:18:46 +01004777 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004778
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004779 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004780 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004781 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004782 PSA_ASSERT( psa_import_key( &attributes,
4783 our_key_data->x, our_key_data->len,
4784 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004785
Gilles Peskine77f40d82019-04-11 21:27:06 +02004786 /* The tests currently include inputs that should fail at either step.
4787 * Test cases that fail at the setup step should be changed to call
4788 * key_derivation_setup instead, and this function should be renamed
4789 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004790 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004791 if( status == PSA_SUCCESS )
4792 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004793 TEST_EQUAL( psa_key_derivation_key_agreement(
4794 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4795 our_key,
4796 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004797 expected_status );
4798 }
4799 else
4800 {
4801 TEST_ASSERT( status == expected_status );
4802 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004803
4804exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004805 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004806 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004807 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004808}
4809/* END_CASE */
4810
4811/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004812void raw_key_agreement( int alg_arg,
4813 int our_key_type_arg, data_t *our_key_data,
4814 data_t *peer_key_data,
4815 data_t *expected_output )
4816{
Ronald Cron5425a212020-08-04 14:58:35 +02004817 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004818 psa_algorithm_t alg = alg_arg;
4819 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004820 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004821 unsigned char *output = NULL;
4822 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004823 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004824
4825 ASSERT_ALLOC( output, expected_output->len );
4826 PSA_ASSERT( psa_crypto_init( ) );
4827
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004828 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4829 psa_set_key_algorithm( &attributes, alg );
4830 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004831 PSA_ASSERT( psa_import_key( &attributes,
4832 our_key_data->x, our_key_data->len,
4833 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004834
gabor-mezei-armceface22021-01-21 12:26:17 +01004835 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4836 key_bits = psa_get_key_bits( &attributes );
4837
Gilles Peskinebe697d82019-05-16 18:00:41 +02004838 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4839 peer_key_data->x, peer_key_data->len,
4840 output, expected_output->len,
4841 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004842 ASSERT_COMPARE( output, output_length,
4843 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01004844 TEST_ASSERT( output_length <=
4845 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
4846 TEST_ASSERT( output_length <=
4847 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004848
4849exit:
4850 mbedtls_free( output );
4851 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004852 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004853}
4854/* END_CASE */
4855
4856/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004857void key_agreement_capacity( int alg_arg,
4858 int our_key_type_arg, data_t *our_key_data,
4859 data_t *peer_key_data,
4860 int expected_capacity_arg )
4861{
Ronald Cron5425a212020-08-04 14:58:35 +02004862 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004863 psa_algorithm_t alg = alg_arg;
4864 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004865 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004866 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004867 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004868 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004869
Gilles Peskine8817f612018-12-18 00:18:46 +01004870 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004871
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004872 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4873 psa_set_key_algorithm( &attributes, alg );
4874 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004875 PSA_ASSERT( psa_import_key( &attributes,
4876 our_key_data->x, our_key_data->len,
4877 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004878
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004879 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004880 PSA_ASSERT( psa_key_derivation_key_agreement(
4881 &operation,
4882 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4883 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004884 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4885 {
4886 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004887 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004888 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004889 NULL, 0 ) );
4890 }
Gilles Peskine59685592018-09-18 12:11:34 +02004891
Gilles Peskinebf491972018-10-25 22:36:12 +02004892 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004893 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004894 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004895 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004896
Gilles Peskinebf491972018-10-25 22:36:12 +02004897 /* Test the actual capacity by reading the output. */
4898 while( actual_capacity > sizeof( output ) )
4899 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004900 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004901 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004902 actual_capacity -= sizeof( output );
4903 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004904 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004905 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004906 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004907 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004908
Gilles Peskine59685592018-09-18 12:11:34 +02004909exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004910 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004911 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004912 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004913}
4914/* END_CASE */
4915
4916/* BEGIN_CASE */
4917void key_agreement_output( int alg_arg,
4918 int our_key_type_arg, data_t *our_key_data,
4919 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004920 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004921{
Ronald Cron5425a212020-08-04 14:58:35 +02004922 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004923 psa_algorithm_t alg = alg_arg;
4924 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004925 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004926 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004927 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004928
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004929 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4930 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004931
Gilles Peskine8817f612018-12-18 00:18:46 +01004932 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004933
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004934 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4935 psa_set_key_algorithm( &attributes, alg );
4936 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004937 PSA_ASSERT( psa_import_key( &attributes,
4938 our_key_data->x, our_key_data->len,
4939 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004940
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004941 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004942 PSA_ASSERT( psa_key_derivation_key_agreement(
4943 &operation,
4944 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4945 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004946 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4947 {
4948 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004949 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004950 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004951 NULL, 0 ) );
4952 }
Gilles Peskine59685592018-09-18 12:11:34 +02004953
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004954 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004955 actual_output,
4956 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004957 ASSERT_COMPARE( actual_output, expected_output1->len,
4958 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004959 if( expected_output2->len != 0 )
4960 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004961 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004962 actual_output,
4963 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004964 ASSERT_COMPARE( actual_output, expected_output2->len,
4965 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004966 }
Gilles Peskine59685592018-09-18 12:11:34 +02004967
4968exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004969 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004970 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004971 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004972 mbedtls_free( actual_output );
4973}
4974/* END_CASE */
4975
4976/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004977void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004978{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004979 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004980 unsigned char *output = NULL;
4981 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004982 size_t i;
4983 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004984
Simon Butcher49f8e312020-03-03 15:51:50 +00004985 TEST_ASSERT( bytes_arg >= 0 );
4986
Gilles Peskine91892022021-02-08 19:50:26 +01004987 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004988 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02004989
Gilles Peskine8817f612018-12-18 00:18:46 +01004990 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004991
Gilles Peskinea50d7392018-06-21 10:22:13 +02004992 /* Run several times, to ensure that every output byte will be
4993 * nonzero at least once with overwhelming probability
4994 * (2^(-8*number_of_runs)). */
4995 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004996 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004997 if( bytes != 0 )
4998 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004999 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005000
Gilles Peskinea50d7392018-06-21 10:22:13 +02005001 for( i = 0; i < bytes; i++ )
5002 {
5003 if( output[i] != 0 )
5004 ++changed[i];
5005 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005006 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005007
5008 /* Check that every byte was changed to nonzero at least once. This
5009 * validates that psa_generate_random is overwriting every byte of
5010 * the output buffer. */
5011 for( i = 0; i < bytes; i++ )
5012 {
5013 TEST_ASSERT( changed[i] != 0 );
5014 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005015
5016exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005017 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005018 mbedtls_free( output );
5019 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005020}
5021/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005022
5023/* BEGIN_CASE */
5024void generate_key( int type_arg,
5025 int bits_arg,
5026 int usage_arg,
5027 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005028 int expected_status_arg,
5029 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005030{
Ronald Cron5425a212020-08-04 14:58:35 +02005031 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005032 psa_key_type_t type = type_arg;
5033 psa_key_usage_t usage = usage_arg;
5034 size_t bits = bits_arg;
5035 psa_algorithm_t alg = alg_arg;
5036 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005037 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005038 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005039
Gilles Peskine8817f612018-12-18 00:18:46 +01005040 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005041
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005042 psa_set_key_usage_flags( &attributes, usage );
5043 psa_set_key_algorithm( &attributes, alg );
5044 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005045 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005046
5047 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005048 psa_status_t status = psa_generate_key( &attributes, &key );
5049
5050 if( is_large_key > 0 )
5051 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5052 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005053 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005054 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005055
5056 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005057 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005058 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5059 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005060
Gilles Peskine818ca122018-06-20 18:16:48 +02005061 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005062 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005063 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005064
5065exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005066 /*
5067 * Key attributes may have been returned by psa_get_key_attributes()
5068 * thus reset them as required.
5069 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005070 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005071
Ronald Cron5425a212020-08-04 14:58:35 +02005072 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005073 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005074}
5075/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005076
Ronald Cronee414c72021-03-18 18:50:08 +01005077/* 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 +02005078void generate_key_rsa( int bits_arg,
5079 data_t *e_arg,
5080 int expected_status_arg )
5081{
Ronald Cron5425a212020-08-04 14:58:35 +02005082 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005083 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005084 size_t bits = bits_arg;
5085 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5086 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5087 psa_status_t expected_status = expected_status_arg;
5088 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5089 uint8_t *exported = NULL;
5090 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005091 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005092 size_t exported_length = SIZE_MAX;
5093 uint8_t *e_read_buffer = NULL;
5094 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005095 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005096 size_t e_read_length = SIZE_MAX;
5097
5098 if( e_arg->len == 0 ||
5099 ( e_arg->len == 3 &&
5100 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5101 {
5102 is_default_public_exponent = 1;
5103 e_read_size = 0;
5104 }
5105 ASSERT_ALLOC( e_read_buffer, e_read_size );
5106 ASSERT_ALLOC( exported, exported_size );
5107
5108 PSA_ASSERT( psa_crypto_init( ) );
5109
5110 psa_set_key_usage_flags( &attributes, usage );
5111 psa_set_key_algorithm( &attributes, alg );
5112 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5113 e_arg->x, e_arg->len ) );
5114 psa_set_key_bits( &attributes, bits );
5115
5116 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005117 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005118 if( expected_status != PSA_SUCCESS )
5119 goto exit;
5120
5121 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005122 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005123 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5124 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5125 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5126 e_read_buffer, e_read_size,
5127 &e_read_length ) );
5128 if( is_default_public_exponent )
5129 TEST_EQUAL( e_read_length, 0 );
5130 else
5131 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5132
5133 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005134 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005135 goto exit;
5136
5137 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005138 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005139 exported, exported_size,
5140 &exported_length ) );
5141 {
5142 uint8_t *p = exported;
5143 uint8_t *end = exported + exported_length;
5144 size_t len;
5145 /* RSAPublicKey ::= SEQUENCE {
5146 * modulus INTEGER, -- n
5147 * publicExponent INTEGER } -- e
5148 */
5149 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005150 MBEDTLS_ASN1_SEQUENCE |
5151 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005152 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005153 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5154 MBEDTLS_ASN1_INTEGER ) );
5155 if( len >= 1 && p[0] == 0 )
5156 {
5157 ++p;
5158 --len;
5159 }
5160 if( e_arg->len == 0 )
5161 {
5162 TEST_EQUAL( len, 3 );
5163 TEST_EQUAL( p[0], 1 );
5164 TEST_EQUAL( p[1], 0 );
5165 TEST_EQUAL( p[2], 1 );
5166 }
5167 else
5168 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5169 }
5170
5171exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005172 /*
5173 * Key attributes may have been returned by psa_get_key_attributes() or
5174 * set by psa_set_key_domain_parameters() thus reset them as required.
5175 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005176 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005177
Ronald Cron5425a212020-08-04 14:58:35 +02005178 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005179 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005180 mbedtls_free( e_read_buffer );
5181 mbedtls_free( exported );
5182}
5183/* END_CASE */
5184
Darryl Greend49a4992018-06-18 17:27:26 +01005185/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005186void persistent_key_load_key_from_storage( data_t *data,
5187 int type_arg, int bits_arg,
5188 int usage_flags_arg, int alg_arg,
5189 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005190{
Ronald Cron71016a92020-08-28 19:01:50 +02005191 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005192 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005193 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5194 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005195 psa_key_type_t type = type_arg;
5196 size_t bits = bits_arg;
5197 psa_key_usage_t usage_flags = usage_flags_arg;
5198 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005199 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005200 unsigned char *first_export = NULL;
5201 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005202 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005203 size_t first_exported_length;
5204 size_t second_exported_length;
5205
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005206 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5207 {
5208 ASSERT_ALLOC( first_export, export_size );
5209 ASSERT_ALLOC( second_export, export_size );
5210 }
Darryl Greend49a4992018-06-18 17:27:26 +01005211
Gilles Peskine8817f612018-12-18 00:18:46 +01005212 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005213
Gilles Peskinec87af662019-05-15 16:12:22 +02005214 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005215 psa_set_key_usage_flags( &attributes, usage_flags );
5216 psa_set_key_algorithm( &attributes, alg );
5217 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005218 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005219
Darryl Green0c6575a2018-11-07 16:05:30 +00005220 switch( generation_method )
5221 {
5222 case IMPORT_KEY:
5223 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005224 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005225 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005226 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005227
Darryl Green0c6575a2018-11-07 16:05:30 +00005228 case GENERATE_KEY:
5229 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005230 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005231 break;
5232
5233 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005234#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005235 {
5236 /* Create base key */
5237 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5238 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5239 psa_set_key_usage_flags( &base_attributes,
5240 PSA_KEY_USAGE_DERIVE );
5241 psa_set_key_algorithm( &base_attributes, derive_alg );
5242 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005243 PSA_ASSERT( psa_import_key( &base_attributes,
5244 data->x, data->len,
5245 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005246 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005247 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005248 PSA_ASSERT( psa_key_derivation_input_key(
5249 &operation,
5250 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005251 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005252 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005253 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005254 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5255 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005256 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005257 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005258 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005259 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005260 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005261#else
5262 TEST_ASSUME( ! "KDF not supported in this configuration" );
5263#endif
5264 break;
5265
5266 default:
5267 TEST_ASSERT( ! "generation_method not implemented in test" );
5268 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005269 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005270 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005271
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005272 /* Export the key if permitted by the key policy. */
5273 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5274 {
Ronald Cron5425a212020-08-04 14:58:35 +02005275 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005276 first_export, export_size,
5277 &first_exported_length ) );
5278 if( generation_method == IMPORT_KEY )
5279 ASSERT_COMPARE( data->x, data->len,
5280 first_export, first_exported_length );
5281 }
Darryl Greend49a4992018-06-18 17:27:26 +01005282
5283 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005284 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005285 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005286 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005287
Darryl Greend49a4992018-06-18 17:27:26 +01005288 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005289 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005290 TEST_ASSERT( mbedtls_svc_key_id_equal(
5291 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005292 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5293 PSA_KEY_LIFETIME_PERSISTENT );
5294 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5295 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4d9009e2021-05-13 12:05:01 +02005296 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-armff03fd62021-06-28 14:05:00 +02005297 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005298 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005299
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005300 /* Export the key again if permitted by the key policy. */
5301 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005302 {
Ronald Cron5425a212020-08-04 14:58:35 +02005303 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005304 second_export, export_size,
5305 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005306 ASSERT_COMPARE( first_export, first_exported_length,
5307 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005308 }
5309
5310 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005311 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005312 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005313
5314exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005315 /*
5316 * Key attributes may have been returned by psa_get_key_attributes()
5317 * thus reset them as required.
5318 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005319 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005320
Darryl Greend49a4992018-06-18 17:27:26 +01005321 mbedtls_free( first_export );
5322 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005323 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005324 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005325 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005326 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005327}
5328/* END_CASE */